🎥 Make videos programmatically with React
`@remotion/renderer`: Remove free-memory check for parallel encoding
Jonny Burgera1af11864ae1
diff --git a/packages/docs/docs/cli/render.mdx b/packages/docs/docs/cli/render.mdxindex 0d11c23a1d..264a7eaadb 100644--- a/packages/docs/docs/cli/render.mdx+++ b/packages/docs/docs/cli/render.mdx@@ -172,6 +172,8 @@ For example only every second frame, every third frame and so on. Only works forDisallows the renderer from doing rendering frames and encoding at the same time. This makes the rendering process more memory-efficient, but possibly slower.+From <AvailableFrom v="4.0.533" inline />, parallel encoding is no longer disabled automatically based on the available memory. Set this flag if the machine runs out of memory while rendering.+### `--number-of-gif-loops`<AvailableFrom v="3.1.0" /><Options id="number-of-gif-loops" />diff --git a/packages/docs/docs/config.mdx b/packages/docs/docs/config.mdxindex 2a58d3635e..0e5858af4e 100644--- a/packages/docs/docs/config.mdx+++ b/packages/docs/docs/config.mdx@@ -558,6 +558,8 @@ The [command line flag](/docs/cli/render#--muted) `--muted` will take precedenceDisallows the renderer from doing rendering frames and encoding at the same time. This makes the rendering process more memory-efficient, but possibly slower. Default `false`.+From <AvailableFrom v="4.0.533" inline />, parallel encoding is no longer disabled automatically based on the available memory. Set this option if the machine runs out of memory while rendering.+```ts twoslash title="remotion.config.ts"import {Config} from '@remotion/cli/config';// ---cut---@@ -1190,7 +1192,7 @@ Config.overrideFfmpegCommand(({args}) => {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 a two-pass process for the video generation. `pre-stitcher` is the encoding phase and `stitcher` is the muxing phase. If the override function is only called once with `stitcher`, then encoding and muxing is done in the same step. You can tell whether parallel encoding is enabled by adding `--log=verbose` to your render command.+- `type`: Either `"stitcher"` or `"pre-stitcher"`. For the `h264`, `h264-mkv` and `h265` codecs, Remotion uses a two-pass process for the video generation unless [parallel encoding is disallowed](#setdisallowparallelencoding). `pre-stitcher` is the encoding phase and `stitcher` is the muxing phase. If the override function is only called once with `stitcher`, then encoding and muxing is done in the same step. 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.@@ -1199,7 +1201,7 @@ Your function must return a modified array of strings.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.+- Depending on the selected codec and [`setDisallowParallelEncoding()`](#setdisallowparallelencoding), 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.- The FFmpeg binary provided by Remotion supports only a small subset of FFmpeg commands, therefore not every passed option will be applied.- This feature is not available when using Remotion Lambda.diff --git a/packages/docs/docs/renderer/render-media.mdx b/packages/docs/docs/renderer/render-media.mdxindex 830fe021c6..a3c7326c22 100644--- a/packages/docs/docs/renderer/render-media.mdx+++ b/packages/docs/docs/renderer/render-media.mdx@@ -443,7 +443,7 @@ const ffmpegOverride: FfmpegOverrideFn = ({type, args}) => {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 a two-pass process for the video generation. `pre-stitcher` is the encoding phase and `stitcher` is the muxing phase. If the override function is only called once with `stitcher`, then encoding and muxing is done in the same step. You can tell whether parallel encoding is enabled by adding `--log=verbose` to your render command.+- `type`: Either `"stitcher"` or `"pre-stitcher"`. For the `h264`, `h264-mkv` and `h265` codecs, Remotion uses a two-pass process for the video generation unless [parallel encoding is disallowed](#disallowparallelencoding). `pre-stitcher` is the encoding phase and `stitcher` is the muxing phase. If the override function is only called once with `stitcher`, then encoding and muxing is done in the same step. 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.@@ -452,7 +452,7 @@ Your function must return a modified array of strings.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.+- Depending on the selected codec and [`disallowParallelEncoding`](#disallowparallelencoding), 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.@@ -462,6 +462,8 @@ Before you use this hack, reach out to the Remotion team on [Discord](https://reDisallows the renderer from doing rendering frames and encoding at the same time. This makes the rendering process more memory-efficient, but possibly slower.+From <AvailableFrom v="4.0.533" inline />, parallel encoding is no longer disabled automatically based on the available memory. Set this option if the machine runs out of memory while rendering.+### `mediaCacheSizeInBytes?`<AvailableFrom v="4.0.352"/><Options id="media-cache-size-in-bytes" />diff --git a/packages/docs/docs/renderer/stitch-frames-to-video.mdx b/packages/docs/docs/renderer/stitch-frames-to-video.mdxindex ab245d28ce..ac1e996fc1 100644--- a/packages/docs/docs/renderer/stitch-frames-to-video.mdx+++ b/packages/docs/docs/renderer/stitch-frames-to-video.mdx@@ -178,7 +178,7 @@ const ffmpegOverride: FfmpegOverrideFn = ({type, args}) => {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 a two-pass process for the video generation. `pre-stitcher` is the encoding phase and `stitcher` is the muxing phase. If the override function is only called once with `stitcher`, then encoding and muxing is done in the same step. You can tell whether parallel encoding is enabled by adding `--log=verbose` to your render command.+- `type`: Either `"stitcher"` or `"pre-stitcher"`. When rendering with [`renderMedia()`](/docs/renderer/render-media), Remotion uses a two-pass process for the `h264`, `h264-mkv` and `h265` codecs unless [parallel encoding is disallowed](/docs/renderer/render-media#disallowparallelencoding). `pre-stitcher` is the encoding phase and `stitcher` is the muxing phase. If the override function is only called once with `stitcher`, then encoding and muxing is done in the same step. 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.@@ -187,7 +187,7 @@ Your function must return a modified array of strings.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.+- Depending on the selected codec and [`disallowParallelEncoding`](/docs/renderer/render-media#disallowparallelencoding), 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.diff --git a/packages/docs/docs/troubleshooting/sigkill.mdx b/packages/docs/docs/troubleshooting/sigkill.mdxindex bf20bdda64..9746fc137e 100644--- a/packages/docs/docs/troubleshooting/sigkill.mdx+++ b/packages/docs/docs/troubleshooting/sigkill.mdx@@ -41,6 +41,10 @@ The last version with improvements is [`v4.0.171`](https://remotion.dev/changeloSet a lower [`concurrency`](/docs/terminology/concurrency) to open less browser tabs at once and thus less memory.+## Disallow parallel encoding++H.264 and H.265 videos are encoded while frames are still being rendered, so the browser and FFmpeg need memory at the same time. Use [`--disallow-parallel-encoding`](/docs/cli/render#--disallow-parallel-encoding) or [`disallowParallelEncoding`](/docs/renderer/render-media#disallowparallelencoding) to only start encoding once all frames are rendered. This lowers the peak memory usage, but makes rendering slower.+## Allocate more memory to your systemAs a last resort, you can allocate more memory to your system to alleviate the problem.diff --git a/packages/renderer/src/prestitcher-memory-usage.ts b/packages/renderer/src/prestitcher-memory-usage.tsdeleted file mode 100644index d20d79e8ad..0000000000--- a/packages/renderer/src/prestitcher-memory-usage.ts+++ /dev/null@@ -1,56 +0,0 @@-import type {LogLevel} from './log-level';-import {getAvailableMemory} from './memory/get-available-memory';--const MINIMUM_MEMORY_LEFT = 2_000_000_000;--export const hasEnoughMemoryForParallelEncoding = ({- freeMemory,- estimatedUsage,-}: {- freeMemory: number;- estimatedUsage: number;-}) => {- return freeMemory - estimatedUsage > MINIMUM_MEMORY_LEFT;-};--const estimateMemoryUsageForPrestitcher = ({- width,- height,-}: {- width: number;- height: number;-}) => {- // Empirically we detected that per 1 million pixels, FFMPEG uses around 1GB of memory, relatively independent of- // the duration of the video.- const memoryUsageFor4K = 1_000_000_000;- const memoryUsageOfPixel = memoryUsageFor4K / 1_000_000;-- return memoryUsageOfPixel * width * height;-};--export const shouldUseParallelEncoding = ({- width,- height,- logLevel,-}: {- width: number;- height: number;- logLevel: LogLevel;-}) => {- const freeMemory = getAvailableMemory(logLevel);- const estimatedUsage = estimateMemoryUsageForPrestitcher({- height,- width,- });-- const hasEnoughMemory = hasEnoughMemoryForParallelEncoding({- estimatedUsage,- freeMemory,- });-- return {- hasEnoughMemory,- freeMemory,- estimatedUsage,- };-};diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.tsindex 1aec11e181..4205243d63 100644--- a/packages/renderer/src/render-media.ts+++ b/packages/renderer/src/render-media.ts@@ -58,7 +58,6 @@ import {import type {RemotionServer} from './prepare-server';import {makeOrReuseServer} from './prepare-server';import {prespawnFfmpeg} from './prespawn-ffmpeg';-import {shouldUseParallelEncoding} from './prestitcher-memory-usage';import {validateSelectedCodecAndProResCombination} from './prores-profile';import type {OnArtifact} from './render-frames';import {internalRenderFrames} from './render-frames';@@ -370,28 +369,9 @@ const internalRenderMediaRaw = ({const renderStart = Date.now();- const {estimatedUsage, freeMemory, hasEnoughMemory} =- shouldUseParallelEncoding({- height: compositionWithPossibleUnevenDimensions.height,- width: compositionWithPossibleUnevenDimensions.width,- logLevel,- });const parallelEncoding =- !disallowParallelEncoding &&- hasEnoughMemory &&- canUseParallelEncoding(codec);+ !disallowParallelEncoding && canUseParallelEncoding(codec);- Log.verbose(- {- indent,- logLevel,- tag: 'renderMedia()',- },- 'Free memory:',- freeMemory,- 'Estimated usage parallel encoding',- estimatedUsage,- );const resolvedConcurrency = resolveConcurrency(concurrency);Log.verbose({diff --git a/packages/renderer/src/test/prestitcher-memory-usage.test.ts b/packages/renderer/src/test/prestitcher-memory-usage.test.tsdeleted file mode 100644index 3df7e94b3f..0000000000--- a/packages/renderer/src/test/prestitcher-memory-usage.test.ts+++ /dev/null@@ -1,19 +0,0 @@-import {expect, test} from 'bun:test';-import {hasEnoughMemoryForParallelEncoding} from '../prestitcher-memory-usage';--test('parallel encoding keeps a fixed 2GB memory reserve', () => {- const estimatedUsage = 8_294_400_000;-- expect(- hasEnoughMemoryForParallelEncoding({- estimatedUsage,- freeMemory: 11_000_000_000,- }),- ).toBe(true);- expect(- hasEnoughMemoryForParallelEncoding({- estimatedUsage,- freeMemory: 10_000_000_000,- }),- ).toBe(false);-});