add services/worker/src/ffmpeg/executor.js
This commit is contained in:
parent
9e2833ba85
commit
5bc6cf7c17
1 changed files with 84 additions and 0 deletions
84
services/worker/src/ffmpeg/executor.js
Normal file
84
services/worker/src/ffmpeg/executor.js
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import { execFile } from 'child_process';
|
||||
import { promisify } from 'util';
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
export const runFFmpeg = async (args, options = {}) => {
|
||||
const { stdio = 'pipe', ...otherOptions } = options;
|
||||
try {
|
||||
const { stdout, stderr } = await execFileAsync('ffmpeg', args, {
|
||||
stdio,
|
||||
...otherOptions,
|
||||
});
|
||||
return { stdout, stderr };
|
||||
} catch (error) {
|
||||
throw new Error(`FFmpeg error: ${error.message}\nStderr: ${error.stderr}`);
|
||||
}
|
||||
};
|
||||
|
||||
export const getMediaDuration = async (inputPath) => {
|
||||
const args = [
|
||||
'-v', 'error',
|
||||
'-show_entries', 'format=duration',
|
||||
'-of', 'default=noprint_wrappers=1:nokey=1:noinvalidchars=1',
|
||||
inputPath,
|
||||
];
|
||||
const { stdout } = await runFFmpeg(args);
|
||||
return parseFloat(stdout.trim());
|
||||
};
|
||||
|
||||
export const extractFrameAtTime = async (inputPath, outputPath, timeCode) => {
|
||||
const args = [
|
||||
'-i', inputPath,
|
||||
'-ss', timeCode,
|
||||
'-vframes', '1',
|
||||
'-q:v', '2',
|
||||
outputPath,
|
||||
];
|
||||
await runFFmpeg(args);
|
||||
};
|
||||
|
||||
export const transcodeVideo = async (inputPath, outputPath, options = {}) => {
|
||||
const {
|
||||
videoCodec = 'libx264',
|
||||
videoPreset = 'fast',
|
||||
videoBitrate = '10M',
|
||||
audioCodec = 'aac',
|
||||
audioBitrate = '192k',
|
||||
} = options;
|
||||
|
||||
const args = [
|
||||
'-i', inputPath,
|
||||
'-c:v', videoCodec,
|
||||
'-preset', videoPreset,
|
||||
'-b:v', videoBitrate,
|
||||
'-c:a', audioCodec,
|
||||
'-b:a', audioBitrate,
|
||||
'-movflags', '+faststart',
|
||||
outputPath,
|
||||
];
|
||||
|
||||
await runFFmpeg(args);
|
||||
};
|
||||
|
||||
export const trimSegment = async (inputPath, outputPath, inPoint, outPoint) => {
|
||||
const args = [
|
||||
'-i', inputPath,
|
||||
'-ss', inPoint,
|
||||
'-to', outPoint,
|
||||
'-c', 'copy',
|
||||
outputPath,
|
||||
];
|
||||
await runFFmpeg(args);
|
||||
};
|
||||
|
||||
export const concatSegments = async (segmentListFile, outputPath) => {
|
||||
const args = [
|
||||
'-f', 'concat',
|
||||
'-safe', '0',
|
||||
'-i', segmentListFile,
|
||||
'-c', 'copy',
|
||||
outputPath,
|
||||
];
|
||||
await runFFmpeg(args);
|
||||
};
|
||||
Loading…
Reference in a new issue