Skip to content

🎥 Make videos programmatically with React

combine videos API

Jonny Burgerad78383b2e95
diff --git a/packages/lambda/src/concat-videos.ts b/packages/lambda/src/concat-videos.ts
new file mode 100644
index 0000000000..3ac13f7ad0
--- /dev/null
+++ b/packages/lambda/src/concat-videos.ts
@@ -0,0 +1,58 @@
+import {
+ GetObjectCommand,
+ ListObjectsCommand,
+ S3Client,
+} from '@aws-sdk/client-s3';
+import {combineVideos} from '@remotion/renderer';
+import {createWriteStream, existsSync, mkdirSync, rmdirSync} from 'fs';
+import {join} from 'path';
+import {Readable} from 'stream';
+import xns from 'xns';
+import {REGION} from './constants';
+
+export const concatVideos = xns(
+ async (
+ s3Client: S3Client = new S3Client({region: REGION}),
+ bucket = 'remotion-renders-0.7182592846197402'
+ ) => {
+ const filesInBucket = await s3Client.send(
+ new ListObjectsCommand({
+ Bucket: bucket,
+ })
+ );
+ if (!filesInBucket.Contents) {
+ throw new Error('No contents');
+ }
+ const outdir = join(process.cwd(), 'bucket');
+ if (existsSync(outdir)) {
+ rmdirSync(outdir, {
+ recursive: true,
+ });
+ }
+ mkdirSync(outdir);
+ for (const file of filesInBucket.Contents) {
+ const {Body} = await s3Client.send(
+ new GetObjectCommand({
+ Bucket: bucket,
+ Key: file.Key,
+ })
+ );
+ const outpath = join(outdir, file.Key as string);
+ await new Promise<void>((resolve, reject) => {
+ (Body as Readable)
+ .pipe(createWriteStream(outpath))
+ .on('error', (err) => reject(err))
+ .on('close', () => resolve());
+ });
+ }
+
+ const files = filesInBucket.Contents.map((file) =>
+ join(outdir, file.Key as string)
+ );
+
+ const outfile = join(__dirname, 'concat.mp4');
+ await combineVideos(files, outfile);
+
+ console.log(outfile);
+ }
+);
diff --git a/packages/renderer/src/combine-videos.ts b/packages/renderer/src/combine-videos.ts
new file mode 100644
index 0000000000..788431583b
--- /dev/null
+++ b/packages/renderer/src/combine-videos.ts
@@ -0,0 +1,30 @@
+// Combine multiple video chunks, useful for decentralized rendering
+
+import execa from 'execa';
+import {mkdtempSync, unlinkSync, writeFileSync} from 'fs';
+import {join} from 'path';
+
+export const combineVideos = async (files: string[], output: string) => {
+ const fileList = files.map((p) => `file '${p}'`).join('\n');
+
+ const fileListTxt = join(mkdtempSync('remotion-filelist'), 'files.txt');
+ writeFileSync(fileListTxt, fileList);
+
+ try {
+ await execa('ffmpeg', [
+ '-f',
+ 'concat',
+ '-safe',
+ '0',
+ '-i',
+ fileListTxt,
+ '-c',
+ 'copy',
+ output,
+ ]);
+ unlinkSync(fileListTxt);
+ } catch (err) {
+ unlinkSync(fileListTxt);
+ throw err;
+ }
+};
diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts
index a9ddf18a7f..33b902847e 100644
--- a/packages/renderer/src/index.ts
+++ b/packages/renderer/src/index.ts
@@ -111,6 +111,7 @@ export const provideScreenshot = async ({
});
};
+export {combineVideos} from './combine-videos';
export * from './ffmpeg-flags';
export * from './get-compositions';
export * from './get-concurrency';