Skip to content

🎥 Make videos programmatically with React

scaffold streaming

Jonny Burger198a9926fb66
diff --git a/packages/cli/src/parse-command-line.ts b/packages/cli/src/parse-command-line.ts
index d1457ac558..edac858e9a 100644
--- a/packages/cli/src/parse-command-line.ts
+++ b/packages/cli/src/parse-command-line.ts
@@ -100,6 +100,7 @@ export const BooleanFlags = [
'disable-keyboard-shortcuts',
'default-only',
'no-open',
+ 'enable-streaming',
];
export const parsedCli = minimist<CommandLineOptions>(process.argv.slice(2), {
diff --git a/packages/it-tests/src/monorepo/go-package.test.ts b/packages/it-tests/src/monorepo/go-package.test.ts
index d7b54fd5c1..c4e98876aa 100644
--- a/packages/it-tests/src/monorepo/go-package.test.ts
+++ b/packages/it-tests/src/monorepo/go-package.test.ts
@@ -68,6 +68,7 @@ test("Go package should create the same payload as normal Lambda package", async
webhook: null,
x264Preset: null,
inputProps: {},
+ enableStreaming: false,
});
expect(removeUndefined(parsed)).toEqual(removeUndefined(nativeVersion));
diff --git a/packages/it-tests/src/monorepo/php-package.test.ts b/packages/it-tests/src/monorepo/php-package.test.ts
index 74d1a47877..9edc980d2c 100644
--- a/packages/it-tests/src/monorepo/php-package.test.ts
+++ b/packages/it-tests/src/monorepo/php-package.test.ts
@@ -98,6 +98,7 @@ test("PHP package should create the same renderMedia payload as normal Lambda pa
videoBitrate: null,
webhook: null,
x264Preset: null,
+ enableStreaming: false,
});
const jsonOutput = toParse.substring(0, toParse.lastIndexOf("}") + 1);
const parsedJson = JSON.parse(jsonOutput);
diff --git a/packages/it-tests/src/monorepo/python-package.test.ts b/packages/it-tests/src/monorepo/python-package.test.ts
index ed878ae9a2..cc278e65e9 100644
--- a/packages/it-tests/src/monorepo/python-package.test.ts
+++ b/packages/it-tests/src/monorepo/python-package.test.ts
@@ -81,6 +81,7 @@ test("Python package should create the same renderMedia payload as normal Lambda
videoBitrate: null,
webhook: null,
x264Preset: null,
+ enableStreaming: false,
});
const jsonOutput = toParse.substring(0, toParse.lastIndexOf("}") + 1);
const parsedJson = JSON.parse(jsonOutput);
diff --git a/packages/lambda/src/api/make-lambda-payload.ts b/packages/lambda/src/api/make-lambda-payload.ts
index 6d42eb300c..87fb2fe8a3 100644
--- a/packages/lambda/src/api/make-lambda-payload.ts
+++ b/packages/lambda/src/api/make-lambda-payload.ts
@@ -74,6 +74,7 @@ export type InnerRenderMediaOnLambdaInput = {
audioCodec: AudioCodec | null;
colorSpace: ColorSpace;
deleteAfter: DeleteAfter | null;
+ enableStreaming: boolean;
} & ToOptions<typeof BrowserSafeApis.optionsMap.renderMediaOnLambda>;
export const makeLambdaRenderMediaPayload = async ({
@@ -115,6 +116,7 @@ export const makeLambdaRenderMediaPayload = async ({
offthreadVideoCacheSizeInBytes,
deleteAfter,
colorSpace,
+ enableStreaming,
}: InnerRenderMediaOnLambdaInput): Promise<LambdaStartPayload> => {
const actualCodec = validateLambdaCodec(codec);
validateServeUrl(serveUrl);
@@ -167,18 +169,19 @@ export const makeLambdaRenderMediaPayload = async ({
downloadBehavior,
muted: muted ?? false,
version: VERSION,
- overwrite: overwrite ?? false,
- audioBitrate: audioBitrate ?? null,
- videoBitrate: videoBitrate ?? null,
- webhook: webhook ?? null,
- forceHeight: forceHeight ?? null,
- forceWidth: forceWidth ?? null,
- bucketName: bucketName ?? null,
- audioCodec: audioCodec ?? null,
+ overwrite,
+ audioBitrate,
+ videoBitrate,
+ webhook,
+ forceHeight,
+ forceWidth,
+ bucketName,
+ audioCodec,
type: LambdaRoutines.start,
- offthreadVideoCacheSizeInBytes: offthreadVideoCacheSizeInBytes ?? null,
- deleteAfter: deleteAfter ?? null,
- colorSpace: colorSpace ?? 'default',
+ offthreadVideoCacheSizeInBytes,
+ deleteAfter,
+ colorSpace,
+ enableStreaming,
};
};
diff --git a/packages/lambda/src/api/render-media-on-lambda.ts b/packages/lambda/src/api/render-media-on-lambda.ts
index 648a616acc..71094ef921 100644
--- a/packages/lambda/src/api/render-media-on-lambda.ts
+++ b/packages/lambda/src/api/render-media-on-lambda.ts
@@ -70,6 +70,7 @@ export type RenderMediaOnLambdaInput = {
dumpBrowserLogs?: boolean;
colorSpace?: ColorSpace;
deleteAfter?: DeleteAfter | null;
+ enableStreaming?: boolean;
} & Partial<ToOptions<typeof BrowserSafeApis.optionsMap.renderMediaOnLambda>>;
export type RenderMediaOnLambdaOutput = {
@@ -194,6 +195,7 @@ export const renderMediaOnLambda = (
webhook: options.webhook ?? null,
x264Preset: options.x264Preset ?? null,
deleteAfter: options.deleteAfter ?? null,
+ enableStreaming: options.enableStreaming ?? false,
});
};
diff --git a/packages/lambda/src/cli/args.ts b/packages/lambda/src/cli/args.ts
index 164ae73160..e1f8daccfe 100644
--- a/packages/lambda/src/cli/args.ts
+++ b/packages/lambda/src/cli/args.ts
@@ -34,6 +34,7 @@ type LambdaCommandLineOptions = {
['renderer-function-name']: string | undefined;
['function-name']: string | undefined;
['force-bucket-name']: string | undefined;
+ ['enable-streaming']: boolean;
[BrowserSafeApis.options.deleteAfterOption.cliFlag]: DeleteAfter | undefined;
[BrowserSafeApis.options.folderExpiryOption.cliFlag]: boolean | undefined;
};
diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.ts
index 57215554fa..68820b397f 100644
--- a/packages/lambda/src/cli/commands/render/render.ts
+++ b/packages/lambda/src/cli/commands/render/render.ts
@@ -199,6 +199,7 @@ export const renderCommand = async (args: string[], remotionRoot: string) => {
downloadBehavior: {type: 'play-in-browser'},
offthreadVideoCacheSizeInBytes: offthreadVideoCacheSizeInBytes ?? null,
x264Preset: x264Preset ?? null,
+ enableStreaming: parsedLambdaCli['enable-streaming'] ?? false,
});
const totalSteps = downloadName ? 6 : 5;
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 723808f8d0..7c9a48c208 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -274,6 +274,7 @@ const innerLaunchHandler = async (
offthreadVideoCacheSizeInBytes: params.offthreadVideoCacheSizeInBytes,
deleteAfter: params.deleteAfter,
colorSpace: params.colorSpace,
+ enableStreaming: params.enableStreaming,
};
return payload;
});
diff --git a/packages/lambda/src/functions/start.ts b/packages/lambda/src/functions/start.ts
index 32be912426..5121790918 100644
--- a/packages/lambda/src/functions/start.ts
+++ b/packages/lambda/src/functions/start.ts
@@ -103,6 +103,7 @@ export const startHandler = async (params: LambdaPayload, options: Options) => {
offthreadVideoCacheSizeInBytes: params.offthreadVideoCacheSizeInBytes,
deleteAfter: params.deleteAfter,
colorSpace: params.colorSpace,
+ enableStreaming: params.enableStreaming,
};
// Don't replace with callLambda(), we want to return before the render is snone
diff --git a/packages/lambda/src/functions/streaming/streaming.ts b/packages/lambda/src/functions/streaming/streaming.ts
new file mode 100644
index 0000000000..0af02fcc12
--- /dev/null
+++ b/packages/lambda/src/functions/streaming/streaming.ts
@@ -0,0 +1,137 @@
+type OnMessage = (
+ type: 'error' | 'success',
+ nonce: string,
+ data: Buffer,
+) => void;
+
+export const makeStreaming = (onMessage: OnMessage) => {
+ let outputBuffer = Buffer.from('');
+
+ const separator = Buffer.from('remotion_buffer:');
+ let unprocessedBuffers: Buffer[] = [];
+
+ let missingData: null | {
+ dataMissing: number;
+ } = null;
+
+ const processInput = () => {
+ let separatorIndex = outputBuffer.indexOf(separator);
+ if (separatorIndex === -1) {
+ return;
+ }
+
+ separatorIndex += separator.length;
+
+ let nonceString = '';
+ let lengthString = '';
+ let statusString = '';
+
+ // Each message from Rust is prefixed with `remotion_buffer;{[nonce]}:{[length]}`
+ // Let's read the buffer to extract the nonce, and if the full length is available,
+ // we'll extract the data and pass it to the callback.
+
+ // eslint-disable-next-line no-constant-condition
+ while (true) {
+ const nextDigit = outputBuffer[separatorIndex];
+ // 0x3a is the character ":"
+ if (nextDigit === 0x3a) {
+ separatorIndex++;
+ break;
+ }
+
+ separatorIndex++;
+
+ nonceString += String.fromCharCode(nextDigit);
+ }
+
+ // eslint-disable-next-line no-constant-condition
+ while (true) {
+ const nextDigit = outputBuffer[separatorIndex];
+ if (nextDigit === 0x3a) {
+ separatorIndex++;
+ break;
+ }
+
+ separatorIndex++;
+
+ lengthString += String.fromCharCode(nextDigit);
+ }
+
+ // eslint-disable-next-line no-constant-condition
+ while (true) {
+ const nextDigit = outputBuffer[separatorIndex];
+ if (nextDigit === 0x3a) {
+ break;
+ }
+
+ separatorIndex++;
+
+ statusString += String.fromCharCode(nextDigit);
+ }
+
+ const length = Number(lengthString);
+ const status = Number(statusString);
+
+ const dataLength = outputBuffer.length - separatorIndex - 1;
+ if (dataLength < length) {
+ missingData = {
+ dataMissing: length - dataLength,
+ };
+
+ return;
+ }
+
+ const data = outputBuffer.subarray(
+ separatorIndex + 1,
+ separatorIndex + 1 + Number(lengthString),
+ );
+ onMessage(status === 1 ? 'error' : 'success', nonceString, data);
+ missingData = null;
+
+ outputBuffer = outputBuffer.subarray(
+ separatorIndex + Number(lengthString) + 1,
+ );
+ processInput();
+ };
+
+ return {
+ addData: (data: Buffer) => {
+ unprocessedBuffers.push(data);
+ const separatorIndex = data.indexOf(separator);
+ if (separatorIndex === -1) {
+ if (missingData) {
+ missingData.dataMissing -= data.length;
+ }
+
+ if (!missingData || missingData.dataMissing > 0) {
+ return;
+ }
+ }
+
+ unprocessedBuffers.unshift(outputBuffer);
+
+ outputBuffer = Buffer.concat(unprocessedBuffers);
+ unprocessedBuffers = [];
+ processInput();
+ },
+ };
+};
+
+export const makePayloadMessage = (
+ nonce: number,
+ data: Buffer,
+ status: 0 | 1,
+): Buffer => {
+ const concat = Buffer.concat([
+ Buffer.from('remotion_buffer:'),
+ Buffer.from(nonce.toString()),
+ Buffer.from(':'),
+ Buffer.from(data.length.toString()),
+ Buffer.from(':'),
+ Buffer.from(String(status)),
+ Buffer.from(':'),
+ data,
+ ]);
+
+ return concat;
+};
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index 8d50b1588b..80763f27ce 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -275,6 +275,7 @@ export type LambdaStartPayload = {
offthreadVideoCacheSizeInBytes: number | null;
deleteAfter: DeleteAfter | null;
colorSpace: ColorSpace;
+ enableStreaming: boolean;
};
export type LambdaStatusPayload = {
@@ -330,6 +331,7 @@ export type LambdaPayloads = {
offthreadVideoCacheSizeInBytes: number | null;
deleteAfter: DeleteAfter | null;
colorSpace: ColorSpace;
+ enableStreaming: boolean;
};
status: LambdaStatusPayload;
renderer: {
@@ -372,6 +374,7 @@ export type LambdaPayloads = {
offthreadVideoCacheSizeInBytes: number | null;
deleteAfter: DeleteAfter | null;
colorSpace: ColorSpace;
+ enableStreaming: boolean;
};
still: {
type: LambdaRoutines.still;
diff --git a/packages/lambda/src/test/integration/renders/gif.test.ts b/packages/lambda/src/test/integration/renders/gif.test.ts
index 0035cf5c2d..b71d00ed4b 100644
--- a/packages/lambda/src/test/integration/renders/gif.test.ts
+++ b/packages/lambda/src/test/integration/renders/gif.test.ts
@@ -66,6 +66,7 @@ test('Should make a distributed GIF', async () => {
offthreadVideoCacheSizeInBytes: null,
deleteAfter: null,
colorSpace: 'default',
+ enableStreaming: false,
},
functionName: 'remotion-dev-lambda',
receivedStreamingPayload: () => undefined,
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 17a7e75eba..ece3cf4172 100644
--- a/packages/lambda/src/test/integration/renders/old-version.test.ts
+++ b/packages/lambda/src/test/integration/renders/old-version.test.ts
@@ -64,6 +64,7 @@ test('Should fail when using an incompatible version', async () => {
offthreadVideoCacheSizeInBytes: null,
deleteAfter: null,
colorSpace: 'default',
+ enableStreaming: false,
},
functionName: 'remotion-dev-render',
receivedStreamingPayload: () => undefined,
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 ec7a6ab361..85e9713dbb 100644
--- a/packages/lambda/src/test/integration/renders/other-bucket.test.ts
+++ b/packages/lambda/src/test/integration/renders/other-bucket.test.ts
@@ -69,6 +69,7 @@ test('Should be able to render to another bucket', async () => {
offthreadVideoCacheSizeInBytes: null,
deleteAfter: null,
colorSpace: 'default',
+ enableStreaming: false,
},
functionName: 'remotion-dev-render',
receivedStreamingPayload: () => undefined,
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 6ca68b18fb..dcad635a0a 100644
--- a/packages/lambda/src/test/integration/renders/silent-audio.test.ts
+++ b/packages/lambda/src/test/integration/renders/silent-audio.test.ts
@@ -65,6 +65,7 @@ test('Should add silent audio if there is no audio', async () => {
offthreadVideoCacheSizeInBytes: null,
deleteAfter: null,
colorSpace: 'default',
+ enableStreaming: false,
},
functionName: 'remotion-dev-render',
receivedStreamingPayload: () => undefined,
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 a0eb66481a..a986b4465b 100644
--- a/packages/lambda/src/test/integration/renders/transparent-webm.test.ts
+++ b/packages/lambda/src/test/integration/renders/transparent-webm.test.ts
@@ -69,6 +69,7 @@ test('Should make a transparent video', async () => {
offthreadVideoCacheSizeInBytes: null,
deleteAfter: null,
colorSpace: 'default',
+ enableStreaming: false,
},
functionName: 'remotion-dev-render',
receivedStreamingPayload: () => undefined,
diff --git a/packages/lambda/src/test/integration/webhooks.test.ts b/packages/lambda/src/test/integration/webhooks.test.ts
index 05aa006905..7cc9cd5acc 100644
--- a/packages/lambda/src/test/integration/webhooks.test.ts
+++ b/packages/lambda/src/test/integration/webhooks.test.ts
@@ -108,6 +108,7 @@ describe('Webhooks', () => {
offthreadVideoCacheSizeInBytes: null,
deleteAfter: null,
colorSpace: 'default',
+ enableStreaming: false,
},
functionName: 'remotion-dev-lambda',
receivedStreamingPayload: () => undefined,
@@ -206,6 +207,7 @@ describe('Webhooks', () => {
audioCodec: null,
deleteAfter: null,
colorSpace: 'default',
+ enableStreaming: false,
},
timeoutInTest: 1000,
retriesRemaining: 0,