Skip to content

🎥 Make videos programmatically with React

refactor

Jonny Burger62b546d74df9
diff --git a/packages/lambda/template/aws-clients.ts b/packages/lambda/template/aws-clients.ts
new file mode 100644
index 0000000000..95c742bafc
--- /dev/null
+++ b/packages/lambda/template/aws-clients.ts
@@ -0,0 +1,6 @@
+import {LambdaClient} from '@aws-sdk/client-lambda';
+import {S3Client} from '@aws-sdk/client-s3';
+import {REGION} from '../src/constants';
+
+export const s3Client = new S3Client({region: REGION});
+export const lambdaClient = new LambdaClient({region: REGION});
diff --git a/packages/lambda/template/get-browser-instance.ts b/packages/lambda/template/get-browser-instance.ts
new file mode 100644
index 0000000000..6926922138
--- /dev/null
+++ b/packages/lambda/template/get-browser-instance.ts
@@ -0,0 +1,20 @@
+import {RenderInternals} from '@remotion/renderer';
+import {executablePath} from './get-chromium-executable-path';
+
+type Await<T> = T extends PromiseLike<infer U> ? U : T;
+
+let _browserInstance: Await<
+ ReturnType<typeof RenderInternals.openBrowser>
+> | null;
+
+// TODO Potential race condition
+export const getBrowserInstance = async () => {
+ if (_browserInstance) {
+ return _browserInstance;
+ }
+ const execPath = await executablePath();
+ _browserInstance = await RenderInternals.openBrowser('chrome', {
+ customExecutable: execPath,
+ });
+ return _browserInstance;
+};
diff --git a/packages/lambda/template/init.ts b/packages/lambda/template/init.ts
new file mode 100644
index 0000000000..65c69cfae1
--- /dev/null
+++ b/packages/lambda/template/init.ts
@@ -0,0 +1,121 @@
+import {InvokeCommand} from '@aws-sdk/client-lambda';
+import {CreateBucketCommand, PutObjectCommand} from '@aws-sdk/client-s3';
+import fs from 'fs';
+import {concatVideos, concatVideosS3} from '../src/concat-videos';
+import {
+ EFS_MOUNT_PATH,
+ ENABLE_EFS,
+ LambdaPayload,
+ REGION,
+ RENDERS_BUCKET_PREFIX,
+} from '../src/constants';
+import {timer} from '../src/timer';
+import {lambdaClient, s3Client} from './aws-clients';
+import {getBrowserInstance} from './get-browser-instance';
+import {validateComposition} from './validate-composition';
+
+export const initHandler = async (params: LambdaPayload) => {
+ if (params.type !== 'init') {
+ return;
+ }
+ const efsRemotionVideoRenderDone = EFS_MOUNT_PATH + '/render-done';
+
+ const bucketName = RENDERS_BUCKET_PREFIX + Math.random();
+ const efsRemotionVideoPath = EFS_MOUNT_PATH + '/remotion-video';
+ if (ENABLE_EFS) {
+ if (fs.existsSync(efsRemotionVideoPath)) {
+ fs.rmdirSync(efsRemotionVideoPath, {recursive: true});
+ }
+ fs.mkdirSync(efsRemotionVideoPath);
+ if (fs.existsSync(efsRemotionVideoRenderDone)) {
+ fs.rmdirSync(efsRemotionVideoRenderDone, {recursive: true});
+ }
+ fs.mkdirSync(efsRemotionVideoRenderDone);
+ }
+ // const bucketTimer = timer('Creating bucket');
+
+ // TODO: Better validation
+ if (!params.chunkSize) {
+ throw new Error('Pass chunkSize');
+ }
+ // TODO: Better validation
+ if (!params.durationInFrames) {
+ throw new Error('Pass durationInFrames');
+ }
+ const browserInstance = await getBrowserInstance();
+
+ const comp = await validateComposition({
+ serveUrl: params.serveUrl,
+ composition: params.composition,
+ browserInstance,
+ });
+ console.log(comp);
+ const bucketTimer = timer('creating bucket');
+ await s3Client.send(
+ new CreateBucketCommand({
+ Bucket: bucketName,
+ ACL: 'public-read',
+ })
+ );
+ bucketTimer.end();
+ const {chunkSize} = params;
+ const chunkCount = Math.ceil(params.durationInFrames / chunkSize);
+
+ const chunks = new Array(chunkCount).fill(1).map((_, i) => {
+ return [
+ i * chunkSize,
+ Math.min(params.durationInFrames, (i + 1) * chunkSize) - 1,
+ ] as [number, number];
+ });
+ const reqSend = timer('sending off requests');
+ await Promise.all(
+ chunks.map(async (chunk) => {
+ const payload: LambdaPayload = {
+ type: 'renderer',
+ frameRange: chunk,
+ serveUrl: params.serveUrl,
+ chunk: chunks.indexOf(chunk),
+ efsRemotionVideoPath,
+ composition: params.composition,
+ fps: comp.fps,
+ height: comp.height,
+ width: comp.width,
+ durationInFrames: params.durationInFrames,
+ bucketName,
+ };
+ const callingLambdaTimer = timer(
+ 'Calling lambda ' + chunks.indexOf(chunk)
+ );
+ await lambdaClient.send(
+ new InvokeCommand({
+ FunctionName: process.env.AWS_LAMBDA_FUNCTION_NAME,
+ // @ts-expect-error
+ Payload: JSON.stringify(payload),
+ InvocationType: 'Event',
+ }),
+ {}
+ );
+ callingLambdaTimer.end();
+ })
+ );
+ reqSend.end();
+ const out = ENABLE_EFS
+ ? await concatVideos(
+ efsRemotionVideoPath,
+ efsRemotionVideoRenderDone,
+ chunkCount
+ )
+ : await concatVideosS3(s3Client, bucketName);
+ const outName = 'out.mp4';
+ await s3Client.send(
+ new PutObjectCommand({
+ Bucket: bucketName,
+ Key: 'out.mp4',
+ Body: fs.createReadStream(out),
+ ACL: 'public-read',
+ })
+ );
+ console.log(
+ 'Done! ' + `https://s3.${REGION}.amazonaws.com/${bucketName}/${outName}`
+ );
+};
diff --git a/packages/lambda/template/render.ts b/packages/lambda/template/render.ts
index 0109445381..1394e1bd57 100644
--- a/packages/lambda/template/render.ts
+++ b/packages/lambda/template/render.ts
@@ -1,277 +1,14 @@
-import {InvokeCommand, LambdaClient} from '@aws-sdk/client-lambda';
-import {
- CreateBucketCommand,
- PutObjectCommand,
- S3Client,
-} from '@aws-sdk/client-s3';
-import {
- getCompositions,
- renderFrames,
- RenderInternals,
- stitchFramesToVideo,
-} from '@remotion/renderer';
-import fs, {copyFileSync, writeFileSync} from 'fs';
-import path from 'path';
-import {concatVideos, concatVideosS3} from '../src/concat-videos';
-import {
- EFS_MOUNT_PATH,
- ENABLE_EFS,
- LambdaPayload,
- REGION,
- RENDERS_BUCKET_PREFIX,
-} from '../src/constants';
-import {timer} from '../src/timer';
-import {executablePath} from './get-chromium-executable-path';
-
-const s3Client = new S3Client({region: REGION});
-const lambdaClient = new LambdaClient({region: REGION});
-
-type Await<T> = T extends PromiseLike<infer U> ? U : T;
-
-let _browserInstance: Await<
- ReturnType<typeof RenderInternals.openBrowser>
-> | null;
-
-// TODO Potential race condition
-const getBrowserInstance = async () => {
- if (_browserInstance) {
- return _browserInstance;
- }
- const execPath = await executablePath();
- _browserInstance = await RenderInternals.openBrowser('chrome', {
- customExecutable: execPath,
- });
- return _browserInstance;
-};
-
-const validateComposition = async ({
- serveUrl,
- composition,
- browserInstance,
-}: {
- serveUrl: string;
- composition: string;
- browserInstance: Await<ReturnType<typeof RenderInternals.openBrowser>>;
-}) => {
- // TODO: Support input props
- const compositions = await getCompositions({
- serveUrl,
- browserInstance,
- });
- const found = compositions.find((c) => c.id === composition);
- if (!found) {
- throw new Error(
- `No composition with ID ${composition} found. Available compositions: ${compositions
- .map((c) => c.id)
- .join(', ')}`
- );
- }
- return found;
-};
+import {LambdaPayload} from '../src/constants';
+import {initHandler} from './init';
+import {rendererHandler} from './renderer';
export const handler = async (params: LambdaPayload) => {
console.log('CONTEXT', params);
- const outputDir = '/tmp/' + 'remotion-render-' + Math.random();
- const efsRemotionVideoRenderDone = EFS_MOUNT_PATH + '/render-done';
- if (fs.existsSync(outputDir)) {
- fs.rmdirSync(outputDir);
- }
- fs.mkdirSync(outputDir);
-
- const browserInstance = await getBrowserInstance();
if (params.type === 'init') {
- const bucketName = RENDERS_BUCKET_PREFIX + Math.random();
- const efsRemotionVideoPath = EFS_MOUNT_PATH + '/remotion-video';
- if (ENABLE_EFS) {
- if (fs.existsSync(efsRemotionVideoPath)) {
- fs.rmdirSync(efsRemotionVideoPath, {recursive: true});
- }
- fs.mkdirSync(efsRemotionVideoPath);
- if (fs.existsSync(efsRemotionVideoRenderDone)) {
- fs.rmdirSync(efsRemotionVideoRenderDone, {recursive: true});
- }
- fs.mkdirSync(efsRemotionVideoRenderDone);
- }
- // const bucketTimer = timer('Creating bucket');
-
- // TODO: Better validation
- if (!params.chunkSize) {
- throw new Error('Pass chunkSize');
- }
- // TODO: Better validation
- if (!params.durationInFrames) {
- throw new Error('Pass durationInFrames');
- }
-
- const comp = await validateComposition({
- serveUrl: params.serveUrl,
- composition: params.composition,
- browserInstance,
- });
- console.log(comp);
- const bucketTimer = timer('creating bucket');
- await s3Client.send(
- new CreateBucketCommand({
- Bucket: bucketName,
- ACL: 'public-read',
- })
- );
- bucketTimer.end();
- const {chunkSize} = params;
- const chunkCount = Math.ceil(params.durationInFrames / chunkSize);
-
- const chunks = new Array(chunkCount).fill(1).map((_, i) => {
- return [
- i * chunkSize,
- Math.min(params.durationInFrames, (i + 1) * chunkSize) - 1,
- ] as [number, number];
- });
- const reqSend = timer('sending off requests');
- await Promise.all(
- chunks.map(async (chunk) => {
- const payload: LambdaPayload = {
- type: 'renderer',
- frameRange: chunk,
- serveUrl: params.serveUrl,
- chunk: chunks.indexOf(chunk),
- efsRemotionVideoPath,
- composition: params.composition,
- fps: comp.fps,
- height: comp.height,
- width: comp.width,
- durationInFrames: params.durationInFrames,
- bucketName,
- };
- const callingLambdaTimer = timer(
- 'Calling lambda ' + chunks.indexOf(chunk)
- );
- await lambdaClient.send(
- new InvokeCommand({
- FunctionName: process.env.AWS_LAMBDA_FUNCTION_NAME,
- // @ts-expect-error
- Payload: JSON.stringify(payload),
- InvocationType: 'Event',
- }),
- {}
- );
- callingLambdaTimer.end();
- })
- );
- reqSend.end();
- const out = ENABLE_EFS
- ? await concatVideos(
- efsRemotionVideoPath,
- efsRemotionVideoRenderDone,
- chunkCount
- )
- : await concatVideosS3(s3Client, bucketName);
- const outName = 'out.mp4';
- await s3Client.send(
- new PutObjectCommand({
- Bucket: bucketName,
- Key: 'out.mp4',
- Body: fs.createReadStream(out),
- ACL: 'public-read',
- })
- );
- console.log(
- 'Done! ' + `https://s3.${REGION}.amazonaws.com/${bucketName}/${outName}`
- );
+ await initHandler(params);
} else if (params.type === 'renderer') {
- if (typeof params.chunk !== 'number') {
- throw new Error('must pass chunk');
- }
- if (!params.frameRange) {
- throw new Error('must pass framerange');
- }
- if (ENABLE_EFS) {
- if (!fs.existsSync(params.efsRemotionVideoPath)) {
- fs.mkdirSync(params.efsRemotionVideoPath);
- }
- if (!fs.existsSync(efsRemotionVideoRenderDone)) {
- fs.mkdirSync(efsRemotionVideoRenderDone);
- }
- }
- console.log(
- `Started rendering ${params.chunk}, frame ${params.frameRange}`
- );
- await renderFrames({
- compositionId: params.composition,
- config: {
- durationInFrames: params.durationInFrames,
- fps: params.fps,
- height: params.height,
- width: params.width,
- },
- imageFormat: 'jpeg',
- inputProps: {},
- frameRange: params.frameRange,
- onFrameUpdate: (i: number, output: string) => {
- console.log('Rendered frames', i, output);
- },
- parallelism: 2,
- onStart: () => {
- console.log('Starting');
- },
- outputDir,
- puppeteerInstance: browserInstance,
- serveUrl: params.serveUrl,
- });
- const outdir = `/tmp/${Math.random()}`;
- fs.mkdirSync(outdir);
-
- const outputLocation = ENABLE_EFS
- ? path.join(
- params.efsRemotionVideoPath,
- `chunk-${String(params.chunk).padStart(8, '0')}.mp4`
- )
- : path.join(outdir, `chunk-${String(params.chunk).padStart(8, '0')}.mp4`);
- const outputFileLocation = path.join(
- efsRemotionVideoRenderDone,
- `chunk-${String(params.chunk).padStart(8, '0')}.txt`
- );
-
- const stitchLabel = timer('stitcher');
- await stitchFramesToVideo({
- assetsInfo: {
- // TODO
- assets: [],
- bundleDir: '',
- },
- dir: outputDir,
- force: true,
- fps: params.fps,
- height: params.height,
- width: params.width,
- outputLocation,
- // TODO
- codec: 'h264',
- // TODO
- imageFormat: 'jpeg',
- });
- stitchLabel.end();
- if (ENABLE_EFS) {
- const copying = timer('copying');
- copyFileSync(outputLocation, outputLocation);
- copying.end();
-
- const flag = timer('writing flag');
- writeFileSync(outputFileLocation, 'true');
- flag.end();
- console.log('Done rendering!', outputDir, outputLocation);
- } else {
- const uploading = timer('uploading');
- await s3Client.send(
- new PutObjectCommand({
- Bucket: params.bucketName,
- Key: `chunk-${String(params.chunk).padStart(8, '0')}.mp4`,
- Body: fs.createReadStream(outputLocation),
- })
- );
- uploading.end();
- console.log('Done rendering!', outputDir, outputLocation);
- }
+ await rendererHandler(params);
} else {
throw new Error('Command not found');
}
diff --git a/packages/lambda/template/renderer.ts b/packages/lambda/template/renderer.ts
new file mode 100644
index 0000000000..3048095fbb
--- /dev/null
+++ b/packages/lambda/template/renderer.ts
@@ -0,0 +1,114 @@
+import {PutObjectCommand} from '@aws-sdk/client-s3';
+import {renderFrames, stitchFramesToVideo} from '@remotion/renderer';
+import fs from 'fs';
+import {copyFileSync, writeFileSync} from 'node:fs';
+import path from 'path';
+import {EFS_MOUNT_PATH, ENABLE_EFS, LambdaPayload} from '../src/constants';
+import {timer} from '../src/timer';
+import {s3Client} from './aws-clients';
+import {getBrowserInstance} from './get-browser-instance';
+
+export const rendererHandler = async (params: LambdaPayload) => {
+ const efsRemotionVideoRenderDone = EFS_MOUNT_PATH + '/render-done';
+ const browserInstance = await getBrowserInstance();
+ const outputDir = '/tmp/' + 'remotion-render-' + Math.random();
+ if (fs.existsSync(outputDir)) {
+ fs.rmdirSync(outputDir);
+ }
+ fs.mkdirSync(outputDir);
+
+ if (params.type !== 'renderer') {
+ throw new Error('Params must be renderer');
+ }
+ if (typeof params.chunk !== 'number') {
+ throw new Error('must pass chunk');
+ }
+ if (!params.frameRange) {
+ throw new Error('must pass framerange');
+ }
+ if (ENABLE_EFS) {
+ if (!fs.existsSync(params.efsRemotionVideoPath)) {
+ fs.mkdirSync(params.efsRemotionVideoPath);
+ }
+ if (!fs.existsSync(efsRemotionVideoRenderDone)) {
+ fs.mkdirSync(efsRemotionVideoRenderDone);
+ }
+ }
+ console.log(`Started rendering ${params.chunk}, frame ${params.frameRange}`);
+ await renderFrames({
+ compositionId: params.composition,
+ config: {
+ durationInFrames: params.durationInFrames,
+ fps: params.fps,
+ height: params.height,
+ width: params.width,
+ },
+ imageFormat: 'jpeg',
+ inputProps: {},
+ frameRange: params.frameRange,
+ onFrameUpdate: (i: number, output: string) => {
+ console.log('Rendered frames', i, output);
+ },
+ parallelism: 2,
+ onStart: () => {
+ console.log('Starting');
+ },
+ outputDir,
+ puppeteerInstance: browserInstance,
+ serveUrl: params.serveUrl,
+ });
+ const outdir = `/tmp/${Math.random()}`;
+ fs.mkdirSync(outdir);
+
+ const outputLocation = ENABLE_EFS
+ ? path.join(
+ params.efsRemotionVideoPath,
+ `chunk-${String(params.chunk).padStart(8, '0')}.mp4`
+ )
+ : path.join(outdir, `chunk-${String(params.chunk).padStart(8, '0')}.mp4`);
+ const outputFileLocation = path.join(
+ efsRemotionVideoRenderDone,
+ `chunk-${String(params.chunk).padStart(8, '0')}.txt`
+ );
+
+ const stitchLabel = timer('stitcher');
+ await stitchFramesToVideo({
+ assetsInfo: {
+ // TODO
+ assets: [],
+ bundleDir: '',
+ },
+ dir: outputDir,
+ force: true,
+ fps: params.fps,
+ height: params.height,
+ width: params.width,
+ outputLocation,
+ // TODO
+ codec: 'h264',
+ // TODO
+ imageFormat: 'jpeg',
+ });
+ stitchLabel.end();
+ if (ENABLE_EFS) {
+ const copying = timer('copying');
+ copyFileSync(outputLocation, outputLocation);
+ copying.end();
+
+ const flag = timer('writing flag');
+ writeFileSync(outputFileLocation, 'true');
+ flag.end();
+ console.log('Done rendering!', outputDir, outputLocation);
+ } else {
+ const uploading = timer('uploading');
+ await s3Client.send(
+ new PutObjectCommand({
+ Bucket: params.bucketName,
+ Key: `chunk-${String(params.chunk).padStart(8, '0')}.mp4`,
+ Body: fs.createReadStream(outputLocation),
+ })
+ );
+ uploading.end();
+ console.log('Done rendering!', outputDir, outputLocation);
+ }
+};
diff --git a/packages/lambda/template/validate-composition.ts b/packages/lambda/template/validate-composition.ts
new file mode 100644
index 0000000000..af498bacf4
--- /dev/null
+++ b/packages/lambda/template/validate-composition.ts
@@ -0,0 +1,29 @@
+import {getCompositions, RenderInternals} from '@remotion/renderer';
+
+// TODO: redundant
+type Await<T> = T extends PromiseLike<infer U> ? U : T;
+
+export const validateComposition = async ({
+ serveUrl,
+ composition,
+ browserInstance,
+}: {
+ serveUrl: string;
+ composition: string;
+ browserInstance: Await<ReturnType<typeof RenderInternals.openBrowser>>;
+}) => {
+ // TODO: Support input props
+ const compositions = await getCompositions({
+ serveUrl,
+ browserInstance,
+ });
+ const found = compositions.find((c) => c.id === composition);
+ if (!found) {
+ throw new Error(
+ `No composition with ID ${composition} found. Available compositions: ${compositions
+ .map((c) => c.id)
+ .join(', ')}`
+ );
+ }
+ return found;
+};