Skip to content

🎥 Make videos programmatically with React

`@remotion/renderer`: Require `videoBitrate` with `encodingBufferSize` for VP8, VP9 and AV1

Jillur Rahmanbe26c5cb02c4
diff --git a/packages/renderer/src/crf.ts b/packages/renderer/src/crf.ts
index 3b71de759c..cf642d8805 100644
--- a/packages/renderer/src/crf.ts
+++ b/packages/renderer/src/crf.ts
@@ -92,6 +92,17 @@ export const validateQualitySettings = ({
);
}
+ // libvpx and libaom-av1 reject -maxrate and -bufsize without -b:v
+ if (
+ (encodingMaxRate || encodingBufferSize) &&
+ !videoBitrate &&
+ (codec === 'vp8' || codec === 'vp9' || codec === 'av1')
+ ) {
+ throw new Error(
+ `The ${codec} codec needs "videoBitrate" when "encodingMaxRate" or "encodingBufferSize" is set.`,
+ );
+ }
+
const bufSizeArray = encodingBufferSize
? ['-bufsize', encodingBufferSize]
: [];
diff --git a/packages/renderer/src/options/encoding-buffer-size.tsx b/packages/renderer/src/options/encoding-buffer-size.tsx
index e72bcd5bc1..cc0a6aa93a 100644
--- a/packages/renderer/src/options/encoding-buffer-size.tsx
+++ b/packages/renderer/src/options/encoding-buffer-size.tsx
@@ -17,7 +17,8 @@ export const encodingBufferSizeOption = {
description: () => (
<>
The value for the <code>-bufsize</code> flag of FFmpeg. Should be used in
- conjunction with the encoding max rate flag.
+ conjunction with the encoding max rate flag. With the VP8, VP9 and AV1
+ codecs, a video bitrate must also be set.
</>
),
ssrName: 'encodingBufferSize' as const,
diff --git a/packages/renderer/src/options/encoding-max-rate.tsx b/packages/renderer/src/options/encoding-max-rate.tsx
index 33900803f4..54abc5fe70 100644
--- a/packages/renderer/src/options/encoding-max-rate.tsx
+++ b/packages/renderer/src/options/encoding-max-rate.tsx
@@ -10,7 +10,8 @@ export const encodingMaxRateOption = {
description: () => (
<>
The value for the <code>-maxrate</code> flag of FFmpeg. Should be used in
- conjunction with the encoding buffer size flag.
+ conjunction with the encoding buffer size flag. With the VP8, VP9 and AV1
+ codecs, a video bitrate must also be set.
</>
),
ssrName: 'encodingMaxRate' as const,
diff --git a/packages/renderer/src/test/crf.test.ts b/packages/renderer/src/test/crf.test.ts
index 47e758d2f7..545009c8cc 100644
--- a/packages/renderer/src/test/crf.test.ts
+++ b/packages/renderer/src/test/crf.test.ts
@@ -158,6 +158,61 @@ test('encodingMaxRate', () => {
);
});
+describe('encodingMaxRate and encodingBufferSize need videoBitrate for vp8, vp9 and av1', () => {
+ const settings = {
+ videoBitrate: null,
+ encodingMaxRate: '1M',
+ encodingBufferSize: '2M',
+ hardwareAcceleration: 'if-possible' as const,
+ hardwareAccelerated: false,
+ };
+
+ (['vp8', 'vp9', 'av1'] as Codec[]).forEach((codec) =>
+ test(`${codec} without videoBitrate throws`, () =>
+ expect(() =>
+ validateQualitySettings({...settings, crf: null, codec}),
+ ).toThrow(
+ `The ${codec} codec needs "videoBitrate" when "encodingMaxRate" or "encodingBufferSize" is set.`,
+ )),
+ );
+
+ test('vp9 with only encodingBufferSize or an explicit crf throws', () => {
+ expect(() =>
+ validateQualitySettings({
+ ...settings,
+ crf: null,
+ codec: 'vp9',
+ encodingMaxRate: null,
+ }),
+ ).toThrow(/The vp9 codec needs "videoBitrate"/);
+ expect(() =>
+ validateQualitySettings({...settings, crf: 30, codec: 'vp9'}),
+ ).toThrow(/The vp9 codec needs "videoBitrate"/);
+ });
+
+ test('vp9 with videoBitrate is allowed', () =>
+ expect(
+ validateQualitySettings({
+ ...settings,
+ crf: null,
+ codec: 'vp9',
+ videoBitrate: '1M',
+ }),
+ ).toEqual(['-b:v', '1M', '-bufsize', '2M', '-maxrate', '1M']));
+
+ (['h264', 'h265'] as Codec[]).forEach((codec) =>
+ test(`${codec} still caps the crf`, () =>
+ expect(validateQualitySettings({...settings, crf: null, codec})).toEqual([
+ '-crf',
+ String(getDefaultCrfForCodec(codec)),
+ '-bufsize',
+ '2M',
+ '-maxrate',
+ '1M',
+ ])),
+ );
+});
+
describe('crf tests getValidCrfRanges invalid input', () => {
// input codec
const invalidInputs = ['abc', '', 3, undefined];