Skip to content

🎥 Make videos programmatically with React

separate video image format and still image format

Jonny Burgerc48329144380
diff --git a/packages/cli/src/config/image-format.ts b/packages/cli/src/config/image-format.ts
index 9d4f40dff5..6a0bf1eae9 100644
--- a/packages/cli/src/config/image-format.ts
+++ b/packages/cli/src/config/image-format.ts
@@ -1,16 +1,17 @@
-import type {ImageFormat} from '@remotion/renderer';
+import type {StillImageFormat, VideoImageFormat} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
import {truthy} from '../truthy';
-let currentImageFormat: ImageFormat | undefined;
+let currentStillImageFormat: StillImageFormat | undefined;
+let currentVideoImageFormat: VideoImageFormat | undefined;
-export const setImageFormat = (format: ImageFormat) => {
+export const setStillImageFormat = (format: StillImageFormat) => {
if (typeof format === 'undefined') {
- currentImageFormat = undefined;
+ currentStillImageFormat = undefined;
return;
}
- if (!RenderInternals.validImageFormats.includes(format)) {
+ if (!RenderInternals.validStillImageFormats.includes(format)) {
throw new TypeError(
[
`Value ${format} is not valid as an image format.`,
@@ -22,9 +23,34 @@ export const setImageFormat = (format: ImageFormat) => {
);
}
- currentImageFormat = format;
+ currentStillImageFormat = format;
};
-export const getUserPreferredImageFormat = () => {
- return currentImageFormat;
+export const setVideoImageFormat = (format: VideoImageFormat) => {
+ if (typeof format === 'undefined') {
+ currentVideoImageFormat = undefined;
+ return;
+ }
+
+ if (!RenderInternals.validVideoImageFormats.includes(format)) {
+ throw new TypeError(
+ [
+ `Value ${format} is not valid as a video image format.`,
+ // @ts-expect-error
+ format === 'jpg' ? 'Did you mean "jpeg"?' : null,
+ ]
+ .filter(truthy)
+ .join(' ')
+ );
+ }
+
+ currentVideoImageFormat = format;
+};
+
+export const getUserPreferredStillImageFormat = () => {
+ return currentStillImageFormat;
+};
+
+export const getUserPreferredVideoImageFormat = () => {
+ return currentVideoImageFormat;
};
diff --git a/packages/cli/src/config/index.ts b/packages/cli/src/config/index.ts
index a36139a80e..4cac22950b 100644
--- a/packages/cli/src/config/index.ts
+++ b/packages/cli/src/config/index.ts
@@ -10,7 +10,12 @@ import {getOutputCodecOrUndefined} from './codec';
import {getConcurrency} from './concurrency';
import {getDotEnvLocation} from './env-file';
import {getRange, setFrameRangeFromCli} from './frame-range';
-import {getUserPreferredImageFormat} from './image-format';
+import {
+ getUserPreferredStillImageFormat,
+ getUserPreferredVideoImageFormat,
+ setStillImageFormat,
+ setVideoImageFormat,
+} from './image-format';
import {getShouldOutputImageSequence} from './image-sequence';
import * as Logging from './log';
import {getMaxTimelineTracks} from './max-timeline-tracks';
@@ -35,6 +40,8 @@ import type {
CodecOrUndefined,
Crf,
FrameRange,
+ StillImageFormat,
+ VideoImageFormat,
} from '@remotion/renderer';
import {getAudioCodec, setAudioCodec} from './audio-codec';
import {
@@ -67,7 +74,6 @@ import {
} from './ffmpeg-override';
import {setFrameRange} from './frame-range';
import {getHeight, overrideHeight} from './height';
-import {setImageFormat} from './image-format';
import {setImageSequence} from './image-sequence';
import {
getKeyboardShortcutsEnabled,
@@ -226,10 +232,13 @@ declare global {
* Default: 80
*/
readonly setQuality: (q: number | undefined) => void;
+ /** Decide the image format for still renders.
+ */
+ readonly setStillImageFormat: (format: StillImageFormat) => void;
/** Decide in which image format to render. Can be either 'jpeg' or 'png'.
* PNG is slower, but supports transparency.
*/
- readonly setImageFormat: (format: 'png' | 'jpeg' | 'none') => void;
+ readonly setVideoImageFormat: (format: VideoImageFormat) => void;
/**
* Render only a subset of a video.
* Pass in a tuple [20, 30] to only render frames 20-30 into a video.
@@ -388,7 +397,8 @@ export const Config: FlatConfig = {
setDotEnvLocation,
setConcurrency,
setQuality,
- setImageFormat,
+ setStillImageFormat,
+ setVideoImageFormat,
setFrameRange,
setScale,
setEveryNthFrame,
@@ -436,7 +446,8 @@ export const ConfigInternals = {
getStillFrame,
getShouldOutputImageSequence,
getDotEnvLocation,
- getUserPreferredImageFormat,
+ getUserPreferredStillImageFormat,
+ getUserPreferredVideoImageFormat,
getWebpackOverrideFn,
getWebpackCaching,
getOutputLocation,
diff --git a/packages/cli/src/determine-image-format.ts b/packages/cli/src/determine-image-format.ts
index 63877a1c69..56bf2a5184 100644
--- a/packages/cli/src/determine-image-format.ts
+++ b/packages/cli/src/determine-image-format.ts
@@ -1,4 +1,4 @@
-import type {ImageFormat, StillImageFormat} from '@remotion/renderer';
+import type {StillImageFormat, VideoImageFormat} from '@remotion/renderer';
const deriveExtensionFromFilename = (
filename: string | null
@@ -18,7 +18,7 @@ const deriveExtensionFromFilename = (
return null;
};
-export const determineFinalImageFormat = ({
+export const determineFinalStillImageFormat = ({
downloadName,
outName,
configImageFormat,
@@ -27,8 +27,8 @@ export const determineFinalImageFormat = ({
}: {
downloadName: string | null;
outName: string | null;
- configImageFormat: ImageFormat | null;
- cliFlag: ImageFormat | null;
+ configImageFormat: StillImageFormat | null;
+ cliFlag: StillImageFormat | VideoImageFormat | null;
isLambda: boolean;
}): {format: StillImageFormat; source: string} => {
const outNameExtension = deriveExtensionFromFilename(outName);
@@ -76,7 +76,14 @@ export const determineFinalImageFormat = ({
return {format: cliFlag, source: '--image-format flag'};
}
- if (configImageFormat !== null && configImageFormat !== 'none') {
+ 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'};
}
diff --git a/packages/cli/src/get-render-media-options.ts b/packages/cli/src/get-render-media-options.ts
index a879919d63..749c90d2e4 100644
--- a/packages/cli/src/get-render-media-options.ts
+++ b/packages/cli/src/get-render-media-options.ts
@@ -3,7 +3,7 @@ import {RenderInternals} from '@remotion/renderer';
import {ConfigInternals} from './config';
import {getResolvedAudioCodec} from './get-audio-codec';
import {getCliOptions} from './get-cli-options';
-import {getImageFormat} from './image-formats';
+import {getVideoImageFormat} from './image-formats';
export const getRenderMediaOptions = async ({
outputLocation,
@@ -47,7 +47,7 @@ export const getRenderMediaOptions = async ({
remotionRoot,
});
- const imageFormat = getImageFormat(codec);
+ const imageFormat = getVideoImageFormat(codec);
const audioCodec = getResolvedAudioCodec();
return {
diff --git a/packages/cli/src/image-formats.ts b/packages/cli/src/image-formats.ts
index a2a389c065..4a8b25c90a 100644
--- a/packages/cli/src/image-formats.ts
+++ b/packages/cli/src/image-formats.ts
@@ -1,11 +1,11 @@
-import type {ImageFormat} from '@remotion/renderer';
+import type {VideoImageFormat} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
import {ConfigInternals} from './config';
-export const getImageFormat = (
+export const getVideoImageFormat = (
codec: ReturnType<typeof ConfigInternals.getOutputCodecOrUndefined>
-): ImageFormat => {
- const userPreferred = ConfigInternals.getUserPreferredImageFormat();
+): VideoImageFormat => {
+ const userPreferred = ConfigInternals.getUserPreferredVideoImageFormat();
if (typeof userPreferred !== 'undefined') {
return userPreferred;
diff --git a/packages/cli/src/index.tsx b/packages/cli/src/index.tsx
index fb257f1be2..e235ed1434 100644
--- a/packages/cli/src/index.tsx
+++ b/packages/cli/src/index.tsx
@@ -4,7 +4,7 @@ import {benchmarkCommand} from './benchmark';
import {chalk} from './chalk';
import {cleanupBeforeQuit, handleCtrlC} from './cleanup-before-quit';
import {listCompositionsCommand} from './compositions';
-import {determineFinalImageFormat} from './determine-image-format';
+import {determineFinalStillImageFormat} from './determine-image-format';
import {getFileSizeDownloadBar} from './download-progress';
import {findEntryPoint} from './entry-point';
import {ffmpegCommand, ffprobeCommand} from './ffmpeg';
@@ -12,7 +12,7 @@ import {formatBytes} from './format-bytes';
import {getCliOptions, getFinalCodec} from './get-cli-options';
import {loadConfig} from './get-config-file-name';
import {handleCommonError} from './handle-common-errors';
-import {getImageFormat} from './image-formats';
+import {getVideoImageFormat} from './image-formats';
import {initializeCli} from './initialize-cli';
import {lambdaCommand} from './lambda-command';
import {listOfRemotionPackages} from './list-of-remotion-packages';
@@ -113,11 +113,11 @@ export const CliInternals = {
formatBytes,
getFileSizeDownloadBar,
getFinalCodec,
- determineFinalImageFormat,
+ determineFinalStillImageFormat,
minimist,
selectComposition,
findEntryPoint,
- getImageFormat,
+ getVideoImageFormat,
printCompositions,
listOfRemotionPackages,
};
diff --git a/packages/cli/src/parse-command-line.ts b/packages/cli/src/parse-command-line.ts
index c28aa9e93b..1e663a5783 100644
--- a/packages/cli/src/parse-command-line.ts
+++ b/packages/cli/src/parse-command-line.ts
@@ -2,11 +2,12 @@ import type {
AudioCodec,
BrowserExecutable,
Codec,
- ImageFormat,
LogLevel,
OpenGlRenderer,
PixelFormat,
ProResProfile,
+ StillImageFormat,
+ VideoImageFormat,
} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
import minimist from 'minimist';
@@ -16,7 +17,7 @@ import {Log} from './log';
export type CommandLineOptions = {
['browser-executable']: BrowserExecutable;
['pixel-format']: PixelFormat;
- ['image-format']: ImageFormat;
+ ['image-format']: VideoImageFormat | StillImageFormat;
['prores-profile']: ProResProfile;
['bundle-cache']: string;
['env-file']: string;
@@ -97,10 +98,6 @@ export const parseCommandLine = () => {
Config.setPixelFormat(parsedCli['pixel-format']);
}
- if (parsedCli['image-format']) {
- Config.setImageFormat(parsedCli['image-format']);
- }
-
if (parsedCli['browser-executable']) {
Config.setBrowserExecutable(parsedCli['browser-executable']);
}
@@ -164,11 +161,9 @@ export const parseCommandLine = () => {
}
if (parsedCli.png) {
- Log.warn(
+ throw new Error(
'The --png flag has been deprecrated. Use --sequence --image-format=png from now on.'
);
- Config.setImageSequence(true);
- Config.setImageFormat('png');
}
if (parsedCli.sequence) {
diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsx
index 20036a21cd..4ed6e9da68 100644
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@ -23,7 +23,7 @@ import {
import {getCompositionWithDimensionOverride} from './get-composition-with-dimension-override';
import {getOutputFilename} from './get-filename';
import {getRenderMediaOptions} from './get-render-media-options';
-import {getImageFormat} from './image-formats';
+import {getVideoImageFormat} from './image-formats';
import {Log} from './log';
import {parsedCli, quietFlagProvided} from './parse-command-line';
import type {DownloadProgress} from './progress-bar';
@@ -255,7 +255,7 @@ export const render = async (remotionRoot: string, args: string[]) => {
);
};
- const imageFormat = getImageFormat(
+ const imageFormat = getVideoImageFormat(
shouldOutputImageSequence ? undefined : codec
);
diff --git a/packages/cli/src/still.ts b/packages/cli/src/still.ts
index 4732aa11c6..3d2f3be0f1 100644
--- a/packages/cli/src/still.ts
+++ b/packages/cli/src/still.ts
@@ -10,7 +10,7 @@ import path from 'path';
import {chalk} from './chalk';
import {registerCleanupJob} from './cleanup-before-quit';
import {ConfigInternals} from './config';
-import {determineFinalImageFormat} from './determine-image-format';
+import {determineFinalStillImageFormat} from './determine-image-format';
import {findEntryPoint} from './entry-point';
import {
getAndValidateAbsoluteOutputFile,
@@ -123,9 +123,10 @@ export const still = async (remotionRoot: string, args: string[]) => {
width,
args: remainingArgs,
});
- const {format: imageFormat, source} = determineFinalImageFormat({
+ const {format: imageFormat, source} = determineFinalStillImageFormat({
cliFlag: parsedCli['image-format'] ?? null,
- configImageFormat: ConfigInternals.getUserPreferredImageFormat() ?? null,
+ configImageFormat:
+ ConfigInternals.getUserPreferredStillImageFormat() ?? null,
downloadName: null,
outName: getUserPassedOutputLocation(argsAfterComposition),
isLambda: false,
diff --git a/packages/example/src/test/stills.test.ts b/packages/example/src/test/stills.test.ts
index 0ee434bfa2..634080867f 100644
--- a/packages/example/src/test/stills.test.ts
+++ b/packages/example/src/test/stills.test.ts
@@ -1,9 +1,9 @@
import {bundle} from '@remotion/bundler';
import {
getCompositions,
- ImageFormat,
RenderInternals,
renderStill,
+ StillImageFormat,
} from '@remotion/renderer';
import {cleanDownloadMap} from '@remotion/renderer/dist/assets/download-map';
import {existsSync, unlinkSync} from 'fs';
@@ -95,11 +95,10 @@ test(
}
);
-// TODO REMOVE - Just for testing
test(
'Can render a still pdf using Node.JS APIs',
async () => {
- const imageFormat: ImageFormat = 'pdf';
+ const imageFormat: StillImageFormat = 'pdf';
const bundled = await bundle({
entryPoint: path.join(process.cwd(), 'src/index.ts'),
webpackOverride,
@@ -115,8 +114,6 @@ test(
onError: (err) => {
throw err;
},
- ffmpegExecutable: null,
- ffprobeExecutable: null,
downloadMap,
remotionRoot: process.cwd(),
});
@@ -150,11 +147,11 @@ test(
});
expect(existsSync(testOut)).toBe(true);
- // TODO unlinkSync(testOut);
+ unlinkSync(testOut);
}
RenderInternals.deleteDirectory(bundled);
- // TODO RenderInternals.deleteDirectory(folder);
+ RenderInternals.deleteDirectory(folder);
cleanDownloadMap(downloadMap);
await close();
diff --git a/packages/lambda/src/api/render-media-on-lambda.ts b/packages/lambda/src/api/render-media-on-lambda.ts
index 5c6e2238b7..33764e44b3 100644
--- a/packages/lambda/src/api/render-media-on-lambda.ts
+++ b/packages/lambda/src/api/render-media-on-lambda.ts
@@ -2,10 +2,10 @@ import type {
AudioCodec,
ChromiumOptions,
FrameRange,
- ImageFormat,
LogLevel,
PixelFormat,
ProResProfile,
+ VideoImageFormat,
} from '@remotion/renderer';
import {VERSION} from 'remotion/version';
import type {AwsRegion} from '../pricing/aws-regions';
@@ -28,7 +28,7 @@ export type RenderMediaOnLambdaInput = {
composition: string;
inputProps?: unknown;
codec: LambdaCodec;
- imageFormat?: ImageFormat;
+ imageFormat?: VideoImageFormat;
crf?: number | undefined;
envVariables?: Record<string, string>;
pixelFormat?: PixelFormat;
diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.ts
index c821052b26..1af25c1638 100644
--- a/packages/lambda/src/cli/commands/render/render.ts
+++ b/packages/lambda/src/cli/commands/render/render.ts
@@ -86,7 +86,7 @@ export const renderCommand = async (args: string[], remotionRoot: string) => {
remotionRoot,
});
- const imageFormat = CliInternals.getImageFormat(codec);
+ const imageFormat = CliInternals.getVideoImageFormat(codec);
const functionName = await findFunctionName();
diff --git a/packages/lambda/src/cli/commands/still.ts b/packages/lambda/src/cli/commands/still.ts
index d3ff62f30f..ab763980ac 100644
--- a/packages/lambda/src/cli/commands/still.ts
+++ b/packages/lambda/src/cli/commands/still.ts
@@ -75,10 +75,11 @@ export const stillCommand = async (args: string[], remotionRoot: string) => {
validatePrivacy(privacy);
const {format: imageFormat, source: imageFormatReason} =
- CliInternals.determineFinalImageFormat({
+ CliInternals.determineFinalStillImageFormat({
downloadName,
outName: outName ?? null,
- configImageFormat: ConfigInternals.getUserPreferredImageFormat() ?? null,
+ configImageFormat:
+ ConfigInternals.getUserPreferredStillImageFormat() ?? null,
cliFlag: CliInternals.parsedCli['image-format'] ?? null,
isLambda: true,
});
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index 0a933ec436..321e951289 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -3,10 +3,11 @@ import type {
ChromiumOptions,
Codec,
FrameRange,
- ImageFormat,
LogLevel,
PixelFormat,
ProResProfile,
+ StillImageFormat,
+ VideoImageFormat,
} from '@remotion/renderer';
import type {VideoConfig} from 'remotion';
import type {ChunkRetry} from '../functions/helpers/get-retry-stats';
@@ -152,7 +153,7 @@ export type OutNameOutput = {
export const getSitesKey = (siteId: string) => `sites/${siteId}`;
export const outName = (renderId: string, extension: string) =>
`${rendersPrefix(renderId)}/out.${extension}`;
-export const outStillName = (renderId: string, imageFormat: ImageFormat) =>
+export const outStillName = (renderId: string, imageFormat: StillImageFormat) =>
`${rendersPrefix(renderId)}/out.${imageFormat}`;
export const customOutName = (
renderId: string,
@@ -225,7 +226,7 @@ export type LambdaPayloads = {
inputProps: SerializedInputProps;
codec: LambdaCodec;
audioCodec: AudioCodec | null;
- imageFormat: ImageFormat;
+ imageFormat: VideoImageFormat;
crf: number | undefined;
envVariables: Record<string, string> | undefined;
pixelFormat: PixelFormat | undefined;
@@ -262,7 +263,7 @@ export type LambdaPayloads = {
bucketName: string;
inputProps: SerializedInputProps;
renderId: string;
- imageFormat: ImageFormat;
+ imageFormat: VideoImageFormat;
codec: LambdaCodec;
audioCodec: AudioCodec | null;
crf: number | undefined;
@@ -312,7 +313,7 @@ export type LambdaPayloads = {
retriesLeft: number;
inputProps: SerializedInputProps;
renderId: string;
- imageFormat: ImageFormat;
+ imageFormat: VideoImageFormat;
codec: LambdaCodec;
crf: number | undefined;
proResProfile: ProResProfile | undefined;
@@ -338,7 +339,7 @@ export type LambdaPayloads = {
serveUrl: string;
composition: string;
inputProps: SerializedInputProps;
- imageFormat: ImageFormat;
+ imageFormat: StillImageFormat;
envVariables: Record<string, string> | undefined;
attempt: number;
quality: number | undefined;
@@ -375,7 +376,17 @@ export type EncodingProgress = {
framesEncoded: number;
};
-export type RenderMetadata = {
+type Discriminated =
+ | {
+ type: 'still';
+ imageFormat: StillImageFormat;
+ }
+ | {
+ type: 'video';
+ imageFormat: VideoImageFormat;
+ };
+
+export type RenderMetadata = Discriminated & {
siteId: string;
videoConfig: VideoConfig;
startedDate: number;
@@ -385,8 +396,6 @@ export type RenderMetadata = {
compositionId: string;
codec: Codec | null;
audioCodec: AudioCodec | null;
- type: 'still' | 'video';
- imageFormat: ImageFormat;
inputProps: SerializedInputProps;
framesPerLambda: number;
memorySizeInMb: number;
diff --git a/packages/lambda/src/test/unit/expected-out-name.test.ts b/packages/lambda/src/test/unit/expected-out-name.test.ts
index d0dc27ba0a..3c44157b16 100644
--- a/packages/lambda/src/test/unit/expected-out-name.test.ts
+++ b/packages/lambda/src/test/unit/expected-out-name.test.ts
@@ -65,6 +65,7 @@ test('For stills', () => {
const newRenderMetadata: RenderMetadata = {
...testRenderMetadata,
type: 'still',
+ imageFormat: 'png',
};
expect(getExpectedOutName(newRenderMetadata, bucketName, null)).toEqual({
customCredentials: null,
@@ -77,6 +78,7 @@ test('Just a custom name', () => {
const newRenderMetadata: RenderMetadata = {
...testRenderMetadata,
type: 'still',
+ imageFormat: 'jpeg',
codec: null,
outName: 'justaname.jpeg',
};
@@ -91,6 +93,7 @@ test('Should throw on invalid names', () => {
const newRenderMetadata: RenderMetadata = {
...testRenderMetadata,
type: 'still',
+ imageFormat: 'png',
codec: null,
outName: '👺.jpeg',
};
@@ -105,6 +108,7 @@ test('Should allow outName an outname with a slash', () => {
codec: null,
audioCodec: null,
type: 'still',
+ imageFormat: 'jpeg',
outName: 'justa/name.jpeg',
};
expect(getExpectedOutName(newRenderMetadata, bucketName, null)).toEqual({
diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts
index 533028c56d..0b6520dee6 100644
--- a/packages/renderer/src/index.ts
+++ b/packages/renderer/src/index.ts
@@ -29,6 +29,7 @@ import {getExtensionOfFilename} from './get-extension-of-filename';
import {getRealFrameRange} from './get-frame-to-render';
import {ensureLocalBrowser} from './get-local-browser-executable';
import {getDesiredPort} from './get-port';
+import {validStillImageFormats, validVideoImageFormats} from './image-format';
import {isAudioCodec} from './is-audio-codec';
import {isServeUrl} from './is-serve-url';
import {isEqualOrBelowLogLevel, isValidLogLevel, logLevels} from './log-level';
@@ -70,6 +71,7 @@ export {getCompositions} from './get-compositions';
export {
StillImageFormat,
validateSelectedPixelFormatAndImageFormatCombination,
+ VideoImageFormat,
} from './image-format';
export type {LogLevel} from './log-level';
export {CancelSignal, makeCancelSignal} from './make-cancel-signal';
@@ -151,6 +153,8 @@ export const RenderInternals = {
getExecutablePath,
callFf,
callFfExtraOptions,
+ validStillImageFormats,
+ validVideoImageFormats,
};
// Warn of potential performance issues with Apple Silicon (M1 chip under Rosetta)