Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(media): clamp the ffmpeg output cap to a value ffmpeg accepts

Yudhi Armyndharis11409fbff008
diff --git a/src/modules/media/ffmpeg.spec.ts b/src/modules/media/ffmpeg.spec.ts
index 20950c16b..b9b28d9c4 100644
--- a/src/modules/media/ffmpeg.spec.ts
+++ b/src/modules/media/ffmpeg.spec.ts
@@ -89,6 +89,15 @@ describe('ffmpeg invocation shape', () => {
expect(at).toBe(args.length - 3);
});
+ // ffmpeg parses -fs as a signed 64-bit integer and exits 1 on anything wider, so a row-of-nines cap
+ // meant as "unlimited" would fail every conversion instead.
+ it('keeps the size cap within what ffmpeg accepts, however large the configured limit', () => {
+ for (const limit of [Number('99999999999999999999'), 1e21]) {
+ const huge = buildFfmpegArgs('/in', '/out', [], limit);
+ expect(huge[huge.indexOf('-fs') + 1]).toBe(String(Number.MAX_SAFE_INTEGER));
+ }
+ });
+
it('confines ffmpeg to the file protocol', () => {
expect(args[args.indexOf('-protocol_whitelist') + 1]).toBe('file');
});
diff --git a/src/modules/media/ffmpeg.ts b/src/modules/media/ffmpeg.ts
index 5c0e945cb..9066cd3dd 100644
--- a/src/modules/media/ffmpeg.ts
+++ b/src/modules/media/ffmpeg.ts
@@ -99,8 +99,10 @@ export function buildFfmpegArgs(
// the temp directory (a RAM-backed tmpfs in the compose files) before the size check after exit.
// It is an output option, so it sits right before the output path. The limit is one byte above
// the cap: a cut-off file is then always over the cap and rejected, and a complete one at the cap
- // still passes.
- return [...BASE_ARGS, '-i', inputPath, ...encodeArgs, '-fs', String(maxOutputBytes + 1), outputPath];
+ // still passes. ffmpeg refuses a -fs past a 64-bit integer, so a limit too large to matter is clamped
+ // rather than failing every conversion.
+ const fs = Math.min(maxOutputBytes, Number.MAX_SAFE_INTEGER - 1) + 1;
+ return [...BASE_ARGS, '-i', inputPath, ...encodeArgs, '-fs', String(fs), outputPath];
}
/**