Skip to content

🎥 Make videos programmatically with React

`@remotion/lambda`: Implement `estimatedBillingDurationInMilliseconds ` in getRenderProgress()

Jonny Burger90b18d41e32e
diff --git a/packages/docs/docs/lambda/getrenderprogress.md b/packages/docs/docs/lambda/getrenderprogress.md
index afa6adae47..b533ce954c 100644
--- a/packages/docs/docs/lambda/getrenderprogress.md
+++ b/packages/docs/docs/lambda/getrenderprogress.md
@@ -74,10 +74,6 @@ How many chunks have been fully rendered so far.
Either `null` if not all chunks have been rendered yet or an object with the signature `{framesEncoded: number}` that tells how many frames have been stitched together so far in the concatenation process.
-### `costs`
-
-Non-binding information about how much the render is costing. At the moment the calculation is still very inaccurate (on the conservative size, probably shows higher cost than real).
-
### `renderId`
Mirrors the `renderId` that has been passed as an input
@@ -151,6 +147,10 @@ An object describing the costs of the render so far. The cost may increase if th
- `displayCost`: The cost formatted as a string.
- `disclaimer`: Textual disclaimer removing any doubt that there is no guarantee.
+## `estimatedBillingDurationInMilliseconds`<AvailableFrom v="4.0.74"/>
+
+The estimated total runtime of all invoked Lambda functions combined, in milliseconds. As the render goes on, this number increases.
+
### `mostExpensiveFrameRanges`
If the render is in progress, this is `null`. If the render is done, it is an array of the 5 most expensive chunks in the following shape:
diff --git a/packages/lambda/src/functions/helpers/calculate-price-from-bucket.ts b/packages/lambda/src/functions/helpers/calculate-price-from-bucket.ts
index 20a2eb7030..d2c03ce95d 100644
--- a/packages/lambda/src/functions/helpers/calculate-price-from-bucket.ts
+++ b/packages/lambda/src/functions/helpers/calculate-price-from-bucket.ts
@@ -52,19 +52,22 @@ export const estimatePriceFromBucket = ({
.map(() => elapsedTime)
.reduce((a, b) => a + b, 0);
+ const estimatedBillingDurationInMilliseconds =
+ calculateChunkTimes({
+ contents,
+ renderId: renderMetadata.renderId,
+ type: 'combined-time-for-cost-calculation',
+ }) + timeElapsedOfUnfinished;
+
const accruedSoFar = Number(
estimatePrice({
region: getCurrentRegionInFunction(),
- durationInMilliseconds:
- calculateChunkTimes({
- contents,
- renderId: renderMetadata.renderId,
- type: 'combined-time-for-cost-calculation',
- }) + timeElapsedOfUnfinished,
+ durationInMilliseconds: estimatedBillingDurationInMilliseconds,
memorySizeInMb,
diskSizeInMb,
lambdasInvoked,
}).toPrecision(5),
);
- return accruedSoFar;
+
+ return {accruedSoFar, estimatedBillingDurationInMilliseconds};
};
diff --git a/packages/lambda/src/functions/helpers/create-post-render-data.ts b/packages/lambda/src/functions/helpers/create-post-render-data.ts
index 41ab96b298..17468a7b69 100644
--- a/packages/lambda/src/functions/helpers/create-post-render-data.ts
+++ b/packages/lambda/src/functions/helpers/create-post-render-data.ts
@@ -48,12 +48,12 @@ export const createPostRenderData = ({
parseLambdaTimingsKey(Key as string),
);
- const times = parsedTimings
+ const estimatedBillingDurationInMilliseconds = parsedTimings
.map((p) => p.rendered - p.start + OVERHEAD_TIME_PER_LAMBDA)
.reduce((a, b) => a + b);
const cost = estimatePrice({
- durationInMilliseconds: times,
+ durationInMilliseconds: estimatedBillingDurationInMilliseconds,
memorySizeInMb,
region,
lambdasInvoked: renderMetadata.estimatedTotalLambdaInvokations,
@@ -124,5 +124,6 @@ export const createPostRenderData = ({
renderMetadata.frameRange[1],
),
deleteAfter: renderMetadata.deleteAfter,
+ estimatedBillingDurationInMilliseconds,
};
};
diff --git a/packages/lambda/src/functions/helpers/get-progress.ts b/packages/lambda/src/functions/helpers/get-progress.ts
index 0eb9bf7a8c..7c6e700c2d 100644
--- a/packages/lambda/src/functions/helpers/get-progress.ts
+++ b/packages/lambda/src/functions/helpers/get-progress.ts
@@ -106,6 +106,8 @@ export const getProgress = async ({
timeToEncode: postRenderData.timeToEncode,
outputSizeInBytes: postRenderData.outputSize,
type: 'success',
+ estimatedBillingDurationInMilliseconds:
+ postRenderData.estimatedBillingDurationInMilliseconds,
};
}
@@ -160,18 +162,16 @@ export const getProgress = async ({
})
: null;
- const accruedSoFar = Number(
- estimatePriceFromBucket({
- contents,
- renderMetadata,
- memorySizeInMb,
- outputFileMetadata: outputFile,
- lambdasInvoked: renderMetadata?.estimatedRenderLambdaInvokations ?? 0,
- // We cannot determine the ephemeral storage size, so we
- // overestimate the price, but will only have a miniscule effect (~0.2%)
- diskSizeInMb: MAX_EPHEMERAL_STORAGE_IN_MB,
- }),
- );
+ const priceFromBucket = estimatePriceFromBucket({
+ contents,
+ renderMetadata,
+ memorySizeInMb,
+ outputFileMetadata: outputFile,
+ lambdasInvoked: renderMetadata?.estimatedRenderLambdaInvokations ?? 0,
+ // We cannot determine the ephemeral storage size, so we
+ // overestimate the price, but will only have a miniscule effect (~0.2%)
+ diskSizeInMb: MAX_EPHEMERAL_STORAGE_IN_MB,
+ });
const cleanup = getCleanupProgress({
chunkCount: renderMetadata?.totalChunks ?? 0,
@@ -278,7 +278,9 @@ export const getProgress = async ({
chunks: chunkCount,
done: false,
encodingStatus,
- costs: formatCostsInfo(accruedSoFar),
+ costs: priceFromBucket
+ ? formatCostsInfo(priceFromBucket.accruedSoFar)
+ : formatCostsInfo(0),
renderId,
renderMetadata,
bucket: bucketName,
@@ -323,6 +325,9 @@ export const getProgress = async ({
mostExpensiveFrameRanges: null,
timeToEncode: null,
outputSizeInBytes: outputFile?.size ?? null,
+ estimatedBillingDurationInMilliseconds: priceFromBucket
+ ? priceFromBucket.estimatedBillingDurationInMilliseconds
+ : null,
type: 'success',
};
};
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index e91ad299ae..3b8c65221c 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -487,6 +487,7 @@ export type PostRenderData = {
timeToRenderChunks: number;
retriesInfo: ChunkRetry[];
mostExpensiveFrameRanges: ExpensiveChunk[] | undefined;
+ estimatedBillingDurationInMilliseconds: number;
deleteAfter: DeleteAfter | null;
};
@@ -529,6 +530,7 @@ export type RenderProgress = {
framesRendered: number;
outputSizeInBytes: number | null;
type: 'success';
+ estimatedBillingDurationInMilliseconds: number | null;
};
export type Privacy = 'public' | 'private' | 'no-acl';