Skip to content

🎥 Make videos programmatically with React

`concurrencyPerLambda` option

Jonny Burger352fc87832c9
diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.ts
index 0a85389172..2881bf7623 100644
--- a/packages/cli/src/get-cli-options.ts
+++ b/packages/cli/src/get-cli-options.ts
@@ -231,6 +231,10 @@ export const getCliOptions = async (options: {
Internals.DEFAULT_OPENGL_RENDERER,
};
+ const parallelism = Internals.getConcurrency();
+
+ RenderInternals.validateConcurrency(parallelism, 'concurrency');
+
return {
puppeteerTimeout: Internals.getCurrentPuppeteerTimeout(),
parallelism: Internals.getConcurrency(),
diff --git a/packages/core/src/config/concurrency.ts b/packages/core/src/config/concurrency.ts
index 4006140b82..ee3dc08fd3 100644
--- a/packages/core/src/config/concurrency.ts
+++ b/packages/core/src/config/concurrency.ts
@@ -3,10 +3,6 @@ export type Concurrency = number | null;
let currentConcurrency: null | number = null;
export const setConcurrency = (newConcurrency: Concurrency) => {
- if (typeof newConcurrency !== 'number') {
- throw new Error('--concurrency flag must be a number.');
- }
-
currentConcurrency = newConcurrency;
};
diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts
index e5693b71b9..ae71d05540 100644
--- a/packages/core/src/config/index.ts
+++ b/packages/core/src/config/index.ts
@@ -1,6 +1,6 @@
import type {Browser} from './browser';
import type {BrowserExecutable} from './browser-executable';
-import { setBrowserExecutable} from './browser-executable';
+import {setBrowserExecutable} from './browser-executable';
import {
setChromiumDisableWebSecurity,
setChromiumHeadlessMode,
@@ -8,34 +8,26 @@ import {
setChromiumOpenGlRenderer,
} from './chromium-flags';
import type {Codec} from './codec';
-import { setCodec, setOutputFormat} from './codec';
+import {setCodec, setOutputFormat} from './codec';
import type {Concurrency} from './concurrency';
-import { setConcurrency} from './concurrency';
+import {setConcurrency} from './concurrency';
import {setCrf} from './crf';
import {setDotEnvLocation} from './env-file';
-import type {
- FfmpegExecutable} from './ffmpeg-executable';
-import {
- setFfmpegExecutable,
- setFfprobeExecutable,
-} from './ffmpeg-executable';
+import type {FfmpegExecutable} from './ffmpeg-executable';
+import {setFfmpegExecutable, setFfprobeExecutable} from './ffmpeg-executable';
import type {FrameRange} from './frame-range';
-import { setFrameRange} from './frame-range';
+import {setFrameRange} from './frame-range';
import type {ImageFormat, StillImageFormat} from './image-format';
-import { setImageFormat} from './image-format';
+import {setImageFormat} from './image-format';
import {setImageSequence} from './image-sequence';
import type {LogLevel} from './log';
-import { setLogLevel} from './log';
+import {setLogLevel} from './log';
import {setMaxTimelineTracks} from './max-timeline-tracks';
-import type {
- WebpackConfiguration,
- WebpackOverrideFn} from './override-webpack';
-import {
- overrideWebpackConfig
-} from './override-webpack';
+import type {WebpackConfiguration, WebpackOverrideFn} from './override-webpack';
+import {overrideWebpackConfig} from './override-webpack';
import {setOverwriteOutput} from './overwrite';
import type {PixelFormat} from './pixel-format';
-import { setPixelFormat} from './pixel-format';
+import {setPixelFormat} from './pixel-format';
import {setPort} from './preview-server';
import {setProResProfile} from './prores-profile';
import {setQuality} from './quality';
diff --git a/packages/core/src/test/concurrency.test.ts b/packages/core/src/test/concurrency.test.ts
deleted file mode 100644
index 35a9336945..0000000000
--- a/packages/core/src/test/concurrency.test.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import type {
- Concurrency} from '../config/concurrency';
-import {
- getConcurrency,
- setConcurrency,
-} from '../config/concurrency';
-import {expectToThrow} from './expect-to-throw';
-
-const invalidConcurrency: String = 'invalidConcurrency';
-let defaultConcurrency: Concurrency;
-
-beforeAll(() => {
- defaultConcurrency = getConcurrency();
-});
-
-test('setConcurrency should throw if concurrency is not a number', () => {
- expectToThrow(
- () =>
- // @ts-expect-error
- setConcurrency(invalidConcurrency),
- /--concurrency flag must be a number./
- );
-});
-test('setConcurrency should NOT throw if concurrency is a number', () => {
- expect(() => setConcurrency(50)).not.toThrow();
-});
-test('getConcurrency should return null by default', () => {
- expect(defaultConcurrency === null);
-});
-test('getConcurrency should return number', () => {
- setConcurrency(100);
- expect(getConcurrency()).toEqual(100);
-});
diff --git a/packages/docs/docs/lambda/cli/render.md b/packages/docs/docs/lambda/cli/render.md
index 43a17bebb8..402ede7489 100644
--- a/packages/docs/docs/lambda/cli/render.md
+++ b/packages/docs/docs/lambda/cli/render.md
@@ -96,6 +96,12 @@ Minimum value: <MinimumFramesPerLambda />
The `framesPerLambda` parameter cannot result in more than 200 functions being spawned. See: [Concurrency](/docs/lambda/concurrency)
:::
+### `--concurrency-per-lambda`
+
+_Available from v3.0.30_
+
+By default, each Lambda function renders with concurrency 1 (one open browser tab). You may use the option to customize this value.
+
### `--quality`
[Value between 0 and 100 for JPEG rendering quality](/docs/config#setquality). Doesn't work when PNG frames are rendered.
diff --git a/packages/docs/docs/lambda/rendermediaonlambda.md b/packages/docs/docs/lambda/rendermediaonlambda.md
index 530f79dfb8..1f4bc47923 100644
--- a/packages/docs/docs/lambda/rendermediaonlambda.md
+++ b/packages/docs/docs/lambda/rendermediaonlambda.md
@@ -139,6 +139,12 @@ _optional, available from v2.6.5_
Allows you to set certain Chromium / Google Chrome flags. See: [Chromium flags](/docs/chromium-flags).
+### `concurrencyPerLambda`
+
+_optional, available from v3.0.30_
+
+By default, each Lambda function renders with concurrency 1 (one open browser tab). You may use the option to customize this value.
+
#### `disableWebSecurity`
_boolean - default `false`_
diff --git a/packages/lambda/src/api/render-media-on-lambda.ts b/packages/lambda/src/api/render-media-on-lambda.ts
index 7efef9b2d4..f82702d490 100644
--- a/packages/lambda/src/api/render-media-on-lambda.ts
+++ b/packages/lambda/src/api/render-media-on-lambda.ts
@@ -1,4 +1,5 @@
import type {ChromiumOptions} from '@remotion/renderer';
+import {RenderInternals} from '@remotion/renderer';
import type {
FrameRange,
ImageFormat,
@@ -39,6 +40,7 @@ export type RenderMediaOnLambdaInput = {
timeoutInMilliseconds?: number;
chromiumOptions?: ChromiumOptions;
scale?: number;
+ concurrencyPerLambda?: number;
};
export type RenderMediaOnLambdaOutput = {
@@ -87,10 +89,15 @@ export const renderMediaOnLambda = async ({
timeoutInMilliseconds,
chromiumOptions,
scale,
+ concurrencyPerLambda,
}: RenderMediaOnLambdaInput): Promise<RenderMediaOnLambdaOutput> => {
const actualCodec = validateLambdaCodec(codec);
validateServeUrl(serveUrl);
validateFramesPerLambda(framesPerLambda ?? null);
+ RenderInternals.validateConcurrency(
+ concurrencyPerLambda,
+ 'concurrencyPerLambda'
+ );
const realServeUrl = await convertToServeUrl(serveUrl, region);
const res = await callLambda({
functionName,
@@ -116,6 +123,7 @@ export const renderMediaOnLambda = async ({
timeoutInMilliseconds ?? Internals.DEFAULT_PUPPETEER_TIMEOUT,
chromiumOptions: chromiumOptions ?? {},
scale: scale ?? 1,
+ concurrencyPerLambda: concurrencyPerLambda ?? 1,
},
region,
});
diff --git a/packages/lambda/src/cli/args.ts b/packages/lambda/src/cli/args.ts
index d7a517f805..901b416182 100644
--- a/packages/lambda/src/cli/args.ts
+++ b/packages/lambda/src/cli/args.ts
@@ -22,6 +22,7 @@ type LambdaCommandLineOptions = {
['disable-cloudwatch']: boolean;
['max-retries']: number;
['frames-per-lambda']: number;
+ ['concurrency-per-lambda']: number;
['out-name']: string | undefined;
['architecture']: LambdaArchitecture;
['custom-role-arn']: string | undefined;
diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.ts
index 572cd425c6..847c3f2da5 100644
--- a/packages/lambda/src/cli/commands/render/render.ts
+++ b/packages/lambda/src/cli/commands/render/render.ts
@@ -104,6 +104,7 @@ export const renderCommand = async (args: string[]) => {
timeoutInMilliseconds: puppeteerTimeout,
chromiumOptions,
scale,
+ concurrencyPerLambda: parsedLambdaCli['concurrency-per-lambda'],
});
const totalSteps = outName ? 5 : 4;
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index db8fc0c56e..afed88a015 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -167,6 +167,7 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {
timeoutInMilliseconds: params.timeoutInMilliseconds,
chromiumOptions: params.chromiumOptions,
scale: params.scale,
+ concurrencyPerLambda: params.concurrencyPerLambda,
};
return payload;
});
diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.ts
index 38885b9023..b428a170b6 100644
--- a/packages/lambda/src/functions/renderer.ts
+++ b/packages/lambda/src/functions/renderer.ts
@@ -1,13 +1,11 @@
import {InvokeCommand} from '@aws-sdk/client-lambda';
import type {BrowserLog} from '@remotion/renderer';
-import { RenderInternals, renderMedia} from '@remotion/renderer';
+import {RenderInternals, renderMedia} from '@remotion/renderer';
import fs from 'fs';
import path from 'path';
import {Internals} from 'remotion';
import {getLambdaClient} from '../shared/aws-clients';
-import type {
- LambdaPayload,
- LambdaPayloads} from '../shared/constants';
+import type {LambdaPayload, LambdaPayloads} from '../shared/constants';
import {
chunkKeyForIndex,
lambdaInitializedKey,
@@ -111,7 +109,7 @@ const renderHandler = async (
chunkTimingData.timings[renderedFrames] = Date.now() - start;
},
- parallelism: 1,
+ parallelism: params.concurrencyPerLambda,
onStart: () => {
lambdaWriteFile({
privacy: 'private',
diff --git a/packages/lambda/src/functions/start.ts b/packages/lambda/src/functions/start.ts
index 851e38b363..3adffac512 100644
--- a/packages/lambda/src/functions/start.ts
+++ b/packages/lambda/src/functions/start.ts
@@ -3,7 +3,7 @@ import {Internals} from 'remotion';
import {getOrCreateBucket} from '../api/get-or-create-bucket';
import {getLambdaClient} from '../shared/aws-clients';
import type {LambdaPayload} from '../shared/constants';
-import { LambdaRoutines} from '../shared/constants';
+import {LambdaRoutines} from '../shared/constants';
import {randomHash} from '../shared/random-hash';
import {getCurrentRegionInFunction} from './helpers/get-current-region';
@@ -40,6 +40,7 @@ export const startHandler = async (params: LambdaPayload) => {
timeoutInMilliseconds: params.timeoutInMilliseconds,
chromiumOptions: params.chromiumOptions,
scale: params.scale,
+ concurrencyPerLambda: params.concurrencyPerLambda,
};
await getLambdaClient(getCurrentRegionInFunction()).send(
new InvokeCommand({
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index 284fab1408..f9e51e0368 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -195,6 +195,7 @@ export type LambdaPayloads = {
timeoutInMilliseconds: number;
chromiumOptions: ChromiumOptions;
scale: number;
+ concurrencyPerLambda: number;
};
launch: {
type: LambdaRoutines.launch;
@@ -219,6 +220,7 @@ export type LambdaPayloads = {
timeoutInMilliseconds: number;
chromiumOptions: ChromiumOptions;
scale: number;
+ concurrencyPerLambda: number;
};
status: {
type: LambdaRoutines.status;
@@ -226,6 +228,7 @@ export type LambdaPayloads = {
renderId: string;
};
renderer: {
+ concurrencyPerLambda: number;
type: LambdaRoutines.renderer;
serveUrl: string;
frameRange: [number, number];
diff --git a/packages/lambda/src/test/integration/renders/old-version.test.ts b/packages/lambda/src/test/integration/renders/old-version.test.ts
index f85e39edd9..03a9558fa9 100644
--- a/packages/lambda/src/test/integration/renders/old-version.test.ts
+++ b/packages/lambda/src/test/integration/renders/old-version.test.ts
@@ -48,6 +48,7 @@ test('Should be able to render to another bucket', async () => {
quality: undefined,
scale: 1,
timeoutInMilliseconds: 12000,
+ concurrencyPerLambda: 1,
},
extraContext
);
diff --git a/packages/lambda/src/test/integration/renders/other-bucket.test.ts b/packages/lambda/src/test/integration/renders/other-bucket.test.ts
index eb39f9c5f2..816a593258 100644
--- a/packages/lambda/src/test/integration/renders/other-bucket.test.ts
+++ b/packages/lambda/src/test/integration/renders/other-bucket.test.ts
@@ -53,6 +53,7 @@ test('Should be able to render to another bucket', async () => {
quality: undefined,
scale: 1,
timeoutInMilliseconds: 12000,
+ concurrencyPerLambda: 1,
},
extraContext
);
diff --git a/packages/lambda/src/test/integration/renders/silent-audio.test.ts b/packages/lambda/src/test/integration/renders/silent-audio.test.ts
index 73e0388aea..b9fcc2903c 100644
--- a/packages/lambda/src/test/integration/renders/silent-audio.test.ts
+++ b/packages/lambda/src/test/integration/renders/silent-audio.test.ts
@@ -50,6 +50,7 @@ test('Should add silent audio if there is no audio', async () => {
quality: undefined,
scale: 1,
timeoutInMilliseconds: 12000,
+ concurrencyPerLambda: 1,
},
extraContext
);
diff --git a/packages/lambda/src/test/integration/renders/transparent-webm.test.ts b/packages/lambda/src/test/integration/renders/transparent-webm.test.ts
index 908144ba9b..b01b33f617 100644
--- a/packages/lambda/src/test/integration/renders/transparent-webm.test.ts
+++ b/packages/lambda/src/test/integration/renders/transparent-webm.test.ts
@@ -52,6 +52,7 @@ test('Should make a transparent video', async () => {
quality: undefined,
scale: 1,
timeoutInMilliseconds: 12000,
+ concurrencyPerLambda: 1,
},
extraContext
);
diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts
index 67d7924960..f62866af19 100644
--- a/packages/renderer/src/index.ts
+++ b/packages/renderer/src/index.ts
@@ -25,6 +25,7 @@ import {isPathInside} from './serve-handler/is-path-inside';
import {serveStatic} from './serve-static';
import {spawnFfmpeg} from './stitch-frames-to-video';
import {tmpDir} from './tmp-dir';
+import {validateConcurrency} from './validate-concurrency';
import {validateEvenDimensionsWithCodec} from './validate-even-dimensions-with-codec';
import {binaryExists, validateFfmpeg} from './validate-ffmpeg';
import {validatePuppeteerTimeout} from './validate-puppeteer-timeout';
@@ -88,4 +89,5 @@ export const RenderInternals = {
unlockErrorSymbolicationLock,
mimeContentType,
mimeLookup,
+ validateConcurrency,
};
diff --git a/packages/renderer/src/test/validate-concurrency.test.ts b/packages/renderer/src/test/validate-concurrency.test.ts
new file mode 100644
index 0000000000..c046a389c5
--- /dev/null
+++ b/packages/renderer/src/test/validate-concurrency.test.ts
@@ -0,0 +1,21 @@
+import {validateConcurrency} from '../validate-concurrency';
+import {expectToThrow} from './expect-to-throw';
+
+const invalidConcurrency: String = 'invalidConcurrency';
+
+test('setConcurrency should throw if concurrency is not a number', () => {
+ expectToThrow(
+ () => validateConcurrency(invalidConcurrency, 'concurrencyPerLambda'),
+ /concurrencyPerLambda must be a number but is/
+ );
+});
+
+test('setConcurrency should NOT throw if concurrency is a number', () => {
+ expect(() => validateConcurrency(2, 'concurrencyPerLambda')).not.toThrow();
+});
+
+test('setConcurrency should throw if concurrency is too high', () => {
+ expect(() => validateConcurrency(50, 'concurrencyPerLambda')).toThrow(
+ /concurrencyPerLambda is set higher than the amount of CPU cores available/
+ );
+});
diff --git a/packages/renderer/src/validate-concurrency.ts b/packages/renderer/src/validate-concurrency.ts
new file mode 100644
index 0000000000..2f3bdff6fe
--- /dev/null
+++ b/packages/renderer/src/validate-concurrency.ts
@@ -0,0 +1,25 @@
+export const validateConcurrency = (value: unknown, setting: string) => {
+ if (typeof value === 'undefined') {
+ return;
+ }
+
+ if (typeof value !== 'number') {
+ throw new Error(setting + ' must be a number but is ' + value);
+ }
+
+ if (value % 1 !== 0) {
+ throw new Error(setting + ' must be an integer, but is ' + value);
+ }
+
+ if (value < 1) {
+ throw new Error(setting + ' must be at least 1, but is ' + value);
+ }
+
+ if (value > require('os').cpus().length) {
+ throw new Error(
+ `${setting} is set higher than the amount of CPU cores available. Available CPU cores: ${
+ require('os').cpus().length
+ }, value set: ${value}`
+ );
+ }
+};