🎥 Make videos programmatically with React
concept
Jonny Burgerfe0279cbe5b7
diff --git a/packages/lambda/remotion-project/MyVideo.tsx b/packages/lambda/remotion-project/MyVideo.tsxindex 017bbc4ac6..1d687b0b28 100644--- a/packages/lambda/remotion-project/MyVideo.tsx+++ b/packages/lambda/remotion-project/MyVideo.tsx@@ -1,15 +1,48 @@-import {interpolate, useCurrentFrame, Video} from 'remotion';+import {AbsoluteFill, Sequence, useCurrentFrame, Video} from 'remotion';export const MyVideo: React.FC = () => {const frame = useCurrentFrame();return (- <Video- style={{- opacity: interpolate(frame, [0, 500], [1, 0], {- extrapolateRight: 'clamp',- }),- }}- src="https://test-videos.co.uk/vids/bigbuckbunny/webm/vp8/1080/Big_Buck_Bunny_1080_10s_2MB.webm"- />+ <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>+ <AbsoluteFill+ style={{+ justifyContent: 'center',+ alignItems: 'center',+ display: 'flex',+ }}+ >+ <div style={{color: 'white', fontSize: 400}}>{frame}</div>+ </AbsoluteFill>+ </div>);};diff --git a/packages/lambda/src/concat-videos.ts b/packages/lambda/src/concat-videos.tsindex 42fab11403..c2c20adc01 100644--- a/packages/lambda/src/concat-videos.ts+++ b/packages/lambda/src/concat-videos.ts@@ -10,6 +10,7 @@ import {Readable} from 'stream';import xns from 'xns';import {REGION} from './constants';import {timer} from './timer';+import {tmpDir} from './tmpdir';const downloadS3File = async ({s3Client,@@ -37,22 +38,20 @@ const downloadS3File = async ({});};-const getAllFiles = async (- s3Client: S3Client,- bucket: string,- expectedFiles: number-): Promise<string[]> => {+const getAllFiles = async ({+ s3Client,+ bucket,+ expectedFiles,+ outdir,+}: {+ s3Client: S3Client;+ bucket: string;+ expectedFiles: number;+ outdir: string;+}): Promise<string[]> => {const alreadyDownloading: {[key: string]: true} = {};const downloaded: {[key: string]: true} = {};- const outdir = join(process.cwd(), 'bucket');- if (existsSync(outdir)) {- rmdirSync(outdir, {- recursive: true,- });- }- mkdirSync(outdir);-const getFiles = async () => {const lsTimer = timer('Listing files');const files = await s3Client.send(@@ -75,6 +74,9 @@ const getAllFiles = async (}};filesInBucket.forEach(async (content) => {+ if (alreadyDownloading[content]) {+ return;+ }alreadyDownloading[content] = true;try {await downloadS3File({@@ -109,13 +111,29 @@ export const concatVideos = xns(bucket = 'remotion-renders-0.7182592846197402',expectedFiles = 20) => {- const files = await getAllFiles(s3Client, bucket, expectedFiles);+ const outdir = join(tmpDir('remotion-concat'), 'bucket');+ if (existsSync(outdir)) {+ rmdirSync(outdir, {+ recursive: true,+ });+ }+ mkdirSync(outdir);++ const files = await getAllFiles({s3Client, bucket, expectedFiles, outdir});- const outfile = join(__dirname, 'concat.mp4');+ const outfile = join(tmpDir('remotion-concated'), 'concat.mp4');const combine = timer('Combine videos');- await combineVideos(files, outfile);+ const filelistDir = tmpDir('remotion-filelist');+ await combineVideos({+ files,+ filelistDir,+ output: outfile,+ });combine();+ rmdirSync(outdir, {+ recursive: true,+ });console.log(outfile);});diff --git a/packages/lambda/src/tmpdir.ts b/packages/lambda/src/tmpdir.tsnew file mode 100644index 0000000000..645f14e2de--- /dev/null+++ b/packages/lambda/src/tmpdir.ts@@ -0,0 +1,12 @@+import {mkdirSync, mkdtempSync} from 'fs';++const isLambda = !!process.env.LAMBDA_TASK_ROOT;++export const tmpDir = (str: string) => {+ if (isLambda) {+ const dir = '/tmp/' + str + Math.random();+ mkdirSync(dir);+ return dir;+ }+ return mkdtempSync(str);+};diff --git a/packages/lambda/template/render.ts b/packages/lambda/template/render.tsindex b79d9a204c..9ca60d98d0 100644--- a/packages/lambda/template/render.ts+++ b/packages/lambda/template/render.ts@@ -11,6 +11,7 @@ import {} from '@remotion/renderer';import fs from 'fs';import path from 'path';+import {concatVideos} from '../src/concat-videos';import {LambdaPayload, REGION, RENDERS_BUCKET_PREFIX} from '../src/constants';import {executablePath} from './get-chromium-executable-path';@@ -50,7 +51,8 @@ export const handler = async (params: LambdaPayload) => {ACL: 'public-read',}));- const chunks = new Array(20).fill(1).map((_, i) => {+ const chunkCount = 20;+ const chunks = new Array(chunkCount).fill(1).map((_, i) => {return [i * 50, (i + 1) * 50 - 1] as [number, number];});await Promise.all(@@ -72,6 +74,7 @@ export const handler = async (params: LambdaPayload) => {}));console.log('invoked x. watch out at ', bucketName);+ await concatVideos(s3Client, bucketName, chunkCount);} else if (params.type === 'renderer') {if (typeof params.chunk !== 'number') {throw new Error('must pass chunk');@@ -120,7 +123,7 @@ export const handler = async (params: LambdaPayload) => {// TODOheight: 1080,//TODO,- width: 1080,+ width: 1920,outputLocation,// TODOcodec: 'h264',diff --git a/packages/renderer/src/combine-videos.ts b/packages/renderer/src/combine-videos.tsindex 0b0ea34733..789efb03a7 100644--- a/packages/renderer/src/combine-videos.ts+++ b/packages/renderer/src/combine-videos.ts@@ -1,13 +1,20 @@// Combine multiple video chunks, useful for decentralized renderingimport execa from 'execa';-import {mkdtempSync, rmdirSync, writeFileSync} from 'fs';+import {rmdirSync, writeFileSync} from 'fs';import {join} from 'path';-export const combineVideos = async (files: string[], output: string) => {+export const combineVideos = async ({+ files,+ filelistDir,+ output,+}: {+ files: string[];+ filelistDir: string;+ output: string;+}) => {const fileList = files.map((p) => `file '${p}'`).join('\n');- const filelistDir = mkdtempSync('remotion-filelist');const fileListTxt = join(filelistDir, 'files.txt');writeFileSync(fileListTxt, fileList);