Skip to content

🎥 Make videos programmatically with React

separate into video image format and still image format

Jonny Burgera37990431ed8
diff --git a/packages/renderer/src/get-frame-padded-index.ts b/packages/renderer/src/get-frame-padded-index.ts
index 55659911a0..f60eac69ef 100644
--- a/packages/renderer/src/get-frame-padded-index.ts
+++ b/packages/renderer/src/get-frame-padded-index.ts
@@ -3,7 +3,7 @@
// - If `--every-nth-frame` is passed, only frames 0, 2, 4 are rendered
// - If an image sequence is created, the filenames should correspond to the frame numbers: element-099.jpg, element-100.jpg
-import type {ImageFormat} from './image-format';
+import type {VideoImageFormat} from './image-format';
export type CountType = 'from-zero' | 'actual-frames';
@@ -27,7 +27,7 @@ export const getFrameOutputFileName = ({
}: {
index: number;
frame: number;
- imageFormat: ImageFormat;
+ imageFormat: VideoImageFormat;
countType: CountType;
lastFrame: number;
totalFrames: number;
diff --git a/packages/renderer/src/image-format.ts b/packages/renderer/src/image-format.ts
index 9d3366ac20..ba0a482ce0 100644
--- a/packages/renderer/src/image-format.ts
+++ b/packages/renderer/src/image-format.ts
@@ -3,22 +3,20 @@
import type {PixelFormat} from './pixel-format';
-export const validImageFormats = ['png', 'jpeg', 'pdf', 'none'] as const;
+export const validVideoImageFormats = ['png', 'jpeg', 'none'] as const;
+export const validStillImageFormats = ['png', 'jpeg', 'pdf'] as const;
-export type ImageFormat = typeof validImageFormats[number];
-export type ImageFormatWithoutNone = Exclude<ImageFormat, 'none'>;
+export type VideoImageFormat = typeof validVideoImageFormats[number];
+export type StillImageFormat = typeof validStillImageFormats[number];
-export type StillImageFormat = ImageFormatWithoutNone;
-
-// However, the CLI will override it and use JPEG if suitable.
-export const DEFAULT_IMAGE_FORMAT: ImageFormat = 'png';
+export const DEFAULT_IMAGE_FORMAT: VideoImageFormat = 'jpeg';
// By returning a value, we improve testability as we can specifically test certain branches
export const validateSelectedPixelFormatAndImageFormatCombination = (
pixelFormat: PixelFormat | undefined,
- imageFormat: ImageFormat
+ videoImageFormat: VideoImageFormat
): 'none' | 'valid' => {
- if (imageFormat === 'none') {
+ if (videoImageFormat === 'none') {
return 'none';
}
@@ -26,9 +24,9 @@ export const validateSelectedPixelFormatAndImageFormatCombination = (
return 'valid';
}
- if (!validImageFormats.includes(imageFormat)) {
+ if (!validVideoImageFormats.includes(videoImageFormat)) {
throw new TypeError(
- `Value ${imageFormat} is not valid as an image format.`
+ `Value ${videoImageFormat} is not valid as an image format.`
);
}
@@ -36,7 +34,7 @@ export const validateSelectedPixelFormatAndImageFormatCombination = (
return 'valid';
}
- if (imageFormat !== 'png') {
+ if (videoImageFormat !== 'png') {
throw new TypeError(
`Pixel format was set to '${pixelFormat}' but the image format is not PNG. To render transparent videos, you need to set PNG as the image format.`
);
@@ -45,8 +43,14 @@ export const validateSelectedPixelFormatAndImageFormatCombination = (
return 'valid';
};
-export const validateNonNullImageFormat = (imageFormat: ImageFormat) => {
- if (!['jpeg', 'png', 'pdf'].includes(imageFormat)) {
- throw new TypeError('Image format should be either "png", "jpeg" or "pdf"');
+export const validateStillImageFormat = (imageFormat: StillImageFormat) => {
+ if (!validStillImageFormats.includes(imageFormat)) {
+ throw new TypeError(
+ String(
+ `Image format should be one of: ${validStillImageFormats
+ .map((v) => `"${v}"`)
+ .join(', ')}`
+ )
+ );
}
};
diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts
index 15c0af347b..533028c56d 100644
--- a/packages/renderer/src/index.ts
+++ b/packages/renderer/src/index.ts
@@ -29,7 +29,6 @@ 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 {validImageFormats} from './image-format';
import {isAudioCodec} from './is-audio-codec';
import {isServeUrl} from './is-serve-url';
import {isEqualOrBelowLogLevel, isValidLogLevel, logLevels} from './log-level';
@@ -69,10 +68,8 @@ export {FrameRange} from './frame-range';
export {getCanExtractFramesFast} from './get-can-extract-frames-fast';
export {getCompositions} from './get-compositions';
export {
- ImageFormat,
StillImageFormat,
validateSelectedPixelFormatAndImageFormatCombination,
- validImageFormats,
} from './image-format';
export type {LogLevel} from './log-level';
export {CancelSignal, makeCancelSignal} from './make-cancel-signal';
@@ -129,7 +126,6 @@ export const RenderInternals = {
validateFrameRange,
DEFAULT_OPENGL_RENDERER,
validateOpenGlRenderer,
- validImageFormats,
validCodecs,
DEFAULT_PIXEL_FORMAT,
validateQuality,
diff --git a/packages/renderer/src/prespawn-ffmpeg.ts b/packages/renderer/src/prespawn-ffmpeg.ts
index d1f70989b4..df324be6b4 100644
--- a/packages/renderer/src/prespawn-ffmpeg.ts
+++ b/packages/renderer/src/prespawn-ffmpeg.ts
@@ -7,7 +7,7 @@ import {validateQualitySettings} from './crf';
import type {FfmpegOverrideFn} from './ffmpeg-override';
import {getCodecName} from './get-codec-name';
import {getProResProfileName} from './get-prores-profile-name';
-import type {ImageFormat} from './image-format';
+import type {VideoImageFormat} from './image-format';
import type {CancelSignal} from './make-cancel-signal';
import {parseFfmpegProgress} from './parse-ffmpeg-progress';
import type {PixelFormat} from './pixel-format';
@@ -29,7 +29,7 @@ type PreSticherOptions = {
onProgress: (progress: number) => void;
proResProfile: ProResProfile | undefined;
verbose: boolean;
- imageFormat: ImageFormat;
+ imageFormat: VideoImageFormat;
ffmpegOverride: FfmpegOverrideFn;
signal: CancelSignal;
videoBitrate: string | null;
diff --git a/packages/renderer/src/provide-screenshot.ts b/packages/renderer/src/provide-screenshot.ts
index 01f15540e7..20ec663424 100644
--- a/packages/renderer/src/provide-screenshot.ts
+++ b/packages/renderer/src/provide-screenshot.ts
@@ -1,6 +1,6 @@
import type {ClipRegion} from 'remotion';
import type {Page} from './browser/BrowserPage';
-import type {ImageFormat} from './image-format';
+import type {StillImageFormat} from './image-format';
import {screenshotDOMElement} from './screenshot-dom-element';
export const provideScreenshot = ({
@@ -13,7 +13,7 @@ export const provideScreenshot = ({
clipRegion,
}: {
page: Page;
- imageFormat: ImageFormat;
+ imageFormat: StillImageFormat;
quality: number | undefined;
options: {
frame: number;
diff --git a/packages/renderer/src/puppeteer-screenshot.ts b/packages/renderer/src/puppeteer-screenshot.ts
index 8a592e2eef..8883ab07e1 100644
--- a/packages/renderer/src/puppeteer-screenshot.ts
+++ b/packages/renderer/src/puppeteer-screenshot.ts
@@ -1,12 +1,12 @@
import * as assert from 'assert';
import type {ClipRegion} from 'remotion';
import type {Page} from './browser/BrowserPage';
-import type {ImageFormatWithoutNone, StillImageFormat} from './image-format';
+import type {StillImageFormat} from './image-format';
import {screenshotTask} from './screenshot-task';
export const screenshot = (options: {
page: Page;
- type: ImageFormatWithoutNone;
+ type: StillImageFormat;
path?: string;
quality?: number;
omitBackground: boolean;
@@ -14,39 +14,10 @@ export const screenshot = (options: {
height: number;
clipRegion: ClipRegion | null;
}): Promise<Buffer | string> => {
- let screenshotType: ImageFormatWithoutNone | null = null;
- // options.type takes precedence over inferring the type from options.path
- // because it may be a 0-length file with no extension created beforehand
- // (i.e. as a temp file).
- if (options.type) {
- assert.ok(
- ['png', 'jpeg', 'pdf'].includes(options.type),
- 'Unknown options.type value: ' + options.type
- );
- screenshotType = options.type;
- } else if (options.path) {
- const filePath = options.path;
- const extension = filePath
- .slice(filePath.lastIndexOf('.') + 1)
- .toLowerCase();
- if (extension === 'png') screenshotType = 'png';
- else if (extension === 'jpg' || extension === 'jpeg')
- screenshotType = 'jpeg';
- else if (extension === 'pdf') screenshotType = 'pdf';
- assert.ok(
- screenshotType,
- `Unsupported screenshot type for extension \`.${extension}\``
- );
- }
-
- if (!screenshotType) screenshotType = 'png';
-
if (options.quality) {
assert.ok(
- screenshotType === 'jpeg',
- 'options.quality is unsupported for the ' +
- screenshotType +
- ' screenshots'
+ options.type === 'jpeg',
+ `options.quality is unsupported for the ${options.type} screenshots`
);
assert.ok(
typeof options.quality === 'number',
@@ -67,7 +38,7 @@ export const screenshot = (options: {
return options.page.screenshotTaskQueue.postTask(() =>
screenshotTask({
page: options.page,
- format: screenshotType as StillImageFormat,
+ format: options.type,
height: options.height,
width: options.width,
omitBackground: options.omitBackground,
diff --git a/packages/renderer/src/render-frames.ts b/packages/renderer/src/render-frames.ts
index f7c7ae137a..180b2bc222 100644
--- a/packages/renderer/src/render-frames.ts
+++ b/packages/renderer/src/render-frames.ts
@@ -26,7 +26,7 @@ import {
getFrameOutputFileName,
} from './get-frame-padded-index';
import {getRealFrameRange} from './get-frame-to-render';
-import type {ImageFormat} from './image-format';
+import type {VideoImageFormat} from './image-format';
import {DEFAULT_IMAGE_FORMAT} from './image-format';
import type {ServeUrlOrWebpackBundle} from './legacy-webpack-config';
import {getServeUrlWithFallback} from './legacy-webpack-config';
@@ -82,7 +82,7 @@ type RenderFramesOptions = {
outputDir: string | null;
inputProps: unknown;
envVariables?: Record<string, string>;
- imageFormat: ImageFormat;
+ imageFormat: VideoImageFormat;
quality?: number;
frameRange?: FrameRange | null;
everyNthFrame?: number;
diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.ts
index f05e3171e7..3883884a1d 100644
--- a/packages/renderer/src/render-media.ts
+++ b/packages/renderer/src/render-media.ts
@@ -24,7 +24,7 @@ import {getFramesToRender} from './get-duration-from-frame-range';
import {getFileExtensionFromCodec} from './get-extension-from-codec';
import {getExtensionOfFilename} from './get-extension-of-filename';
import {getRealFrameRange} from './get-frame-to-render';
-import type {ImageFormat} from './image-format';
+import type {VideoImageFormat} from './image-format';
import {validateSelectedPixelFormatAndImageFormatCombination} from './image-format';
import {isAudioCodec} from './is-audio-codec';
import type {ServeUrlOrWebpackBundle} from './legacy-webpack-config';
@@ -256,7 +256,7 @@ export const renderMedia = ({
}
}
- const imageFormat: ImageFormat = isAudioCodec(codec)
+ const imageFormat: VideoImageFormat = isAudioCodec(codec)
? 'none'
: options.imageFormat ?? 'jpeg';
const quality = imageFormat === 'jpeg' ? options.quality : undefined;
diff --git a/packages/renderer/src/render-still.ts b/packages/renderer/src/render-still.ts
index a396adea8a..7417da6a35 100644
--- a/packages/renderer/src/render-still.ts
+++ b/packages/renderer/src/render-still.ts
@@ -13,7 +13,7 @@ import {ensureOutputDirectory} from './ensure-output-directory';
import {handleJavascriptException} from './error-handling/handle-javascript-exception';
import {findRemotionRoot} from './find-closest-package-json';
import type {StillImageFormat} from './image-format';
-import {validateNonNullImageFormat} from './image-format';
+import {validateStillImageFormat} from './image-format';
import type {ServeUrlOrWebpackBundle} from './legacy-webpack-config';
import {getServeUrlWithFallback} from './legacy-webpack-config';
import type {CancelSignal} from './make-cancel-signal';
@@ -105,7 +105,7 @@ const innerRenderStill = async ({
component: 'in the `config` object passed to `renderStill()`',
allowFloats: false,
});
- validateNonNullImageFormat(imageFormat);
+ validateStillImageFormat(imageFormat);
Internals.validateFrame(frame, composition.durationInFrames);
const stillFrame = convertToPositiveFrameIndex({
durationInFrames: composition.durationInFrames,
diff --git a/packages/renderer/src/screenshot-dom-element.ts b/packages/renderer/src/screenshot-dom-element.ts
index 9413999c1c..6895f64a92 100644
--- a/packages/renderer/src/screenshot-dom-element.ts
+++ b/packages/renderer/src/screenshot-dom-element.ts
@@ -1,6 +1,6 @@
import type {ClipRegion} from 'remotion';
import type {Page} from './browser/BrowserPage';
-import type {ImageFormat} from './image-format';
+import type {StillImageFormat} from './image-format';
import {puppeteerEvaluateWithCatch} from './puppeteer-evaluate';
import {screenshot} from './puppeteer-screenshot';
@@ -14,7 +14,7 @@ export const screenshotDOMElement = async ({
clipRegion,
}: {
page: Page;
- imageFormat: ImageFormat;
+ imageFormat: StillImageFormat;
quality: number | undefined;
opts: {
path: string | null;
@@ -25,7 +25,7 @@ export const screenshotDOMElement = async ({
}): Promise<Buffer> => {
const {path} = opts;
- if (['png', 'pdf'].includes(imageFormat)) {
+ if (imageFormat === 'png' || imageFormat === 'pdf') {
await puppeteerEvaluateWithCatch({
pageFunction: () => {
document.body.style.background = 'transparent';
@@ -45,6 +45,7 @@ export const screenshotDOMElement = async ({
});
}
+ // @ts-expect-error
if (imageFormat === 'none') {
throw new TypeError('Tried to make a screenshot with format "none"');
}
diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.ts
index 66b9426491..1e2413e8de 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -26,7 +26,7 @@ import {findRemotionRoot} from './find-closest-package-json';
import {getCodecName} from './get-codec-name';
import {getFileExtensionFromCodec} from './get-extension-from-codec';
import {getProResProfileName} from './get-prores-profile-name';
-import type {ImageFormat} from './image-format';
+import type {VideoImageFormat} from './image-format';
import type {CancelSignal} from './make-cancel-signal';
import {cancelErrorMessages} from './make-cancel-signal';
import {mergeAudioTrack} from './merge-audio-track';
@@ -71,7 +71,7 @@ export type StitcherOptions = {
cancelSignal?: CancelSignal;
internalOptions?: {
preEncodedFileLocation: string | null;
- imageFormat: ImageFormat;
+ imageFormat: VideoImageFormat;
preferLossless: boolean;
};
muted?: boolean;
diff --git a/packages/renderer/src/take-frame-and-compose.ts b/packages/renderer/src/take-frame-and-compose.ts
index d05b8cbb76..79578f6146 100644
--- a/packages/renderer/src/take-frame-and-compose.ts
+++ b/packages/renderer/src/take-frame-and-compose.ts
@@ -4,7 +4,7 @@ import type {ClipRegion, TAsset} from 'remotion';
import type {DownloadMap} from './assets/download-map';
import type {Page} from './browser/BrowserPage';
import {compose} from './compositor/compose';
-import type {ImageFormat} from './image-format';
+import type {StillImageFormat, VideoImageFormat} from './image-format';
import {provideScreenshot} from './provide-screenshot';
import {puppeteerEvaluateWithCatch} from './puppeteer-evaluate';
import {truthy} from './truthy';
@@ -22,7 +22,7 @@ export const takeFrameAndCompose = async ({
wantsBuffer,
}: {
freePage: Page;
- imageFormat: ImageFormat;
+ imageFormat: VideoImageFormat | StillImageFormat;
quality: number | undefined;
frame: number;
height: number;
@@ -60,9 +60,7 @@ export const takeFrameAndCompose = async ({
}
if (imageFormat === 'pdf' && clipRegion !== null) {
- throw new Error(
- "You can only pass a `clip region` if `imageFormat` is 'jpeg' or 'png'."
- );
+ throw new Error("You cannot use `<Clipper>` if `imageFormat` is 'pdf'.");
}
const needsComposing =
diff --git a/packages/renderer/src/test/validate-image-format.test.ts b/packages/renderer/src/test/validate-image-format.test.ts
index 788bb81115..7da6e3fde7 100644
--- a/packages/renderer/src/test/validate-image-format.test.ts
+++ b/packages/renderer/src/test/validate-image-format.test.ts
@@ -1,11 +1,12 @@
import {expect, test} from 'vitest';
-import {validateNonNullImageFormat} from '../image-format';
+import {validateStillImageFormat} from '../image-format';
test('"none" is not a valid image format', () => {
- expect(() => validateNonNullImageFormat('jpeg')).not.toThrow();
- expect(() => validateNonNullImageFormat('png')).not.toThrow();
- expect(() => validateNonNullImageFormat('pdf')).not.toThrow();
- expect(() => validateNonNullImageFormat('none')).toThrow(
- /Image format should be either "png", "jpeg" or "pdf"/
+ expect(() => validateStillImageFormat('jpeg')).not.toThrow();
+ expect(() => validateStillImageFormat('png')).not.toThrow();
+ expect(() => validateStillImageFormat('pdf')).not.toThrow();
+ // @ts-expect-error
+ expect(() => validateStillImageFormat('none')).toThrow(
+ /Image format should be either "png", "jpeg", "pdf"/
);
});