From b1fcf02d1318f009cbaaf01c86b3c37fd19ae6a5 Mon Sep 17 00:00:00 2001 From: Mert <101130780+mertalev@users.noreply.github.com> Date: Fri, 30 Jun 2023 21:48:05 -0400 Subject: [PATCH] fix(server): h264 and hevc not respecting max bitrate (#3052) * added `-bufsize` flag * updated test --- server/src/domain/media/media.service.spec.ts | 1 + server/src/domain/media/media.service.ts | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/server/src/domain/media/media.service.spec.ts b/server/src/domain/media/media.service.spec.ts index 4189a773fe..616debedee 100644 --- a/server/src/domain/media/media.service.spec.ts +++ b/server/src/domain/media/media.service.spec.ts @@ -396,6 +396,7 @@ describe(MediaService.name, () => { '-preset ultrafast', '-crf 23', '-maxrate 4500k', + '-bufsize 9000k', ], twoPass: false, }, diff --git a/server/src/domain/media/media.service.ts b/server/src/domain/media/media.service.ts index d11d4582ef..50f49032ca 100644 --- a/server/src/domain/media/media.service.ts +++ b/server/src/domain/media/media.service.ts @@ -284,7 +284,14 @@ export class MediaService { } else if (constrainMaximumBitrate || isVP9) { // for vp9, these flags work for both one-pass and two-pass options.push(`-crf ${ffmpeg.crf}`); - options.push(`${isVP9 ? '-b:v' : '-maxrate'} ${maxBitrateValue}${bitrateUnit}`); + if (isVP9) { + options.push(`-b:v ${maxBitrateValue}${bitrateUnit}`); + } else { + options.push(`-maxrate ${maxBitrateValue}${bitrateUnit}`); + // -bufsize is the peak possible bitrate at any moment, while -maxrate is the max rolling average bitrate + // needed for -maxrate to be enforced + options.push(`-bufsize ${maxBitrateValue * 2}${bitrateUnit}`); + } } else { options.push(`-crf ${ffmpeg.crf}`); }