Skip to content

🎥 Make videos programmatically with React

document it

Jonny Burgeree974d1df4cf
diff --git a/packages/docs/docs/renderer/render-media.md b/packages/docs/docs/renderer/render-media.md
index b852f528e3..c0e269700f 100644
--- a/packages/docs/docs/renderer/render-media.md
+++ b/packages/docs/docs/renderer/render-media.md
@@ -21,7 +21,7 @@ _string_
Either a local path pointing to a Remotion Webpack bundle generated by [`bundle()`](/docs/bundle) or a URL where the bundle is hosted.
-### `outputLocation`
+### `outputLocation?`
_string - optional since v3.0.26_
@@ -41,7 +41,7 @@ _"h264" (default) | "h265" | "vp8" | "vp9" | "mp3" | "aac" | "wav" | "prores" |
Choose a suitable codec for your output media. Refer to the [Encoding guide](/docs/encoding) to find the best codec for your use case.
-### `inputProps`
+### `inputProps?`
_object - optional_
@@ -57,13 +57,13 @@ A `number` specifying how many render processes should be started in parallel or
Renamed to `concurrency` in v3.2.17.
-### `crf`
+### `crf?`
_number | null - optional_
The constant rate factor, controlling the quality. See: [Controlling quality using the CRF setting.](/docs/encoding/#controlling-quality-using-the-crf-setting)
-### `imageFormat`
+### `imageFormat?`
_"jpeg" (default) | "png" | "none" - optional_
@@ -73,7 +73,7 @@ In which image format the frames should be rendered.
- `png` if you want to [render transparent videos](/docs/transparent-videos/)
- `none` if you are rendering audio
-### `ffmpegExecutable`
+### `ffmpegExecutable?`
_string - optional_
@@ -103,13 +103,13 @@ _optional, available since v3.1_
[Set the looping behavior.](/docs/config#setnumberofgifloops) This option may only be set when rendering GIFs. [See here for more details.](/docs/render-as-gif#changing-the-number-of-loops)
-### `pixelFormat`
+### `pixelFormat?`
_string - optional_
[A custom pixel format to use.](/docs/transparent-videos/) Usually used for special use cases like transparent videos.
-### `envVariables`
+### `envVariables?`
`Record<string, string> - optional`
@@ -117,7 +117,7 @@ An object containing environment variables to be injected in your project.
See: [Environment variables](/docs/env-variables/)
-### `quality`
+### `quality?`
_number - optional_
@@ -125,43 +125,43 @@ Sets the quality of the generated JPEG images. Must be an integer between 0 and
Only applies if `imageFormat` is `'jpeg'`, otherwise this option is invalid.
-### `frameRange`
+### `frameRange?`
_number | [number, number] - optional_
Specify a single frame (passing a `number`) or a range of frames (passing a tuple `[number, number]`) to be rendered. By passing `null` (default) all frames of a composition get rendered.
-### `muted`
+### `muted?`
_boolean - optional - available since v3.2.1_
If set to true, no audio is being rendered.
-### `enforceAudioTrack`
+### `enforceAudioTrack?`
_boolean - optional - available since v3.2.1_
Render a silent audio track if there wouldn't be any otherwise.
-### `puppeteerInstance`
+### `puppeteerInstance?`
_puppeteer.Browser - optional_
An already open Puppeteer [`Browser`](https://pptr.dev/#?product=Puppeteer&version=main&show=api-class-browser) instance. Use [`openBrowser()`](/docs/renderer/open-browser) to create a new instance. Reusing a browser across multiple function calls can speed up the rendering process. You are responsible for opening and closing the browser yourself. If you don't specify this option, a new browser will be opened and closed at the end.
-### `scale`
+### `scale?`
_optional_
Scales the output dimensions by a factor. See [Scaling](/docs/scaling) to learn more about this feature.
-### `overwrite`
+### `overwrite?`
_boolean - optional_
If set to false, the output file will not be written if a file already exists.
-### `onStart`
+### `onStart?`
_function_ - optional
@@ -175,7 +175,7 @@ const onStart = ({ frameCount }: OnStartData) => {
};
```
-### `onProgress`
+### `onProgress?`
_function - optional_
@@ -220,7 +220,7 @@ const onProgress: RenderMediaOnProgress = ({
The `progress` attribute is available from v3.2.17
:::
-### `onDownload`
+### `onDownload?`
_function - optional_
@@ -243,19 +243,19 @@ const onDownload: RenderMediaOnDownload = (src) => {
};
```
-### `proResProfile`
+### `proResProfile?`
_string - optional_
Sets a ProRes profile. Only applies to videos rendered with `prores` codec. See [Encoding guide](/docs/encoding/#controlling-quality-using-prores-profile) for possible options.
-### `dumpBrowserLogs`
+### `dumpBrowserLogs?`
_boolean - optional_
If true, will print browser console output to standard output.
-### `onBrowserLog`
+### `onBrowserLog?`
_function - optional_
@@ -284,7 +284,7 @@ _optional, available from v3.0.15_
A token that allows the render to be cancelled. See: [`makeCancelSignal()`](/docs/renderer/make-cancel-signal)
-### `verbose`
+### `verbose?`
_optional, available from v3.1.6_
@@ -334,27 +334,36 @@ Accepted values:
**Default for local rendering**: `null`.
**Default for Lambda rendering**: `"swangle"`.
-### `ffmpegArgsHook`
+### `ffmpegOverride?`
-_function - optional_
-
-Get the ffmpeg command of the rendered video. You can modify the ffmpeg command, and finally return a new ffmpeg command.
+_function - optional - available from v3.2.22_
-Example:
+Modifies the FFMPEG command that Remotion uses under the hood. It works reducer-style, meaning that you pass a function that takes a command as an argument and returns a new command.
```tsx twoslash
-import { FfmpegArgsHook } from "@remotion/renderer";
+import type { FfmpegArgsHook } from "@remotion/renderer";
-const ffmpegArgsHook: FfmpegArgsHook = (args) => {
- // add command
- args.push("-vf", "eq=brightness=0:saturation=1");
- console.log(args);
- return args;
+const ffmpegArgsHook: FfmpegArgsHook = ({ type, args }) => {
+ console.log(type); // "stitcher" | "pre-stitcher
+ return [...args, "-vf", "eq=brightness=0:saturation=1"];
};
```
+The function you pass must accept an object as it's only parameter which contains the following properties:
+
+- `type`: Either `"stitcher"` or `"pre-stitcher"`. If enough memory and CPU is available, Remotion may use parallel rendering and encoding, which means that a pre-stitcher process gets spawned before all frames are rendered. You can tell whether parallel encoding is enabled by adding `--log=verbose` to your render command.
+- `args`: An array of strings that is passed as arguments to the FFMPEG command.
+
+Your function must return a modified array of strings.
+
:::warning
-You should understand the impact of the command you added to prevent unknown errors. You should use it carefully.
+Using this feature is discouraged. Before using it, we want to make you aware of some caveats:
+
+- The render command can change with any new Remotion version, even when it is a patch upgrade. This might break your usage of this feature.
+- Depending on the selected codec, available CPU and RAM, Remotion may or may not use "parallel encoding" which will result in multiple FFMPEG commands being executed. Your function must be able to handle being called multiple times.
+- This feature is not available when using Remotion Lambda.
+
+Before you use this hack, reach out to the Remotion team on [Discord](https://remotion.dev/discord) and ask us if we are open to implement the feature you need in a clean way - we often do implement new features quickly based on users feedback.
:::
## See also
diff --git a/packages/docs/docs/renderer/stitch-frames-to-video.md b/packages/docs/docs/renderer/stitch-frames-to-video.md
index c8f7bf779f..bce860b93a 100644
--- a/packages/docs/docs/renderer/stitch-frames-to-video.md
+++ b/packages/docs/docs/renderer/stitch-frames-to-video.md
@@ -31,7 +31,11 @@ A `number` specifying the desired output width in pixels for the video.
A `number` specifying the desired output height in pixels for the video.
-### `outputLocation`
+### `assetsInfo`
+
+Information about the audio mix. This is part of the return value of [renderFrames()](/docs/renderer/render-frames#return-value).
+
+### `outputLocation?`
_optional since v3.0.26_
@@ -39,13 +43,11 @@ An absolute path specify where the output file should be written to.
If not specified or set to `null`, the file will be returned in-memory as a buffer.
-### `force`
-
-Whether in case of an existing file in `outputLocation` it should be overwritten. Type `boolean`.
+### `force?`
-### `assetsInfo`
+_optional_
-Information about the audio mix. This is part of the return value of [renderFrames()](/docs/renderer/render-frames#return-value).
+Whether Remotion should overwrite the file in `outputLocation` if it already exists. `true` by default.
### `pixelFormat?`
@@ -101,19 +103,19 @@ _optional, available since v3.1_
Set the looping behavior. This option may only be set when rendering GIFs. [See here for more details.](/docs/render-as-gif#changing-the-number-of-loops)
-### `muted`
+### `muted?`
_optional, available since v3.2.1_
Disables audio output. This option may only be set in combination with a video codec and should also be passed to [`renderFrames()`](/docs/renderer/render-frames).
-### `verbose`
+### `verbose?`
_optional_
A boolean value that when set to `true`, will log all kinds of debug information. Default `false`.
-### `ffmpegExecutable`
+### `ffmpegExecutable?`
_optional_
@@ -131,6 +133,38 @@ _optional, available from v3.0.15_
A token that allows the render to be cancelled. See: [`makeCancelSignal()`](/docs/renderer/make-cancel-signal)
+### `ffmpegOverride?`
+
+_function - optional - available from v3.2.22_
+
+Modifies the FFMPEG command that Remotion uses under the hood. It works reducer-style, meaning that you pass a function that takes a command as an argument and returns a new command.
+
+```tsx twoslash
+import type { FfmpegArgsHook } from "@remotion/renderer";
+
+const ffmpegArgsHook: FfmpegArgsHook = ({ type, args }) => {
+ console.log(type); // "stitcher" | "pre-stitcher
+ return [...args, "-vf", "eq=brightness=0:saturation=1"];
+};
+```
+
+The function you pass must accept an object as it's only parameter which contains the following properties:
+
+- `type`: Either `"stitcher"` or `"pre-stitcher"`. If enough memory and CPU is available, Remotion may use parallel rendering and encoding, which means that a pre-stitcher process gets spawned before all frames are rendered. You can tell whether parallel encoding is enabled by adding `--log=verbose` to your render command.
+- `args`: An array of strings that is passed as arguments to the FFMPEG command.
+
+Your function must return a modified array of strings.
+
+:::warning
+Using this feature is discouraged. Before using it, we want to make you aware of some caveats:
+
+- The render command can change with any new Remotion version, even when it is a patch upgrade. This might break your usage of this feature.
+- Depending on the selected codec, available CPU and RAM, Remotion may or may not use "parallel encoding" which will result in multiple FFMPEG commands being executed. Your function must be able to handle being called multiple times.
+- This feature is not available when using Remotion Lambda.
+
+Before you use this hack, reach out to the Remotion team on [Discord](https://remotion.dev/discord) and ask us if we are open to implement the feature you need in a clean way - we often do implement new features quickly based on users feedback.
+:::
+
## Return value
`stitchFramesToVideo()` returns a promise which resolves to nothing. If everything goes well, the output will be placed in `outputLocation`.
diff --git a/packages/renderer/src/ffmpeg-args-hook.ts b/packages/renderer/src/ffmpeg-args-hook.ts
index bfa98a82b2..9a04f82348 100644
--- a/packages/renderer/src/ffmpeg-args-hook.ts
+++ b/packages/renderer/src/ffmpeg-args-hook.ts
@@ -1 +1,4 @@
-export type FfmpegArgsHook = (args: string[]) => string[];
+export type FfmpegArgsHook = (info: {
+ type: 'pre-stitcher' | 'stitcher';
+ args: string[];
+}) => string[];
diff --git a/packages/renderer/src/prespawn-ffmpeg.ts b/packages/renderer/src/prespawn-ffmpeg.ts
index 4fe06342c5..e2a52822d7 100644
--- a/packages/renderer/src/prespawn-ffmpeg.ts
+++ b/packages/renderer/src/prespawn-ffmpeg.ts
@@ -6,6 +6,7 @@ import {
getDefaultCrfForCodec,
validateSelectedCrfAndCodecCombination,
} from './crf';
+import type {FfmpegArgsHook} from './ffmpeg-args-hook';
import type {FfmpegExecutable} from './ffmpeg-executable';
import {getCodecName} from './get-codec-name';
import {getProResProfileName} from './get-prores-profile-name';
@@ -34,6 +35,7 @@ type PreSticherOptions = {
verbose: boolean;
ffmpegExecutable: FfmpegExecutable | undefined;
imageFormat: ImageFormat;
+ ffmpegArgsHook?: FfmpegArgsHook;
signal: CancelSignal;
};
@@ -122,8 +124,11 @@ export const prespawnFfmpeg = async (options: PreSticherOptions) => {
}
const ffmpegString = ffmpegArgs.flat(2).filter(Boolean) as string[];
+ const finalFfmpegString = options.ffmpegArgsHook
+ ? options.ffmpegArgsHook({type: 'pre-stitcher', args: ffmpegString})
+ : ffmpegString;
- const task = execa(options.ffmpegExecutable ?? 'ffmpeg', ffmpegString);
+ const task = execa(options.ffmpegExecutable ?? 'ffmpeg', finalFfmpegString);
options.signal(() => {
task.kill();
diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.ts
index cb4c208b4d..6f0a274636 100644
--- a/packages/renderer/src/render-media.ts
+++ b/packages/renderer/src/render-media.ts
@@ -173,9 +173,7 @@ export const renderMedia = ({
validateScale(scale);
const concurrency = getConcurrency(options);
- if (ffmpegArgsHook) {
- validateFfmpegArgsHook(ffmpegArgsHook);
- }
+ validateFfmpegArgsHook(ffmpegArgsHook);
const everyNthFrame = options.everyNthFrame ?? 1;
const numberOfGifLoops = options.numberOfGifLoops ?? null;
@@ -294,6 +292,7 @@ export const renderMedia = ({
ffmpegExecutable,
imageFormat,
signal: cancelPrestitcher.cancelSignal,
+ ffmpegArgsHook,
});
stitcherFfmpeg = preStitcher.task;
}
diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.ts
index 470bf7a404..9b1e44b157 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -370,7 +370,7 @@ export const spawnFfmpeg = async (
const ffmpegString = ffmpegArgs.flat(2).filter(Boolean) as string[];
const finalFfmpegString = options.ffmpegArgsHook
- ? options.ffmpegArgsHook(ffmpegString)
+ ? options.ffmpegArgsHook({type: 'stitcher', args: ffmpegString})
: ffmpegString;
if (options.verbose && options.ffmpegArgsHook) {
diff --git a/packages/renderer/src/validate-ffmpeg-args-hook.ts b/packages/renderer/src/validate-ffmpeg-args-hook.ts
index a776c1da1c..e9185047f7 100644
--- a/packages/renderer/src/validate-ffmpeg-args-hook.ts
+++ b/packages/renderer/src/validate-ffmpeg-args-hook.ts
@@ -1,6 +1,10 @@
import type {FfmpegArgsHook} from './ffmpeg-args-hook';
export const validateFfmpegArgsHook = (ffmpegArgsHook?: FfmpegArgsHook) => {
+ if (typeof ffmpegArgsHook === 'undefined') {
+ return;
+ }
+
if (ffmpegArgsHook && typeof ffmpegArgsHook !== 'function') {
throw new TypeError(
`Argument passed for "ffmpegArgsHook" is not a function: ${ffmpegArgsHook}`