Skip to content

🎥 Make videos programmatically with React

docs and more

Jonny Burgerd8fea4b65c09
diff --git a/packages/docs/docs/encoding.mdx b/packages/docs/docs/encoding.mdx
index fa6c7b63af..eb81d1fd88 100644
--- a/packages/docs/docs/encoding.mdx
+++ b/packages/docs/docs/encoding.mdx
@@ -2,7 +2,7 @@
image: /generated/articles-docs-encoding.png
id: encoding
title: Encoding Guide
-crumb: "Codecs and more"
+crumb: 'Codecs and more'
---
Backed by [FFmpeg](https://ffmpeg.org/), Remotion allows you to configure a variety of encoding settings. The goal of this page is to help you navigate through the settings and to help you choose the right one.
@@ -18,67 +18,73 @@ Remotion supports 5 video codecs: `h264` (_default_), `h265`, `vp8`, `vp9` and `
<th>File size</th>
<th>Encoding time</th>
<th>Browser compatibility</th>
+ <th>Hardware acceleration possible</th>
</tr>
<tr>
<td>
H.264 <sub>also known as MPEG-4</sub>
</td>
<td>.mp4, .mov or .mkv</td>
- <td style={{ color: "darkorange" }}>Medium</td>
- <td style={{ color: "green", fontWeight: "bold" }}>Very fast</td>
+ <td style={{color: 'darkorange'}}>Medium</td>
+ <td style={{color: 'green', fontWeight: 'bold'}}>Very fast</td>
<td>
<a
href="https://caniuse.com/mpeg4"
- style={{ color: "green", fontWeight: "bold" }}
+ style={{color: 'green', fontWeight: 'bold'}}
>
Very good
</a>
</td>
+ <td>No</td>
</tr>
<tr>
<td>
H.265 <sub>also known as HEVC</sub>
</td>
<td>.mp4 or .hevc</td>
- <td style={{ color: "darkorange" }}>Medium</td>
- <td style={{ color: "green" }}>Fast</td>
+ <td style={{color: 'darkorange'}}>Medium</td>
+ <td style={{color: 'green'}}>Fast</td>
<td>
<a
href="https://caniuse.com/hevc"
- style={{ color: "red", fontWeight: "bold" }}
+ style={{color: 'red', fontWeight: 'bold'}}
>
Very poor
</a>
</td>
+ <td>No</td>
</tr>
<tr>
<td>VP8</td>
<td>.webm</td>
- <td style={{ color: "green" }}>Small</td>
- <td style={{ color: "red" }}>Slow</td>
+ <td style={{color: 'green'}}>Small</td>
+ <td style={{color: 'red'}}>Slow</td>
<td>
- <a href="https://caniuse.com/webm" style={{ color: "darkorange" }}>
+ <a href="https://caniuse.com/webm" style={{color: 'darkorange'}}>
Okay
</a>
</td>
+ <td>No</td>
</tr>
<tr>
<td>VP9</td>
<td>.webm</td>
- <td style={{ color: "green", fontWeight: "bold" }}>Very small</td>
- <td style={{ color: "red", fontWeight: "bold" }}>Very slow</td>
+ <td style={{color: 'green', fontWeight: 'bold'}}>Very small</td>
+ <td style={{color: 'red', fontWeight: 'bold'}}>Very slow</td>
<td>
- <a href="https://caniuse.com/webm" style={{ color: "darkorange" }}>
+ <a href="https://caniuse.com/webm" style={{color: 'darkorange'}}>
Okay
</a>
</td>
+ <td>No</td>
</tr>
<tr>
<td>ProRes</td>
<td>.mov</td>
- <td style={{ color: "red" }}>Large</td>
- <td style={{ color: "green" }}>Fast</td>
- <td style={{ color: "red", fontWeight: "bold" }}>None</td>
+ <td style={{color: 'red'}}>Large</td>
+ <td style={{color: 'green'}}>Fast</td>
+ <td style={{color: 'red', fontWeight: 'bold'}}>None</td>
+ <td style={{color: 'green', fontWeight: 'bold'}}>On macOS</td>
</tr>
</table>
@@ -88,6 +94,8 @@ Click on a browser compatibility link to see exactly which browsers are supporte
You can set a config using [`Config.setCodec()` in the config file](/docs/config#setcodec) or the [`--codec`](/docs/cli) CLI flag.
+[Hardware acceleration](/docs/hardware-acceleration) is available from Remotion 4.0.228.
+
## Controlling quality using the CRF setting
_Applies only to `h264`, `h265`, `vp8` and `vp9`._
@@ -96,17 +104,17 @@ No matter which codec you end up using, there's always a tradeoff between file s
Be cautious: Every codec has it's own range of acceptable values and a different default. So while `23` will look very good on a H264 video, it will look terrible on a WebM video. Use this chart to determine which CRF value to use:
-<details style={{ fontSize: "0.9em" }}>
+<details style={{fontSize: '0.9em'}}>
<summary>Changelog</summary>
<ul>
<li>
- Since version 2.1.3, Remotion doesn't allow the CRF to be set to{" "}
+ Since version 2.1.3, Remotion doesn't allow the CRF to be set to{' '}
<code>0</code> anymore because of the issues it causes on macOS/iOS and
possible other scenarios. Set the CRF to 1 or higher.
</li>
</ul>
</details>
-<div style={{ height: 10 }} />
+<div style={{height: 10}} />
<table>
<tr>
<th>Codec</th>
@@ -228,7 +236,7 @@ The container format will be automatically derived based on the file extension.
import {
SupportedAudioCodecTable,
FileExtensionTable,
-} from "../components/SupportedAudioCodec";
+} from '../components/SupportedAudioCodec';
<SupportedAudioCodecTable />
diff --git a/packages/docs/docs/gpu.mdx b/packages/docs/docs/gpu.mdx
index 8d21d50ce5..a934dbe878 100644
--- a/packages/docs/docs/gpu.mdx
+++ b/packages/docs/docs/gpu.mdx
@@ -3,7 +3,7 @@ image: /generated/articles-docs-gpu.png
id: gpu
title: Using the GPU
sidebar_label: Using the GPU
-crumb: "Need for Speed"
+crumb: 'Need for Speed'
---
Some types of content in Remotion can benefit from a GPU being available on the machine that is used for rendering.
@@ -54,3 +54,4 @@ We'd love to learn and document more findings about the GPU. Let us know and we
## See also
- [OpenGL renderer backends](/docs/gl-options)
+- [Hardware accelerated rendering](/docs/hardware-acceleration)
diff --git a/packages/docs/docs/hardware-acceleration.mdx b/packages/docs/docs/hardware-acceleration.mdx
new file mode 100644
index 0000000000..23d083b523
--- /dev/null
+++ b/packages/docs/docs/hardware-acceleration.mdx
@@ -0,0 +1,98 @@
+---
+image: /generated/articles-docs-hardware-acceleration.png
+title: Hardware accelerated encoding
+sidebar_label: Hardware acceleration
+id: hardware-acceleration
+crumb: 'Rendering'
+---
+
+Encoding is the process of converting a sequence of images into a video file.
+Besides rendering frames, encoding is one of the two steps required to create a video.
+
+From Remotion v4.0.228, Remotion supports hardware-accelerated encoding in some cases.
+Since encoding is platform- and codec-specific, only a few scenarios are supported at the moment.
+
+- Currently, only macOS is supported (Acceleration using VideoToolbox)
+- Only ProRes is supported
+
+## Enable hardware accelerated encoding
+
+By default, hardware acceleration is `"disabled"`.
+You can set the `hardwareAcceleration` option to `"if-possible"` to enable hardware acceleration if it is available.
+If you want the render to fail if hardware acceleration is not possible, set the option to `"required"`.
+
+### In SSR APIs
+
+Use the [`hardwareAcceleration`](/docs/renderer/render-media#hardwareacceleration) option in the [`renderMedia()`](docs/renderer/render-media) function.
+
+```tsx twoslash {8}
+// @module: esnext
+// @target: es2017
+const serveUrl = '/path/to/bundle';
+const outputLocation = '/path/to/frames';
+
+import {renderMedia, selectComposition} from '@remotion/renderer';
+
+const inputProps = {
+ titleText: 'Hello World',
+};
+
+const composition = await selectComposition({
+ serveUrl,
+ id: 'my-video',
+ inputProps,
+});
+
+// ---cut---
+
+await renderMedia({
+ composition,
+ serveUrl,
+ codec: 'prores',
+ outputLocation,
+ inputProps,
+ hardwareAcceleration: 'if-possible',
+});
+```
+
+### In the CLI
+
+Use the [`--hardware-acceleration`](/docs/cli/render#--hardware-acceleration) option in the `npx remotion studio` command.
+
+```bash
+npx remotion render MyComp --codec prores --hardware-acceleration if-possible
+```
+
+### In the Studio
+
+In the [Remotion Studio](/docs/studio), you can set the hardware acceleration option in the "Advanced" tab when rendering a video.
+
+### With the config file
+
+You can set the [`setHardwareAcceleration()`](/docs/config#sethardwareacceleration) option in the [config file](/docs/config).
+
+```ts twoslash
+import {Config} from '@remotion/cli/config';
+
+Config.setHardwareAcceleration('if-possible');
+```
+
+### In Remotion Lambda and Cloud Run
+
+These options are not supported in Remotion Lambda and Cloud Run, because those cloud services do not support hardware acceleration.
+
+## Tell if hardware acceleration is being used
+
+[Run the render with verbose logging.](/docs/troubleshooting/debug-failed-render)
+If the render is using hardware acceleration, you will see a log message like this:
+
+```
+Encoder: prores_videotoolbox, hardware accelerated: true
+```
+
+Don't rely on the exact wording of the log message to determine if hardware acceleration is being used.
+
+## See also
+
+- [Encoding guide](/docs/encoding)
+- [Using the GPU](/docs/gpu)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index b248ce23a1..d7fffb9f53 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -684,6 +684,7 @@ module.exports = {
'video-vs-offthreadvideo',
'artifacts',
'metadata',
+ 'hardware-acceleration',
],
},
{
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 0b72a16553..91cf520107 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -1210,6 +1210,13 @@ export const articles = [
compId: 'articles-docs-how-props-flow',
crumb: 'Parameterized videos',
},
+ {
+ id: 'hardware-acceleration',
+ title: 'Hardware accelerated encoding',
+ relativePath: 'docs/hardware-acceleration.mdx',
+ compId: 'articles-docs-hardware-acceleration',
+ crumb: 'Rendering',
+ },
{
id: 'preview',
title: 'Preview your video',
diff --git a/packages/docs/static/generated/articles-docs-hardware-acceleration.png b/packages/docs/static/generated/articles-docs-hardware-acceleration.png
new file mode 100644
index 0000000000..7a0a24a715
Binary files /dev/null and b/packages/docs/static/generated/articles-docs-hardware-acceleration.png differ
diff --git a/packages/renderer/src/crf.ts b/packages/renderer/src/crf.ts
index 02e199d0e2..de679b62fb 100644
--- a/packages/renderer/src/crf.ts
+++ b/packages/renderer/src/crf.ts
@@ -1,24 +1,25 @@
/* eslint-disable no-console */
+import type {HardwareAccelerationOption} from './client';
import type {Codec} from './codec';
import {isAudioCodec} from './is-audio-codec';
export type Crf = number | undefined;
-const defaultCrfMap: {[key in Codec]: number} = {
+const defaultCrfMap: {[key in Codec]: number | null} = {
h264: 18,
h265: 23,
vp8: 9,
vp9: 28,
- prores: 0,
- gif: 0,
+ prores: null,
+ gif: null,
'h264-mkv': 18,
'h264-ts': 18,
- aac: 0,
- mp3: 0,
- wav: 0,
+ aac: null,
+ mp3: null,
+ wav: null,
};
-export const getDefaultCrfForCodec = (codec: Codec): number => {
+export const getDefaultCrfForCodec = (codec: Codec): number | null => {
const val = defaultCrfMap[codec];
if (val === undefined) {
throw new TypeError(`Got unexpected codec "${codec}"`);
@@ -56,12 +57,14 @@ export const validateQualitySettings = ({
videoBitrate,
encodingMaxRate,
encodingBufferSize,
+ hardwareAcceleration,
}: {
crf: unknown;
codec: Codec;
videoBitrate: string | null;
encodingMaxRate: string | null;
encodingBufferSize: string | null;
+ hardwareAcceleration: HardwareAccelerationOption;
}): string[] => {
if (crf && videoBitrate) {
throw new Error(
@@ -69,6 +72,10 @@ export const validateQualitySettings = ({
);
}
+ if (crf && hardwareAcceleration === 'required') {
+ throw new Error('"crf" option is not supported with hardware acceleration');
+ }
+
if (encodingMaxRate && !encodingBufferSize) {
throw new Error(
'"encodingMaxRate" can not be set without also setting "encodingBufferSize".',
@@ -96,6 +103,10 @@ export const validateQualitySettings = ({
if (crf === null || typeof crf === 'undefined') {
const actualCrf = getDefaultCrfForCodec(codec);
+ if (actualCrf === null) {
+ return [...bufSizeArray, ...maxRateArray];
+ }
+
return ['-crf', String(actualCrf), ...bufSizeArray, ...maxRateArray];
}
diff --git a/packages/renderer/src/ffmpeg-args.ts b/packages/renderer/src/ffmpeg-args.ts
index d919f6cc10..3eaffe78f4 100644
--- a/packages/renderer/src/ffmpeg-args.ts
+++ b/packages/renderer/src/ffmpeg-args.ts
@@ -19,6 +19,7 @@ const firstEncodingStepOnly = ({
videoBitrate,
encodingMaxRate,
encodingBufferSize,
+ hardwareAcceleration,
}: {
hasPreencoded: boolean;
proResProfileName: string | null;
@@ -29,6 +30,7 @@ const firstEncodingStepOnly = ({
videoBitrate: string | null;
encodingMaxRate: string | null;
encodingBufferSize: string | null;
+ hardwareAcceleration: HardwareAccelerationOption;
}): string[][] => {
if (hasPreencoded || codec === 'gif') {
return [];
@@ -48,6 +50,7 @@ const firstEncodingStepOnly = ({
codec,
encodingMaxRate,
encodingBufferSize,
+ hardwareAcceleration,
}),
].filter(truthy);
};
@@ -152,6 +155,7 @@ export const generateFfmpegArgs = ({
encodingMaxRate,
encodingBufferSize,
x264Preset,
+ hardwareAcceleration,
}),
].filter(truthy);
};
diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.ts
index 820b51efd4..51170adc51 100644
--- a/packages/renderer/src/render-media.ts
+++ b/packages/renderer/src/render-media.ts
@@ -268,6 +268,7 @@ const internalRenderMediaRaw = ({
videoBitrate,
encodingMaxRate,
encodingBufferSize,
+ hardwareAcceleration,
});
validateBitrate(audioBitrate, 'audioBitrate');
validateBitrate(videoBitrate, 'videoBitrate');
diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.ts
index 5e0ad72d58..9cb42d2560 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -255,6 +255,7 @@ const innerStitchFramesToVideo = async (
videoBitrate,
encodingMaxRate: maxRate,
encodingBufferSize: bufferSize,
+ hardwareAcceleration,
});
validateSelectedPixelFormatAndCodecCombination(pixelFormat, codec);
diff --git a/packages/renderer/src/test/crf.test.ts b/packages/renderer/src/test/crf.test.ts
index d1b7360981..1fa06bc7c6 100644
--- a/packages/renderer/src/test/crf.test.ts
+++ b/packages/renderer/src/test/crf.test.ts
@@ -48,6 +48,7 @@ describe('validateSelectedCrfAndCodecCombination valid input', () => {
videoBitrate: null,
encodingMaxRate: null,
encodingBufferSize: null,
+ hardwareAcceleration: 'if-possible',
}),
).not.toThrow()),
);
@@ -78,6 +79,7 @@ describe('validateSelectedCrfAndCodecCombination invalid input', () => {
videoBitrate: null,
encodingMaxRate: null,
encodingBufferSize: null,
+ hardwareAcceleration: 'if-possible',
}),
).toThrow(
new RegExp(
@@ -95,6 +97,7 @@ test('ProRes', () => {
videoBitrate: null,
encodingMaxRate: null,
encodingBufferSize: null,
+ hardwareAcceleration: 'if-possible',
}),
).toThrow(/The "prores" codec does not support the --crf option\./);
});
@@ -107,6 +110,7 @@ test('WAV', () => {
videoBitrate: null,
encodingMaxRate: null,
encodingBufferSize: null,
+ hardwareAcceleration: 'if-possible',
}),
).toThrow(/The "wav" codec does not support the --crf option\./);
});
@@ -119,6 +123,7 @@ test('WAV', () => {
videoBitrate: '1M',
encodingMaxRate: null,
encodingBufferSize: null,
+ hardwareAcceleration: 'if-possible',
}),
).toThrow(
/"crf" and "videoBitrate" can not both be set. Choose one of either./,
@@ -133,6 +138,7 @@ test('encodingMaxRate', () => {
videoBitrate: null,
encodingMaxRate: '1M',
encodingBufferSize: null,
+ hardwareAcceleration: 'if-possible',
}),
).toThrow(
/"encodingMaxRate" can not be set without also setting "encodingBufferSize"./,
diff --git a/packages/studio/src/components/RenderModal/CrfSetting.tsx b/packages/studio/src/components/RenderModal/CrfSetting.tsx
index f79d98b972..f3cb0f3406 100644
--- a/packages/studio/src/components/RenderModal/CrfSetting.tsx
+++ b/packages/studio/src/components/RenderModal/CrfSetting.tsx
@@ -4,7 +4,7 @@ import {BrowserSafeApis} from '@remotion/renderer/client';
import {useState} from 'react';
import {NumberSetting} from './NumberSetting';
-type CrfState = Record<Codec, number>;
+type CrfState = Record<Codec, number | null>;
const getDefaultCrfState = () => {
return BrowserSafeApis.validCodecs
@@ -14,7 +14,7 @@ const getDefaultCrfState = () => {
.reduce((acc, [codec, crf]) => {
return {
...acc,
- [codec]: crf,
+ [codec as Codec]: crf,
};
}, {} as CrfState);
};
@@ -25,9 +25,14 @@ export const useCrfState = (codec: Codec) => {
const setCrf: React.Dispatch<React.SetStateAction<number>> = (updater) => {
setState((q) => {
+ const val = q[codec];
+ if (val === null) {
+ throw new TypeError(`Got unexpected codec "${codec}"`);
+ }
+
return {
...q,
- [codec]: typeof updater === 'number' ? updater : updater(q[codec]),
+ [codec]: typeof updater === 'number' ? updater : updater(val),
};
});
};
diff --git a/packages/studio/src/components/RenderModal/RenderModalPicture.tsx b/packages/studio/src/components/RenderModal/RenderModalPicture.tsx
index 05332c723c..e4c3a3d13f 100644
--- a/packages/studio/src/components/RenderModal/RenderModalPicture.tsx
+++ b/packages/studio/src/components/RenderModal/RenderModalPicture.tsx
@@ -54,7 +54,7 @@ export const RenderModalPicture: React.FC<{
readonly setCustomTargetVideoBitrateValue: React.Dispatch<
React.SetStateAction<string>
>;
- readonly crf: number;
+ readonly crf: number | null;
readonly customTargetVideoBitrate: string;
readonly shouldDisplayQualityControlPicker: boolean;
readonly pixelFormatOptions: ComboboxValue[];
@@ -198,12 +198,13 @@ export const RenderModalPicture: React.FC<{
) : null}
{qualityControlType === 'crf' &&
renderMode !== 'still' &&
- renderMode !== 'sequence' ? (
+ renderMode !== 'sequence' &&
+ crf !== null ? (
<CrfSetting
crf={crf}
min={minCrf}
max={maxCrf}
- setCrf={setCrf}
+ setCrf={setCrf as React.Dispatch<React.SetStateAction<number>>}
option="crfOption"
/>
) : null}