π₯ Make videos programmatically with React
better cli progress
Jonny Burger3164a37b3aff
diff --git a/packages/lambda/remotionlambda.zip b/packages/lambda/remotionlambda.zipindex d8f163e078..cf1f1880b0 100644Binary files a/packages/lambda/remotionlambda.zip and b/packages/lambda/remotionlambda.zip differdiff --git a/packages/lambda/src/cli/commands/render/progress.ts b/packages/lambda/src/cli/commands/render/progress.tsindex d53637598f..3000d9349f 100644--- a/packages/lambda/src/cli/commands/render/progress.ts+++ b/packages/lambda/src/cli/commands/render/progress.ts@@ -1,4 +1,5 @@import {CliInternals} from '@remotion/cli';+import {Internals} from 'remotion';import {CleanupInfo, EncodingProgress, RenderProgress} from '../../../defaults';type LambdaInvokeProgress = {@@ -21,13 +22,17 @@ export type MultiRenderProgress = {};export const makeInvokeProgress = (- chunkProgress: LambdaInvokeProgress,+ invokeProgress: LambdaInvokeProgress,totalSteps: number) => {- const {lambdasInvoked, totalLambdas, doneIn} = chunkProgress;- const progress = totalLambdas === null ? 0 : lambdasInvoked / totalLambdas;+ const {lambdasInvoked, totalLambdas, doneIn} = invokeProgress;+ const progress = doneIn+ ? 1+ : totalLambdas === null+ ? 0+ : lambdasInvoked / totalLambdas;return [- 'π© ',+ 'βοΈ ',`(1/${totalSteps})`,CliInternals.makeProgressBar(progress),`${doneIn === null ? 'Invoking' : 'Invoked'} lambdas`,@@ -37,12 +42,23 @@ export const makeInvokeProgress = (].join(' ');};-export const makeChunkProgress = (- chunkProgress: ChunkProgress,- totalSteps: number-) => {+export const makeChunkProgress = ({+ chunkProgress,+ invokeProgress,+ totalSteps,+}: {+ chunkProgress: ChunkProgress;+ invokeProgress: LambdaInvokeProgress;+ totalSteps: number;+}) => {+ const lambdaIsDone = invokeProgress.doneIn !== null;const {chunksInvoked, totalChunks, doneIn} = chunkProgress;const progress = totalChunks === null ? 0 : chunksInvoked / totalChunks;+ const shouldShow = lambdaIsDone || progress > 0;+ if (!shouldShow) {+ return '';+ }+return ['π§©',`(2/${totalSteps})`,@@ -54,12 +70,23 @@ export const makeChunkProgress = (].join(' ');};-export const makeEncodingProgress = (- encodingProgress: EncodingProgress,- totalSteps: number-) => {+export const makeEncodingProgress = ({+ encodingProgress,+ chunkProgress,+ totalSteps,+}: {+ encodingProgress: EncodingProgress;+ chunkProgress: ChunkProgress;+ totalSteps: number;+}) => {const {framesEncoded, totalFrames, doneIn} = encodingProgress;const progress = totalFrames === null ? 0 : framesEncoded / totalFrames;+ const chunksDone = chunkProgress.doneIn !== null;+ const shouldShow = progress > 0 || chunksDone;+ if (!shouldShow) {+ return '';+ }+return ['π½ ',`(3/${totalSteps})`,@@ -120,6 +147,7 @@ export const makeMultiProgressFromStatus = (framesEncoded: status.encodingStatus?.framesEncoded ?? 0,totalFrames: status.renderMetadata?.videoConfig.durationInFrames ?? 1,doneIn: status.encodingStatus?.doneIn ?? null,+ timeToInvoke: status.encodingStatus?.timeToInvoke ?? null,},lambdaInvokeProgress: {doneIn: status.timeToInvokeLambdas,@@ -130,3 +158,31 @@ export const makeMultiProgressFromStatus = (cleanupInfo: status.cleanup,};};++export const makeProgressString = ({+ outName,+ progress,+ steps,+}: {+ outName: string | null;+ progress: MultiRenderProgress;+ steps: number;+}) => {+ return [+ makeInvokeProgress(progress.lambdaInvokeProgress, steps),+ makeChunkProgress({+ chunkProgress: progress.chunkProgress,+ invokeProgress: progress.lambdaInvokeProgress,+ totalSteps: steps,+ }),+ makeEncodingProgress({+ encodingProgress: progress.encodingProgress,+ chunkProgress: progress.chunkProgress,+ totalSteps: steps,+ }),+ makeCleanupProgress(progress.cleanupInfo, steps),+ outName ? makeDownloadProgess(steps, true) : null,+ ]+ .filter(Internals.truthy)+ .join('\n');+};diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.tsindex 7b4c029793..cdc783f5dd 100644--- a/packages/lambda/src/cli/commands/render/render.ts+++ b/packages/lambda/src/cli/commands/render/render.ts@@ -1,5 +1,4 @@import {CliInternals} from '@remotion/cli';-import {Internals} from 'remotion';import {downloadVideo} from '../../../api/download-video';import {getRenderProgress} from '../../../api/get-render-progress';import {renderVideoOnLambda} from '../../../api/render-video-on-lambda';@@ -12,14 +11,7 @@ import {getAwsRegion} from '../../get-aws-region';import {findFunctionName} from '../../helpers/find-function-name';import {formatBytes} from '../../helpers/format-bytes';import {Log} from '../../log';-import {- makeChunkProgress,- makeCleanupProgress,- makeDownloadProgess,- makeEncodingProgress,- makeInvokeProgress,- makeMultiProgressFromStatus,-} from './progress';+import {makeMultiProgressFromStatus, makeProgressString} from './progress';export const RENDER_COMMAND = 'render';@@ -86,12 +78,11 @@ export const renderCommand = async (args: string[]) => {});const multiProgress = makeMultiProgressFromStatus(status);progressBar.update(- [- makeInvokeProgress(multiProgress.lambdaInvokeProgress, totalSteps),- makeChunkProgress(multiProgress.chunkProgress, totalSteps),- makeEncodingProgress(multiProgress.encodingProgress, totalSteps),- makeCleanupProgress(multiProgress.cleanupInfo, totalSteps),- ].join('\n')+ makeProgressString({+ progress: multiProgress,+ outName,+ steps: totalSteps,+ }));for (let i = 0; i < 3000; i++) {@@ -105,27 +96,21 @@ export const renderCommand = async (args: string[]) => {CliInternals.Log.verbose(JSON.stringify(newStatus, null, 2));const newProgress = makeMultiProgressFromStatus(newStatus);progressBar.update(- [- makeInvokeProgress(newProgress.lambdaInvokeProgress, totalSteps),- makeChunkProgress(newProgress.chunkProgress, totalSteps),- makeEncodingProgress(newProgress.encodingProgress, totalSteps),- makeCleanupProgress(newProgress.cleanupInfo, totalSteps),- ]- .filter(Internals.truthy)- .join('\n')+ makeProgressString({+ outName,+ progress: newProgress,+ steps: totalSteps,+ }));// Log.info(newStatus);if (newStatus.done) {progressBar.update(- [- makeInvokeProgress(newProgress.lambdaInvokeProgress, totalSteps),- makeChunkProgress(newProgress.chunkProgress, totalSteps),- makeEncodingProgress(newProgress.encodingProgress, totalSteps),- makeCleanupProgress(newProgress.cleanupInfo, totalSteps),- ]- .filter(Internals.truthy)- .join('\n')+ makeProgressString({+ outName,+ progress: newProgress,+ steps: totalSteps,+ }));if (outName) {const {outputPath, size} = await downloadVideo({@@ -135,15 +120,11 @@ export const renderCommand = async (args: string[]) => {renderId: res.renderId,});progressBar.update(- [- makeInvokeProgress(newProgress.lambdaInvokeProgress, totalSteps),- makeChunkProgress(newProgress.chunkProgress, totalSteps),- makeEncodingProgress(newProgress.encodingProgress, totalSteps),- makeCleanupProgress(newProgress.cleanupInfo, totalSteps),- outName ? makeDownloadProgess(totalSteps, true) : null,- ]- .filter(Internals.truthy)- .join('\n')+ makeProgressString({+ outName,+ progress: newProgress,+ steps: totalSteps,+ }));Log.info();Log.info();diff --git a/packages/lambda/src/functions/helpers/delete-chunks.ts b/packages/lambda/src/functions/helpers/delete-chunks.tsindex 71a41fd0ee..d60cd0709e 100644--- a/packages/lambda/src/functions/helpers/delete-chunks.ts+++ b/packages/lambda/src/functions/helpers/delete-chunks.ts@@ -3,7 +3,7 @@ import {AwsRegion} from '../..';import {cleanItems} from '../../api/clean-items';import {CleanupJob} from './get-files-to-delete';-export const deleteChunks = async ({+export const cleanupFiles = async ({bucket,region,contents,diff --git a/packages/lambda/src/functions/helpers/get-files-to-delete.ts b/packages/lambda/src/functions/helpers/get-files-to-delete.tsindex 4a7f8da04d..e076744643 100644--- a/packages/lambda/src/functions/helpers/get-files-to-delete.ts+++ b/packages/lambda/src/functions/helpers/get-files-to-delete.ts@@ -10,15 +10,6 @@ export type CleanupJob = {type: 'exact' | 'prefix';};-export const getFinalFileToDelete = (renderId: string): CleanupJob[] => {- return [- {- name: encodingProgressKey(renderId),- type: 'exact',- },- ];-};-export const getFilesToDelete = ({chunkCount,renderId,@@ -60,5 +51,9 @@ export const getFilesToDelete = ({type: 'prefix' as const,};}),+ {+ name: encodingProgressKey(renderId),+ type: 'exact',+ },];};diff --git a/packages/lambda/src/functions/helpers/get-final-encoding-status.ts b/packages/lambda/src/functions/helpers/get-final-encoding-status.tsindex adbcd05dd9..b9b658cdbe 100644--- a/packages/lambda/src/functions/helpers/get-final-encoding-status.ts+++ b/packages/lambda/src/functions/helpers/get-final-encoding-status.ts@@ -1,13 +1,16 @@import {EncodingProgress, RenderMetadata} from '../../shared/constants';+import {LambdaInvokeStats} from './get-lambdas-invoked-stats';export const getFinalEncodingStatus = ({encodingStatus: encodingProgress,renderMetadata,outputFileExists,+ lambdaInvokeStatus,}: {encodingStatus: EncodingProgress | null;renderMetadata: RenderMetadata | null;outputFileExists: boolean;+ lambdaInvokeStatus: LambdaInvokeStats;}): EncodingProgress | null => {if (!renderMetadata) {return null;@@ -22,6 +25,7 @@ export const getFinalEncodingStatus = ({framesEncoded: renderMetadata.videoConfig.durationInFrames,totalFrames: renderMetadata.videoConfig.durationInFrames,doneIn: null,+ timeToInvoke: lambdaInvokeStatus.timeToInvokeLambdas,};}diff --git a/packages/lambda/src/functions/helpers/get-lambdas-invoked-stats.ts b/packages/lambda/src/functions/helpers/get-lambdas-invoked-stats.tsindex 4c648e1a27..c944d8ad7d 100644--- a/packages/lambda/src/functions/helpers/get-lambdas-invoked-stats.ts+++ b/packages/lambda/src/functions/helpers/get-lambdas-invoked-stats.ts@@ -2,16 +2,23 @@ import {_Object} from '@aws-sdk/client-s3';import {RenderInternals} from '@remotion/renderer';import {lambdaInitializedPrefix} from '../../shared/constants';+export type LambdaInvokeStats = {+ timeToInvokeLambdas: number | null;+ allLambdasInvoked: boolean;+ lambdasInvoked: number;+};+export const getLambdasInvokedStats = (contents: _Object[],renderId: string,- estimatedInvokations: number | null,+ estimatedRenderLambdaInvokations: number | null,startDate: number | null-) => {+): LambdaInvokeStats => {const lambdasInvoked = contents.filter((c) =>c.Key?.startsWith(lambdaInitializedPrefix(renderId)));- const allLambdasInvoked = lambdasInvoked.length === estimatedInvokations;+ const allLambdasInvoked =+ lambdasInvoked.length === estimatedRenderLambdaInvokations;const timeToInvokeLambdas =allLambdasInvoked && startDatediff --git a/packages/lambda/src/functions/helpers/get-progress.ts b/packages/lambda/src/functions/helpers/get-progress.tsindex 37c8b2bb94..c9d8c919db 100644--- a/packages/lambda/src/functions/helpers/get-progress.ts+++ b/packages/lambda/src/functions/helpers/get-progress.ts@@ -65,6 +65,7 @@ export const getProgress = async ({postRenderData.renderMetadata.videoConfig.durationInFrames,totalFrames: postRenderData.renderMetadata.videoConfig.durationInFrames,doneIn: postRenderData.timeToEncode,+ timeToInvoke: postRenderData.timeToInvokeLambdas,},errors: postRenderData.errors,fatalErrorEncountered: false,@@ -163,6 +164,7 @@ export const getProgress = async ({encodingStatus,outputFileExists: Boolean(outputFile),renderMetadata,+ lambdaInvokeStatus: lambdasInvokedStats,}),costs: formatCostsInfo(accruedSoFar),renderId,@@ -183,6 +185,7 @@ export const getProgress = async ({type: 'absolute-time',}): null,- timeToInvokeLambdas: lambdasInvokedStats.timeToInvokeLambdas,+ timeToInvokeLambdas:+ encodingStatus?.timeToInvoke ?? lambdasInvokedStats.timeToInvokeLambdas,};};diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.tsindex 3526907bc3..a5f3f08783 100644--- a/packages/lambda/src/functions/launch.ts+++ b/packages/lambda/src/functions/launch.ts@@ -29,13 +29,11 @@ import {} from './chunk-optimization/s3-optimization-file';import {concatVideosS3} from './helpers/concat-videos';import {createPostRenderData} from './helpers/create-post-render-data';-import {deleteChunks} from './helpers/delete-chunks';+import {cleanupFiles} from './helpers/delete-chunks';import {closeBrowser, getBrowserInstance} from './helpers/get-browser-instance';import {getCurrentRegionInFunction} from './helpers/get-current-region';-import {- getFilesToDelete,- getFinalFileToDelete,-} from './helpers/get-files-to-delete';+import {getFilesToDelete} from './helpers/get-files-to-delete';+import {getLambdasInvokedStats} from './helpers/get-lambdas-invoked-stats';import {inspectErrors} from './helpers/inspect-errors';import {lambdaLs, lambdaWriteFile} from './helpers/io';import {timer} from './helpers/timer';@@ -218,6 +216,7 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {framesEncoded,totalFrames: comp.durationInFrames,doneIn: encodingStop ? encodingStop - start : null,+ timeToInvoke: null,};lambdaWriteFile({bucketName: params.bucketName,@@ -306,6 +305,25 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {region: getCurrentRegionInFunction(),}),]);+ const finalEncodingProgress: EncodingProgress = {+ framesEncoded: comp.durationInFrames,+ totalFrames: comp.durationInFrames,+ doneIn: encodingStop ? encodingStop - encodingStart : null,+ timeToInvoke: getLambdasInvokedStats(+ contents,+ params.renderId,+ renderMetadata.estimatedRenderLambdaInvokations,+ renderMetadata.startedDate+ ).timeToInvokeLambdas,+ };+ const finalEncodingProgressProm = lambdaWriteFile({+ bucketName: params.bucketName,+ key: encodingProgressKey(params.renderId),+ body: JSON.stringify(finalEncodingProgress),+ region: getCurrentRegionInFunction(),+ acl: 'private',+ expectedBucketOwner: options.expectedBucketOwner,+ });const errorExplanationsProm = inspectErrors({contents,@@ -320,7 +338,7 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {renderId: params.renderId,});- const deletProm = deleteChunks({+ const deletProm = cleanupFiles({region: getCurrentRegionInFunction(),bucket: params.bucketName,contents,@@ -339,6 +357,7 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {timeToEncode: encodingStop - encodingStart,timeToDelete: await deletProm,});+ await finalEncodingProgressProm;await writePostRenderData({bucketName: params.bucketName,expectedBucketOwner: options.expectedBucketOwner,@@ -346,21 +365,8 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {region: getCurrentRegionInFunction(),renderId: params.renderId,});- // This file will be deleted last, if we delete it before,- // The invoke progress timing info returned by getRenderProgress() will go- // backwards- const encodingDelete = deleteChunks({- region: getCurrentRegionInFunction(),- bucket: params.bucketName,- contents,- jobs: getFinalFileToDelete(params.renderId),- });- await Promise.all([- cleanupChunksProm,- encodingDelete,- fs.promises.rm(outfile),- ]);+ await Promise.all([cleanupChunksProm, fs.promises.rm(outfile)]);};export const launchHandler = async (diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.tsindex d6f2dd1c9d..f4452cfc04 100644--- a/packages/lambda/src/shared/constants.ts+++ b/packages/lambda/src/shared/constants.ts@@ -199,6 +199,7 @@ export type EncodingProgress = {framesEncoded: number;totalFrames: number | null;doneIn: number | null;+ timeToInvoke: number | null;};export type RenderMetadata = {