Skip to content

🎥 Make videos programmatically with React

updaet

Jonny Burger41444afcdb47
diff --git a/packages/docs/docs/lambda.md b/packages/docs/docs/lambda.md
index d50bdaaaf0..a32c05a88a 100644
--- a/packages/docs/docs/lambda.md
+++ b/packages/docs/docs/lambda.md
@@ -11,25 +11,40 @@ No need to create security group, use default security group and VPC.
create user role with this access `AmazonS3FullAccess` `AmazonElasticFileSystemFullAccess` `AWSLambdaBasicExecutionRole` `AWSLambdaVPCAccessExecutionRole` `AmazonElasticFileSystemClientFullAccess`
`AWSLambda_FullAccess`
+## Create VPC
+
+- Go to [VPC console](https://eu-central-1.console.aws.amazon.com/vpc/home)
+- Create new VPC
+- `IPv4 CIDR blockInfo`: 10.0.0.0/24, 20.0.0.0/24, 30.0.0.0/24
+- Amazon-provided IPv6 CIDR block
+- Tenancy default
+
## Create EFS
- go to efs [home](https://eu-central-1.console.aws.amazon.com/efs/home)
- click on create file system
-- give a name and use default vpc
-- `Availability and Durability` is ``one zone`
+- give a name and use vpc you created
+- `Availability and Durability` is `regional`
- `Availability Zone` is `eu-central-1a`
### Create access point for efs
-- click on the file system that was created
+- click on the file system that was created
- click on access point on bottom right and then create access point
- give a root path (if root path is `/efs` then mount path in lambda will be `/mnt/efs`).
- posix user: userID 1000, Group Id:1000,
- Root directory creation permissions, Owner user ID: 1000, Owner group ID:1000, POSIX permissions: 777
- click on create access point
-- now go to Network tab besides Access point tab, you will get your SubnetIds (should be 1 subnetId) and SecurityGroupIds
+
+### Create subnets
+
+- Go to VPC console, click on create subnet
+- Select your VPC
+
+- now go to the EFS, click on the network tab, you will get your SubnetIds (should be 1 subnetId) and SecurityGroupIds
on create-lambda it will be something like this
+
```tsx
VpcConfig: ENABLE_EFS
? {
@@ -60,4 +75,4 @@ FileSystemConfigs: ENABLE_EFS
- use default VPC and policy `Full Access` and click on `Create Endpoint`
- Now you should have 2 endpoints one for `s3` and one for `lambda`
-Pretty much done, and you should be done with it.
\ No newline at end of file
+Pretty much done, and you should be done with it.
diff --git a/packages/lambda/remotion-project/Root.tsx b/packages/lambda/remotion-project/Root.tsx
index 93c69581b5..61d3d5ab11 100644
--- a/packages/lambda/remotion-project/Root.tsx
+++ b/packages/lambda/remotion-project/Root.tsx
@@ -1,12 +1,13 @@
import React from 'react';
-import {Composition} from 'remotion';
+import {Composition, getInputProps} from 'remotion';
import {MyVideo} from './MyVideo';
export const RemotionRoot: React.FC = () => {
+ const inputProps = getInputProps();
return (
<Composition
fps={30}
- durationInFrames={20000}
+ durationInFrames={inputProps.duration ?? 200}
component={MyVideo}
height={1080}
width={1920}
diff --git a/packages/lambda/src/concat-videos.ts b/packages/lambda/src/concat-videos.ts
index 6209c117b2..619bc24c3e 100644
--- a/packages/lambda/src/concat-videos.ts
+++ b/packages/lambda/src/concat-videos.ts
@@ -45,6 +45,7 @@ const getAllFiles = async ({
return new Promise<string[]>((resolve) => {
const loop = async () => {
const files = readdirSync(efsRemotionVideoPath);
+ console.log(files);
const txtFiles = readdirSync(efsRemotionVideoRenderDone);
const areAllFilesDownloading = Boolean(
files.length === expectedFiles && txtFiles.length === expectedFiles
@@ -70,16 +71,17 @@ export const concatVideos = xns(
efsRemotionVideoRenderDone,
expectedFiles = 20
) => {
+ const getAllTimes = timer('get all files');
const files = await getAllFiles({
efsRemotionVideoPath,
expectedFiles,
efsRemotionVideoRenderDone,
});
+ getAllTimes.end();
const outfile = join(tmpDir('remotion-concated'), 'concat.mp4');
const combine = timer('Combine videos');
const filelistDir = tmpDir('remotion-filelist');
- console.log('all path name', outfile, combine, efsRemotionVideoPath);
await combineVideos({
files,
filelistDir,
diff --git a/packages/lambda/src/constants.ts b/packages/lambda/src/constants.ts
index 248d0caf36..955671d99d 100644
--- a/packages/lambda/src/constants.ts
+++ b/packages/lambda/src/constants.ts
@@ -13,6 +13,8 @@ export type LambdaPayload =
serveUrl: string;
composition: string;
chunkSize: number;
+ // Just for debugging
+ durationInFrames: number;
}
| {
type: 'renderer';
diff --git a/packages/lambda/src/create-lambda.ts b/packages/lambda/src/create-lambda.ts
index 9f2f651691..2dc0007e59 100644
--- a/packages/lambda/src/create-lambda.ts
+++ b/packages/lambda/src/create-lambda.ts
@@ -26,6 +26,8 @@ const lambdaClient = new LambdaClient({
const s3Client = new S3Client({region: REGION});
const ENABLE_EFS = true;
+type Developer = 'jonny' | 'shankhadeep';
+const developer: Developer = 'jonny' as Developer;
xns(async () => {
const {layerArn} = await ensureLayers(lambdaClient);
@@ -93,23 +95,37 @@ xns(async () => {
},
FunctionName: fnNameRender,
Handler: 'index.handler',
- Role: 'arn:aws:iam::363307378317:role/awesomeLambda', // IAM_ROLE_ARN; e.g., arn:aws:iam::650138640062:role/v3-lambda-tutorial-lambda-role
+ Role:
+ developer === 'shankhadeep'
+ ? 'arn:aws:iam::363307378317:role/awesomeLambda'
+ : 'arn:aws:iam::976210361945:role/lambda-admin', // IAM_ROLE_ARN; e.g., arn:aws:iam::650138640062:role/v3-lambda-tutorial-lambda-role
Runtime: 'nodejs12.x',
Description: 'Renders a Remotion video.',
- MemorySize: 1769 * 2,
- Timeout: 300,
+ MemorySize: 1769,
+ Timeout: 60,
Layers: [layerArn],
+
VpcConfig: ENABLE_EFS
? {
- SubnetIds: ['subnet-be85fcd4'],
- SecurityGroupIds: ['sg-8f2391fc'],
+ SubnetIds: [
+ developer === 'jonny'
+ ? 'subnet-be85fcd4'
+ : 'subnet-00e3aa99745996ff7',
+ ],
+ SecurityGroupIds: [
+ developer === 'shankhadeep'
+ ? 'sg-8f2391fc'
+ : 'sg-0251b3f6fa6af4577',
+ ],
}
: undefined,
FileSystemConfigs: ENABLE_EFS
? [
{
Arn:
- 'arn:aws:elasticfilesystem:eu-central-1:363307378317:access-point/fsap-05a31f7aad4e47581',
+ developer === 'shankhadeep'
+ ? 'arn:aws:elasticfilesystem:eu-central-1:363307378317:access-point/fsap-05a31f7aad4e47581'
+ : 'arn:aws:elasticfilesystem:eu-central-1:976210361945:access-point/fsap-0e697d03f5bf221a7',
LocalMountPath: EFS_MOUNT_PATH,
},
]
diff --git a/packages/lambda/template/render.ts b/packages/lambda/template/render.ts
index 135172440a..f6d8315ad1 100644
--- a/packages/lambda/template/render.ts
+++ b/packages/lambda/template/render.ts
@@ -6,11 +6,11 @@ import {
} from '@aws-sdk/client-s3';
import {
getCompositions,
- openBrowser,
renderFrames,
+ RenderInternals,
stitchFramesToVideo,
} from '@remotion/renderer';
-import fs, {readdirSync, writeFileSync} from 'fs';
+import fs, {copyFileSync, writeFileSync} from 'fs';
import path from 'path';
import {concatVideos} from '../src/concat-videos';
import {
@@ -27,7 +27,9 @@ const lambdaClient = new LambdaClient({region: REGION});
type Await<T> = T extends PromiseLike<infer U> ? U : T;
-let _browserInstance: Await<ReturnType<typeof openBrowser>> | null;
+let _browserInstance: Await<
+ ReturnType<typeof RenderInternals.openBrowser>
+> | null;
// TODO Potential race condition
const getBrowserInstance = async () => {
@@ -35,7 +37,7 @@ const getBrowserInstance = async () => {
return _browserInstance;
}
const execPath = await executablePath();
- _browserInstance = await openBrowser('chrome', {
+ _browserInstance = await RenderInternals.openBrowser('chrome', {
customExecutable: execPath,
});
return _browserInstance;
@@ -48,7 +50,7 @@ const validateComposition = async ({
}: {
serveUrl: string;
composition: string;
- browserInstance: Await<ReturnType<typeof openBrowser>>;
+ browserInstance: Await<ReturnType<typeof RenderInternals.openBrowser>>;
}) => {
// TODO: Support input props
const compositions = await getCompositions({
@@ -94,7 +96,10 @@ export const handler = async (params: LambdaPayload) => {
if (!params.chunkSize) {
throw new Error('Pass chunkSize');
}
- console.log('browser instanse', browserInstance);
+ // TODO: Better validation
+ if (!params.durationInFrames) {
+ throw new Error('Pass durationInFrames');
+ }
const comp = await validateComposition({
serveUrl: params.serveUrl,
@@ -111,15 +116,15 @@ export const handler = async (params: LambdaPayload) => {
);
bucketTimer.end();
const {chunkSize} = params;
- const chunkCount = Math.ceil(comp.durationInFrames / chunkSize);
+ const chunkCount = Math.ceil(params.durationInFrames / chunkSize);
const chunks = new Array(chunkCount).fill(1).map((_, i) => {
return [
i * chunkSize,
- Math.min(comp.durationInFrames, (i + 1) * chunkSize) - 1,
+ Math.min(params.durationInFrames, (i + 1) * chunkSize) - 1,
] as [number, number];
});
- console.log(chunks);
+ const reqSend = timer('sending off requests');
await Promise.all(
chunks.map(async (chunk) => {
const payload: LambdaPayload = {
@@ -132,10 +137,11 @@ export const handler = async (params: LambdaPayload) => {
fps: comp.fps,
height: comp.height,
width: comp.width,
- durationInFrames: comp.durationInFrames,
+ durationInFrames: params.durationInFrames,
};
- const callingLambdaTimer = timer('Calling lambda');
- console.log(`calling lambda ${chunk}`);
+ const callingLambdaTimer = timer(
+ 'Calling lambda ' + chunks.indexOf(chunk)
+ );
await lambdaClient.send(
new InvokeCommand({
FunctionName: process.env.AWS_LAMBDA_FUNCTION_NAME,
@@ -147,6 +153,7 @@ export const handler = async (params: LambdaPayload) => {
callingLambdaTimer.end();
})
);
+ reqSend.end();
const out = await concatVideos(
efsRemotionVideoPath,
efsRemotionVideoRenderDone,
@@ -171,6 +178,12 @@ export const handler = async (params: LambdaPayload) => {
if (!params.frameRange) {
throw new Error('must pass framerange');
}
+ 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}`
);
@@ -203,12 +216,17 @@ export const handler = async (params: LambdaPayload) => {
params.efsRemotionVideoPath,
`chunk-${String(params.chunk).padStart(8, '0')}.mp4`
);
+ const intermediary = path.join(
+ outdir,
+ `chunk-${String(params.chunk).padStart(8, '0')}.mp4`
+ );
const outputFileLocation = path.join(
efsRemotionVideoRenderDone,
`chunk-${String(params.chunk).padStart(8, '0')}.txt`
);
console.log(outputLocation);
+ const stitchLabel = timer('stitcher');
await stitchFramesToVideo({
assetsInfo: {
// TODO
@@ -220,18 +238,21 @@ export const handler = async (params: LambdaPayload) => {
fps: params.fps,
height: params.height,
width: params.width,
- outputLocation,
+ outputLocation: intermediary,
// TODO
codec: 'h264',
// TODO
imageFormat: 'jpeg',
});
+ stitchLabel.end();
+ const copying = timer('copying');
+ copyFileSync(intermediary, outputLocation);
+ copying.end();
+
+ const flag = timer('writing flag');
writeFileSync(outputFileLocation, 'true');
+ flag.end();
console.log('Done rendering!', outputDir, outputLocation);
- console.log(
- 'all in render 226 files',
- readdirSync(params.efsRemotionVideoPath)
- );
} else {
throw new Error('Command not found');
}