Skip to content

🎥 Make videos programmatically with React

alright

Jonny Burger1d8965ed8683
diff --git a/packages/cli/src/benchmark.ts b/packages/cli/src/benchmark.ts
index 2bb41088ab..b030afa3c5 100644
--- a/packages/cli/src/benchmark.ts
+++ b/packages/cli/src/benchmark.ts
@@ -9,7 +9,6 @@ import {getRendererPortFromConfigFileAndCliFlag} from './config/preview-server';
import {convertEntryPointToServeUrl} from './convert-entry-point-to-serve-url';
import {findEntryPoint} from './entry-point';
import {getCliOptions} from './get-cli-options';
-import {getFinalOutputCodec} from './get-final-output-codec';
import {getVideoImageFormat} from './image-formats';
import {Log} from './log';
import {makeProgressBar} from './make-progress-bar';
@@ -32,6 +31,7 @@ const {
videoBitrateOption,
enforceAudioOption,
mutedOption,
+ videoCodecOption,
} = BrowserSafeApis.options;
const getValidConcurrency = (cliConcurrency: number | string | null) => {
@@ -317,14 +317,18 @@ export const benchmarkCommand = async (
const muted = mutedOption.getValue({commandLine: parsedCli}).value;
for (const composition of compositions) {
- const {codec, reason: codecReason} = getFinalOutputCodec({
- cliFlag: parsedCli.codec,
- downloadName: null,
- outName: null,
- configFile: ConfigInternals.getOutputCodecOrUndefined() ?? null,
- uiCodec: null,
- compositionCodec: composition.defaultCodec ?? null,
- });
+ const {value: videoCodec, source: codecReason} = videoCodecOption.getValue(
+ {
+ commandLine: parsedCli,
+ },
+ {
+ downloadName: null,
+ outName: null,
+ configFile: ConfigInternals.getOutputCodecOrUndefined() ?? null,
+ uiCodec: null,
+ compositionCodec: composition.defaultCodec ?? null,
+ },
+ );
const concurrency = getValidConcurrency(unparsedConcurrency);
benchmark[composition.id] = {};
@@ -339,7 +343,7 @@ export const benchmarkCommand = async (
Log.infoAdvanced(
{indent: false, logLevel},
`${chalk.bold(`Benchmark #${count++}:`)} ${chalk.gray(
- `composition=${composition.id} concurrency=${con} codec=${codec} (${codecReason})`,
+ `composition=${composition.id} concurrency=${con} codec=${videoCodec} (${codecReason})`,
)}`,
);
@@ -356,7 +360,7 @@ export const benchmarkCommand = async (
envVariables,
frameRange: defaultFrameRange,
imageFormat: getVideoImageFormat({
- codec,
+ codec: videoCodec,
uiImageFormat: null,
}),
serializedInputPropsWithCustomSchema,
@@ -377,7 +381,7 @@ export const benchmarkCommand = async (
browserExecutable,
ffmpegOverride,
serveUrl: bundleLocation,
- codec,
+ codec: videoCodec,
audioBitrate,
videoBitrate,
encodingMaxRate,
diff --git a/packages/cli/src/get-final-output-codec.ts b/packages/cli/src/get-final-output-codec.ts
deleted file mode 100644
index 3ec9892254..0000000000
--- a/packages/cli/src/get-final-output-codec.ts
+++ /dev/null
@@ -1,112 +0,0 @@
-import type {Codec, CodecOrUndefined, FileExtension} from '@remotion/renderer';
-import {RenderInternals} from '@remotion/renderer';
-
-const deriveCodecsFromFilename = (
- extension: string | null,
-): {
- possible: Codec[];
- default: Codec | null;
-} => {
- if (extension === null) {
- return {possible: [], default: null};
- }
-
- return {
- default:
- RenderInternals.defaultCodecsForFileExtension[
- extension as FileExtension
- ] ?? null,
- possible: RenderInternals.makeFileExtensionMap()[extension] ?? [],
- };
-};
-
-export const getFinalOutputCodec = ({
- cliFlag,
- configFile,
- downloadName,
- outName,
- uiCodec,
- compositionCodec,
-}: {
- cliFlag: CodecOrUndefined;
- outName: string | null;
- downloadName: string | null;
- configFile: Codec | null;
- uiCodec: Codec | null;
- compositionCodec: Codec | null;
-}): {codec: Codec; reason: string} => {
- if (uiCodec) {
- return {codec: uiCodec, reason: 'via UI'};
- }
-
- const downloadNameExtension =
- RenderInternals.getExtensionOfFilename(downloadName);
- const outNameExtension = RenderInternals.getExtensionOfFilename(outName);
-
- const derivedDownloadCodecs = deriveCodecsFromFilename(downloadNameExtension);
- const derivedOutNameCodecs = deriveCodecsFromFilename(outNameExtension);
-
- if (
- derivedDownloadCodecs.possible.length > 0 &&
- derivedOutNameCodecs.possible.length > 0 &&
- derivedDownloadCodecs.possible.join('') !==
- derivedOutNameCodecs.possible.join('')
- ) {
- throw new TypeError(
- `The download name is ${downloadName} but the output name is ${outName}. The file extensions must match`,
- );
- }
-
- if (cliFlag) {
- if (
- derivedDownloadCodecs.possible.length > 0 &&
- derivedDownloadCodecs.possible.indexOf(cliFlag) === -1
- ) {
- throw new TypeError(
- `The download name is ${downloadName} but --codec=${cliFlag} was passed. The download name implies a codec of ${derivedDownloadCodecs.possible.join(
- ' or ',
- )} which does not align with the --codec flag.`,
- );
- }
-
- if (
- derivedOutNameCodecs.possible.length > 0 &&
- derivedOutNameCodecs.possible.indexOf(cliFlag) === -1
- ) {
- throw new TypeError(
- `The out name is ${outName} but --codec=${cliFlag} was passed. The out name implies a codec of ${derivedOutNameCodecs.possible.join(
- ' or ',
- )} which does not align with the --codec flag.`,
- );
- }
-
- return {codec: cliFlag, reason: 'from --codec flag'};
- }
-
- if (derivedDownloadCodecs.possible.length > 0) {
- return {
- codec: derivedDownloadCodecs.default as Codec,
- reason: 'derived from download name',
- };
- }
-
- if (derivedOutNameCodecs.possible.length > 0) {
- return {
- codec: derivedOutNameCodecs.default as Codec,
- reason: 'derived from out name',
- };
- }
-
- if (compositionCodec) {
- return {codec: compositionCodec, reason: 'via calculateMetadata'};
- }
-
- if (configFile) {
- return {
- codec: configFile,
- reason: 'Config file',
- };
- }
-
- return {codec: RenderInternals.DEFAULT_CODEC, reason: 'default'};
-};
diff --git a/packages/cli/src/index.tsx b/packages/cli/src/index.tsx
index b39da22cae..23675dcd6e 100644
--- a/packages/cli/src/index.tsx
+++ b/packages/cli/src/index.tsx
@@ -14,7 +14,6 @@ import {ffmpegCommand, ffprobeCommand} from './ffmpeg';
import {getCliOptions} from './get-cli-options';
import {getCompositionWithDimensionOverride} from './get-composition-with-dimension-override';
import {loadConfig} from './get-config-file-name';
-import {getFinalOutputCodec} from './get-final-output-codec';
import {gpuCommand} from './gpu';
import {getVideoImageFormat} from './image-formats';
import {initializeCli} from './initialize-cli';
@@ -151,7 +150,6 @@ export const CliInternals = {
findEntryPoint,
getVideoImageFormat,
printCompositions,
- getFinalOutputCodec,
listOfRemotionPackages,
shouldUseNonOverlayingLogger,
getCompositionWithDimensionOverride,
diff --git a/packages/cli/src/render-flows/render.ts b/packages/cli/src/render-flows/render.ts
index 3dbb2be3ed..6ce6993ce1 100644
--- a/packages/cli/src/render-flows/render.ts
+++ b/packages/cli/src/render-flows/render.ts
@@ -17,6 +17,7 @@ import type {
X264Preset,
} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
+import {BrowserSafeApis} from '@remotion/renderer/client';
import type {
AggregateRenderProgress,
BundlingState,
@@ -37,7 +38,6 @@ import type {Loop} from '../config/number-of-gif-loops';
import {getAndValidateAbsoluteOutputFile} from '../get-cli-options';
import {getCompositionWithDimensionOverride} from '../get-composition-with-dimension-override';
import {getOutputFilename} from '../get-filename';
-import {getFinalOutputCodec} from '../get-final-output-codec';
import {getVideoImageFormat} from '../image-formats';
import {Log} from '../log';
import {makeOnDownload} from '../make-on-download';
@@ -292,17 +292,22 @@ export const renderVideoFlow = async ({
offthreadVideoCacheSizeInBytes,
});
- const {codec, reason: codecReason} = getFinalOutputCodec({
- cliFlag: parsedCli.codec,
- configFile: ConfigInternals.getOutputCodecOrUndefined() ?? null,
- downloadName: null,
- outName: getUserPassedOutputLocation(
- argsAfterComposition,
- outputLocationFromUI,
- ),
- uiCodec,
- compositionCodec: config.defaultCodec,
- });
+ const {value: codec, source: codecReason} =
+ BrowserSafeApis.options.videoCodecOption.getValue(
+ {
+ commandLine: parsedCli,
+ },
+ {
+ configFile: ConfigInternals.getOutputCodecOrUndefined() ?? null,
+ downloadName: null,
+ outName: getUserPassedOutputLocation(
+ argsAfterComposition,
+ outputLocationFromUI,
+ ),
+ uiCodec,
+ compositionCodec: config.defaultCodec,
+ },
+ );
RenderInternals.validateEvenDimensionsWithCodec({
width: config.width,
diff --git a/packages/cli/src/test/codec.test.ts b/packages/cli/src/test/codec.test.ts
index 1153a3c97f..823dba51ab 100644
--- a/packages/cli/src/test/codec.test.ts
+++ b/packages/cli/src/test/codec.test.ts
@@ -22,7 +22,7 @@ describe('Codec tests setCodec', () => {
test('setCodec with invalid coded', () => {
expectToThrow(
// @ts-expect-error
- () => setCodec('invalid'),
+ () => BrowserSafeApis.options.videoCodecOption.setConfig('invalid'),
/Codec must be one of the following:/,
);
});
diff --git a/packages/cli/src/test/final-output-codec.test.ts b/packages/cli/src/test/final-output-codec.test.ts
index bfefb720bf..1b3492fae8 100644
--- a/packages/cli/src/test/final-output-codec.test.ts
+++ b/packages/cli/src/test/final-output-codec.test.ts
@@ -1,6 +1,8 @@
import type {CodecOrUndefined} from '@remotion/renderer';
+import {BrowserSafeApis} from '@remotion/renderer/client';
import {describe, expect, test} from 'vitest';
-import {getFinalOutputCodec} from '../get-final-output-codec';
+
+const {videoCodecOption} = BrowserSafeApis.options;
describe('Codec tests valid codec input', () => {
const validCodecInput: CodecOrUndefined[] = [
@@ -16,17 +18,19 @@ describe('Codec tests valid codec input', () => {
validCodecInput.forEach((entry) =>
test(`codec ${entry}`, () =>
expect(
- getFinalOutputCodec({
- cliFlag: entry,
- outName: 'out',
- configFile: null,
- downloadName: null,
- uiCodec: null,
- compositionCodec: null,
- }),
+ videoCodecOption.getValue(
+ {commandLine: {codec: entry}},
+ {
+ outName: 'out',
+ configFile: null,
+ downloadName: null,
+ uiCodec: null,
+ compositionCodec: null,
+ },
+ ),
).toEqual({
- codec: entry,
- reason: 'from --codec flag',
+ value: entry,
+ source: 'from --codec flag',
})),
);
});
@@ -43,17 +47,21 @@ describe('Codec tests undefined codec input with known extension', () => {
codecExtensionCombination.forEach((entry) =>
test(`${entry[1]} should be recognized as ${entry[0]}`, () => {
return expect(
- getFinalOutputCodec({
- cliFlag: undefined,
- configFile: null,
- downloadName: null,
- outName: 'hi.' + entry[1],
- uiCodec: null,
- compositionCodec: null,
- }),
+ videoCodecOption.getValue(
+ {
+ commandLine: {},
+ },
+ {
+ configFile: null,
+ downloadName: null,
+ outName: 'hi.' + entry[1],
+ uiCodec: null,
+ compositionCodec: null,
+ },
+ ),
).toEqual({
- codec: entry[0],
- reason: 'derived from out name',
+ value: entry[0],
+ source: 'derived from out name',
});
}),
);
@@ -61,23 +69,31 @@ describe('Codec tests undefined codec input with known extension', () => {
test('Codec tests undefined codec input with unknown extension', () => {
expect(
- getFinalOutputCodec({
- cliFlag: undefined,
- outName: 'hi.',
- configFile: null,
- downloadName: null,
- uiCodec: null,
- compositionCodec: null,
- }),
- ).toEqual({codec: 'h264', reason: 'default'});
+ videoCodecOption.getValue(
+ {
+ commandLine: {},
+ },
+ {
+ outName: 'hi.',
+ configFile: null,
+ downloadName: null,
+ uiCodec: null,
+ compositionCodec: null,
+ },
+ ),
+ ).toEqual({value: 'h264', source: 'default'});
expect(
- getFinalOutputCodec({
- cliFlag: undefined,
- outName: 'hi.abc',
- configFile: null,
- downloadName: null,
- uiCodec: null,
- compositionCodec: null,
- }),
- ).toEqual({codec: 'h264', reason: 'default'});
+ videoCodecOption.getValue(
+ {
+ commandLine: {},
+ },
+ {
+ outName: 'hi.abc',
+ configFile: null,
+ downloadName: null,
+ uiCodec: null,
+ compositionCodec: null,
+ },
+ ),
+ ).toEqual({value: 'h264', source: 'default'});
});
diff --git a/packages/cloudrun/src/cli/commands/render/index.ts b/packages/cloudrun/src/cli/commands/render/index.ts
index 0de696097b..a897527bf2 100644
--- a/packages/cloudrun/src/cli/commands/render/index.ts
+++ b/packages/cloudrun/src/cli/commands/render/index.ts
@@ -42,14 +42,19 @@ export const renderCommand = async (
region,
} = await renderArgsCheck(RENDER_COMMAND, args, logLevel);
- const {codec, reason: codecReason} = CliInternals.getFinalOutputCodec({
- cliFlag: CliInternals.parsedCli.codec,
- downloadName,
- outName: outName ?? null,
- configFile: ConfigInternals.getOutputCodecOrUndefined() ?? null,
- uiCodec: null,
- compositionCodec: null,
- });
+ const {value: codec, source: codecReason} =
+ BrowserSafeApis.options.videoCodecOption.getValue(
+ {
+ commandLine: CliInternals.parsedCli,
+ },
+ {
+ downloadName,
+ outName: outName ?? null,
+ configFile: ConfigInternals.getOutputCodecOrUndefined() ?? null,
+ uiCodec: null,
+ compositionCodec: null,
+ },
+ );
const imageFormat = parsedCloudrunCli['image-format'];
diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.ts
index 27d461e4c6..5132e505a6 100644
--- a/packages/lambda/src/cli/commands/render/render.ts
+++ b/packages/lambda/src/cli/commands/render/render.ts
@@ -174,14 +174,19 @@ export const renderCommand = async (
const outName = parsedLambdaCli['out-name'];
const downloadName = args[2] ?? null;
- const {codec, reason} = CliInternals.getFinalOutputCodec({
- cliFlag: CliInternals.parsedCli.codec,
- downloadName,
- outName: outName ?? null,
- configFile: ConfigInternals.getOutputCodecOrUndefined() ?? null,
- uiCodec: null,
- compositionCodec: null,
- });
+ const {value: codec, source: reason} =
+ BrowserSafeApis.options.videoCodecOption.getValue(
+ {
+ commandLine: CliInternals.parsedCli,
+ },
+ {
+ downloadName,
+ outName: outName ?? null,
+ configFile: ConfigInternals.getOutputCodecOrUndefined() ?? null,
+ uiCodec: null,
+ compositionCodec: null,
+ },
+ );
const imageFormat = CliInternals.getVideoImageFormat({
codec,
diff --git a/packages/renderer/src/options/option.ts b/packages/renderer/src/options/option.ts
index 1226252002..09eef9897c 100644
--- a/packages/renderer/src/options/option.ts
+++ b/packages/renderer/src/options/option.ts
@@ -9,7 +9,11 @@ export type RemotionOption<SsrName extends string, Type> = {
description: (mode: 'ssr' | 'cli') => React.ReactNode;
docLink: string;
type: Type;
- getValue?: (values: {commandLine: Record<string, unknown>}) => {
+ getValue?: (
+ values: {commandLine: Record<string, unknown>},
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ more?: any,
+ ) => {
value: Type;
source: string;
};
diff --git a/packages/renderer/src/options/video-codec.tsx b/packages/renderer/src/options/video-codec.tsx
index 157ebb865e..a60ac4e5bc 100644
--- a/packages/renderer/src/options/video-codec.tsx
+++ b/packages/renderer/src/options/video-codec.tsx
@@ -1,5 +1,11 @@
import type {Codec, CodecOrUndefined} from '../codec';
-import {validCodecs} from '../codec';
+import {DEFAULT_CODEC, validCodecs} from '../codec';
+import type {FileExtension} from '../file-extensions';
+import {
+ defaultCodecsForFileExtension,
+ makeFileExtensionMap,
+} from '../get-extension-from-codec';
+import {getExtensionOfFilename} from '../get-extension-of-filename';
import type {AnyRemotionOption} from './option';
let codec: CodecOrUndefined;
@@ -25,6 +31,22 @@ export const getOutputCodecOrUndefined = () => {
return codec;
};
+const deriveCodecsFromFilename = (
+ extension: string | null,
+): {
+ possible: Codec[];
+ default: Codec | null;
+} => {
+ if (extension === null) {
+ return {possible: [], default: null};
+ }
+
+ return {
+ default: defaultCodecsForFileExtension[extension as FileExtension] ?? null,
+ possible: makeFileExtensionMap()[extension] ?? [],
+ };
+};
+
const cliFlag = 'codec' as const;
export const videoCodecOption = {
@@ -40,25 +62,102 @@ export const videoCodecOption = {
ssrName: 'codec',
docLink: 'https://www.remotion.dev/docs/encoding/#choosing-a-codec',
type: '' as Codec,
- getValue: ({commandLine}) => {
- if (commandLine[cliFlag] !== undefined) {
+ getValue: (
+ {commandLine}: {commandLine: Record<string, unknown>},
+ {
+ compositionCodec,
+ configFile,
+ downloadName,
+ outName,
+ uiCodec,
+ }: {
+ outName: string | null;
+ downloadName: string | null;
+ configFile: Codec | null;
+ uiCodec: Codec | null;
+ compositionCodec: Codec | null;
+ },
+ ): {
+ value: Codec;
+ source: string;
+ } => {
+ if (uiCodec) {
+ return {value: uiCodec, source: 'via UI'};
+ }
+
+ const downloadNameExtension = getExtensionOfFilename(downloadName);
+ const outNameExtension = getExtensionOfFilename(outName);
+
+ const derivedDownloadCodecs = deriveCodecsFromFilename(
+ downloadNameExtension,
+ );
+ const derivedOutNameCodecs = deriveCodecsFromFilename(outNameExtension);
+
+ if (
+ derivedDownloadCodecs.possible.length > 0 &&
+ derivedOutNameCodecs.possible.length > 0 &&
+ derivedDownloadCodecs.possible.join('') !==
+ derivedOutNameCodecs.possible.join('')
+ ) {
+ throw new TypeError(
+ `The download name is ${downloadName} but the output name is ${outName}. The file extensions must match`,
+ );
+ }
+
+ const cliArgument = commandLine[cliFlag];
+
+ if (cliArgument) {
+ if (
+ derivedDownloadCodecs.possible.length > 0 &&
+ derivedDownloadCodecs.possible.indexOf(cliArgument as Codec) === -1
+ ) {
+ throw new TypeError(
+ `The download name is ${downloadName} but --codec=${cliArgument} was passed. The download name implies a codec of ${derivedDownloadCodecs.possible.join(
+ ' or ',
+ )} which does not align with the --codec flag.`,
+ );
+ }
+
+ if (
+ derivedOutNameCodecs.possible.length > 0 &&
+ derivedOutNameCodecs.possible.indexOf(cliArgument as Codec) === -1
+ ) {
+ throw new TypeError(
+ `The out name is ${outName} but --codec=${cliArgument} was passed. The out name implies a codec of ${derivedOutNameCodecs.possible.join(
+ ' or ',
+ )} which does not align with the --codec flag.`,
+ );
+ }
+
+ return {value: cliArgument as Codec, source: 'from --codec flag'};
+ }
+
+ if (derivedDownloadCodecs.possible.length > 0) {
return {
- source: 'cli',
- value: commandLine[cliFlag] as Codec,
+ value: derivedDownloadCodecs.default as Codec,
+ source: 'derived from download name',
};
}
- if (codec !== undefined) {
+ if (derivedOutNameCodecs.possible.length > 0) {
+ return {
+ value: derivedOutNameCodecs.default as Codec,
+ source: 'derived from out name',
+ };
+ }
+
+ if (compositionCodec) {
+ return {value: compositionCodec, source: 'via calculateMetadata'};
+ }
+
+ if (configFile) {
return {
- source: 'config',
- value: codec,
+ value: configFile,
+ source: 'Config file',
};
}
- return {
- source: 'default',
- value: 'h264',
- };
+ return {value: DEFAULT_CODEC, source: 'default'};
},
setConfig: setCodec,
} satisfies AnyRemotionOption<Codec>;