🎥 Make videos programmatically with React
`@remotion/cli`: Respect calculateMetadata encoding defaults (#8654)
Jonny Burger64537c4a2b06
diff --git a/packages/cli/src/benchmark.ts b/packages/cli/src/benchmark.tsindex f76a87aa68..d22f88dee8 100644--- a/packages/cli/src/benchmark.ts+++ b/packages/cli/src/benchmark.ts@@ -14,7 +14,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 {getVideoImageFormat} from './image-formats';import {Log} from './log';import {makeProgressBar} from './make-progress-bar';import {parsedCli, quietFlagProvided} from './parsed-cli';@@ -64,6 +63,7 @@ const {browserExecutableOption,everyNthFrameOption,proResProfileOption,+ videoImageFormatOption,userAgentOption,disableWebSecurityOption,ignoreCertificateErrorsOption,@@ -241,18 +241,12 @@ export const benchmarkCommand = async (commandLine: parsedCli,}).value;- const pixelFormat = pixelFormatOption.getValue({- commandLine: parsedCli,- }).value;const browserExecutable = browserExecutableOption.getValue({commandLine: parsedCli,}).value;const everyNthFrame = everyNthFrameOption.getValue({commandLine: parsedCli,}).value;- const proResProfile = proResProfileOption.getValue({- commandLine: parsedCli,- }).value;const userAgent = userAgentOption.getValue({commandLine: parsedCli}).value;const disableWebSecurity = disableWebSecurityOption.getValue({commandLine: parsedCli,@@ -530,14 +524,31 @@ export const benchmarkCommand = async (gopSize,envVariables,frameRange: defaultFrameRange,- imageFormat: getVideoImageFormat({- codec: videoCodec,- uiImageFormat: null,- }),+ imageFormat: videoImageFormatOption.getValue(+ {commandLine: parsedCli},+ {+ codec: videoCodec,+ uiVideoImageFormat: null,+ compositionDefaultVideoImageFormat:+ composition.defaultVideoImageFormat,+ },+ ).value,serializedInputPropsWithCustomSchema,overwrite,- pixelFormat,- proResProfile,+ pixelFormat: pixelFormatOption.getValue(+ {commandLine: parsedCli},+ {+ uiPixelFormat: null,+ compositionDefaultPixelFormat: composition.defaultPixelFormat,+ },+ ).value,+ proResProfile: proResProfileOption.getValue(+ {commandLine: parsedCli},+ {+ uiProResProfile: undefined,+ compositionDefaultProResProfile: composition.defaultProResProfile,+ },+ ).value,x264Preset,jpegQuality,chromiumOptions,diff --git a/packages/cli/src/image-formats.ts b/packages/cli/src/image-formats.tsdeleted file mode 100644index 3da68e6469..0000000000--- a/packages/cli/src/image-formats.ts+++ /dev/null@@ -1,49 +0,0 @@-import type {VideoImageFormat} from '@remotion/renderer';-import {BrowserSafeApis} from '@remotion/renderer/client';-import {NoReactAPIs} from '@remotion/renderer/pure';-import type {ConfigInternals} from './config';-import {parsedCli} from './parsed-cli';--export const getVideoImageFormat = ({- codec,- uiImageFormat,-}: {- codec: ReturnType<typeof ConfigInternals.getOutputCodecOrUndefined>;- uiImageFormat: VideoImageFormat | null;-}): VideoImageFormat => {- if (uiImageFormat !== null) {- return uiImageFormat;- }-- const configured = BrowserSafeApis.options.videoImageFormatOption.getValue({- commandLine: parsedCli,- }).value;-- if (configured !== null) {- return configured;- }-- if (NoReactAPIs.isAudioCodec(codec)) {- return 'none';- }-- if (- codec === 'h264' ||- codec === 'h264-mkv' ||- codec === 'h264-ts' ||- codec === 'h265' ||- codec === 'av1' ||- codec === 'vp8' ||- codec === 'vp9' ||- codec === 'prores' ||- codec === 'gif'- ) {- return 'jpeg';- }-- if (codec === undefined) {- return 'png';- }-- throw new Error('Unrecognized codec ' + codec);-};diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.tsindex 01464651d0..6c735f8f96 100644--- a/packages/cli/src/index.ts+++ b/packages/cli/src/index.ts@@ -23,7 +23,6 @@ import {getGitSource} from './get-github-repository';import {gpuCommand} from './gpu';import {supportsHyperlink} from './hyperlinks/is-supported';import {makeHyperlink} from './hyperlinks/make-link';-import {getVideoImageFormat} from './image-formats';import {initializeCli} from './initialize-cli';import {lambdaCommand} from './lambda-command';import {listOfRemotionPackages} from './list-of-remotion-packages';@@ -193,7 +192,6 @@ export const CliInternals = {determineFinalStillImageFormat,minimist,findEntryPoint,- getVideoImageFormat,printCompositions,listOfRemotionPackages,shouldUseNonOverlayingLogger,diff --git a/packages/cli/src/render-flows/render.ts b/packages/cli/src/render-flows/render.tsindex 7918a1f58e..61643b85d8 100644--- a/packages/cli/src/render-flows/render.ts+++ b/packages/cli/src/render-flows/render.ts@@ -47,7 +47,6 @@ import {getAndValidateAbsoluteOutputFile} from '../get-cli-options';import {getCompositionWithDimensionOverride} from '../get-composition-with-dimension-override';import {getOutputFilename} from '../get-filename';import {makeHyperlink} from '../hyperlinks/make-link';-import {getVideoImageFormat} from '../image-formats';import {Log} from '../log';import {makeOnDownload} from '../make-on-download';import {handleOnArtifact} from '../on-artifact';@@ -178,7 +177,7 @@ export const renderVideoFlow = async ({enforceAudioTrack: boolean;proResProfile: _InternalTypes['ProResProfile'] | undefined;x264Preset: X264Preset | null;- pixelFormat: PixelFormat;+ pixelFormat: PixelFormat | null;numberOfGifLoops: NumberOfGifLoops;audioCodec: AudioCodec | null;disallowParallelEncoding: boolean;@@ -541,10 +540,17 @@ export const renderVideoFlow = async ({timeRemainingInMilliseconds: null,};- const imageFormat = getVideoImageFormat({- codec: shouldOutputImageSequence ? undefined : codec,- uiImageFormat,- });+ const imageFormat = BrowserSafeApis.options.videoImageFormatOption.getValue(+ {commandLine: parsedCli},+ {+ codec: shouldOutputImageSequence ? undefined : codec,+ uiVideoImageFormat: uiImageFormat,+ compositionDefaultVideoImageFormat: config.defaultVideoImageFormat,+ },+ ).value;+ if (imageFormat === null) {+ throw new Error('Expected image format to be resolved');+ }const onLog: OnLog = ({logLevel: logLogLevel, previewString, tag}) => {addLogToAggregateProgress({@@ -679,8 +685,20 @@ export const renderVideoFlow = async ({frameRange,serializedInputPropsWithCustomSchema,overwrite,- pixelFormat,- proResProfile,+ pixelFormat: BrowserSafeApis.options.pixelFormatOption.getValue(+ {commandLine: parsedCli},+ {+ uiPixelFormat: pixelFormat,+ compositionDefaultPixelFormat: config.defaultPixelFormat,+ },+ ).value,+ proResProfile: BrowserSafeApis.options.proResProfileOption.getValue(+ {commandLine: parsedCli},+ {+ uiProResProfile: proResProfile,+ compositionDefaultProResProfile: config.defaultProResProfile,+ },+ ).value,x264Preset: x264Preset ?? null,jpegQuality: jpegQuality ?? RenderInternals.DEFAULT_JPEG_QUALITY,chromiumOptions,diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsxindex 4671c8162f..7d143c492c 100644--- a/packages/cli/src/render.tsx+++ b/packages/cli/src/render.tsx@@ -50,10 +50,8 @@ const {experimentalClientSideRenderingOption,keyboardShortcutsOption,rspackOption,- pixelFormatOption,browserExecutableOption,everyNthFrameOption,- proResProfileOption,userAgentOption,disableWebSecurityOption,ignoreCertificateErrorsOption,@@ -129,18 +127,12 @@ export const render = async (commandLine: parsedCli,}).value;- const pixelFormat = pixelFormatOption.getValue({- commandLine: parsedCli,- }).value;const browserExecutable = browserExecutableOption.getValue({commandLine: parsedCli,}).value;const everyNthFrame = everyNthFrameOption.getValue({commandLine: parsedCli,}).value;- const proResProfile = proResProfileOption.getValue({- commandLine: parsedCli,- }).value;const userAgent = userAgentOption.getValue({commandLine: parsedCli}).value;const disableWebSecurity = disableWebSecurityOption.getValue({commandLine: parsedCli,@@ -310,9 +302,9 @@ export const render = async (audioBitrate,muted,enforceAudioTrack,- proResProfile,+ proResProfile: undefined,x264Preset,- pixelFormat,+ pixelFormat: null,videoBitrate,encodingMaxRate,encodingBufferSize,diff --git a/packages/cli/src/test/image-formats.test.ts b/packages/cli/src/test/image-formats.test.tsindex cd40698854..b71264414e 100644--- a/packages/cli/src/test/image-formats.test.ts+++ b/packages/cli/src/test/image-formats.test.ts@@ -1,12 +1,121 @@-import {expect, test} from 'bun:test';-import {getVideoImageFormat} from '../image-formats';+import {afterEach, expect, test} from 'bun:test';+import {BrowserSafeApis} from '@remotion/renderer/client';+import {parsedCli} from '../parsed-cli';++const {pixelFormatOption, proResProfileOption, videoImageFormatOption} =+ BrowserSafeApis.options;++afterEach(() => {+ Reflect.deleteProperty(parsedCli, 'image-format');+ Reflect.deleteProperty(parsedCli, 'pixel-format');+ Reflect.deleteProperty(parsedCli, 'prores-profile');+ videoImageFormatOption.setConfig(null);+ pixelFormatOption.setConfig('yuv420p');+ proResProfileOption.setConfig(undefined);+});test('returns jpeg for av1 codec', () => {- expect(getVideoImageFormat({codec: 'av1', uiImageFormat: null})).toBe('jpeg');+ expect(+ videoImageFormatOption.getValue(+ {commandLine: parsedCli},+ {+ codec: 'av1',+ uiVideoImageFormat: null,+ compositionDefaultVideoImageFormat: null,+ },+ ).value,+ ).toBe('jpeg');});test('returns png if codec is undefined', () => {- expect(getVideoImageFormat({codec: undefined, uiImageFormat: null})).toBe(- 'png',- );+ expect(+ videoImageFormatOption.getValue(+ {commandLine: parsedCli},+ {+ codec: undefined,+ uiVideoImageFormat: null,+ compositionDefaultVideoImageFormat: null,+ },+ ).value,+ ).toBe('png');+});++test('uses composition video image format before config', () => {+ videoImageFormatOption.setConfig('jpeg');+ expect(+ videoImageFormatOption.getValue(+ {commandLine: parsedCli},+ {+ codec: 'prores',+ uiVideoImageFormat: null,+ compositionDefaultVideoImageFormat: 'png',+ },+ ).value,+ ).toBe('png');+});++test('uses explicit video image format before composition default', () => {+ parsedCli['image-format'] = 'jpeg';+ expect(+ videoImageFormatOption.getValue(+ {commandLine: parsedCli},+ {+ codec: 'prores',+ uiVideoImageFormat: null,+ compositionDefaultVideoImageFormat: 'png',+ },+ ).value,+ ).toBe('jpeg');+});++test('uses composition pixel format before config', () => {+ pixelFormatOption.setConfig('yuv444p');+ expect(+ pixelFormatOption.getValue(+ {commandLine: parsedCli},+ {+ uiPixelFormat: null,+ compositionDefaultPixelFormat: 'yuva444p10le',+ },+ ).value,+ ).toBe('yuva444p10le');+});++test('uses CLI pixel format before composition default', () => {+ parsedCli['pixel-format'] = 'yuv422p';+ expect(+ pixelFormatOption.getValue(+ {commandLine: parsedCli},+ {+ uiPixelFormat: null,+ compositionDefaultPixelFormat: 'yuva444p10le',+ },+ ).value,+ ).toBe('yuv422p');+});++test('uses composition ProRes profile before config', () => {+ proResProfileOption.setConfig('hq');+ expect(+ proResProfileOption.getValue(+ {commandLine: parsedCli},+ {+ uiProResProfile: undefined,+ compositionDefaultProResProfile: '4444',+ },+ ).value,+ ).toBe('4444');+});++test('uses CLI ProRes profile before composition default', () => {+ parsedCli['prores-profile'] = 'light';+ expect(+ proResProfileOption.getValue(+ {commandLine: parsedCli},+ {+ uiProResProfile: undefined,+ compositionDefaultProResProfile: '4444',+ },+ ).value,+ ).toBe('light');});diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.tsindex 38abfa5b60..e53868a18f 100644--- a/packages/lambda/src/cli/commands/render/render.ts+++ b/packages/lambda/src/cli/commands/render/render.ts@@ -324,10 +324,19 @@ export const renderCommand = async ({},);- const imageFormat = CliInternals.getVideoImageFormat({- codec,- uiImageFormat: null,- });+ const imageFormat = BrowserSafeApis.options.videoImageFormatOption.getValue(+ {+ commandLine: CliInternals.parsedCli,+ },+ {+ codec,+ uiVideoImageFormat: null,+ compositionDefaultVideoImageFormat: null,+ },+ ).value;+ if (imageFormat === null) {+ throw new Error('Expected image format to be resolved');+ }const functionName = await findFunctionName({logLevel, providerSpecifics});diff --git a/packages/renderer/src/options/pixel-format.tsx b/packages/renderer/src/options/pixel-format.tsxindex a82e82f3a9..56b0eac4f1 100644--- a/packages/renderer/src/options/pixel-format.tsx+++ b/packages/renderer/src/options/pixel-format.tsx@@ -21,17 +21,37 @@ export const pixelFormatOption = {ssrName: 'pixelFormat' as const,docLink: 'https://www.remotion.dev/docs/config#setpixelformat',type: DEFAULT_PIXEL_FORMAT as PixelFormat,- getValue: ({commandLine}) => {+ getValue: (+ {commandLine},+ options?: {+ uiPixelFormat: PixelFormat | null;+ compositionDefaultPixelFormat: PixelFormat | null;+ },+ ) => {+ if (options?.uiPixelFormat) {+ return {+ source: 'via UI',+ value: options.uiPixelFormat,+ };+ }+if (commandLine[cliFlag] !== undefined) {return {- source: 'cli',+ source: 'from --pixel-format flag',value: commandLine[cliFlag] as PixelFormat,};}+ if (options && options.compositionDefaultPixelFormat !== null) {+ return {+ source: 'via calculateMetadata',+ value: options.compositionDefaultPixelFormat,+ };+ }+if (currentPixelFormat !== DEFAULT_PIXEL_FORMAT) {return {- source: 'config',+ source: 'Config file',value: currentPixelFormat,};}diff --git a/packages/renderer/src/options/prores-profile.tsx b/packages/renderer/src/options/prores-profile.tsxindex 0a57be5a27..d259c34751 100644--- a/packages/renderer/src/options/prores-profile.tsx+++ b/packages/renderer/src/options/prores-profile.tsx@@ -37,17 +37,37 @@ export const proResProfileOption = {ssrName: 'proResProfile' as const,docLink: 'https://www.remotion.dev/docs/config#setproresprofile',type: undefined as ProResProfile | undefined,- getValue: ({commandLine}) => {+ getValue: (+ {commandLine},+ options?: {+ uiProResProfile: ProResProfile | undefined;+ compositionDefaultProResProfile: ProResProfile | null;+ },+ ) => {+ if (options?.uiProResProfile) {+ return {+ source: 'via UI',+ value: options.uiProResProfile,+ };+ }+if (commandLine[cliFlag] !== undefined) {return {- source: 'cli',+ source: 'from --prores-profile flag',value: String(commandLine[cliFlag]) as ProResProfile,};}+ if (options && options.compositionDefaultProResProfile !== null) {+ return {+ source: 'via calculateMetadata',+ value: options.compositionDefaultProResProfile,+ };+ }+if (proResProfile !== undefined) {return {- source: 'config',+ source: 'Config file',value: proResProfile,};}diff --git a/packages/renderer/src/options/video-image-format.tsx b/packages/renderer/src/options/video-image-format.tsxindex 409b9d7d20..40141c582c 100644--- a/packages/renderer/src/options/video-image-format.tsx+++ b/packages/renderer/src/options/video-image-format.tsx@@ -1,5 +1,7 @@+import type {CodecOrUndefined} from '../codec';import type {VideoImageFormat} from '../image-format';import {validVideoImageFormats} from '../image-format';+import {isAudioCodec} from '../is-audio-codec';import type {AnyRemotionOption} from './option';let currentVideoImageFormat: VideoImageFormat | null = null;@@ -20,7 +22,21 @@ export const videoImageFormatOption = {ssrName: 'imageFormat' as const,docLink: 'https://www.remotion.dev/docs/renderer/render-media#imageformat',type: null as VideoImageFormat | null,- getValue: ({commandLine}) => {+ getValue: (+ {commandLine},+ options?: {+ codec: CodecOrUndefined;+ uiVideoImageFormat: VideoImageFormat | null;+ compositionDefaultVideoImageFormat: VideoImageFormat | null;+ },+ ) => {+ if (options?.uiVideoImageFormat) {+ return {+ source: 'via UI',+ value: options.uiVideoImageFormat,+ };+ }+if (commandLine[cliFlag] !== undefined) {const value = commandLine[cliFlag] as VideoImageFormat;if (@@ -32,18 +48,60 @@ export const videoImageFormatOption = {}return {- source: 'cli',+ source: 'from --image-format flag',value,};}+ if (options && options.compositionDefaultVideoImageFormat !== null) {+ return {+ source: 'via calculateMetadata',+ value: options.compositionDefaultVideoImageFormat,+ };+ }+if (currentVideoImageFormat !== null) {return {- source: 'config',+ source: 'Config file',value: currentVideoImageFormat,};}+ if (options) {+ if (isAudioCodec(options.codec)) {+ return {+ source: 'default',+ value: 'none',+ };+ }++ if (+ options.codec === 'h264' ||+ options.codec === 'h264-mkv' ||+ options.codec === 'h264-ts' ||+ options.codec === 'h265' ||+ options.codec === 'av1' ||+ options.codec === 'vp8' ||+ options.codec === 'vp9' ||+ options.codec === 'prores' ||+ options.codec === 'gif'+ ) {+ return {+ source: 'default',+ value: 'jpeg',+ };+ }++ if (options.codec === undefined) {+ return {+ source: 'default',+ value: 'png',+ };+ }++ throw new Error('Unrecognized codec ' + options.codec);+ }+return {source: 'default',value: null,