Skip to content

🎥 Make videos programmatically with React

not uploading to s3 now

Shankhadeep Dey1360628a92f3
diff --git a/packages/lambda/remotion-project/Root.tsx b/packages/lambda/remotion-project/Root.tsx
index 93c69581b5..0deea97c73 100644
--- a/packages/lambda/remotion-project/Root.tsx
+++ b/packages/lambda/remotion-project/Root.tsx
@@ -6,7 +6,7 @@ export const RemotionRoot: React.FC = () => {
return (
<Composition
fps={30}
- durationInFrames={20000}
+ durationInFrames={1000}
component={MyVideo}
height={1080}
width={1920}
diff --git a/packages/lambda/src/concat-videos.ts b/packages/lambda/src/concat-videos.ts
index 461d7f5935..bdebc25816 100644
--- a/packages/lambda/src/concat-videos.ts
+++ b/packages/lambda/src/concat-videos.ts
@@ -1,14 +1,9 @@
-import {
- GetObjectCommand,
- ListObjectsCommand,
- S3Client,
-} from '@aws-sdk/client-s3';
+import {GetObjectCommand, S3Client} from '@aws-sdk/client-s3';
import {combineVideos} from '@remotion/renderer';
-import {createWriteStream, existsSync, mkdirSync, rmdirSync} from 'fs';
+import {createWriteStream, readdirSync, rmdirSync} from 'fs';
import {join} from 'path';
import {Readable} from 'stream';
import xns from 'xns';
-import {REGION} from './constants';
import {timer} from './timer';
import {tmpDir} from './tmpdir';
@@ -39,68 +34,81 @@ const downloadS3File = async ({
};
const getAllFiles = async ({
- s3Client,
- bucket,
+ efsRemotionVideoPath,
expectedFiles,
- outdir,
+ efsRemotionVideoRenderDone,
}: {
- s3Client: S3Client;
- bucket: string;
+ efsRemotionVideoPath: string;
expectedFiles: number;
- outdir: string;
+ efsRemotionVideoRenderDone: string;
}): Promise<string[]> => {
- const alreadyDownloading: {[key: string]: true} = {};
- const downloaded: {[key: string]: true} = {};
+ // const alreadyDownloading: {[key: string]: true} = {};
+ // const downloaded: {[key: string]: true} = {};
- const getFiles = async () => {
- const lsTimer = timer('Listing files');
- const files = await s3Client.send(
- new ListObjectsCommand({
- Bucket: bucket,
- })
- );
- lsTimer.end();
- return (files.Contents || []).map((_) => _.Key as string);
- };
+ // const getFiles = async () => {
+ // const lsTimer = timer('Listing files');
+ // const files = await s3Client.send(
+ // new ListObjectsCommand({
+ // Bucket: bucket,
+ // })
+ // );
+ // lsTimer.end();
+ // return (files.Contents || []).map((_) => _.Key as string);
+ // };
- return new Promise<string[]>((resolve, reject) => {
+ return new Promise<string[]>((resolve) => {
const loop = async () => {
- const filesInBucket = await getFiles();
- const checkFinish = () => {
- const areAllFilesDownloaded =
- Object.keys(downloaded).length === expectedFiles;
- if (areAllFilesDownloaded) {
- resolve(filesInBucket.map((file) => join(outdir, file)));
- }
- };
- filesInBucket.forEach(async (content) => {
- if (alreadyDownloading[content]) {
- return;
- }
- alreadyDownloading[content] = true;
- try {
- const downloadTimer = timer('Downloading ' + content);
- await downloadS3File({
- bucket,
- content,
- outdir,
- s3Client,
- });
- downloadTimer.end();
- downloaded[content] = true;
- checkFinish();
- } catch (err) {
- reject(err);
- }
- });
+ // const filesInBucket = await getFiles();
+ // const checkFinish = () => {
+ // const areAllFilesDownloaded =
+ // Object.keys(downloaded).length === expectedFiles;
+ // if (areAllFilesDownloaded) {
+ // resolve(filesInBucket.map((file) => join(outdir, file)));
+ // }
+ // };
+ // filesInBucket.forEach(async (content) => {
+ // if (alreadyDownloading[content]) {
+ // return;
+ // }
+ // alreadyDownloading[content] = true;
+ // try {
+ // const downloadTimer = timer('Downloading ' + content);
+ // await downloadS3File({
+ // bucket,
+ // content,
+ // outdir,
+ // s3Client,
+ // });
+ // downloadTimer.end();
+ // downloaded[content] = true;
+ // checkFinish();
+ // } catch (err) {
+ // reject(err);
+ // }
+ // });
+ const files = readdirSync(efsRemotionVideoPath);
+ const txtFiles = readdirSync(efsRemotionVideoRenderDone);
+ const areAllFilesDownloading = Boolean(
+ files.length === expectedFiles && txtFiles.length === expectedFiles
+ );
+
+ // console.log(
+ // readdirSync(efsRemotionVideoPath),
+ // 'all files in getAllFiles',
+ // expectedFiles,
+ // files.length,
+ // 'expected files',
+ // areAllFilesDownloading
+ // );
- const areAllFilesDownloading =
- Object.keys(alreadyDownloading).length === expectedFiles;
if (!areAllFilesDownloading) {
setTimeout(() => {
loop();
}, 100);
}
+ if (areAllFilesDownloading) {
+ resolve(files.map((file) => join(efsRemotionVideoPath, file)));
+ }
};
loop();
@@ -109,25 +117,40 @@ const getAllFiles = async ({
export const concatVideos = xns(
async (
- s3Client: S3Client = new S3Client({region: REGION}),
- bucket = 'remotion-renders-0.7182592846197402',
+ efsRemotionVideoPath,
+ efsRemotionVideoRenderDone,
expectedFiles = 20
) => {
- const outdir = join(tmpDir('remotion-concat'), 'bucket');
- console.log('outpur dir ', outdir);
- if (existsSync(outdir)) {
- rmdirSync(outdir, {
- recursive: true,
- });
- }
- mkdirSync(outdir);
+ // const outdir = join(tmpDir('remotion-concat'), 'bucket');
+ // console.log('outpur dir ', outdir);
+ // if (existsSync(outdir)) {
+ // rmdirSync(outdir, {
+ // recursive: true,
+ // });
+ // }
+ // mkdirSync(outdir);
+
+ const files = await getAllFiles({
+ efsRemotionVideoPath,
+ expectedFiles,
+ efsRemotionVideoRenderDone,
+ });
- const files = await getAllFiles({s3Client, bucket, expectedFiles, outdir});
+ // while (expectedFiles !== readdirSync(efsRemotionVideoPath).length) {
+ // console.log(
+ // 'files in concat',
+ // readdirSync(efsRemotionVideoPath),
+ // readdirSync(efsRemotionVideoPath).length
+ // );
+ // await awaitTimer(100);
+ // }
+ // const files = readdirSync(efsRemotionVideoPath);
+ // console.log(files, 'all files in remotion-video');
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, filelistDir);
+ console.log('all path name', outfile, combine, efsRemotionVideoPath);
await combineVideos({
files,
filelistDir,
@@ -135,7 +158,7 @@ export const concatVideos = xns(
});
combine.end();
- rmdirSync(outdir, {
+ rmdirSync(efsRemotionVideoPath, {
recursive: true,
});
return outfile;
diff --git a/packages/lambda/src/constants.ts b/packages/lambda/src/constants.ts
index b7a3545903..248d0caf36 100644
--- a/packages/lambda/src/constants.ts
+++ b/packages/lambda/src/constants.ts
@@ -19,7 +19,7 @@ export type LambdaPayload =
serveUrl: string;
frameRange: [number, number];
chunk: number;
- bucketName: string;
+ efsRemotionVideoPath: string;
composition: string;
fps: number;
height: number;
diff --git a/packages/lambda/src/create-lambda.ts b/packages/lambda/src/create-lambda.ts
index 9f2f651691..f84ef3683d 100644
--- a/packages/lambda/src/create-lambda.ts
+++ b/packages/lambda/src/create-lambda.ts
@@ -97,7 +97,7 @@ xns(async () => {
Runtime: 'nodejs12.x',
Description: 'Renders a Remotion video.',
MemorySize: 1769 * 2,
- Timeout: 300,
+ Timeout: 60,
Layers: [layerArn],
VpcConfig: ENABLE_EFS
? {
diff --git a/packages/lambda/template/render.ts b/packages/lambda/template/render.ts
index abc7068938..3462564754 100644
--- a/packages/lambda/template/render.ts
+++ b/packages/lambda/template/render.ts
@@ -10,10 +10,15 @@ import {
renderFrames,
stitchFramesToVideo,
} from '@remotion/renderer';
-import fs, {unlinkSync} from 'fs';
+import fs, {readdirSync, writeFileSync} from 'fs';
import path from 'path';
import {concatVideos} from '../src/concat-videos';
-import {LambdaPayload, REGION, RENDERS_BUCKET_PREFIX} from '../src/constants';
+import {
+ EFS_MOUNT_PATH,
+ LambdaPayload,
+ REGION,
+ RENDERS_BUCKET_PREFIX,
+} from '../src/constants';
import {timer} from '../src/timer';
import {executablePath} from './get-chromium-executable-path';
@@ -64,6 +69,7 @@ const validateComposition = async ({
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);
}
@@ -73,7 +79,16 @@ export const handler = async (params: LambdaPayload) => {
if (params.type === 'init') {
const bucketName = RENDERS_BUCKET_PREFIX + Math.random();
- const bucketTimer = timer('Creating bucket');
+ const efsRemotionVideoPath = EFS_MOUNT_PATH + '/remotion-video';
+ 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) {
@@ -87,7 +102,7 @@ export const handler = async (params: LambdaPayload) => {
browserInstance,
});
console.log(comp);
-
+ const bucketTimer = timer('creating bucket');
await s3Client.send(
new CreateBucketCommand({
Bucket: bucketName,
@@ -112,7 +127,7 @@ export const handler = async (params: LambdaPayload) => {
frameRange: chunk,
serveUrl: params.serveUrl,
chunk: chunks.indexOf(chunk),
- bucketName,
+ efsRemotionVideoPath,
composition: params.composition,
fps: comp.fps,
height: comp.height,
@@ -132,7 +147,11 @@ export const handler = async (params: LambdaPayload) => {
callingLambdaTimer.end();
})
);
- const out = await concatVideos(s3Client, bucketName, chunkCount);
+ const out = await concatVideos(
+ efsRemotionVideoPath,
+ efsRemotionVideoRenderDone,
+ chunkCount
+ );
const outName = 'out.mp4';
await s3Client.send(
new PutObjectCommand({
@@ -180,7 +199,14 @@ export const handler = async (params: LambdaPayload) => {
const outdir = `/tmp/${Math.random()}`;
fs.mkdirSync(outdir);
- const outputLocation = path.join(outdir, 'out.mp4');
+ const outputLocation = path.join(
+ params.efsRemotionVideoPath,
+ `chunk-${String(params.chunk).padStart(8, '0')}.mp4`
+ );
+ const outputFileLocation = path.join(
+ efsRemotionVideoRenderDone,
+ `chunk-${String(params.chunk).padStart(8, '0')}.txt`
+ );
console.log(outputLocation);
await stitchFramesToVideo({
@@ -200,15 +226,20 @@ export const handler = async (params: LambdaPayload) => {
// TODO
imageFormat: 'jpeg',
});
- await s3Client.send(
- new PutObjectCommand({
- Bucket: params.bucketName,
- Key: `chunk-${String(params.chunk).padStart(8, '0')}.mp4`,
- Body: fs.createReadStream(outputLocation),
- })
+ // await s3Client.send(
+ // new PutObjectCommand({
+ // Bucket: params.bucketName,
+ // Key: `chunk-${String(params.chunk).padStart(8, '0')}.mp4`,
+ // Body: fs.createReadStream(outputLocation),
+ // })
+ // );
+ // unlinkSync(outputLocation);
+ writeFileSync(outputFileLocation, 'true');
+ console.log('Done rendering!', outputDir, outputLocation);
+ console.log(
+ 'all in render 226 files',
+ readdirSync(params.efsRemotionVideoPath)
);
- unlinkSync(outputLocation);
- console.log('Done rendering!', outputDir, params.bucketName);
} else {
throw new Error('Command not found');
}
diff --git a/packages/renderer/src/combine-videos.ts b/packages/renderer/src/combine-videos.ts
index 789efb03a7..74efec2e35 100644
--- a/packages/renderer/src/combine-videos.ts
+++ b/packages/renderer/src/combine-videos.ts
@@ -16,6 +16,7 @@ export const combineVideos = async ({
const fileList = files.map((p) => `file '${p}'`).join('\n');
const fileListTxt = join(filelistDir, 'files.txt');
+ console.log(fileList, 'file list');
writeFileSync(fileListTxt, fileList);
try {