Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(media): cap converted video at 30 fps to stay within level 3.1

Yudhi Armyndharis31655e8b9166
diff --git a/src/modules/media/ffmpeg.spec.ts b/src/modules/media/ffmpeg.spec.ts
index a47aaee0e..d8c25a73f 100644
--- a/src/modules/media/ffmpeg.spec.ts
+++ b/src/modules/media/ffmpeg.spec.ts
@@ -63,6 +63,12 @@ describe('ffmpeg encoder arguments', () => {
':force_original_aspect_ratio=decrease:force_divisible_by=2',
);
});
+
+ // Level 3.1 also caps the macroblock rate, which is 1280x720 at 30 fps: a 60 fps phone clip kept
+ // its rate and declared a level it exceeded. `-fpsmax` lowers a faster rate and leaves a slower one.
+ it('caps the frame rate at the 30 fps level 3.1 allows for a 720p frame', () => {
+ expect(valueOf(args, '-fpsmax')).toBe('30');
+ });
});
});
diff --git a/src/modules/media/ffmpeg.ts b/src/modules/media/ffmpeg.ts
index bfd87ab9c..b9ddea05e 100644
--- a/src/modules/media/ffmpeg.ts
+++ b/src/modules/media/ffmpeg.ts
@@ -132,7 +132,9 @@ export function voiceEncodeArgs(): string[] {
* receiver can begin playback before the whole file arrives. The scale filter fits the frame inside
* 1280x720 (720x1280 for a portrait one), the largest box within the Baseline 3.1 frame-size limit
* whatever the aspect ratio, and keeps both edges even, which H.264 requires. `min()` means a
- * smaller video is never upscaled into a larger file than it started as.
+ * smaller video is never upscaled into a larger file than it started as. Level 3.1 also caps the
+ * macroblock rate, which is 1280x720 at 30 fps, so `-fpsmax` lowers a faster source to 30 fps and
+ * leaves a slower one as it is.
*/
export function videoEncodeArgs(): string[] {
return [
@@ -146,6 +148,8 @@ export function videoEncodeArgs(): string[] {
'yuv420p',
'-vf',
"scale='min(iw,if(gte(iw,ih),1280,720))':'min(ih,if(gte(iw,ih),720,1280))':force_original_aspect_ratio=decrease:force_divisible_by=2",
+ '-fpsmax',
+ '30',
'-c:a',
'aac',
'-b:a',