Skip to content

🎥 Make videos programmatically with React

encoding progress

Jonny Burger511a5d593ba2
diff --git a/packages/lambda/src/concat-videos.ts b/packages/lambda/src/concat-videos.ts
index 8b3f0833df..5ad66f5c2a 100644
--- a/packages/lambda/src/concat-videos.ts
+++ b/packages/lambda/src/concat-videos.ts
@@ -14,7 +14,6 @@ import {
import {join} from 'path';
import {Readable} from 'stream';
import xns from 'xns';
-import {REGION} from './constants';
import {timer} from './timer';
import {tmpDir} from './tmpdir';
@@ -148,25 +147,23 @@ const getAllFilesS3 = async ({
});
};
-export const concatVideosS3 = async (
- s3Client: S3Client = new S3Client({region: REGION}),
- bucket = 'remotion-renders-0.7182592846197402',
- expectedFiles = 20
-) => {
- const outdir = join(tmpDir('remotion-concat'), 'bucket');
- if (existsSync(outdir)) {
- rmdirSync(outdir, {
- recursive: true,
- });
- }
-
- mkdirSync(outdir);
- const files = await getAllFilesS3({
- s3Client,
- bucket,
+export const concatVideos = async ({
+ efsRemotionVideoPath,
+ efsRemotionVideoRenderDone,
+ expectedFiles,
+}: {
+ efsRemotionVideoPath: string;
+ efsRemotionVideoRenderDone: string;
+ expectedFiles: number;
+ onProgress: (progress: number) => void;
+}) => {
+ const getAllTimes = timer('get all files');
+ const files = await getAllFiles({
+ efsRemotionVideoPath,
expectedFiles,
- outdir,
+ efsRemotionVideoRenderDone,
});
+ getAllTimes.end();
const outfile = join(tmpDir('remotion-concated'), 'concat.mp4');
const combine = timer('Combine videos');
@@ -175,27 +172,41 @@ export const concatVideosS3 = async (
files,
filelistDir,
output: outfile,
+ onProgress,
});
combine.end();
- rmdirSync(outdir, {
+ rmdirSync(efsRemotionVideoPath, {
recursive: true,
});
return outfile;
};
-export const concatVideos = async (
- efsRemotionVideoPath: string,
- efsRemotionVideoRenderDone: string,
- expectedFiles = 20
-) => {
- const getAllTimes = timer('get all files');
- const files = await getAllFiles({
- efsRemotionVideoPath,
+export const concatVideosS3 = async ({
+ s3Client,
+ bucket,
+ expectedFiles,
+ onProgress,
+}: {
+ s3Client: S3Client;
+ bucket: string;
+ expectedFiles: number;
+ onProgress: (frames: number) => void;
+}) => {
+ const outdir = join(tmpDir('remotion-concat'), 'bucket');
+ if (existsSync(outdir)) {
+ rmdirSync(outdir, {
+ recursive: true,
+ });
+ }
+
+ mkdirSync(outdir);
+ const files = await getAllFilesS3({
+ s3Client,
+ bucket,
expectedFiles,
- efsRemotionVideoRenderDone,
+ outdir,
});
- getAllTimes.end();
const outfile = join(tmpDir('remotion-concated'), 'concat.mp4');
const combine = timer('Combine videos');
@@ -204,10 +215,11 @@ export const concatVideos = async (
files,
filelistDir,
output: outfile,
+ onProgress,
});
combine.end();
- rmdirSync(efsRemotionVideoPath, {
+ rmdirSync(outdir, {
recursive: true,
});
return outfile;
diff --git a/packages/lambda/src/constants.ts b/packages/lambda/src/constants.ts
index a72a0810dc..01d0aa76ac 100644
--- a/packages/lambda/src/constants.ts
+++ b/packages/lambda/src/constants.ts
@@ -7,6 +7,7 @@ export const RENDER_FN_PREFIX = 'remotion-render-test-';
export const RENDER_STITCHER_PREFIX = 'remotion-stitcher-test-';
export const EFS_MOUNT_PATH = '/mnt/efs';
export const ENABLE_EFS = false;
+export const ENCODING_PROGRESS_KEY = 'encoding-progress.json';
export enum LambdaRoutines {
start = 'start',
@@ -58,3 +59,7 @@ export type LambdaPayloads = {
};
export type LambdaPayload = LambdaPayloads[LambdaRoutines];
+
+export type EncodingProgress = {
+ framesRendered: number;
+};
diff --git a/packages/lambda/src/get-progress.ts b/packages/lambda/src/get-progress.ts
index d07d143b86..2be7db3693 100644
--- a/packages/lambda/src/get-progress.ts
+++ b/packages/lambda/src/get-progress.ts
@@ -1,11 +1,19 @@
-import {ListObjectsV2Command} from '@aws-sdk/client-s3';
+import {GetObjectCommand, ListObjectsV2Command} from '@aws-sdk/client-s3';
+import {Readable} from 'stream';
import {s3Client} from './aws-clients';
-import {LambdaPayload, LambdaRoutines} from './constants';
+import {
+ EncodingProgress,
+ ENCODING_PROGRESS_KEY,
+ LambdaPayload,
+ LambdaRoutines,
+} from './constants';
+import {streamToString} from './stream-to-string';
export const progressHandler = async (lambdaParams: LambdaPayload) => {
if (lambdaParams.type !== LambdaRoutines.status) {
throw new TypeError('Expected status type');
}
+
const list = await s3Client.send(
new ListObjectsV2Command({
Bucket: lambdaParams.bucketName,
@@ -19,8 +27,25 @@ export const progressHandler = async (lambdaParams: LambdaPayload) => {
const chunks = contents.filter((c) => c.Key?.match(/chunk(.*).mp4/));
// TODO: out.mp4 is hardcodec
const output = contents.find((c) => c.Key?.includes('out.mp4'));
+
+ const framesExists = contents.find((c) => c.Key === ENCODING_PROGRESS_KEY)
+ ? await s3Client.send(
+ new GetObjectCommand({
+ Bucket: lambdaParams.bucketName,
+ Key: ENCODING_PROGRESS_KEY,
+ })
+ )
+ : null;
+
+ const frameResponse = framesExists
+ ? (JSON.parse(
+ await streamToString(framesExists.Body as Readable)
+ ) as EncodingProgress)
+ : null;
+
return {
chunks: chunks.length,
done: Boolean(output),
+ frameResponse,
};
};
diff --git a/packages/lambda/src/launch.ts b/packages/lambda/src/launch.ts
index b5a52b5705..1dccf5780f 100644
--- a/packages/lambda/src/launch.ts
+++ b/packages/lambda/src/launch.ts
@@ -7,6 +7,8 @@ import {concatVideos, concatVideosS3} from './concat-videos';
import {
EFS_MOUNT_PATH,
ENABLE_EFS,
+ EncodingProgress,
+ ENCODING_PROGRESS_KEY,
LambdaPayload,
LambdaRoutines,
REGION,
@@ -19,6 +21,7 @@ export const launchHandler = async (params: LambdaPayload) => {
if (params.type !== LambdaRoutines.launch) {
throw new Error('Expected launch type');
}
+
const efsRemotionVideoRenderDone = EFS_MOUNT_PATH + '/render-done';
const efsRemotionVideoPath = EFS_MOUNT_PATH + '/remotion-video';
@@ -26,10 +29,12 @@ export const launchHandler = async (params: LambdaPayload) => {
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');
@@ -38,10 +43,12 @@ export const launchHandler = async (params: LambdaPayload) => {
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({
@@ -98,13 +105,33 @@ export const launchHandler = async (params: LambdaPayload) => {
})
);
reqSend.end();
+
+ const onProgress = (framesRendered: number) => {
+ const encodingProgress: EncodingProgress = {
+ framesRendered,
+ };
+ s3Client.send(
+ new PutObjectCommand({
+ Bucket: params.bucketName,
+ Key: ENCODING_PROGRESS_KEY,
+ Body: JSON.stringify(encodingProgress),
+ })
+ );
+ };
+
const out = ENABLE_EFS
- ? await concatVideos(
+ ? await concatVideos({
efsRemotionVideoPath,
efsRemotionVideoRenderDone,
- chunkCount
- )
- : await concatVideosS3(s3Client, params.bucketName, lambdaPayloads.length);
+ expectedFiles: chunkCount,
+ onProgress,
+ })
+ : await concatVideosS3({
+ s3Client,
+ bucket: params.bucketName,
+ expectedFiles: lambdaPayloads.length,
+ onProgress,
+ });
const outName = 'out.mp4';
await s3Client.send(
new PutObjectCommand({
diff --git a/packages/lambda/src/run.ts b/packages/lambda/src/run.ts
index 6cc8fac898..1232a6c53a 100644
--- a/packages/lambda/src/run.ts
+++ b/packages/lambda/src/run.ts
@@ -26,7 +26,7 @@ xns(async () => {
for (let i = 0; i < 30; i++) {
await sleep(1000);
const status = await checkLambdaStatus(functionName, res.bucketName);
- console.log('');
+ console.log(status);
if (status.done) {
console.log('Done! ' + res.bucketName);
process.exit(0);
diff --git a/packages/lambda/src/stream-to-string.ts b/packages/lambda/src/stream-to-string.ts
new file mode 100644
index 0000000000..32986a97b3
--- /dev/null
+++ b/packages/lambda/src/stream-to-string.ts
@@ -0,0 +1,10 @@
+import {Readable} from 'stream';
+
+export function streamToString(stream: Readable) {
+ const chunks: Buffer[] = [];
+ return new Promise<string>((resolve, reject) => {
+ stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
+ stream.on('error', (err) => reject(err));
+ stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
+ });
+}
diff --git a/packages/renderer/src/combine-videos.ts b/packages/renderer/src/combine-videos.ts
index 74efec2e35..1eced56cf6 100644
--- a/packages/renderer/src/combine-videos.ts
+++ b/packages/renderer/src/combine-videos.ts
@@ -3,15 +3,18 @@
import execa from 'execa';
import {rmdirSync, writeFileSync} from 'fs';
import {join} from 'path';
+import {parseFfmpegProgress} from './parse-ffmpeg-progress';
export const combineVideos = async ({
files,
filelistDir,
output,
+ onProgress,
}: {
files: string[];
filelistDir: string;
output: string;
+ onProgress: (progress: number) => void;
}) => {
const fileList = files.map((p) => `file '${p}'`).join('\n');
@@ -20,7 +23,7 @@ export const combineVideos = async ({
writeFileSync(fileListTxt, fileList);
try {
- await execa('ffmpeg', [
+ const task = execa('ffmpeg', [
'-f',
'concat',
'-safe',
@@ -32,6 +35,16 @@ export const combineVideos = async ({
'-y',
output,
]);
+ task.stderr?.on('data', (data: Buffer) => {
+ if (onProgress) {
+ const parsed = parseFfmpegProgress(data.toString());
+ if (parsed !== undefined) {
+ onProgress(parsed);
+ }
+ }
+ });
+
+ await task;
rmdirSync(filelistDir, {recursive: true});
} catch (err) {
rmdirSync(filelistDir, {recursive: true});