Skip to content

🎥 Make videos programmatically with React

Add bufsize and maxrate ffmpeg rendering options

llccrrfe1617d4545e
diff --git a/packages/bundler/src/index-html.ts b/packages/bundler/src/index-html.ts
index db6d65306a..b04d1abeed 100644
--- a/packages/bundler/src/index-html.ts
+++ b/packages/bundler/src/index-html.ts
@@ -20,6 +20,8 @@ export type RenderDefaults = {
pixelFormat: string;
audioBitrate: string | null;
videoBitrate: string | null;
+ bufSize: string | null;
+ maxRate: string | null;
everyNthFrame: number;
numberOfGifLoops: number | null;
delayRenderTimeout: number;
diff --git a/packages/cli/src/benchmark.ts b/packages/cli/src/benchmark.ts
index d0291c01a2..d2948a3a39 100644
--- a/packages/cli/src/benchmark.ts
+++ b/packages/cli/src/benchmark.ts
@@ -182,6 +182,8 @@ export const benchmarkCommand = async (
ffmpegOverride,
audioBitrate,
videoBitrate,
+ maxRate,
+ bufSize,
height,
width,
concurrency: unparsedConcurrency,
@@ -349,6 +351,8 @@ export const benchmarkCommand = async (
codec,
audioBitrate,
videoBitrate,
+ maxRate,
+ bufSize,
puppeteerInstance,
concurrency: con,
audioCodec: null,
diff --git a/packages/cli/src/config/bitrate.ts b/packages/cli/src/config/bitrate.ts
index da23e87421..f039f9b864 100644
--- a/packages/cli/src/config/bitrate.ts
+++ b/packages/cli/src/config/bitrate.ts
@@ -17,3 +17,27 @@ export const setVideoBitrate = (bitrate: string | null) => {
export const getVideoBitrate = () => {
return videoBitrate;
};
+
+let maxRate: string | null = null;
+
+export const setVideoMaxRate = (bitrate: string | null) => {
+ maxRate = bitrate;
+}
+
+export const getVideoMaxRate = () => {
+ return maxRate;
+}
+
+/**
+ * bufSize is not a bitrate, but it is a bitrate-related option and get validated like a bitrate.
+ */
+let bufSize: string | null = null;
+
+export const setVideoBufSize = (bitrate: string | null) => {
+ bufSize = bitrate;
+}
+
+export const getVideoBufSize = () => {
+ return bufSize;
+}
+
diff --git a/packages/cli/src/config/index.ts b/packages/cli/src/config/index.ts
index f27d9e3767..a7ba881b79 100644
--- a/packages/cli/src/config/index.ts
+++ b/packages/cli/src/config/index.ts
@@ -60,8 +60,13 @@ import {getAudioCodec, setAudioCodec} from './audio-codec';
import {
getAudioBitrate,
getVideoBitrate,
+ getVideoBufSize,
+ getVideoMaxRate,
setAudioBitrate,
setVideoBitrate,
+ setVideoBufSize,
+ setVideoMaxRate
+
} from './bitrate';
import {setBrowserExecutable} from './browser-executable';
import {
@@ -428,6 +433,16 @@ declare global {
*/
readonly setVideoBitrate: (bitrate: string | null) => void;
+ /**
+ * Set a maximum bitrate to be passed to FFMPEG.
+ */
+ readonly setVideoMaxRate: (bitrate: string | null) => void;
+
+ /**
+ * Set a buffer size to be passed to FFMPEG.
+ */
+ readonly setVideoBufSize: (bitrate: string | null) => void;
+
/**
* Opt into bt709 rendering.
*/
@@ -555,6 +570,8 @@ export const Config: FlatConfig = {
setJpegQuality,
setStillImageFormat,
setVideoImageFormat,
+ setVideoMaxRate,
+ setVideoBufSize,
setFrameRange,
setScale,
setEveryNthFrame,
@@ -625,6 +642,8 @@ export const ConfigInternals = {
getFfmpegOverrideFunction,
getAudioBitrate,
getVideoBitrate,
+ getVideoBufSize,
+ getVideoMaxRate,
getHeight,
getWidth,
getCrfOrUndefined,
diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.ts
index 45cdad70c0..c7c2214c33 100644
--- a/packages/cli/src/get-cli-options.ts
+++ b/packages/cli/src/get-cli-options.ts
@@ -12,6 +12,7 @@ import {getEnvironmentVariables} from './get-env';
import {getInputProps} from './get-input-props';
import {Log} from './log';
+
const getAndValidateFrameRange = (logLevel: LogLevel, indent: boolean) => {
const frameRange = ConfigInternals.getRange();
if (typeof frameRange === 'number') {
@@ -129,7 +130,8 @@ export const getCliOptions = async (options: {
});
const crf = getCrf(shouldOutputImageSequence);
const videoBitrate = ConfigInternals.getVideoBitrate();
-
+ const bufSize = ConfigInternals.getVideoBufSize()
+ const maxRate = ConfigInternals.getVideoMaxRate()
const pixelFormat = ConfigInternals.getPixelFormat();
const proResProfile = getProResProfile();
const x264Preset = getx264Preset();
@@ -191,6 +193,8 @@ export const getCliOptions = async (options: {
ffmpegOverride: ConfigInternals.getFfmpegOverrideFunction(),
audioBitrate: ConfigInternals.getAudioBitrate(),
videoBitrate,
+ bufSize,
+ maxRate,
height,
width,
configFileImageFormat: ConfigInternals.getUserPreferredVideoImageFormat(),
diff --git a/packages/cli/src/parse-command-line.ts b/packages/cli/src/parse-command-line.ts
index 3a6d5abd62..259f107ee4 100644
--- a/packages/cli/src/parse-command-line.ts
+++ b/packages/cli/src/parse-command-line.ts
@@ -41,6 +41,8 @@ type CommandLineOptions = {
['public-dir']: string;
['audio-bitrate']: string;
['video-bitrate']: string;
+ ['buf-size']: string;
+ ['max-rate']: string;
['audio-codec']: AudioCodec;
crf: number;
force: boolean;
@@ -263,6 +265,14 @@ export const parseCommandLine = () => {
Config.setVideoBitrate(parsedCli['video-bitrate']);
}
+ if (typeof parsedCli['buf-size'] !== 'undefined') {
+ Config.setVideoBufSize(parsedCli['buf-size']);
+ }
+
+ if (typeof parsedCli['buf-size'] !== 'undefined') {
+ Config.setVideoMaxRate(parsedCli['buf-size']);
+ }
+
if (typeof parsedCli['offthreadvideo-cache-size-in-bytes'] !== 'undefined') {
Config.setOffthreadVideoCacheSizeInBytes(
parsedCli['offthreadvideo-cache-size-in-bytes'],
diff --git a/packages/cli/src/preview-server/render-queue/job.ts b/packages/cli/src/preview-server/render-queue/job.ts
index d137f5dd11..16f871a3b3 100644
--- a/packages/cli/src/preview-server/render-queue/job.ts
+++ b/packages/cli/src/preview-server/render-queue/job.ts
@@ -81,6 +81,8 @@ type RenderJobDynamicFields =
pixelFormat: PixelFormat;
audioBitrate: string | null;
videoBitrate: string | null;
+ bufSize: string | null;
+ maxRate: string | null;
everyNthFrame: number;
numberOfGifLoops: number | null;
disallowParallelEncoding: boolean;
@@ -146,6 +148,8 @@ type AddRenderRequestDynamicFields =
pixelFormat: PixelFormat;
audioBitrate: string | null;
videoBitrate: string | null;
+ bufSize: string | null;
+ maxRate: string | null;
everyNthFrame: number;
numberOfGifLoops: number | null;
disallowParallelEncoding: boolean;
diff --git a/packages/cli/src/preview-server/render-queue/process-video.ts b/packages/cli/src/preview-server/render-queue/process-video.ts
index f8dde8a9e3..2a3d665e4d 100644
--- a/packages/cli/src/preview-server/render-queue/process-video.ts
+++ b/packages/cli/src/preview-server/render-queue/process-video.ts
@@ -75,6 +75,8 @@ export const processVideoJob = async ({
x264Preset: job.type === 'video' ? job.x264Preset ?? undefined : undefined,
pixelFormat: job.type === 'video' ? job.pixelFormat : 'yuv420p',
videoBitrate: job.type === 'video' ? job.videoBitrate : null,
+ bufSize: job.type === 'video' ? job.bufSize : null,
+ maxRate: job.type === 'video' ? job.maxRate : null,
numberOfGifLoops: job.type === 'video' ? job.numberOfGifLoops : null,
audioCodec: job.type === 'video' ? job.audioCodec : null,
disallowParallelEncoding:
diff --git a/packages/cli/src/preview-server/routes.ts b/packages/cli/src/preview-server/routes.ts
index 8e36edb43a..a6ab39b970 100644
--- a/packages/cli/src/preview-server/routes.ts
+++ b/packages/cli/src/preview-server/routes.ts
@@ -74,6 +74,8 @@ const handleFallback = async ({
const x264Preset = ConfigInternals.getPresetProfile() ?? 'medium';
const audioBitrate = ConfigInternals.getAudioBitrate();
const videoBitrate = ConfigInternals.getVideoBitrate();
+ const bufSize = ConfigInternals.getVideoBufSize();
+ const maxRate = ConfigInternals.getVideoMaxRate();
const everyNthFrame = ConfigInternals.getEveryNthFrame();
const numberOfGifLoops = ConfigInternals.getNumberOfGifLoops();
const delayRenderTimeout = ConfigInternals.getCurrentPuppeteerTimeout();
@@ -132,6 +134,8 @@ const handleFallback = async ({
pixelFormat,
audioBitrate,
videoBitrate,
+ bufSize,
+ maxRate,
everyNthFrame,
numberOfGifLoops,
delayRenderTimeout,
diff --git a/packages/cli/src/preview-server/routes/add-render.ts b/packages/cli/src/preview-server/routes/add-render.ts
index ad129acec6..06fea7a85a 100644
--- a/packages/cli/src/preview-server/routes/add-render.ts
+++ b/packages/cli/src/preview-server/routes/add-render.ts
@@ -41,6 +41,8 @@ export const handleAddRender: ApiHandler<AddRenderRequest, undefined> = ({
pixelFormat: input.pixelFormat,
audioBitrate: input.audioBitrate,
videoBitrate: input.videoBitrate,
+ maxRate: input.maxRate,
+ bufSize: input.bufSize,
everyNthFrame: input.everyNthFrame,
numberOfGifLoops: input.numberOfGifLoops,
delayRenderTimeout: input.delayRenderTimeout,
@@ -48,7 +50,7 @@ export const handleAddRender: ApiHandler<AddRenderRequest, undefined> = ({
chromiumOptions: input.chromiumOptions,
envVariables: input.envVariables,
serializedInputPropsWithCustomSchema:
- input.serializedInputPropsWithCustomSchema,
+ input.serializedInputPropsWithCustomSchema,
offthreadVideoCacheSizeInBytes: input.offthreadVideoCacheSizeInBytes,
colorSpace: input.colorSpace,
multiProcessOnLinux: input.multiProcessOnLinux,
diff --git a/packages/cli/src/render-flows/render.ts b/packages/cli/src/render-flows/render.ts
index c80926fa53..15e6257190 100644
--- a/packages/cli/src/render-flows/render.ts
+++ b/packages/cli/src/render-flows/render.ts
@@ -90,6 +90,8 @@ export const renderVideoFlow = async ({
x264Preset,
pixelFormat,
videoBitrate,
+ maxRate,
+ bufSize,
numberOfGifLoops,
audioCodec,
serializedInputPropsWithCustomSchema,
@@ -132,6 +134,8 @@ export const renderVideoFlow = async ({
ffmpegOverride: FfmpegOverrideFn;
audioBitrate: string | null;
videoBitrate: string | null;
+ maxRate: string | null;
+ bufSize: string | null;
muted: boolean;
enforceAudioTrack: boolean;
proResProfile: ProResProfile | undefined;
@@ -458,6 +462,8 @@ export const renderVideoFlow = async ({
codec,
audioBitrate,
videoBitrate,
+ maxRate,
+ bufSize,
onProgress: (update) => {
(stitchingProgress as StitchingProgressInput).doneIn =
update.encodedDoneIn;
diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsx
index 152047e9bd..9c84d8a292 100644
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@ -67,6 +67,8 @@ export const render = async (
x264Preset,
pixelFormat,
videoBitrate,
+ maxRate,
+ bufSize,
numberOfGifLoops,
offthreadVideoCacheSizeInBytes,
colorSpace,
@@ -127,6 +129,8 @@ export const render = async (
x264Preset,
pixelFormat,
videoBitrate,
+ maxRate,
+ bufSize,
numberOfGifLoops,
audioCodec,
disallowParallelEncoding: false,
diff --git a/packages/cloudrun/src/api/render-media-on-cloudrun.ts b/packages/cloudrun/src/api/render-media-on-cloudrun.ts
index e1b6a30ce5..ff4b55bcfa 100644
--- a/packages/cloudrun/src/api/render-media-on-cloudrun.ts
+++ b/packages/cloudrun/src/api/render-media-on-cloudrun.ts
@@ -48,6 +48,8 @@ type InternalRenderMediaOnCloudrun = {
jpegQuality: number | undefined;
audioBitrate: string | null;
videoBitrate: string | null;
+ maxRate: string | null;
+ bufSize: string | null;
proResProfile: ProResProfile | undefined;
x264Preset: X264Preset | undefined;
crf: Crf | null;
@@ -86,6 +88,8 @@ export type RenderMediaOnCloudrunInput = {
jpegQuality?: number;
audioBitrate?: string | null;
videoBitrate?: string | null;
+ maxRate?: string | null;
+ bufSize?: string | null;
proResProfile?: ProResProfile;
x264Preset?: X264Preset;
crf?: number | undefined;
@@ -124,6 +128,8 @@ const internalRenderMediaOnCloudrunRaw = async ({
audioCodec,
audioBitrate,
videoBitrate,
+ maxRate,
+ bufSize,
proResProfile,
x264Preset,
crf,
@@ -175,6 +181,8 @@ const internalRenderMediaOnCloudrunRaw = async ({
audioCodec: audioCodec ?? null,
audioBitrate: audioBitrate ?? null,
videoBitrate: videoBitrate ?? null,
+ bufSize: maxRate ?? null,
+ maxRate: bufSize ?? null,
crf: crf ?? null,
pixelFormat: pixelFormat ?? RenderInternals.DEFAULT_PIXEL_FORMAT,
imageFormat: imageFormat ?? RenderInternals.DEFAULT_VIDEO_IMAGE_FORMAT,
@@ -305,6 +313,8 @@ export const internalRenderMediaOnCloudrun = PureJSAPIs.wrapWithErrorHandling(
* @param params.audioCodec The encoding of the audio of the output video.
* @param params.audioBitrate The target bitrate for the audio of the generated video.
* @param params.videoBitrate The target bitrate of the generated video.
+ * @param params.bufSize The decoder buffer size, which determines the variability of the generated video bitrate.
+ * @param params.maxRate The maximum bitrate tolerance to be used, this is only used in conjunction with bufsize.
* @param params.proResProfile Sets a ProRes profile. Only applies to videos rendered with prores codec.
* @param params.x264Preset Sets a Preset profile. Only applies to videos rendered with h.264 codec.
* @param params.crf Constant Rate Factor, controlling the quality.
@@ -342,6 +352,8 @@ export const renderMediaOnCloudrun = ({
audioCodec,
audioBitrate,
videoBitrate,
+ maxRate,
+ bufSize,
proResProfile,
x264Preset,
crf,
@@ -382,6 +394,8 @@ export const renderMediaOnCloudrun = ({
audioCodec: audioCodec ?? undefined,
audioBitrate: audioBitrate ?? null,
videoBitrate: videoBitrate ?? null,
+ maxRate: maxRate ?? null,
+ bufSize: bufSize ?? null,
proResProfile: proResProfile ?? undefined,
x264Preset: x264Preset ?? undefined,
crf: crf ?? null,
diff --git a/packages/cloudrun/src/cli/commands/render/index.ts b/packages/cloudrun/src/cli/commands/render/index.ts
index d82e82acc1..c933c44bd8 100644
--- a/packages/cloudrun/src/cli/commands/render/index.ts
+++ b/packages/cloudrun/src/cli/commands/render/index.ts
@@ -59,6 +59,8 @@ export const renderCommand = async (
muted,
audioBitrate,
videoBitrate,
+ maxRate,
+ bufSize,
height,
width,
browserExecutable,
@@ -196,6 +198,8 @@ ${downloadName ? ` Downloaded File = ${downloadName}` : ''}
audioCodec,
audioBitrate,
videoBitrate,
+ maxRate,
+ bufSize,
proResProfile,
x264Preset,
crf,
diff --git a/packages/cloudrun/src/functions/helpers/payloads.ts b/packages/cloudrun/src/functions/helpers/payloads.ts
index cdeb8758b5..adaa565c9f 100644
--- a/packages/cloudrun/src/functions/helpers/payloads.ts
+++ b/packages/cloudrun/src/functions/helpers/payloads.ts
@@ -31,6 +31,8 @@ export const CloudRunPayload = z.discriminatedUnion('type', [
audioCodec: audioCodec.nullable(),
audioBitrate: z.string().nullable(),
videoBitrate: z.string().nullable(),
+ maxRate: z.string().nullable(),
+ bufSize: z.string().nullable(),
crf: z.number().nullable(),
pixelFormat,
imageFormat: videoImageFormat,
diff --git a/packages/cloudrun/src/functions/render-media-single-thread.ts b/packages/cloudrun/src/functions/render-media-single-thread.ts
index 8356b1f9f4..4f3692efe7 100644
--- a/packages/cloudrun/src/functions/render-media-single-thread.ts
+++ b/packages/cloudrun/src/functions/render-media-single-thread.ts
@@ -67,6 +67,8 @@ export const renderMediaSingleThread = async (
audioCodec: body.audioCodec,
audioBitrate: body.audioBitrate,
videoBitrate: body.videoBitrate,
+ maxRate: body.maxRate,
+ bufSize: body.bufSize,
crf: body.crf,
pixelFormat: body.pixelFormat,
imageFormat: body.imageFormat,
diff --git a/packages/cloudrun/src/gcpInstaller/gcpInstaller.tar b/packages/cloudrun/src/gcpInstaller/gcpInstaller.tar
index d221ca9448..777346fc2d 100644
Binary files a/packages/cloudrun/src/gcpInstaller/gcpInstaller.tar and b/packages/cloudrun/src/gcpInstaller/gcpInstaller.tar differ
diff --git a/packages/it-tests/src/monorepo/go-package.test.ts b/packages/it-tests/src/monorepo/go-package.test.ts
index d7b54fd5c1..2050e9a1d6 100644
--- a/packages/it-tests/src/monorepo/go-package.test.ts
+++ b/packages/it-tests/src/monorepo/go-package.test.ts
@@ -65,6 +65,8 @@ test("Go package should create the same payload as normal Lambda package", async
scale: 1,
timeoutInMilliseconds: 30000,
videoBitrate: null,
+ maxRate: null,
+ bufSize: null,
webhook: null,
x264Preset: null,
inputProps: {},
diff --git a/packages/it-tests/src/monorepo/php-package.test.ts b/packages/it-tests/src/monorepo/php-package.test.ts
index 74d1a47877..2d0460a0d1 100644
--- a/packages/it-tests/src/monorepo/php-package.test.ts
+++ b/packages/it-tests/src/monorepo/php-package.test.ts
@@ -96,6 +96,8 @@ test("PHP package should create the same renderMedia payload as normal Lambda pa
scale: 1,
timeoutInMilliseconds: 30000,
videoBitrate: null,
+ maxRate: null,
+ bufSize: null,
webhook: null,
x264Preset: null,
});
diff --git a/packages/it-tests/src/monorepo/python-package.test.ts b/packages/it-tests/src/monorepo/python-package.test.ts
index ed878ae9a2..734d008dd8 100644
--- a/packages/it-tests/src/monorepo/python-package.test.ts
+++ b/packages/it-tests/src/monorepo/python-package.test.ts
@@ -79,6 +79,8 @@ test("Python package should create the same renderMedia payload as normal Lambda
scale: 1,
timeoutInMilliseconds: 30000,
videoBitrate: null,
+ maxRate: null,
+ bufSize: null,
webhook: null,
x264Preset: null,
});
diff --git a/packages/lambda/src/api/make-lambda-payload.ts b/packages/lambda/src/api/make-lambda-payload.ts
index 6d42eb300c..250fd623a0 100644
--- a/packages/lambda/src/api/make-lambda-payload.ts
+++ b/packages/lambda/src/api/make-lambda-payload.ts
@@ -66,6 +66,8 @@ export type InnerRenderMediaOnLambdaInput = {
overwrite: boolean;
audioBitrate: string | null;
videoBitrate: string | null;
+ maxRate: string | null;
+ bufSize: string | null;
webhook: WebhookOption | null;
forceWidth: number | null;
forceHeight: number | null;
@@ -108,6 +110,8 @@ export const makeLambdaRenderMediaPayload = async ({
forceWidth,
webhook,
videoBitrate,
+ maxRate,
+ bufSize,
downloadBehavior,
muted,
overwrite,
@@ -170,6 +174,8 @@ export const makeLambdaRenderMediaPayload = async ({
overwrite: overwrite ?? false,
audioBitrate: audioBitrate ?? null,
videoBitrate: videoBitrate ?? null,
+ bufSize: bufSize ?? null,
+ maxRate: maxRate ?? null,
webhook: webhook ?? null,
forceHeight: forceHeight ?? null,
forceWidth: forceWidth ?? null,
diff --git a/packages/lambda/src/api/render-media-on-lambda.ts b/packages/lambda/src/api/render-media-on-lambda.ts
index 5f1324735a..298fa67ba1 100644
--- a/packages/lambda/src/api/render-media-on-lambda.ts
+++ b/packages/lambda/src/api/render-media-on-lambda.ts
@@ -62,6 +62,8 @@ export type RenderMediaOnLambdaInput = {
overwrite?: boolean;
audioBitrate?: string | null;
videoBitrate?: string | null;
+ maxRate?: string | null;
+ bufSize?: string | null;
webhook?: WebhookOption | null;
forceWidth?: number | null;
forceHeight?: number | null;
@@ -200,6 +202,8 @@ export const renderMediaOnLambda = (
serveUrl: options.serveUrl,
timeoutInMilliseconds: options.timeoutInMilliseconds ?? 30000,
videoBitrate: options.videoBitrate ?? null,
+ maxRate: options.maxRate ?? null,
+ bufSize: options.bufSize ?? null,
webhook: options.webhook ?? null,
x264Preset: options.x264Preset ?? null,
deleteAfter: options.deleteAfter ?? null,
diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.ts
index 4451330910..711b0a3538 100644
--- a/packages/lambda/src/cli/commands/render/render.ts
+++ b/packages/lambda/src/cli/commands/render/render.ts
@@ -76,6 +76,8 @@ export const renderCommand = async (
overwrite,
audioBitrate,
videoBitrate,
+ maxRate,
+ bufSize,
height,
width,
browserExecutable,
@@ -197,6 +199,8 @@ export const renderCommand = async (
overwrite,
audioBitrate,
videoBitrate,
+ bufSize,
+ maxRate,
forceHeight: height,
forceWidth: width,
webhook: parsedLambdaCli.webhook
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index e1a0dfc2ba..d5abb2fa92 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -271,6 +271,8 @@ const innerLaunchHandler = async ({
muted: params.muted,
audioBitrate: params.audioBitrate,
videoBitrate: params.videoBitrate,
+ maxRate: params.maxRate,
+ bufSize: params.bufSize,
launchFunctionConfig: {
version: VERSION,
},
diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.ts
index 10716dfd64..62c05736bf 100644
--- a/packages/lambda/src/functions/renderer.ts
+++ b/packages/lambda/src/functions/renderer.ts
@@ -207,6 +207,8 @@ const renderHandler = async (
enforceAudioTrack: true,
audioBitrate: params.audioBitrate,
videoBitrate: params.videoBitrate,
+ bufSize: params.bufSize,
+ maxRate: params.maxRate,
// Lossless flag takes priority over audio codec
// https://github.com/remotion-dev/remotion/issues/1647
// Special flag only in Lambda renderer which improves the audio quality
diff --git a/packages/lambda/src/functions/start.ts b/packages/lambda/src/functions/start.ts
index 443bdf55b1..1acab324d5 100644
--- a/packages/lambda/src/functions/start.ts
+++ b/packages/lambda/src/functions/start.ts
@@ -96,6 +96,8 @@ export const startHandler = async (params: LambdaPayload, options: Options) => {
webhook: params.webhook,
audioBitrate: params.audioBitrate,
videoBitrate: params.videoBitrate,
+ bufSize: params.bufSize,
+ maxRate: params.maxRate,
forceHeight: params.forceHeight,
forceWidth: params.forceWidth,
rendererFunctionName: params.rendererFunctionName,
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index 3b8c65221c..19650fcff0 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -269,6 +269,8 @@ export type LambdaStartPayload = {
overwrite: boolean;
audioBitrate: string | null;
videoBitrate: string | null;
+ maxRate: string | null;
+ bufSize: string | null;
webhook: WebhookOption;
forceHeight: number | null;
forceWidth: number | null;
@@ -325,6 +327,8 @@ export type LambdaPayloads = {
overwrite: boolean;
audioBitrate: string | null;
videoBitrate: string | null;
+ maxRate: string | null;
+ bufSize: string | null;
webhook: WebhookOption;
forceHeight: number | null;
forceWidth: number | null;
@@ -367,6 +371,8 @@ export type LambdaPayloads = {
muted: boolean;
audioBitrate: string | null;
videoBitrate: string | null;
+ bufSize: string | null;
+ maxRate: string | null;
launchFunctionConfig: {
version: string;
};
diff --git a/packages/lambda/src/test/integration/renders/gif.test.ts b/packages/lambda/src/test/integration/renders/gif.test.ts
index cc025e2507..e46e53762c 100644
--- a/packages/lambda/src/test/integration/renders/gif.test.ts
+++ b/packages/lambda/src/test/integration/renders/gif.test.ts
@@ -58,6 +58,8 @@ test('Should make a distributed GIF', async () => {
webhook: null,
audioBitrate: null,
videoBitrate: null,
+ bufSize: null,
+ maxRate: null,
forceHeight: null,
forceWidth: null,
rendererFunctionName: null,
diff --git a/packages/lambda/src/test/integration/renders/old-version.test.ts b/packages/lambda/src/test/integration/renders/old-version.test.ts
index 17a7e75eba..306ee54836 100644
--- a/packages/lambda/src/test/integration/renders/old-version.test.ts
+++ b/packages/lambda/src/test/integration/renders/old-version.test.ts
@@ -55,6 +55,8 @@ test('Should fail when using an incompatible version', async () => {
webhook: null,
audioBitrate: null,
videoBitrate: null,
+ bufSize: null,
+ maxRate: null,
forceHeight: null,
forceWidth: null,
rendererFunctionName: null,
diff --git a/packages/lambda/src/test/integration/renders/other-bucket.test.ts b/packages/lambda/src/test/integration/renders/other-bucket.test.ts
index 7458f91fd0..cb3e719131 100644
--- a/packages/lambda/src/test/integration/renders/other-bucket.test.ts
+++ b/packages/lambda/src/test/integration/renders/other-bucket.test.ts
@@ -61,6 +61,8 @@ test('Should be able to render to another bucket', async () => {
webhook: null,
audioBitrate: null,
videoBitrate: null,
+ bufSize: null,
+ maxRate: null,
forceHeight: null,
forceWidth: null,
rendererFunctionName: null,
diff --git a/packages/lambda/src/test/integration/renders/silent-audio.test.ts b/packages/lambda/src/test/integration/renders/silent-audio.test.ts
index 2272c58502..3cdb553310 100644
--- a/packages/lambda/src/test/integration/renders/silent-audio.test.ts
+++ b/packages/lambda/src/test/integration/renders/silent-audio.test.ts
@@ -57,6 +57,8 @@ test('Should add silent audio if there is no audio', async () => {
webhook: null,
audioBitrate: null,
videoBitrate: null,
+ maxRate: null,
+ bufSize: null,
forceHeight: null,
forceWidth: null,
rendererFunctionName: null,
diff --git a/packages/lambda/src/test/integration/renders/transparent-webm.test.ts b/packages/lambda/src/test/integration/renders/transparent-webm.test.ts
index 3957211bfc..7d31abd0d7 100644
--- a/packages/lambda/src/test/integration/renders/transparent-webm.test.ts
+++ b/packages/lambda/src/test/integration/renders/transparent-webm.test.ts
@@ -61,6 +61,8 @@ test('Should make a transparent video', async () => {
webhook: null,
audioBitrate: null,
videoBitrate: null,
+ bufSize: null,
+ maxRate: null,
forceHeight: null,
forceWidth: null,
rendererFunctionName: null,
diff --git a/packages/lambda/src/test/integration/webhooks.test.ts b/packages/lambda/src/test/integration/webhooks.test.ts
index 05aa006905..47433f0698 100644
--- a/packages/lambda/src/test/integration/webhooks.test.ts
+++ b/packages/lambda/src/test/integration/webhooks.test.ts
@@ -100,6 +100,8 @@ describe('Webhooks', () => {
},
audioBitrate: null,
videoBitrate: null,
+ bufSize: null,
+ maxRate: null,
forceHeight: null,
forceWidth: null,
rendererFunctionName: null,
@@ -198,6 +200,8 @@ describe('Webhooks', () => {
},
audioBitrate: null,
videoBitrate: null,
+ bufSize: null,
+ maxRate: null,
bucketName: 'abc',
renderId: 'abc',
forceHeight: null,
diff --git a/packages/renderer/src/crf.ts b/packages/renderer/src/crf.ts
index 4fe83fb884..52c792d61d 100644
--- a/packages/renderer/src/crf.ts
+++ b/packages/renderer/src/crf.ts
@@ -71,10 +71,14 @@ export const validateQualitySettings = ({
codec,
crf,
videoBitrate,
+ maxRate,
+ bufSize
}: {
crf: unknown;
codec: Codec;
videoBitrate: string | null | undefined;
+ maxRate: string | null | undefined;
+ bufSize: string | null | undefined;
}): string[] => {
if (crf && videoBitrate) {
throw new Error(
@@ -82,6 +86,15 @@ export const validateQualitySettings = ({
);
}
+ if (maxRate && !bufSize) {
+ throw new Error(
+ '"maxRate" can not be set without "bufSize".',
+ );
+ }
+
+ const bufSizeArray = bufSize ? ['-bufsize', bufSize] : [];
+ const maxRateArray = maxRate ? ['-maxrate', maxRate] : [];
+
if (videoBitrate) {
if (codec === 'prores') {
console.warn('ProRes does not support videoBitrate. Ignoring.');
@@ -93,13 +106,12 @@ export const validateQualitySettings = ({
return [];
}
- return ['-b:v', videoBitrate];
+ return ['-b:v', videoBitrate, ...bufSizeArray, ...maxRateArray];
}
if (crf === null || typeof crf === 'undefined') {
const actualCrf = getDefaultCrfForCodec(codec);
-
- return ['-crf', String(actualCrf)];
+ return ['-crf', String(actualCrf), ...bufSizeArray, ...maxRateArray];
}
if (typeof crf !== 'number') {
@@ -137,5 +149,5 @@ export const validateQualitySettings = ({
return [];
}
- return ['-crf', String(crf)];
+ return ['-crf', String(crf), ...bufSizeArray, ...maxRateArray];
};
diff --git a/packages/renderer/src/ffmpeg-args.ts b/packages/renderer/src/ffmpeg-args.ts
index 6ba1fde7e0..fa96f2127b 100644
--- a/packages/renderer/src/ffmpeg-args.ts
+++ b/packages/renderer/src/ffmpeg-args.ts
@@ -14,6 +14,8 @@ const firstEncodingStepOnly = ({
codec,
crf,
videoBitrate,
+ maxRate,
+ bufSize
}: {
hasPreencoded: boolean;
proResProfileName: string | null;
@@ -22,6 +24,8 @@ const firstEncodingStepOnly = ({
crf: unknown;
codec: Codec;
videoBitrate: string | null | undefined;
+ maxRate: string | null | undefined;
+ bufSize: string | null | undefined;
}): string[][] => {
if (hasPreencoded || codec === 'gif') {
return [];
@@ -39,6 +43,8 @@ const firstEncodingStepOnly = ({
crf,
videoBitrate,
codec,
+ maxRate,
+ bufSize
}),
].filter(truthy);
};
@@ -51,6 +57,8 @@ export const generateFfmpegArgs = ({
codec,
crf,
videoBitrate,
+ maxRate,
+ bufSize,
colorSpace,
}: {
hasPreencoded: boolean;
@@ -60,6 +68,8 @@ export const generateFfmpegArgs = ({
crf: unknown;
codec: Codec;
videoBitrate: string | null | undefined;
+ maxRate: string | null | undefined;
+ bufSize: string | null | undefined;
colorSpace: ColorSpace;
}): string[][] => {
const encoderName = getCodecName(codec);
@@ -90,6 +100,8 @@ export const generateFfmpegArgs = ({
pixelFormat,
proResProfileName,
videoBitrate,
+ maxRate,
+ bufSize,
x264Preset,
}),
].filter(truthy);
diff --git a/packages/renderer/src/prespawn-ffmpeg.ts b/packages/renderer/src/prespawn-ffmpeg.ts
index 1e95e1c6ed..1466a8e726 100644
--- a/packages/renderer/src/prespawn-ffmpeg.ts
+++ b/packages/renderer/src/prespawn-ffmpeg.ts
@@ -50,6 +50,8 @@ type PreStitcherOptions = {
ffmpegOverride: FfmpegOverrideFn;
signal: CancelSignal;
videoBitrate: string | null;
+ maxRate: string | null;
+ bufSize: string | null;
indent: boolean;
colorSpace: ColorSpace;
};
@@ -98,6 +100,8 @@ export const prespawnFfmpeg = (options: PreStitcherOptions) => {
codec,
crf: options.crf,
videoBitrate: options.videoBitrate,
+ maxRate: options.maxRate,
+ bufSize: options.bufSize,
colorSpace: options.colorSpace,
}),
diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.ts
index 58e832dce9..70aec4bf33 100644
--- a/packages/renderer/src/render-media.ts
+++ b/packages/renderer/src/render-media.ts
@@ -121,6 +121,8 @@ export type InternalRenderMediaOptions = {
ffmpegOverride: FfmpegOverrideFn | undefined;
audioBitrate: string | null;
videoBitrate: string | null;
+ maxRate: string | null;
+ bufSize: string | null;
disallowParallelEncoding: boolean;
audioCodec: AudioCodec | null;
serveUrl: string;
@@ -173,6 +175,8 @@ export type RenderMediaOptions = {
ffmpegOverride?: FfmpegOverrideFn;
audioBitrate?: string | null;
videoBitrate?: string | null;
+ maxRate?: string | null;
+ bufSize?: string | null;
disallowParallelEncoding?: boolean;
audioCodec?: AudioCodec | null;
serveUrl: string;
@@ -217,6 +221,8 @@ const internalRenderMediaRaw = ({
ffmpegOverride,
audioBitrate,
videoBitrate,
+ maxRate,
+ bufSize,
audioCodec,
concurrency,
disallowParallelEncoding,
@@ -235,7 +241,7 @@ const internalRenderMediaRaw = ({
colorSpace,
}: InternalRenderMediaOptions): Promise<RenderMediaResult> => {
validateJpegQuality(jpegQuality);
- validateQualitySettings({crf, codec, videoBitrate});
+ validateQualitySettings({crf, codec, videoBitrate, maxRate, bufSize});
validateBitrate(audioBitrate, 'audioBitrate');
validateBitrate(videoBitrate, 'videoBitrate');
@@ -447,6 +453,8 @@ const internalRenderMediaRaw = ({
signal: cancelPrestitcher.cancelSignal,
ffmpegOverride: ffmpegOverride ?? (({args}) => args),
videoBitrate,
+ maxRate,
+ bufSize,
indent,
x264Preset: x264Preset ?? null,
colorSpace,
@@ -657,6 +665,8 @@ const internalRenderMediaRaw = ({
ffmpegOverride: ffmpegOverride ?? null,
audioBitrate,
videoBitrate,
+ bufSize,
+ maxRate,
audioCodec,
x264Preset: x264Preset ?? null,
colorSpace,
@@ -768,6 +778,8 @@ export const renderMedia = ({
ffmpegOverride,
audioBitrate,
videoBitrate,
+ maxRate,
+ bufSize,
audioCodec,
jpegQuality,
concurrency,
@@ -831,6 +843,8 @@ export const renderMedia = ({
scale: scale ?? 1,
timeoutInMilliseconds: timeoutInMilliseconds ?? DEFAULT_TIMEOUT,
videoBitrate: videoBitrate ?? null,
+ maxRate: maxRate ?? null,
+ bufSize: bufSize ?? null,
logLevel:
verbose || dumpBrowserLogs ? 'verbose' : logLevel ?? getLogLevel(),
preferLossless: preferLossless ?? false,
diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.ts
index 574024fda7..4076f52263 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -56,6 +56,8 @@ const packageJson = fs.existsSync(packageJsonPath)
type InternalStitchFramesToVideoOptions = {
audioBitrate: string | null;
videoBitrate: string | null;
+ maxRate: string | null;
+ bufSize: string | null;
fps: number;
width: number;
height: number;
@@ -86,6 +88,8 @@ type InternalStitchFramesToVideoOptions = {
export type StitchFramesToVideoOptions = {
audioBitrate?: string | null;
videoBitrate?: string | null;
+ maxRate?: string | null;
+ bufSize?: string | null;
fps: number;
width: number;
height: number;
@@ -227,6 +231,8 @@ const innerStitchFramesToVideo = async (
proResProfile,
logLevel,
videoBitrate,
+ maxRate,
+ bufSize,
width,
numberOfGifLoops,
onProgress,
@@ -251,7 +257,9 @@ const innerStitchFramesToVideo = async (
validateBitrate(audioBitrate, 'audioBitrate');
validateBitrate(videoBitrate, 'videoBitrate');
-
+ validateBitrate(maxRate, 'maxRate');
+ // bufSize is not a bitrate but need to be validated using the same format
+ validateBitrate(bufSize, 'bufSize');
validateFps(fps, 'in `stitchFramesToVideo()`', false);
const proResProfileName = getProResProfileName(codec, proResProfile);
@@ -333,6 +341,8 @@ const innerStitchFramesToVideo = async (
crf,
codec,
videoBitrate,
+ maxRate,
+ bufSize,
});
validateSelectedPixelFormatAndCodecCombination(pixelFormat, codec);
@@ -429,6 +439,8 @@ const innerStitchFramesToVideo = async (
codec,
crf,
videoBitrate,
+ maxRate,
+ bufSize,
hasPreencoded: Boolean(preEncodedFileLocation),
proResProfileName,
pixelFormat,
@@ -579,12 +591,16 @@ export const stitchFramesToVideo = ({
proResProfile,
verbose,
videoBitrate,
+ maxRate,
+ bufSize,
x264Preset,
colorSpace,
}: StitchFramesToVideoOptions): Promise<Buffer | null> => {
return internalStitchFramesToVideo({
assetsInfo,
audioBitrate: audioBitrate ?? null,
+ maxRate: maxRate ?? null,
+ bufSize: bufSize ?? null,
audioCodec: audioCodec ?? null,
cancelSignal: cancelSignal ?? null,
codec: codec ?? DEFAULT_CODEC,
diff --git a/packages/renderer/src/test/crf.test.ts b/packages/renderer/src/test/crf.test.ts
index 47068b50fa..12f6f0aed2 100644
--- a/packages/renderer/src/test/crf.test.ts
+++ b/packages/renderer/src/test/crf.test.ts
@@ -46,6 +46,8 @@ describe('validateSelectedCrfAndCodecCombination valid input', () => {
crf: entry[0],
codec: entry[1],
videoBitrate: undefined,
+ maxRate: undefined,
+ bufSize: undefined
}),
).not.toThrow()),
);
@@ -74,6 +76,8 @@ describe('validateSelectedCrfAndCodecCombination invalid input', () => {
crf: entry[0],
codec: entry[1],
videoBitrate: undefined,
+ maxRate: undefined,
+ bufSize: undefined
}),
).toThrow(
new RegExp(
@@ -85,24 +89,32 @@ describe('validateSelectedCrfAndCodecCombination invalid input', () => {
test('ProRes', () => {
expect(() =>
- validateQualitySettings({crf: 3, codec: 'prores', videoBitrate: undefined}),
+ validateQualitySettings({crf: 3, codec: 'prores', videoBitrate: undefined, maxRate: undefined, bufSize: undefined}),
).toThrow(/The "prores" codec does not support the --crf option\./);
});
test('WAV', () => {
expect(() =>
- validateQualitySettings({crf: 3, codec: 'wav', videoBitrate: undefined}),
+ validateQualitySettings({crf: 3, codec: 'wav', videoBitrate: undefined, maxRate: undefined, bufSize: undefined}),
).toThrow(/The "wav" codec does not support the --crf option\./);
});
test('WAV', () => {
expect(() =>
- validateQualitySettings({crf: 10, codec: 'h264', videoBitrate: '1M'}),
+ validateQualitySettings({crf: 10, codec: 'h264', videoBitrate: '1M', maxRate: undefined, bufSize: undefined}),
).toThrow(
/"crf" and "videoBitrate" can not both be set. Choose one of either./,
);
});
+test('maxRate', () => {
+ expect(() =>
+ validateQualitySettings({crf: 10, codec: 'h264', videoBitrate: undefined, maxRate: '1M', bufSize: undefined}),
+ ).toThrow(
+ /"maxRate" can not be set without "bufSize"./,
+ );
+});
+
describe('crf tests getValidCrfRanges invalid input', () => {
// input codec
const invalidInputs = ['abc', '', 3, undefined];
diff --git a/packages/renderer/src/validate-videobitrate.ts b/packages/renderer/src/validate-videobitrate.ts
index a8ae144eb0..0dbb02ab34 100644
--- a/packages/renderer/src/validate-videobitrate.ts
+++ b/packages/renderer/src/validate-videobitrate.ts
@@ -15,6 +15,7 @@ export const validateBitrate = (bitrate: unknown, name: string) => {
);
}
+
if (
!bitrate.endsWith('K') &&
!bitrate.endsWith('k') &&