Skip to content

🎥 Make videos programmatically with React

concat asynchronously

Jonny Burgercbf69a18a4a4
diff --git a/packages/lambda/src/concat-videos.ts b/packages/lambda/src/concat-videos.ts
index 512a91c10d..42fab11403 100644
--- a/packages/lambda/src/concat-videos.ts
+++ b/packages/lambda/src/concat-videos.ts
@@ -11,50 +11,105 @@ 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',
- expectedFiles = 20
- ) => {
+const downloadS3File = async ({
+ s3Client,
+ bucket,
+ content,
+ outdir,
+}: {
+ s3Client: S3Client;
+ bucket: string;
+ content: string;
+ outdir: string;
+}) => {
+ const {Body} = await s3Client.send(
+ new GetObjectCommand({
+ Bucket: bucket,
+ Key: content,
+ })
+ );
+ const outpath = join(outdir, content);
+ return new Promise<void>((resolve, reject) => {
+ (Body as Readable)
+ .pipe(createWriteStream(outpath))
+ .on('error', (err) => reject(err))
+ .on('close', () => resolve());
+ });
+};
+
+const getAllFiles = async (
+ s3Client: S3Client,
+ bucket: string,
+ expectedFiles: number
+): 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 filesInBucket = await s3Client.send(
+ const files = await s3Client.send(
new ListObjectsCommand({
Bucket: bucket,
})
);
lsTimer();
- 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 end = timer(`Download ${file.Key}`);
- 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());
+ return (files.Contents || []).map((_) => _.Key as string);
+ };
+
+ return new Promise<string[]>((resolve, reject) => {
+ const loop = async () => {
+ const filesInBucket = await getFiles();
+ const checkFinish = () => {
+ const areAllFilesDownloaded =
+ Object.keys(downloaded).length === expectedFiles;
+ if (areAllFilesDownloaded) {
+ resolve(filesInBucket.map((file) => join(outdir, file)));
+ }
+ };
+ filesInBucket.forEach(async (content) => {
+ alreadyDownloading[content] = true;
+ try {
+ await downloadS3File({
+ bucket,
+ content,
+ outdir,
+ s3Client,
+ });
+ downloaded[content] = true;
+ checkFinish();
+ } catch (err) {
+ reject(err);
+ }
});
- end();
- }
- const files = filesInBucket.Contents.map((file) =>
- join(outdir, file.Key as string)
- );
+ const areAllFilesDownloading =
+ Object.keys(alreadyDownloading).length === expectedFiles;
+ if (!areAllFilesDownloading) {
+ setTimeout(() => {
+ loop();
+ }, 300);
+ }
+ };
+
+ loop();
+ });
+};
+
+export const concatVideos = xns(
+ async (
+ s3Client: S3Client = new S3Client({region: REGION}),
+ bucket = 'remotion-renders-0.7182592846197402',
+ expectedFiles = 20
+ ) => {
+ const files = await getAllFiles(s3Client, bucket, expectedFiles);
const outfile = join(__dirname, 'concat.mp4');
const combine = timer('Combine videos');