Skip to content

🎥 Make videos programmatically with React

crf

Jonny Burger1815b4d5260f
diff --git a/packages/cli/src/benchmark.ts b/packages/cli/src/benchmark.ts
index eca5aef02d..494d67fe3b 100644
--- a/packages/cli/src/benchmark.ts
+++ b/packages/cli/src/benchmark.ts
@@ -27,6 +27,7 @@ const {
x264Option,
offthreadVideoCacheSizeInBytesOption,
scaleOption,
+ crfOption,
} = BrowserSafeApis.options;
const getValidConcurrency = (cliConcurrency: number | string | null) => {
@@ -177,7 +178,6 @@ export const benchmarkCommand = async (
frameRange: defaultFrameRange,
overwrite,
jpegQuality,
- crf: configFileCrf,
pixelFormat,
numberOfGifLoops,
everyNthFrame,
@@ -304,6 +304,7 @@ export const benchmarkCommand = async (
const {value: audioBitrate} = audioBitrateOption.getValue({
commandLine: parsedCli,
});
+ const configFileCrf = crfOption.getValue({commandLine: parsedCli}).value;
for (const composition of compositions) {
const {codec, reason: codecReason} = getFinalOutputCodec({
@@ -390,11 +391,9 @@ export const benchmarkCommand = async (
staticBase: null,
}).serializedString,
offthreadVideoCacheSizeInBytes:
- BrowserSafeApis.options.offthreadVideoCacheSizeInBytesOption.getValue(
- {
- commandLine: parsedCli,
- },
- ).value,
+ offthreadVideoCacheSizeInBytesOption.getValue({
+ commandLine: parsedCli,
+ }).value,
colorSpace,
repro: false,
finishRenderProgress: () => undefined,
diff --git a/packages/cli/src/config/crf.ts b/packages/cli/src/config/crf.ts
deleted file mode 100644
index 255c59c6ce..0000000000
--- a/packages/cli/src/config/crf.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import type {Crf} from '@remotion/renderer';
-
-let currentCrf: Crf;
-
-export const setCrf = (newCrf: Crf) => {
- if (typeof newCrf !== 'number' && newCrf !== undefined) {
- throw new TypeError('The CRF must be a number or undefined.');
- }
-
- currentCrf = newCrf;
-};
-
-export const getCrfOrUndefined = () => {
- return currentCrf;
-};
diff --git a/packages/cli/src/config/index.ts b/packages/cli/src/config/index.ts
index b48a194afb..834f0ffdb9 100644
--- a/packages/cli/src/config/index.ts
+++ b/packages/cli/src/config/index.ts
@@ -80,7 +80,6 @@ import {setCodec} from './codec';
import {getColorSpace, setColorSpace} from './color-space';
import type {Concurrency} from './concurrency';
import {setConcurrency} from './concurrency';
-import {getCrfOrUndefined, setCrf} from './crf';
import {
getEnforceAudioTrack,
setEnforceAudioTrack,
@@ -131,6 +130,7 @@ const {
x264Option,
audioBitrateOption,
scaleOption,
+ crfOption,
} = BrowserSafeApis.options;
declare global {
@@ -601,7 +601,9 @@ export const Config: FlatConfig = {
setOverwriteOutput,
setPixelFormat,
setCodec,
- setCrf,
+ setCrf: (crf) => {
+ crfOption.setConfig(crf);
+ },
setImageSequence,
setProResProfile,
setX264Preset: (preset) => {
@@ -672,7 +674,6 @@ export const ConfigInternals = {
getEncodingMaxRate,
getHeight,
getWidth,
- getCrfOrUndefined,
getEntryPoint,
getNumberOfGifLoops,
getWebpackPolling,
diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.ts
index 9ac39d38e5..159d4594d7 100644
--- a/packages/cli/src/get-cli-options.ts
+++ b/packages/cli/src/get-cli-options.ts
@@ -56,14 +56,6 @@ const getAndValidateShouldOutputImageSequence = ({
return shouldOutputImageSequence;
};
-const getCrf = (shouldOutputImageSequence: boolean) => {
- const crf = shouldOutputImageSequence
- ? null
- : ConfigInternals.getCrfOrUndefined();
-
- return crf;
-};
-
const getProResProfile = () => {
const proResProfile = ConfigInternals.getProResProfile();
@@ -88,7 +80,6 @@ export const getCliOptions = (options: {
const overwrite = ConfigInternals.getShouldOverwrite({
defaultValue: !options.isLambda,
});
- const crf = getCrf(shouldOutputImageSequence);
const videoBitrate = ConfigInternals.getVideoBitrate();
const encodingBufferSize = ConfigInternals.getEncodingBufferSize();
const encodingMaxRate = ConfigInternals.getEncodingMaxRate();
@@ -128,7 +119,6 @@ export const getCliOptions = (options: {
inputProps: getInputProps(null, options.logLevel),
envVariables: getEnvironmentVariables(null, options.logLevel, false),
jpegQuality: ConfigInternals.getJpegQuality(),
- crf,
pixelFormat,
proResProfile,
everyNthFrame,
diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsx
index 6742114835..cc2a36e708 100644
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@ -18,6 +18,7 @@ const {
audioBitrateOption,
offthreadVideoCacheSizeInBytesOption,
scaleOption,
+ crfOption,
} = BrowserSafeApis.options;
export const render = async (
@@ -64,7 +65,6 @@ export const render = async (
publicDir,
height,
width,
- crf,
ffmpegOverride,
muted,
enforceAudioTrack,
@@ -83,15 +83,18 @@ export const render = async (
logLevel,
});
- const {value: x264Preset} = x264Option.getValue({commandLine: parsedCli});
- const {value: audioBitrate} = audioBitrateOption.getValue({
+ const x264Preset = x264Option.getValue({commandLine: parsedCli}).value;
+ const audioBitrate = audioBitrateOption.getValue({
commandLine: parsedCli,
- });
- const {value: offthreadVideoCacheSizeInBytes} =
+ }).value;
+ const offthreadVideoCacheSizeInBytes =
offthreadVideoCacheSizeInBytesOption.getValue({
commandLine: parsedCli,
- });
- const {value: scale} = scaleOption.getValue({commandLine: parsedCli});
+ }).value;
+ const scale = scaleOption.getValue({commandLine: parsedCli}).value;
+ const crf = shouldOutputImageSequence
+ ? null
+ : crfOption.getValue({commandLine: parsedCli}).value;
const audioCodec = getResolvedAudioCodec();
diff --git a/packages/cli/src/test/crf.test.ts b/packages/cli/src/test/crf.test.ts
index decbd07cfc..d510a398aa 100644
--- a/packages/cli/src/test/crf.test.ts
+++ b/packages/cli/src/test/crf.test.ts
@@ -1,5 +1,5 @@
+import {BrowserSafeApis} from '@remotion/renderer/client';
import {describe, test} from 'vitest';
-import {setCrf} from '../config/crf';
import {expectToThrow} from './expect-to-throw';
describe('set crf invalid input', () => {
@@ -8,7 +8,7 @@ describe('set crf invalid input', () => {
test(`test for ${entry}`, () =>
expectToThrow(
// @ts-expect-error
- () => setCrf(entry),
+ () => BrowserSafeApis.options.crfOption.setConfig(entry),
/The CRF must be a number or undefined/,
)),
);
diff --git a/packages/cloudrun/src/cli/commands/render/index.ts b/packages/cloudrun/src/cli/commands/render/index.ts
index b8fbf2a1c5..256b55bf97 100644
--- a/packages/cloudrun/src/cli/commands/render/index.ts
+++ b/packages/cloudrun/src/cli/commands/render/index.ts
@@ -20,6 +20,7 @@ const {
x264Option,
offthreadVideoCacheSizeInBytesOption,
scaleOption,
+ crfOption,
} = BrowserSafeApis.options;
export const renderCommand = async (
@@ -52,7 +53,6 @@ export const renderCommand = async (
const {
chromiumOptions,
- crf,
envVariables,
frameRange,
inputProps,
@@ -199,6 +199,9 @@ ${downloadName ? ` Downloaded File = ${downloadName}` : ''}
const scale = scaleOption.getValue({
commandLine: CliInternals.parsedCli,
}).value;
+ const crf = crfOption.getValue({
+ commandLine: CliInternals.parsedCli,
+ }).value;
const res = await internalRenderMediaOnCloudrun({
cloudRunUrl,
serviceName: undefined,
diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.ts
index 391055dbbf..a8a2ffde85 100644
--- a/packages/lambda/src/cli/commands/render/render.ts
+++ b/packages/lambda/src/cli/commands/render/render.ts
@@ -45,6 +45,7 @@ const {
audioBitrateOption,
offthreadVideoCacheSizeInBytesOption,
scaleOption,
+ crfOption,
} = BrowserSafeApis.options;
export const renderCommand = async (
@@ -69,7 +70,6 @@ export const renderCommand = async (
const {
chromiumOptions,
- crf,
envVariables,
frameRange,
inputProps,
@@ -109,6 +109,9 @@ export const renderCommand = async (
const scale = scaleOption.getValue({
commandLine: CliInternals.parsedCli,
}).value;
+ const crf = crfOption.getValue({
+ commandLine: CliInternals.parsedCli,
+ }).value;
let composition: string = args[1];
if (!composition) {
diff --git a/packages/renderer/src/options/crf.tsx b/packages/renderer/src/options/crf.tsx
index 87feb48682..bf6718644e 100644
--- a/packages/renderer/src/options/crf.tsx
+++ b/packages/renderer/src/options/crf.tsx
@@ -1,8 +1,19 @@
+import type {Crf} from '../crf';
import type {AnyRemotionOption} from './option';
+let currentCrf: Crf;
+
+export const validateCrf = (newCrf: Crf) => {
+ if (typeof newCrf !== 'number' && newCrf !== undefined) {
+ throw new TypeError('The CRF must be a number or undefined.');
+ }
+};
+
+const cliFlag = 'crf' as const;
+
export const crfOption = {
name: 'CRF',
- cliFlag: 'crf' as const,
+ cliFlag,
description: () => (
<>
No matter which codec you end up using, there&apos;s always a tradeoff
@@ -16,4 +27,29 @@ export const crfOption = {
docLink:
'https://www.remotion.dev/docs/encoding/#controlling-quality-using-the-crf-setting',
type: 0 as number,
-} satisfies AnyRemotionOption<number>;
+ getValue: ({commandLine}) => {
+ if (commandLine[cliFlag] !== undefined) {
+ validateCrf(commandLine[cliFlag] as number);
+ return {
+ source: 'cli',
+ value: commandLine[cliFlag] as Crf,
+ };
+ }
+
+ if (currentCrf !== null) {
+ return {
+ source: 'config',
+ value: currentCrf,
+ };
+ }
+
+ return {
+ source: 'default',
+ value: undefined,
+ };
+ },
+ setConfig: (crf) => {
+ validateCrf(crf);
+ currentCrf = crf;
+ },
+} satisfies AnyRemotionOption<Crf>;