🎥 Make videos programmatically with React
lit
Jonny Burgera0d54fc3c5b3
diff --git a/packages/lambda/remotion-project/MyVideo.tsx b/packages/lambda/remotion-project/MyVideo.tsxindex 1d687b0b28..d7e398b807 100644--- a/packages/lambda/remotion-project/MyVideo.tsx+++ b/packages/lambda/remotion-project/MyVideo.tsx@@ -4,36 +4,21 @@ export const MyVideo: React.FC = () => {const frame = useCurrentFrame();return (<div>- <Video- style={{- transform: `scale(${(Math.sin(frame / 30) + 1) / 2})`,- }}- src="https://test-videos.co.uk/vids/bigbuckbunny/webm/vp8/1080/Big_Buck_Bunny_1080_10s_2MB.webm"- />- <Sequence from={300} durationInFrames={300}>- <Video- style={{- transform: `scale(${(Math.sin(frame / 30) + 1) / 2})`,- }}- src="https://test-videos.co.uk/vids/bigbuckbunny/webm/vp8/1080/Big_Buck_Bunny_1080_10s_2MB.webm"- />- </Sequence>- <Sequence from={600} durationInFrames={300}>- <Video- style={{- transform: `scale(${(Math.sin(frame / 30) + 1) / 2})`,- }}- src="https://test-videos.co.uk/vids/bigbuckbunny/webm/vp8/1080/Big_Buck_Bunny_1080_10s_2MB.webm"- />- </Sequence>- <Sequence from={900} durationInFrames={300}>- <Video- style={{- transform: `scale(${(Math.sin(frame / 30) + 1) / 2})`,- }}- src="https://test-videos.co.uk/vids/bigbuckbunny/webm/vp8/1080/Big_Buck_Bunny_1080_10s_2MB.webm"- />- </Sequence>+ {new Array(30)+ .fill(1)+ .map((_, k) => k)+ .map((k) => {+ return (+ <Sequence key={k} from={k * 300} durationInFrames={300}>+ <Video+ style={{+ transform: `scale(${(Math.sin(frame / 30) + 1) / 2})`,+ }}+ src="https://test-videos.co.uk/vids/bigbuckbunny/webm/vp8/1080/Big_Buck_Bunny_1080_10s_2MB.webm"+ />+ </Sequence>+ );+ })}<AbsoluteFillstyle={{justifyContent: 'center',diff --git a/packages/lambda/src/concat-videos.ts b/packages/lambda/src/concat-videos.tsindex c2c20adc01..0e3e99c17e 100644--- a/packages/lambda/src/concat-videos.ts+++ b/packages/lambda/src/concat-videos.ts@@ -59,7 +59,7 @@ const getAllFiles = async ({Bucket: bucket,}));- lsTimer();+ lsTimer.end();return (files.Contents || []).map((_) => _.Key as string);};@@ -79,12 +79,14 @@ const getAllFiles = async ({}alreadyDownloading[content] = true;try {+ const downloadTimer = timer('Downloading ' + content);await downloadS3File({bucket,content,outdir,s3Client,});+ downloadTimer.end();downloaded[content] = true;checkFinish();} catch (err) {@@ -97,7 +99,7 @@ const getAllFiles = async ({if (!areAllFilesDownloading) {setTimeout(() => {loop();- }, 300);+ }, 100);}};@@ -129,11 +131,11 @@ export const concatVideos = xns(filelistDir,output: outfile,});- combine();+ combine.end();rmdirSync(outdir, {recursive: true,});- console.log(outfile);+ return outfile;});diff --git a/packages/lambda/src/timer.ts b/packages/lambda/src/timer.tsindex c7ccbf800f..e0062e7b8e 100644--- a/packages/lambda/src/timer.ts+++ b/packages/lambda/src/timer.ts@@ -12,12 +12,13 @@ const formatTime = (time: number) => {export const timer = (label: string) => {const start = Date.now();- process.stdout.write(label + '...');- return () => {- const end = Date.now();- const time = end - start;+ return {+ end: () => {+ const end = Date.now();+ const time = end - start;- process.stdout.write(` ${formatTime(time)}\n`);+ process.stdout.write(`${label} - ${formatTime(time)}\n`);+ },};};diff --git a/packages/lambda/template/render.ts b/packages/lambda/template/render.tsindex 9ca60d98d0..c2bdbf1be3 100644--- a/packages/lambda/template/render.ts+++ b/packages/lambda/template/render.ts@@ -9,10 +9,11 @@ import {renderFrames,stitchFramesToVideo,} from '@remotion/renderer';-import fs from 'fs';+import fs, {unlinkSync} from 'fs';import path from 'path';import {concatVideos} from '../src/concat-videos';import {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});@@ -33,7 +34,9 @@ const getBrowserInstance = async () => {return browserInstance;};-const totalFrames = 1000;+const chunkCount = 300;+const chunkSize = 20;+const totalFrames = chunkCount * chunkSize;export const handler = async (params: LambdaPayload) => {console.log('CONTEXT', params);@@ -45,18 +48,19 @@ export const handler = async (params: LambdaPayload) => {if (params.type === 'init') {const bucketName = RENDERS_BUCKET_PREFIX + Math.random();+ const bucketTimer = timer('Creating bucket');await s3Client.send(new CreateBucketCommand({Bucket: bucketName,ACL: 'public-read',}));- const chunkCount = 20;+ bucketTimer.end();const chunks = new Array(chunkCount).fill(1).map((_, i) => {- return [i * 50, (i + 1) * 50 - 1] as [number, number];+ return [i * chunkSize, (i + 1) * chunkSize - 1] as [number, number];});await Promise.all(- chunks.map((chunk) => {+ chunks.map(async (chunk) => {const payload: LambdaPayload = {type: 'renderer',frameRange: chunk,@@ -64,17 +68,31 @@ export const handler = async (params: LambdaPayload) => {chunk: chunks.indexOf(chunk),bucketName,};- return lambdaClient.send(+ const callingLambdaTimer = timer('Calling lambda');+ await lambdaClient.send(new InvokeCommand({FunctionName: process.env.AWS_LAMBDA_FUNCTION_NAME,// @ts-expect-errorPayload: JSON.stringify(payload),+ InvocationType: 'Event',}));+ callingLambdaTimer.end();}));- console.log('invoked x. watch out at ', bucketName);- await concatVideos(s3Client, bucketName, chunkCount);+ const out = await concatVideos(s3Client, bucketName, chunkCount);+ 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}`+ );} else if (params.type === 'renderer') {if (typeof params.chunk !== 'number') {throw new Error('must pass chunk');@@ -90,7 +108,7 @@ export const handler = async (params: LambdaPayload) => {durationInFrames: totalFrames,fps: 30,height: 1080,- width: 1080,+ width: 1920,},imageFormat: 'jpeg',inputProps: {},@@ -137,6 +155,7 @@ export const handler = async (params: LambdaPayload) => {Body: fs.createReadStream(outputLocation),}));+ unlinkSync(outputLocation);console.log('Done rendering!', outputDir, params.bucketName);} else {throw new Error('Command not found');diff --git a/packages/renderer/src/render.ts b/packages/renderer/src/render.tsindex a28754bbac..40e2df56c4 100644--- a/packages/renderer/src/render.ts+++ b/packages/renderer/src/render.ts@@ -94,12 +94,10 @@ export const renderFrames = async ({const pool = new Pool(puppeteerPages);const frameCount = getFrameCount(config.durationInFrames, frameRange ?? null);+ const lastFrameIndex = getFrameToRender(frameRange ?? null, frameCount - 1);// Substract one because 100 frames will be 00-99// --> 2 digits- let filePadLength = 0;- if (frameCount) {- filePadLength = String(frameCount - 1).length;- }+ const filePadLength = String(lastFrameIndex).length;let framesRendered = 0;onStart({