Skip to content

🎥 Make videos programmatically with React

`@remotion/cli`: Simplify determineFinalStillImageFormat to take single configured value

Jonny Burger45504dba7646
diff --git a/packages/cli/src/determine-image-format.ts b/packages/cli/src/determine-image-format.ts
index 6354f6c5cb..b2d69ad087 100644
--- a/packages/cli/src/determine-image-format.ts
+++ b/packages/cli/src/determine-image-format.ts
@@ -1,4 +1,4 @@
-import type {StillImageFormat, VideoImageFormat} from '@remotion/renderer';
+import type {StillImageFormat} from '@remotion/renderer';
const deriveExtensionFromFilename = (
filename: string | null,
@@ -29,15 +29,13 @@ const deriveExtensionFromFilename = (
export const determineFinalStillImageFormat = ({
downloadName,
outName,
- configImageFormat,
- cliFlag,
+ configuredImageFormat,
isLambda,
fromUi,
}: {
downloadName: string | null;
outName: string | null;
- configImageFormat: StillImageFormat | null;
- cliFlag: StillImageFormat | VideoImageFormat | null;
+ configuredImageFormat: StillImageFormat | null;
isLambda: boolean;
fromUi: StillImageFormat | null;
}): {format: StillImageFormat; source: string} => {
@@ -61,9 +59,12 @@ export const determineFinalStillImageFormat = ({
}
if (downloadNameExtension) {
- if (cliFlag && downloadNameExtension !== cliFlag) {
+ if (
+ configuredImageFormat &&
+ downloadNameExtension !== configuredImageFormat
+ ) {
throw new TypeError(
- `Image format mismatch: ${downloadName} was given as the download name, but --image-format=${cliFlag} was passed. The image formats must match.`,
+ `Image format mismatch: ${downloadName} was given as the download name, but --image-format=${configuredImageFormat} was passed. The image formats must match.`,
);
}
@@ -71,34 +72,17 @@ export const determineFinalStillImageFormat = ({
}
if (outNameExtension) {
- if (cliFlag && outNameExtension !== cliFlag) {
+ if (configuredImageFormat && outNameExtension !== configuredImageFormat) {
throw new TypeError(
- `Image format mismatch: ${outName} was given as the ${outNameDescription}, but --image-format=${cliFlag} was passed. The image formats must match.`,
+ `Image format mismatch: ${outName} was given as the ${outNameDescription}, but --image-format=${configuredImageFormat} was passed. The image formats must match.`,
);
}
return {format: outNameExtension, source: 'Out name extension'};
}
- if (cliFlag === 'none') {
- throw new TypeError(
- 'The --image-format flag must not be "none" for stills.',
- );
- }
-
- if (cliFlag !== null) {
- return {format: cliFlag, source: '--image-format flag'};
- }
-
- if (configImageFormat !== null) {
- // @ts-expect-error
- if (configImageFormat === 'none') {
- throw new Error(
- 'The still simage format in the config file must not be "none"',
- );
- }
-
- return {format: configImageFormat, source: 'Config file'};
+ if (configuredImageFormat !== null) {
+ return {format: configuredImageFormat, source: '--image-format flag'};
}
return {format: 'png', source: 'Default'};
diff --git a/packages/cli/src/render-flows/still.ts b/packages/cli/src/render-flows/still.ts
index cd97ae3934..5e2699d1eb 100644
--- a/packages/cli/src/render-flows/still.ts
+++ b/packages/cli/src/render-flows/still.ts
@@ -276,22 +276,11 @@ export const renderStillFlow = async ({
mediaCacheSizeInBytes,
});
- const {
- value: resolvedStillImageFormat,
- source: resolvedStillImageFormatSource,
- } = BrowserSafeApis.options.stillImageFormatOption.getValue({
- commandLine: parsedCli,
- });
-
const {format: imageFormat, source} = determineFinalStillImageFormat({
- cliFlag:
- resolvedStillImageFormatSource === 'cli'
- ? resolvedStillImageFormat
- : null,
- configImageFormat:
- resolvedStillImageFormatSource === 'config'
- ? resolvedStillImageFormat
- : null,
+ configuredImageFormat:
+ BrowserSafeApis.options.stillImageFormatOption.getValue({
+ commandLine: parsedCli,
+ }).value,
downloadName: null,
outName: getUserPassedOutputLocation(
argsAfterComposition,
diff --git a/packages/cloudrun/src/cli/commands/still.ts b/packages/cloudrun/src/cli/commands/still.ts
index e9b8087fe7..b309b4d68a 100644
--- a/packages/cloudrun/src/cli/commands/still.ts
+++ b/packages/cloudrun/src/cli/commands/still.ts
@@ -159,27 +159,16 @@ export const stillCommand = async (
}
const {stillImageFormatOption} = BrowserSafeApis.options;
- const {
- value: resolvedStillImageFormat,
- source: resolvedStillImageFormatSource,
- } = stillImageFormatOption.getValue({
- commandLine: CliInternals.parsedCli,
- });
const {format: imageFormat, source: imageFormatReason} =
CliInternals.determineFinalStillImageFormat({
downloadName,
outName: outName ?? null,
- cliFlag:
- resolvedStillImageFormatSource === 'cli'
- ? resolvedStillImageFormat
- : null,
+ configuredImageFormat: stillImageFormatOption.getValue({
+ commandLine: CliInternals.parsedCli,
+ }).value,
isLambda: true,
fromUi: null,
- configImageFormat:
- resolvedStillImageFormatSource === 'config'
- ? resolvedStillImageFormat
- : null,
});
Log.verbose(
{indent: false, logLevel},
diff --git a/packages/example/src/test/still-image-format.test.ts b/packages/example/src/test/still-image-format.test.ts
new file mode 100644
index 0000000000..b27f01eb51
--- /dev/null
+++ b/packages/example/src/test/still-image-format.test.ts
@@ -0,0 +1,133 @@
+import {
+ RenderInternals,
+ renderStill,
+ selectComposition,
+} from '@remotion/renderer';
+import {BrowserSafeApis} from '@remotion/renderer/client';
+import {$} from 'bun';
+import {beforeAll, expect, test} from 'bun:test';
+import {existsSync, readFileSync, unlinkSync} from 'node:fs';
+import {tmpdir} from 'node:os';
+import path from 'node:path';
+
+if (process.platform === 'win32') {
+ process.exit(0);
+}
+
+const exampleDir = path.join(__dirname, '..', '..');
+const buildDir = path.join(exampleDir, 'build');
+
+beforeAll(async () => {
+ await $`bunx remotion browser ensure`.cwd(exampleDir);
+});
+
+const isPng = (filePath: string) => {
+ const buf = readFileSync(filePath);
+ return (
+ buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47
+ );
+};
+
+const isJpeg = (filePath: string) => {
+ const buf = readFileSync(filePath);
+ return buf[0] === 0xff && buf[1] === 0xd8;
+};
+
+test(
+ '--image-format=jpeg produces a JPEG still',
+ async () => {
+ const outFile = path.join(tmpdir(), 'remotion-test', 'still-fmt-jpeg.jpeg');
+
+ await $`bunx remotion still ${buildDir} --image-format=jpeg react-svg ${outFile}`.cwd(
+ exampleDir,
+ );
+
+ expect(existsSync(outFile)).toBe(true);
+ expect(isJpeg(outFile)).toBe(true);
+ unlinkSync(outFile);
+ },
+ {timeout: 90000},
+);
+
+test(
+ '--image-format=png produces a PNG still',
+ async () => {
+ const outFile = path.join(tmpdir(), 'remotion-test', 'still-fmt-png.png');
+
+ await $`bunx remotion still ${buildDir} --image-format=png react-svg ${outFile}`.cwd(
+ exampleDir,
+ );
+
+ expect(existsSync(outFile)).toBe(true);
+ expect(isPng(outFile)).toBe(true);
+ unlinkSync(outFile);
+ },
+ {timeout: 90000},
+);
+
+test(
+ 'Config.setStillImageFormat works via the option',
+ async () => {
+ const {stillImageFormatOption} = BrowserSafeApis.options;
+
+ stillImageFormatOption.setConfig('jpeg');
+
+ const composition = await selectComposition({
+ serveUrl: buildDir,
+ id: 'react-svg',
+ inputProps: {},
+ });
+
+ const folder = path.join(tmpdir(), 'remotion-test', 'still-fmt-config');
+ const testOut = path.join(folder, 'still.jpeg');
+
+ await renderStill({
+ composition,
+ output: testOut,
+ serveUrl: buildDir,
+ imageFormat:
+ stillImageFormatOption.getValue({commandLine: {}}).value ?? undefined,
+ });
+
+ expect(existsSync(testOut)).toBe(true);
+ expect(isJpeg(testOut)).toBe(true);
+ unlinkSync(testOut);
+ RenderInternals.deleteDirectory(folder);
+
+ stillImageFormatOption.setConfig(null);
+ },
+ {timeout: 90000},
+);
+
+test(
+ 'CLI --image-format takes precedence over Config.setStillImageFormat',
+ async () => {
+ const {stillImageFormatOption} = BrowserSafeApis.options;
+
+ stillImageFormatOption.setConfig('jpeg');
+
+ const resolved = stillImageFormatOption.getValue({
+ commandLine: {'image-format': 'png'},
+ });
+
+ expect(resolved.value).toBe('png');
+ expect(resolved.source).toBe('cli');
+
+ const configOnly = stillImageFormatOption.getValue({
+ commandLine: {},
+ });
+
+ expect(configOnly.value).toBe('jpeg');
+ expect(configOnly.source).toBe('config');
+
+ stillImageFormatOption.setConfig(null);
+
+ const defaultVal = stillImageFormatOption.getValue({
+ commandLine: {},
+ });
+
+ expect(defaultVal.value).toBeNull();
+ expect(defaultVal.source).toBe('default');
+ },
+ {timeout: 90000},
+);
diff --git a/packages/lambda/src/cli/commands/still.ts b/packages/lambda/src/cli/commands/still.ts
index fd7ce5b34a..bd2a5a8c5c 100644
--- a/packages/lambda/src/cli/commands/still.ts
+++ b/packages/lambda/src/cli/commands/still.ts
@@ -207,25 +207,16 @@ export const stillCommand = async ({
validatePrivacy(privacy, true);
const {stillImageFormatOption} = BrowserSafeApis.options;
- const {
- value: resolvedStillImageFormat,
- source: resolvedStillImageFormatSource,
- } = stillImageFormatOption.getValue({commandLine: parsedCli});
const {format: imageFormat, source: imageFormatReason} =
determineFinalStillImageFormat({
downloadName,
outName: outName ?? null,
- cliFlag:
- resolvedStillImageFormatSource === 'cli'
- ? resolvedStillImageFormat
- : null,
+ configuredImageFormat: stillImageFormatOption.getValue({
+ commandLine: parsedCli,
+ }).value,
isLambda: true,
fromUi: null,
- configImageFormat:
- resolvedStillImageFormatSource === 'config'
- ? resolvedStillImageFormat
- : null,
});
Log.info(