From 8fa041a484dbbbb95eb8dc6c96156149a035bf6b Mon Sep 17 00:00:00 2001 From: Webysther Sperandio Date: Wed, 28 Jan 2026 03:42:13 +0100 Subject: [PATCH] feat(ffmpeg): support ffmpeg env args and hw codecs Parse FFMPEG_ARGS and FFMPEG_OUTPUT_ARGS into arrays Detect 'qsv' and 'cuda' flags and map codecs to Hardware encoders (h264_qsv/hevc_qsv and h264_nvenc/hevc_nvenc) Make codec_short mutable to allow reassignment Log constructed ffmpeg command for visibility Enables runtime custom ffmpeg args and hw encoder selection Improves deployment flexibility and performance tuning --- src/converters/ffmpeg.ts | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/converters/ffmpeg.ts b/src/converters/ffmpeg.ts index 8f26385..953f7c3 100644 --- a/src/converters/ffmpeg.ts +++ b/src/converters/ffmpeg.ts @@ -708,12 +708,44 @@ export async function convert( message = "Done: resized to 256x256"; } + // Parse FFMPEG_ARGS environment variable into array + const ffmpegArgs = process.env.FFMPEG_ARGS ? process.env.FFMPEG_ARGS.split(/\s+/) : []; + const ffmpegOutputArgs = process.env.FFMPEG_OUTPUT_ARGS + ? process.env.FFMPEG_OUTPUT_ARGS.split(/\s+/) + : []; + if (convertTo.split(".").length > 1) { // Support av1.mkv and av1.mp4 and h265.mp4 etc. const split = convertTo.split("."); - const codec_short = split[0]; + var codec_short = split[0]; + if (ffmpegArgs.includes('qsv')){ switch (codec_short) { + case "h264": + codec_short = "h264_qsv" + extraArgs.push("-c:v", codec_short); + break; + case "h265": + codec_short = "hevc_qsv" + extraArgs.push("-c:v", codec_short); + break; + } + } + + if (ffmpegArgs.includes('cuda')){ + switch (codec_short) { + case "h264": + codec_short = "h264_nvenc" + extraArgs.push("-c:v", codec_short); + break; + case "h265": + codec_short = "hevc_nvenc" + extraArgs.push("-c:v", codec_short); + break; + } + } + + switch (codec_short) { case "av1": extraArgs.push("-c:v", "libaom-av1"); break; @@ -729,11 +761,7 @@ export async function convert( } } - // Parse FFMPEG_ARGS environment variable into array - const ffmpegArgs = process.env.FFMPEG_ARGS ? process.env.FFMPEG_ARGS.split(/\s+/) : []; - const ffmpegOutputArgs = process.env.FFMPEG_OUTPUT_ARGS - ? process.env.FFMPEG_OUTPUT_ARGS.split(/\s+/) - : []; + console.log(`ffmpeg ${ffmpegArgs} -i filePath ${ffmpegOutputArgs} ${extraArgs} ${targetPath}`) return new Promise((resolve, reject) => { execFile(