Skip to content

🎥 Make videos programmatically with React

getting there kinda

Jonny Burgerd864d9dbfe66
diff --git a/packages/lambda/src/concat-videos.ts b/packages/lambda/src/concat-videos.ts
index 3ac13f7ad0..512a91c10d 100644
--- a/packages/lambda/src/concat-videos.ts
+++ b/packages/lambda/src/concat-videos.ts
@@ -9,17 +9,21 @@ import {join} from 'path';
import {Readable} from 'stream';
import xns from 'xns';
import {REGION} from './constants';
+import {timer} from './timer';
export const concatVideos = xns(
async (
s3Client: S3Client = new S3Client({region: REGION}),
- bucket = 'remotion-renders-0.7182592846197402'
+ bucket = 'remotion-renders-0.7182592846197402',
+ expectedFiles = 20
) => {
+ const lsTimer = timer('Listing files');
const filesInBucket = await s3Client.send(
new ListObjectsCommand({
Bucket: bucket,
})
);
+ lsTimer();
if (!filesInBucket.Contents) {
throw new Error('No contents');
}
@@ -31,6 +35,7 @@ export const concatVideos = xns(
}
mkdirSync(outdir);
for (const file of filesInBucket.Contents) {
+ const end = timer(`Download ${file.Key}`);
const {Body} = await s3Client.send(
new GetObjectCommand({
Bucket: bucket,
@@ -44,6 +49,7 @@ export const concatVideos = xns(
.on('error', (err) => reject(err))
.on('close', () => resolve());
});
+ end();
}
const files = filesInBucket.Contents.map((file) =>
@@ -51,7 +57,9 @@ export const concatVideos = xns(
);
const outfile = join(__dirname, 'concat.mp4');
+ const combine = timer('Combine videos');
await combineVideos(files, outfile);
+ combine();
console.log(outfile);
}
diff --git a/packages/lambda/src/timer.ts b/packages/lambda/src/timer.ts
new file mode 100644
index 0000000000..c7ccbf800f
--- /dev/null
+++ b/packages/lambda/src/timer.ts
@@ -0,0 +1,23 @@
+import chalk from 'chalk';
+
+const formatTime = (time: number) => {
+ if (time < 200) {
+ return chalk.green(time + 'ms');
+ }
+ if (time < 500) {
+ return chalk.yellow(time + 'ms');
+ }
+ return chalk.red(time + 'ms');
+};
+
+export const timer = (label: string) => {
+ const start = Date.now();
+ process.stdout.write(label + '...');
+
+ return () => {
+ const end = Date.now();
+ const time = end - start;
+
+ process.stdout.write(` ${formatTime(time)}\n`);
+ };
+};
diff --git a/packages/renderer/src/combine-videos.ts b/packages/renderer/src/combine-videos.ts
index 788431583b..0b0ea34733 100644
--- a/packages/renderer/src/combine-videos.ts
+++ b/packages/renderer/src/combine-videos.ts
@@ -1,13 +1,14 @@
// Combine multiple video chunks, useful for decentralized rendering
import execa from 'execa';
-import {mkdtempSync, unlinkSync, writeFileSync} from 'fs';
+import {mkdtempSync, rmdirSync, 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');
+ const filelistDir = mkdtempSync('remotion-filelist');
+ const fileListTxt = join(filelistDir, 'files.txt');
writeFileSync(fileListTxt, fileList);
try {
@@ -20,11 +21,12 @@ export const combineVideos = async (files: string[], output: string) => {
fileListTxt,
'-c',
'copy',
+ '-y',
output,
]);
- unlinkSync(fileListTxt);
+ rmdirSync(filelistDir, {recursive: true});
} catch (err) {
- unlinkSync(fileListTxt);
+ rmdirSync(filelistDir, {recursive: true});
throw err;
}
};