Skip to content

🎥 Make videos programmatically with React

cleanup chunks

Jonny Burger283cc82974a1
diff --git a/packages/cli/src/lambda-command.ts b/packages/cli/src/lambda-command.ts
index 331a189f40..58b7ea280b 100644
--- a/packages/cli/src/lambda-command.ts
+++ b/packages/cli/src/lambda-command.ts
@@ -12,8 +12,8 @@ export const lambdaCommand = async () => {
} catch (err) {
Log.error(err);
Log.error('Remotion Lambda is not installed.');
- Log.info('You can install it using:');
Log.info('');
+ Log.info('You can install it using:');
Log.info('npm i @remotion/lambda');
process.exit(1);
}
diff --git a/packages/lambda/src/api/clean-items.ts b/packages/lambda/src/api/clean-items.ts
index 818640f8b2..ec568265da 100644
--- a/packages/lambda/src/api/clean-items.ts
+++ b/packages/lambda/src/api/clean-items.ts
@@ -1,4 +1,4 @@
-import {DeleteObjectCommand, _Object} from '@aws-sdk/client-s3';
+import {DeleteObjectCommand} from '@aws-sdk/client-s3';
import pLimit from 'p-limit';
import {AwsRegion} from '../pricing/aws-regions';
import {getS3Client} from '../shared/aws-clients';
@@ -14,7 +14,7 @@ export const cleanItems = async ({
}: {
bucket: string;
region: AwsRegion;
- list: _Object[];
+ list: string[];
onBeforeItemDeleted: (data: {bucketName: string; itemName: string}) => void;
onAfterItemDeleted: (data: {bucketName: string; itemName: string}) => void;
}) => {
@@ -23,17 +23,17 @@ export const cleanItems = async ({
limit(async () => {
onBeforeItemDeleted({
bucketName: bucket,
- itemName: object.Key as string,
+ itemName: object,
});
await getS3Client(region).send(
new DeleteObjectCommand({
Bucket: bucket,
- Key: object.Key,
+ Key: object,
})
);
onAfterItemDeleted({
bucketName: bucket,
- itemName: object.Key as string,
+ itemName: object,
});
})
)
diff --git a/packages/lambda/src/cleanup/s3-buckets.ts b/packages/lambda/src/cleanup/s3-buckets.ts
index e07fcf0b00..81fa348f2d 100644
--- a/packages/lambda/src/cleanup/s3-buckets.ts
+++ b/packages/lambda/src/cleanup/s3-buckets.ts
@@ -26,7 +26,7 @@ const cleanBucket = async ({
});
while (list.length > 0) {
await cleanItems({
- list,
+ list: list.map((l) => l.Key as string),
region,
bucket,
onAfterItemDeleted,
diff --git a/packages/lambda/src/cli/commands/sites/rm.ts b/packages/lambda/src/cli/commands/sites/rm.ts
index 5cb86fcef2..0f85affb12 100644
--- a/packages/lambda/src/cli/commands/sites/rm.ts
+++ b/packages/lambda/src/cli/commands/sites/rm.ts
@@ -54,7 +54,7 @@ export const sitesRmSubcommand = async (args: string[]) => {
return a + (b.Size ?? 0);
}, 0);
await cleanItems({
- list: files,
+ list: files.map((f) => f.Key as string),
bucket: remotionBuckets[0].Name as string,
onAfterItemDeleted: ({itemName}) => {
Log.info(CliInternals.chalk.gray(`Deleted ${itemName}`));
diff --git a/packages/lambda/src/functions/get-progress.ts b/packages/lambda/src/functions/get-progress.ts
index c290022ee1..ee5c3b54b9 100644
--- a/packages/lambda/src/functions/get-progress.ts
+++ b/packages/lambda/src/functions/get-progress.ts
@@ -18,6 +18,7 @@ import {
} from '../shared/constants';
import {parseLambdaTimingsKey} from '../shared/parse-lambda-timings-key';
import {streamToString} from '../shared/stream-to-string';
+import {getCleanupProgress} from './helpers/get-cleanup-progress';
import {getCurrentRegion} from './helpers/get-current-region';
import {inspectErrors} from './helpers/inspect-errors';
import {lambdaLs, lambdaReadFile} from './helpers/io';
@@ -42,7 +43,7 @@ const getFinalEncodingStatus = ({
if (outputFileExists) {
return {
- framesEncoded: renderMetadata.totalFrames,
+ framesEncoded: renderMetadata.videoConfig.durationInFrames,
};
}
@@ -171,6 +172,7 @@ export const progressHandler = async (
parseLambdaTimingsKey(f.Key as string)
);
+ // TODO: Should also calculate invoker functions, and main function
const totalEncodingTimings = parsedTimings
.map((p) => p.end - p.start)
.reduce((a, b) => a + b, 0);
@@ -240,7 +242,7 @@ export const progressHandler = async (
}).toPrecision(5)
);
- const outputFile = () => {
+ const getOutputFile = () => {
if (!output) {
return null;
}
@@ -254,9 +256,15 @@ export const progressHandler = async (
}/${outName(lambdaParams.renderId, renderMetadata.codec)}`;
};
+ const cleanup = getCleanupProgress({
+ chunkCount: renderMetadata?.totalChunks ?? 0,
+ chunks,
+ output: getOutputFile(),
+ });
+
return {
chunks: chunks.length,
- done: Boolean(output),
+ done: Boolean(output) && cleanup?.done,
encodingStatus: getFinalEncodingStatus({
encodingStatus,
outputFileExists: Boolean(output),
@@ -275,12 +283,13 @@ export const progressHandler = async (
renderId: lambdaParams.renderId,
renderMetadata,
bucket: lambdaParams.bucketName,
- outputFile: outputFile(),
+ outputFile: getOutputFile(),
timeToFinish,
errors: errorExplanations,
fatalErrorEncountered: errorExplanations.some(isFatalError),
currentTime: Date.now(),
bucketSize,
lambdasInvoked,
+ cleanup,
};
};
diff --git a/packages/lambda/src/functions/helpers/delete-chunks.ts b/packages/lambda/src/functions/helpers/delete-chunks.ts
new file mode 100644
index 0000000000..7ad46fae9f
--- /dev/null
+++ b/packages/lambda/src/functions/helpers/delete-chunks.ts
@@ -0,0 +1,30 @@
+import {AwsRegion} from '../..';
+import {cleanItems} from '../../api/clean-items';
+import {chunkKeyForIndex} from '../../shared/constants';
+
+export const deleteChunks = async ({
+ renderId,
+ chunkCount,
+ bucket,
+ region,
+}: {
+ renderId: string;
+ chunkCount: number;
+ bucket: string;
+ region: AwsRegion;
+}) => {
+ const toDelete = new Array(chunkCount).fill(true).map((x, i) =>
+ chunkKeyForIndex({
+ index: i,
+ renderId,
+ })
+ );
+
+ await cleanItems({
+ bucket,
+ region,
+ list: toDelete,
+ onAfterItemDeleted: () => undefined,
+ onBeforeItemDeleted: () => undefined,
+ });
+};
diff --git a/packages/lambda/src/functions/helpers/get-cleanup-progress.ts b/packages/lambda/src/functions/helpers/get-cleanup-progress.ts
new file mode 100644
index 0000000000..1390f5a6a4
--- /dev/null
+++ b/packages/lambda/src/functions/helpers/get-cleanup-progress.ts
@@ -0,0 +1,25 @@
+import {_Object} from '@aws-sdk/client-s3';
+
+export const getCleanupProgress = ({
+ chunks,
+ output,
+ chunkCount,
+}: {
+ chunks: _Object[];
+ output: string | null;
+ chunkCount: number;
+}): null | {
+ filesDeleted: number;
+ done: boolean;
+} => {
+ if (output === null) {
+ return null;
+ }
+
+ const filesDeleted = chunkCount - chunks.length;
+
+ return {
+ filesDeleted,
+ done: chunks.length === 0,
+ };
+};
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 731ee9367b..5426ccb2ba 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -27,6 +27,7 @@ import {
import {writeTimingProfile} from './chunk-optimization/write-profile';
import {deleteTmpDir} from './helpers/clean-tmpdir';
import {concatVideosS3} from './helpers/concat-videos';
+import {deleteChunks} from './helpers/delete-chunks';
import {
closeBrowser,
getBrowserInstance,
@@ -274,6 +275,12 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {
siteId: getServeUrlHash(params.serveUrl),
region: getCurrentRegion(),
});
+ await deleteChunks({
+ region: getCurrentRegion(),
+ renderId: params.renderId,
+ bucket: params.bucketName,
+ chunkCount,
+ });
};
export const launchHandler = async (
diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.ts
index 3579fe71b9..22efc13996 100644
--- a/packages/lambda/src/functions/renderer.ts
+++ b/packages/lambda/src/functions/renderer.ts
@@ -8,7 +8,7 @@ import fs from 'fs';
import path from 'path';
import {getLambdaClient} from '../shared/aws-clients';
import {
- chunkKey,
+ chunkKeyForIndex,
DOWNLOADS_DIR,
lambdaInitializedKey,
LambdaPayload,
@@ -182,7 +182,10 @@ const renderHandler = async (params: LambdaPayload, options: Options) => {
await lambdaWriteFile({
bucketName: params.bucketName,
- key: `${chunkKey(params.renderId)}${String(params.chunk).padStart(8, '0')}`,
+ key: chunkKeyForIndex({
+ renderId: params.renderId,
+ index: params.chunk,
+ }),
body: fs.createReadStream(outputLocation),
region: getCurrentRegion(),
acl: 'public-read',
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index 3bd9657252..c826aa8089 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -48,6 +48,13 @@ export const lambdaTimingsKey = ({
)}/chunk:${chunk}-start:${start}-end:${end}.txt`;
export const chunkKey = (renderId: string) =>
`${rendersPrefix(renderId)}/chunks/chunk-`;
+export const chunkKeyForIndex = ({
+ renderId,
+ index,
+}: {
+ renderId: string;
+ index: number;
+}) => `${chunkKey(renderId)}${String(index).padStart(8, '0')}`;
export const timingProfileName = (renderId: string) =>
`${rendersPrefix(renderId)}/timing-profile`;
export const getErrorKeyPrefix = (renderId: string) =>