Skip to content

🎥 Make videos programmatically with React

nicer progress subtitle

Jonny Burger2d32371d66bb
diff --git a/packages/bundler/src/bundle.ts b/packages/bundler/src/bundle.ts
index c8800aa9bd..4c5b378f9e 100644
--- a/packages/bundler/src/bundle.ts
+++ b/packages/bundler/src/bundle.ts
@@ -234,6 +234,7 @@ export async function bundle(...args: Arguments): Promise<string> {
dest: to,
onSymlinkDetected: showSymlinkWarning,
onProgress: (prog) => options.onPublicDirCopyProgress?.(prog),
+ copied: 0,
});
}
diff --git a/packages/bundler/src/copy-dir.ts b/packages/bundler/src/copy-dir.ts
index 5d42bbcf72..ca2de9df0a 100644
--- a/packages/bundler/src/copy-dir.ts
+++ b/packages/bundler/src/copy-dir.ts
@@ -6,17 +6,17 @@ export async function copyDir({
dest,
onSymlinkDetected,
onProgress,
+ copied = 0,
}: {
src: string;
dest: string;
onSymlinkDetected: (entry: fs.Dirent, dir: string) => void;
onProgress: (bytes: number) => void;
+ copied: number;
}) {
await fs.promises.mkdir(dest, {recursive: true});
const entries = await fs.promises.readdir(src, {withFileTypes: true});
- let copied = 0;
-
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
@@ -27,6 +27,7 @@ export async function copyDir({
dest: destPath,
onSymlinkDetected,
onProgress,
+ copied,
});
} else if (entry.isSymbolicLink()) {
const realpath = await fs.promises.realpath(srcPath);
diff --git a/packages/cli/src/benchmark.ts b/packages/cli/src/benchmark.ts
index 3c957ac4bd..0b2509aa32 100644
--- a/packages/cli/src/benchmark.ts
+++ b/packages/cli/src/benchmark.ts
@@ -182,10 +182,11 @@ export const benchmarkCommand = async (
fullPath: fullEntryPoint,
publicDir,
remotionRoot,
- steps: ['bundling'],
onProgress: () => undefined,
indentOutput: false,
logLevel: ConfigInternals.Logging.getLogLevel(),
+ bundlingStep: 0,
+ steps: 1,
});
registerCleanupJob(() => cleanupBundle());
diff --git a/packages/cli/src/compositions.ts b/packages/cli/src/compositions.ts
index c65dc6201b..526d89b0b8 100644
--- a/packages/cli/src/compositions.ts
+++ b/packages/cli/src/compositions.ts
@@ -50,11 +50,12 @@ export const listCompositionsCommand = async (
await bundleOnCliOrTakeServeUrl({
remotionRoot,
fullPath: file,
- steps: ['bundling'],
publicDir,
onProgress: () => undefined,
indentOutput: false,
logLevel: ConfigInternals.Logging.getLogLevel(),
+ bundlingStep: 0,
+ steps: 1,
});
registerCleanupJob(() => cleanupBundle());
diff --git a/packages/cli/src/progress-bar.ts b/packages/cli/src/progress-bar.ts
index fd2dcd050b..b8c07e4631 100644
--- a/packages/cli/src/progress-bar.ts
+++ b/packages/cli/src/progress-bar.ts
@@ -14,7 +14,6 @@ import type {
RenderingProgressInput,
StitchingProgressInput,
} from './progress-types';
-import type {RenderStep} from './step';
import {truthy} from './truthy';
export const createProgressBar = (
@@ -60,21 +59,22 @@ export const createOverwriteableCliOutput = (options: {
};
};
-const makeBundlingProgress = (
- {
- progress,
- steps,
- doneIn,
- }: {
- progress: number;
- steps: RenderStep[];
- doneIn: number | null;
- },
- indent: boolean
-) =>
- [
+const makeBundlingProgress = ({
+ bundlingState,
+ indent,
+ bundlingStep,
+ steps,
+}: {
+ bundlingState: BundlingState;
+ indent: boolean;
+ bundlingStep: number;
+ steps: number;
+}) => {
+ const {doneIn, progress} = bundlingState;
+
+ return [
indent ? INDENT_TOKEN : null,
- `(${steps.indexOf('bundling') + 1}/${steps.length})`,
+ `(${bundlingStep + 1}/${steps})`,
makeProgressBar(progress),
`${doneIn ? 'Bundled' : 'Bundling'} code`,
doneIn === null
@@ -83,6 +83,7 @@ const makeBundlingProgress = (
]
.filter(truthy)
.join(' ');
+};
const makeCopyingProgress = (options: CopyingState, indent: boolean) => {
// Don't show copy progress lower than 200MB
@@ -137,7 +138,6 @@ export type CopyingState = {
export type BundlingState = {
progress: number;
- steps: RenderStep[];
doneIn: number | null;
};
@@ -153,10 +153,17 @@ export const makeBundlingAndCopyProgress = (
copying: CopyingState;
symLinks: SymbolicLinksState;
},
- indent: boolean
+ indent: boolean,
+ bundlingStep: number,
+ steps: number
) => {
return [
- makeBundlingProgress(bundling, indent),
+ makeBundlingProgress({
+ bundlingState: bundling,
+ indent,
+ bundlingStep,
+ steps,
+ }),
makeCopyingProgress(copying, indent),
makeSymlinkProgress(symLinks, indent),
]
@@ -182,10 +189,18 @@ export const makeRenderingProgress = (
.join(' ');
};
-export const makeStitchingProgress = (
- {frames, totalFrames, steps, doneIn, stage, codec}: StitchingProgressInput,
- indent: boolean
-) => {
+export const makeStitchingProgress = ({
+ stitchingProgress,
+ indent,
+ steps,
+ stitchingStep,
+}: {
+ stitchingProgress: StitchingProgressInput;
+ indent: boolean;
+ steps: number;
+ stitchingStep: number;
+}) => {
+ const {frames, totalFrames, doneIn, stage, codec} = stitchingProgress;
const progress = frames / totalFrames;
const mediaType =
codec === 'gif'
@@ -196,7 +211,7 @@ export const makeStitchingProgress = (
return [
indent ? INDENT_TOKEN : null,
- `(${steps.indexOf('stitching') + 1}/${steps.length})`,
+ `(${stitchingStep + 1}/${steps})`,
makeProgressBar(progress),
stage === 'muxing' && RenderInternals.canUseParallelEncoding(codec)
? `${doneIn ? 'Muxed' : 'Muxing'} ${mediaType}`
@@ -207,10 +222,17 @@ export const makeStitchingProgress = (
.join(' ');
};
-export const makeRenderingAndStitchingProgress = (
- prog: AggregateRenderProgress,
- indent: boolean
-): {
+export const makeRenderingAndStitchingProgress = ({
+ prog,
+ indent,
+ steps,
+ stitchingStep,
+}: {
+ prog: AggregateRenderProgress;
+ indent: boolean;
+ steps: number;
+ stitchingStep: number;
+}): {
output: string;
progress: number;
message: string;
@@ -219,7 +241,14 @@ export const makeRenderingAndStitchingProgress = (
const output = [
rendering ? makeRenderingProgress(rendering, indent) : null,
makeMultiDownloadProgress(downloads, indent),
- stitching === null ? null : makeStitchingProgress(stitching, indent),
+ stitching === null
+ ? null
+ : makeStitchingProgress({
+ stitchingProgress: stitching,
+ indent,
+ steps,
+ stitchingStep,
+ }),
]
.filter(truthy)
.join('\n');
@@ -230,14 +259,40 @@ export const makeRenderingAndStitchingProgress = (
// TODO: Factor in stitching progress
const progress = bundling.progress * 0.3 + renderProgress * 0.7;
- return {output, progress, message: getMessage(prog)};
+ return {output, progress, message: getGuiProgressSubtitle(prog)};
};
-export const getMessage = (progress: AggregateRenderProgress): string => {
- if (progress.bundling.progress < 1 && progress.bundling.message) {
- return progress.bundling.message;
+export const getGuiProgressSubtitle = (
+ progress: AggregateRenderProgress
+): string => {
+ if (progress.bundling.progress < 1) {
+ return `Bundling ${Math.round(progress.bundling.progress * 100)}%`;
+ }
+
+ if (!progress.copyingState.doneIn) {
+ if (progress.copyingState.bytes < 100_000_000) {
+ return 'Bundling 100%';
+ }
+
+ const bytes = new Intl.NumberFormat('en', {
+ notation: 'compact',
+ style: 'unit',
+ unit: 'byte',
+ unitDisplay: 'narrow',
+ });
+ return `Copying ${bytes.format(progress.copyingState.bytes)}`;
+ }
+
+ if (!progress.rendering) {
+ return `Getting compositions`;
+ }
+
+ const allRendered =
+ progress.rendering.frames === progress.rendering.totalFrames;
+
+ if (!allRendered || !progress.stitching || progress.stitching.frames === 0) {
+ return `Rendering ${progress.rendering.frames}/${progress.rendering.totalFrames}`;
}
- // TODO: Better message than "Rendering..."
- return `Rendering...`;
+ return `Stitching ${progress.stitching.frames}/${progress.stitching.totalFrames}`;
};
diff --git a/packages/cli/src/progress-types.ts b/packages/cli/src/progress-types.ts
index 5bb0d29608..ce06f2cdf7 100644
--- a/packages/cli/src/progress-types.ts
+++ b/packages/cli/src/progress-types.ts
@@ -1,4 +1,5 @@
import type {Codec, StitchingState} from '@remotion/renderer';
+import type {BundlingState, CopyingState} from './progress-bar';
import type {RenderStep} from './step';
export type DownloadProgress = {
@@ -20,22 +21,17 @@ export type RenderingProgressInput = {
export type StitchingProgressInput = {
frames: number;
totalFrames: number;
- steps: RenderStep[];
doneIn: number | null;
stage: StitchingState;
codec: Codec;
};
-export type BundlingProgress = {
- progress: number;
- message: string | null;
-};
-
export type AggregateRenderProgress = {
rendering: RenderingProgressInput | null;
stitching: StitchingProgressInput | null;
downloads: DownloadProgress[];
- bundling: BundlingProgress;
+ bundling: BundlingState;
+ copyingState: CopyingState;
};
export const initialAggregateRenderProgress = (): AggregateRenderProgress => ({
@@ -43,7 +39,11 @@ export const initialAggregateRenderProgress = (): AggregateRenderProgress => ({
downloads: [],
stitching: null,
bundling: {
- message: null,
progress: 0,
+ doneIn: null,
+ },
+ copyingState: {
+ bytes: 0,
+ doneIn: null,
},
});
diff --git a/packages/cli/src/render-flows/render.ts b/packages/cli/src/render-flows/render.ts
index 51e2610ebe..804fdfd6b4 100644
--- a/packages/cli/src/render-flows/render.ts
+++ b/packages/cli/src/render-flows/render.ts
@@ -39,13 +39,13 @@ import type {
GuiRenderProgress,
JobProgressCallback,
} from '../preview-server/render-queue/job';
+import type {BundlingState, CopyingState} from '../progress-bar';
import {
createOverwriteableCliOutput,
makeRenderingAndStitchingProgress,
} from '../progress-bar';
import type {
AggregateRenderProgress,
- BundlingProgress,
DownloadProgress,
RenderingProgressInput,
StitchingProgressInput,
@@ -174,13 +174,17 @@ export const renderVideoFlow = async ({
shouldOutputImageSequence ? null : ('stitching' as const),
].filter(truthy);
- const bundlingProgress: BundlingProgress = {
- message: null,
+ const bundlingProgress: BundlingState = {
+ doneIn: null,
progress: 0,
};
let renderingProgress: RenderingProgressInput | null = null;
let stitchingProgress: StitchingProgressInput | null = null;
+ let copyingState: CopyingState = {
+ bytes: 0,
+ doneIn: null,
+ };
const updateRenderProgress = () => {
const aggregateRenderProgress: AggregateRenderProgress = {
@@ -188,12 +192,15 @@ export const renderVideoFlow = async ({
stitching: shouldOutputImageSequence ? null : stitchingProgress,
downloads,
bundling: bundlingProgress,
+ copyingState,
};
- const {output, message, progress} = makeRenderingAndStitchingProgress(
- aggregateRenderProgress,
- indent
- );
+ const {output, message, progress} = makeRenderingAndStitchingProgress({
+ prog: aggregateRenderProgress,
+ indent,
+ steps: steps.length,
+ stitchingStep: steps.indexOf('bundling'),
+ });
onProgress({message, value: progress, ...aggregateRenderProgress});
return renderProgress.update(output);
@@ -203,15 +210,16 @@ export const renderVideoFlow = async ({
{
fullPath: fullEntryPoint,
remotionRoot,
- steps,
publicDir,
- onProgress: ({message, progress}) => {
- bundlingProgress.message = message;
+ onProgress: ({progress, copying}) => {
bundlingProgress.progress = progress;
+ copyingState = copying;
updateRenderProgress();
},
indentOutput: indent,
logLevel,
+ bundlingStep: steps.indexOf('bundling'),
+ steps: steps.length,
}
);
@@ -392,7 +400,6 @@ export const renderVideoFlow = async ({
doneIn: null,
frames: 0,
stage: 'encoding',
- steps,
totalFrames: totalFrames.length,
codec,
};
diff --git a/packages/cli/src/render-flows/still.ts b/packages/cli/src/render-flows/still.ts
index 0ecacd15b4..5cc756be7f 100644
--- a/packages/cli/src/render-flows/still.ts
+++ b/packages/cli/src/render-flows/still.ts
@@ -105,11 +105,18 @@ export const renderStillFlow = async ({
const aggregate: AggregateRenderProgress = initialAggregateRenderProgress();
let renderProgress: OverwriteableCliOutput | null = null;
+ const steps: RenderStep[] = [
+ RenderInternals.isServeUrl(fullEntryPoint) ? null : ('bundling' as const),
+ 'rendering' as const,
+ ].filter(truthy);
+
const updateProgress = () => {
- const {output, progress, message} = makeRenderingAndStitchingProgress(
- aggregate,
- indentOutput
- );
+ const {output, progress, message} = makeRenderingAndStitchingProgress({
+ prog: aggregate,
+ indent: indentOutput,
+ steps: steps.length,
+ stitchingStep: steps.indexOf('stitching'),
+ });
if (renderProgress) {
renderProgress.update(output);
}
@@ -135,23 +142,20 @@ export const renderStillFlow = async ({
indentationString: indentOutput ? INDENT_TOKEN + ' ' : '',
});
- const steps: RenderStep[] = [
- RenderInternals.isServeUrl(fullEntryPoint) ? null : ('bundling' as const),
- 'rendering' as const,
- ].filter(truthy);
-
const {cleanup: cleanupBundle, urlOrBundle} = await bundleOnCliOrTakeServeUrl(
{
fullPath: fullEntryPoint,
remotionRoot,
- steps,
+ steps: steps.length,
publicDir,
- onProgress: (progress) => {
- aggregate.bundling = progress;
+ onProgress: ({copying, progress}) => {
+ aggregate.bundling.progress = progress;
+ aggregate.copyingState = copying;
updateProgress();
},
indentOutput,
logLevel,
+ bundlingStep: steps.indexOf('bundling'),
}
);
diff --git a/packages/cli/src/setup-cache.ts b/packages/cli/src/setup-cache.ts
index a1030255c4..896c43d1db 100644
--- a/packages/cli/src/setup-cache.ts
+++ b/packages/cli/src/setup-cache.ts
@@ -14,24 +14,25 @@ import {
createOverwriteableCliOutput,
makeBundlingAndCopyProgress,
} from './progress-bar';
-import type {RenderStep} from './step';
export const bundleOnCliOrTakeServeUrl = async ({
fullPath,
remotionRoot,
- steps,
publicDir,
onProgress,
indentOutput,
logLevel,
+ bundlingStep,
+ steps,
}: {
fullPath: string;
remotionRoot: string;
- steps: RenderStep[];
publicDir: string | null;
- onProgress: (params: {progress: number; message: string}) => void;
+ onProgress: (params: {progress: number; copying: CopyingState}) => void;
indentOutput: boolean;
logLevel: LogLevel;
+ bundlingStep: number;
+ steps: number;
}): Promise<{
urlOrBundle: string;
cleanup: () => void;
@@ -46,11 +47,12 @@ export const bundleOnCliOrTakeServeUrl = async ({
const bundled = await bundleOnCli({
fullPath,
remotionRoot,
- steps,
publicDir,
onProgressCallback: onProgress,
indent: indentOutput,
logLevel,
+ bundlingStep,
+ steps,
});
return {
@@ -61,20 +63,25 @@ export const bundleOnCliOrTakeServeUrl = async ({
export const bundleOnCli = async ({
fullPath,
- steps,
remotionRoot,
publicDir,
onProgressCallback,
indent,
logLevel,
+ bundlingStep,
+ steps,
}: {
fullPath: string;
- steps: RenderStep[];
remotionRoot: string;
publicDir: string | null;
- onProgressCallback: (params: {progress: number; message: string}) => void;
+ onProgressCallback: (params: {
+ progress: number;
+ copying: CopyingState;
+ }) => void;
indent: boolean;
logLevel: LogLevel;
+ bundlingStep: number;
+ steps: number;
}) => {
const shouldCache = ConfigInternals.getWebpackCaching();
@@ -85,7 +92,6 @@ export const bundleOnCli = async ({
const onProgress = (progress: number) => {
bundlingState = {
progress: progress / 100,
- steps,
doneIn: null,
};
updateProgress(false);
@@ -106,13 +112,14 @@ export const bundleOnCli = async ({
copying: copyingState,
symLinks: symlinkState,
},
- indent
+ indent,
+ bundlingStep,
+ steps
) + (newline ? '\n' : '')
);
- // TODO: Take copying into account
onProgressCallback({
progress: bundlingState.progress,
- message: `Bundling (${Math.round(bundlingState.progress * 100)}%)`,
+ copying: copyingState,
});
};
@@ -179,7 +186,6 @@ export const bundleOnCli = async ({
let bundlingState: BundlingState = {
progress: 0,
- steps,
doneIn: null,
};
@@ -188,7 +194,6 @@ export const bundleOnCli = async ({
onProgress: (progress) => {
bundlingState = {
progress: progress / 100,
- steps,
doneIn: null,
};
updateProgress(false);
@@ -198,7 +203,6 @@ export const bundleOnCli = async ({
bundlingState = {
progress: 1,
- steps,
doneIn: Date.now() - bundleStartTime,
};
copyingState = {