Skip to content

🎥 Make videos programmatically with React

image format

Jonny Burger1d172fab4ad2
diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsx
index 028e0c18b4..e56a8012d0 100644
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@ -90,8 +90,16 @@ export const render = async () => {
}
const crf = shouldOutputImageSequence ? null : Internals.getActualCrf(codec);
if (crf !== null) {
- Internals.validateSelectedCrf(crf, codec);
+ Internals.validateSelectedCrfAndCodecCombination(crf, codec);
}
+ const pixelFormat = Internals.getPixelFormat();
+ const imageFormat = getImageFormat(codec);
+
+ Internals.validateSelectedPixelFormatAndCodecCombination(pixelFormat, codec);
+ Internals.validateSelectedPixelFormatAndImageFormatCombination(
+ pixelFormat,
+ imageFormat
+ );
if (shouldOutputImageSequence) {
fs.mkdirSync(absoluteOutputFile, {
recursive: true,
@@ -136,7 +144,6 @@ export const render = async () => {
},
cliProgress.Presets.shades_grey
);
- const imageFormat = getImageFormat(codec);
await renderFrames({
config,
onFrameUpdate: (frame) => renderProgress.update(frame),
@@ -182,7 +189,7 @@ export const render = async () => {
outputLocation: absoluteOutputFile,
force: overwrite,
imageFormat,
- pixelFormat: Internals.getPixelFormat(),
+ pixelFormat,
codec,
crf,
onProgress: (frame) => {
diff --git a/packages/core/src/config/crf.ts b/packages/core/src/config/crf.ts
index 379b3e1a8b..1c70d5c6d5 100644
--- a/packages/core/src/config/crf.ts
+++ b/packages/core/src/config/crf.ts
@@ -47,7 +47,10 @@ export const getValidCrfRanges = (codec: Codec): [number, number] => {
throw new TypeError(`Got unexpected codec "${codec}"`);
};
-export const validateSelectedCrf = (crf: number, codec: Codec) => {
+export const validateSelectedCrfAndCodecCombination = (
+ crf: number,
+ codec: Codec
+) => {
const range = getValidCrfRanges(codec);
if (crf < range[0] || crf > range[1]) {
throw new TypeError(
diff --git a/packages/core/src/config/image-format.ts b/packages/core/src/config/image-format.ts
index 8f3212ad38..8ace1bf422 100644
--- a/packages/core/src/config/image-format.ts
+++ b/packages/core/src/config/image-format.ts
@@ -1,3 +1,5 @@
+import {PixelFormat} from '.';
+
const validOptions = ['png', 'jpeg'] as const;
export type ImageFormat = typeof validOptions[number];
@@ -18,3 +20,17 @@ export const setImageFormat = (format: ImageFormat) => {
export const getUserPreferredImageFormat = () => {
return currentImageFormat;
};
+
+export const validateSelectedPixelFormatAndImageFormatCombination = (
+ pixelFormat: PixelFormat,
+ imageFormat: ImageFormat
+) => {
+ if (pixelFormat !== 'yuva420p') {
+ return;
+ }
+ if (imageFormat !== 'png') {
+ throw new TypeError(
+ "Pixel format was set to 'yuva420p' but the image format is not PNG. To render transparent videos, you need to set PNG as the image format."
+ );
+ }
+};
diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts
index 48ed8e83d8..097f921839 100644
--- a/packages/core/src/config/index.ts
+++ b/packages/core/src/config/index.ts
@@ -1,4 +1,4 @@
-import {setCodec, setOutputFormat} from './codec';
+import {Codec, setCodec, setOutputFormat} from './codec';
import {Concurrency, setConcurrency} from './concurrency';
import {setCrf} from './crf';
import {ImageFormat, setImageFormat} from './image-format';
@@ -75,11 +75,11 @@ export const Config = {
},
} as const;
-export type {Codec} from './codec';
export type {
PixelFormat,
Concurrency,
WebpackConfiguration,
WebpackOverrideFn,
ImageFormat,
+ Codec,
};
diff --git a/packages/core/src/config/pixel-format.ts b/packages/core/src/config/pixel-format.ts
index 9a2d58dcb8..d256608918 100644
--- a/packages/core/src/config/pixel-format.ts
+++ b/packages/core/src/config/pixel-format.ts
@@ -1,5 +1,8 @@
+import {Codec} from './codec';
+
const validOptions = [
'yuv420p',
+ 'yuva420p',
'yuv422p',
'yuv444p',
'yuv420p10le',
@@ -9,7 +12,9 @@ const validOptions = [
export type PixelFormat = typeof validOptions[number];
-let currentPixelFormat: PixelFormat = 'yuv420p';
+export const DEFAULT_PIXEL_FORMAT: PixelFormat = 'yuv420p';
+
+let currentPixelFormat: PixelFormat = DEFAULT_PIXEL_FORMAT;
export const setPixelFormat = (format: PixelFormat) => {
if (!validOptions.includes(format)) {
@@ -21,3 +26,17 @@ export const setPixelFormat = (format: PixelFormat) => {
export const getPixelFormat = () => {
return currentPixelFormat;
};
+
+export const validateSelectedPixelFormatAndCodecCombination = (
+ pixelFormat: PixelFormat,
+ codec: Codec
+) => {
+ if (pixelFormat !== 'yuva420p') {
+ return;
+ }
+ if (codec !== 'vp8' && codec !== 'vp9') {
+ throw new TypeError(
+ "Pixel format was set to 'yuva420p' but codec is not 'vp8' or 'vp8'. To render videos with alpha channel, you must choose a codec that supports it."
+ );
+ }
+};
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index 6b1063e546..f9db536ea6 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -13,16 +13,23 @@ import {getConcurrency} from './config/concurrency';
import {
getActualCrf,
getDefaultCrfForCodec,
- validateSelectedCrf,
+ validateSelectedCrfAndCodecCombination,
} from './config/crf';
-import {getUserPreferredImageFormat} from './config/image-format';
+import {
+ getUserPreferredImageFormat,
+ validateSelectedPixelFormatAndImageFormatCombination,
+} from './config/image-format';
import {getShouldOutputImageSequence} from './config/image-sequence';
import {
getWebpackOverrideFn,
WebpackOverrideFn,
} from './config/override-webpack';
import {getShouldOverwrite} from './config/overwrite';
-import {getPixelFormat} from './config/pixel-format';
+import {
+ DEFAULT_PIXEL_FORMAT,
+ getPixelFormat,
+ validateSelectedPixelFormatAndCodecCombination,
+} from './config/pixel-format';
import {getQuality} from './config/quality';
import * as perf from './perf';
import {getCompositionName, getIsEvaluation, getRoot} from './register-root';
@@ -50,12 +57,15 @@ export const Internals = {
getWebpackOverrideFn,
getQuality,
getShouldOutputImageSequence,
- validateSelectedCrf,
+ validateSelectedCrfAndCodecCombination,
getFinalOutputCodec,
DEFAULT_CODEC,
+ DEFAULT_PIXEL_FORMAT,
getDefaultCrfForCodec,
getActualCrf,
getUserPreferredImageFormat,
+ validateSelectedPixelFormatAndImageFormatCombination,
+ validateSelectedPixelFormatAndCodecCombination,
};
export type {
diff --git a/packages/docs/docs/config.md b/packages/docs/docs/config.md
index 98ba5634fa..eae3b69fd8 100644
--- a/packages/docs/docs/config.md
+++ b/packages/docs/docs/config.md
@@ -10,7 +10,9 @@ You can control several behaviors of Remotion here.
```tsx
import {Config} from 'remotion';
+Config.Rendering.Concurrency(8);
Config.Output.setPixelFormat('yuv444p');
+Config.Output.setCodec('h265');
```
## Bundling
diff --git a/packages/renderer/src/stitcher.ts b/packages/renderer/src/stitcher.ts
index d1755696e0..4e65373c29 100644
--- a/packages/renderer/src/stitcher.ts
+++ b/packages/renderer/src/stitcher.ts
@@ -36,8 +36,8 @@ export const stitchFramesToVideo = async (options: {
}): Promise<void> => {
const codec = options.codec ?? Internals.DEFAULT_CODEC;
const crf = options.crf ?? Internals.getDefaultCrfForCodec(codec);
- const format = options.imageFormat ?? DEFAULT_IMAGE_FORMAT;
- Internals.validateSelectedCrf(crf, codec);
+ const imageFormat = options.imageFormat ?? DEFAULT_IMAGE_FORMAT;
+ const pixelFormat = options.pixelFormat ?? Internals.DEFAULT_PIXEL_FORMAT;
await validateFfmpeg();
const files = await fs.promises.readdir(options.dir);
const biggestNumber = Math.max(
@@ -51,7 +51,12 @@ export const stitchFramesToVideo = async (options: {
const numberLength = String(biggestNumber).length;
const encoderName = getCodecName(codec);
- Internals.validateSelectedCrf(crf, codec);
+ Internals.validateSelectedCrfAndCodecCombination(crf, codec);
+ Internals.validateSelectedPixelFormatAndImageFormatCombination(
+ pixelFormat,
+ imageFormat
+ );
+ Internals.validateSelectedPixelFormatAndCodecCombination(pixelFormat, codec);
const task = execa(
'ffmpeg',
@@ -63,7 +68,7 @@ export const stitchFramesToVideo = async (options: {
'-s',
`${options.width}x${options.height}`,
'-i',
- `element-%0${numberLength}d.${format}`,
+ `element-%0${numberLength}d.${imageFormat}`,
'-c:v',
encoderName,
'-crf',
@@ -72,7 +77,7 @@ export const stitchFramesToVideo = async (options: {
'1M',
options.force ? '-y' : null,
'-pix_fmt',
- options.pixelFormat ?? 'yuv420p',
+ pixelFormat,
options.outputLocation,
].filter(Boolean) as string[],
{cwd: options.dir}