Skip to content

πŸŽ₯ Make videos programmatically with React

don't destroy CLI output

Jonny Burger961de5a61bdd
diff --git a/packages/cli/package-lock.json b/packages/cli/package-lock.json
index fa8667ef43..a74ef7478d 100644
--- a/packages/cli/package-lock.json
+++ b/packages/cli/package-lock.json
@@ -1438,6 +1438,14 @@
"uri-js": "^4.2.2"
}
},
+ "ansi-diff": {
+ "version": "1.1.1",
+ "resolved": "https://registry.npmjs.org/ansi-diff/-/ansi-diff-1.1.1.tgz",
+ "integrity": "sha512-XnTdFDQzbEewrDx8epWXdw7oqHMvv315vEtfqDiEhhWghIf4++h26c3/FMz7iTLhNrnj56DNIXpbxHZq+3s6qw==",
+ "requires": {
+ "ansi-split": "^1.0.1"
+ }
+ },
"ansi-escapes": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.2.tgz",
@@ -1458,6 +1466,21 @@
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz",
"integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg=="
},
+ "ansi-split": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/ansi-split/-/ansi-split-1.0.1.tgz",
+ "integrity": "sha512-RRxQym4DFtDNmHIkW6aeFVvrXURb11lGAEPXNiryjCe8bK8RsANjzJ0M2aGOkvBYwP4Bl/xZ8ijtr6D3j1x/eg==",
+ "requires": {
+ "ansi-regex": "^3.0.0"
+ },
+ "dependencies": {
+ "ansi-regex": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz",
+ "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg="
+ }
+ }
+ },
"ansi-styles": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
@@ -1961,15 +1984,6 @@
}
}
},
- "cli-progress": {
- "version": "3.8.2",
- "resolved": "https://registry.npmjs.org/cli-progress/-/cli-progress-3.8.2.tgz",
- "integrity": "sha512-qRwBxLldMSfxB+YGFgNRaj5vyyHe1yMpVeDL79c+7puGujdKJHQHydgqXDcrkvQgJ5U/d3lpf6vffSoVVUftVQ==",
- "requires": {
- "colors": "^1.1.2",
- "string-width": "^4.2.0"
- }
- },
"cliui": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz",
@@ -2017,11 +2031,6 @@
"resolved": "https://registry.npmjs.org/colorette/-/colorette-1.2.2.tgz",
"integrity": "sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w=="
},
- "colors": {
- "version": "1.4.0",
- "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz",
- "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
- },
"combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
diff --git a/packages/cli/package.json b/packages/cli/package.json
index 8c503522ef..93be83f562 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -22,9 +22,9 @@
"@remotion/bundler": "^2.0.7",
"@remotion/media-utils": "^2.0.7",
"@remotion/renderer": "^2.0.7",
+ "ansi-diff": "^1.1.1",
"better-opn": "^2.1.1",
"chalk": "^4.1.0",
- "cli-progress": "^3.8.2",
"execa": "^4.0.2",
"minimist": "^1.2.5",
"remotion": "^2.0.7",
diff --git a/packages/cli/src/progress-bar.ts b/packages/cli/src/progress-bar.ts
new file mode 100644
index 0000000000..40011b8ad3
--- /dev/null
+++ b/packages/cli/src/progress-bar.ts
@@ -0,0 +1,79 @@
+// @ts-expect-error
+import ansiDiff from 'ansi-diff';
+import chalk from 'chalk';
+
+export const createProgressBar = () => {
+ const diff = ansiDiff();
+ process.stdout.write('');
+ return {
+ update: (up: string): boolean => process.stdout.write(diff.update(up)),
+ };
+};
+
+export const makeProgressBar = (percentage: number) => {
+ const totalBars = 20;
+ const barsToShow = Math.floor(percentage * totalBars);
+ return `[${'='.repeat(barsToShow).padEnd(totalBars, ' ')}]`;
+};
+
+export const makeBundlingProgress = ({
+ progress,
+ steps,
+ doneIn,
+}: {
+ progress: number;
+ steps: number;
+ doneIn: number | null;
+}) =>
+ [
+ 'πŸ“¦',
+ `(1/${steps})`,
+ makeProgressBar(progress),
+ `${doneIn ? 'Bundled' : 'Bundling'} video`,
+ doneIn !== null
+ ? chalk.gray(`${doneIn}ms`)
+ : (progress * 100).toFixed(0) + '%',
+ ].join(' ');
+
+export const makeRenderingProgress = ({
+ frames,
+ totalFrames,
+ steps,
+ concurrency,
+ doneIn,
+}: {
+ frames: number;
+ totalFrames: number;
+ steps: number;
+ concurrency: number;
+ doneIn: number | null;
+}) => {
+ const progress = frames / totalFrames;
+ return [
+ 'πŸ–Ό ',
+ `(2/${steps})`,
+ makeProgressBar(progress),
+ `${doneIn ? 'Rendered' : 'Rendering'} frames (${concurrency}x)`,
+ doneIn !== null ? chalk.gray(`${doneIn}ms`) : `${frames}/${totalFrames}`,
+ ].join(' ');
+};
+export const makeStitchingProgres = ({
+ frames,
+ totalFrames,
+ steps,
+ doneIn,
+}: {
+ frames: number;
+ totalFrames: number;
+ steps: number;
+ doneIn: number | null;
+}) => {
+ const progress = frames / totalFrames;
+ return [
+ '🎞 ',
+ `(3/${steps})`,
+ makeProgressBar(progress),
+ `${doneIn ? 'Encoded' : 'Encoding'} video`,
+ doneIn !== null ? chalk.gray(`${doneIn}ms`) : `${frames}/${totalFrames}`,
+ ].join(' ');
+};
diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsx
index 4c5b679d38..2b87e7c81c 100644
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@ -7,12 +7,10 @@ import {
stitchFramesToVideo,
} from '@remotion/renderer';
import chalk from 'chalk';
-import cliProgress from 'cli-progress';
import fs from 'fs';
import os from 'os';
import path from 'path';
import {Config, Internals} from 'remotion';
-import {getFinalOutputCodec} from 'remotion/dist/config/codec';
import {getCompositionId} from './get-composition-id';
import {getConfigFileName} from './get-config-file-name';
import {getOutputFilename} from './get-filename';
@@ -21,6 +19,12 @@ import {getImageFormat} from './image-formats';
import {loadConfigFile} from './load-config';
import {Log} from './log';
import {parseCommandLine, parsedCli} from './parse-command-line';
+import {
+ createProgressBar,
+ makeBundlingProgress,
+ makeRenderingProgress,
+ makeStitchingProgres,
+} from './progress-bar';
import {getUserPassedFileExtension} from './user-passed-output-location';
import {warnAboutFfmpegVersion} from './warn-about-ffmpeg-version';
@@ -48,7 +52,7 @@ export const render = async () => {
const shouldOutputImageSequence = Internals.getShouldOutputImageSequence();
const userCodec = Internals.getOutputCodecOrUndefined();
- const codec = getFinalOutputCodec({
+ const codec = Internals.getFinalOutputCodec({
codec: userCodec,
fileExtension: getUserPassedFileExtension(),
emitWarning: true,
@@ -140,15 +144,6 @@ export const render = async () => {
});
}
const steps = shouldOutputImageSequence ? 2 : 3;
- process.stdout.write(`πŸ“¦ (1/${steps}) Bundling video...\n`);
-
- const bundlingProgress = new cliProgress.Bar(
- {
- clearOnComplete: true,
- format: '[{bar}] {percentage}%',
- },
- cliProgress.Presets.shades_grey
- );
const shouldCache = Internals.getWebpackCaching();
const cacheExistedBefore = BundlerInternals.cacheExists('production', null);
@@ -157,17 +152,26 @@ export const render = async () => {
await BundlerInternals.clearCache('production', null);
process.stdout.write('done. \n');
}
- bundlingProgress.start(100, 0);
+ const bundleStartTime = Date.now();
+ const bundlingProgress = createProgressBar('');
const bundled = await bundle(
fullPath,
(progress) => {
- bundlingProgress.update(progress);
+ bundlingProgress.update(
+ makeBundlingProgress({progress: progress / 100, steps, doneIn: null})
+ );
},
{
enableCaching: shouldCache,
}
);
- bundlingProgress.stop();
+ bundlingProgress.update(
+ makeBundlingProgress({
+ progress: 1,
+ steps,
+ doneIn: Date.now() - bundleStartTime,
+ }) + '\n'
+ );
Log.Verbose('Bundled under', bundled);
const cacheExistedAfter = BundlerInternals.cacheExists('production', null);
if (cacheExistedAfter && !cacheExistedBefore) {
@@ -192,27 +196,36 @@ export const render = async () => {
Log.Verbose('Output dir', outputDir);
- const renderProgress = new cliProgress.Bar(
- {
- clearOnComplete: true,
- etaBuffer: 50,
- format: '[{bar}] {percentage}% | ETA: {eta}s | {value}/{total}',
- },
- cliProgress.Presets.shades_grey
- );
- const {assetsInfo, frameCount} = await renderFrames({
+ const renderProgress = createProgressBar('');
+ let totalFrames = 0;
+ const renderStart = Date.now();
+ const {assetsInfo} = await renderFrames({
config,
- onFrameUpdate: (frame: number) => renderProgress.update(frame),
+ onFrameUpdate: (frame: number) => {
+ renderProgress.update(
+ makeRenderingProgress({
+ frames: frame,
+ totalFrames,
+ concurrency: RenderInternals.getActualConcurrency(parallelism),
+ doneIn: null,
+ steps,
+ })
+ );
+ },
parallelism,
compositionId,
outputDir,
onStart: ({frameCount: fc}: OnStartData) => {
- process.stdout.write(
- `πŸ“Ό (2/${steps}) Rendering frames (${RenderInternals.getActualConcurrency(
- parallelism
- )}x concurrency)...\n`
+ renderProgress.update(
+ makeRenderingProgress({
+ frames: 0,
+ totalFrames: fc,
+ concurrency: RenderInternals.getActualConcurrency(parallelism),
+ doneIn: null,
+ steps,
+ })
);
- renderProgress.start(fc, 0);
+ totalFrames = fc;
},
inputProps,
webpackBundle: bundled,
@@ -225,24 +238,31 @@ export const render = async () => {
});
const closeBrowserPromise = openedBrowser.close();
- renderProgress.stop();
+ renderProgress.update(
+ makeRenderingProgress({
+ frames: totalFrames,
+ totalFrames,
+ steps,
+ concurrency: RenderInternals.getActualConcurrency(parallelism),
+ doneIn: Date.now() - renderStart,
+ }) + '\n'
+ );
if (process.env.DEBUG) {
Internals.perf.logPerf();
}
if (!shouldOutputImageSequence) {
- process.stdout.write(`🧡 (3/${steps}) Stitching frames together...\n`);
if (typeof crf !== 'number') {
throw TypeError('CRF is unexpectedly not a number');
}
- const stitchingProgress = new cliProgress.Bar(
- {
- clearOnComplete: true,
- etaBuffer: 50,
- format: '[{bar}] {percentage}% | ETA: {eta}s | {value}/{total}',
- },
- cliProgress.Presets.shades_grey
+ const stitchingProgress = createProgressBar(
+ makeStitchingProgres({
+ doneIn: null,
+ frames: 0,
+ steps,
+ totalFrames,
+ })
);
- stitchingProgress.start(frameCount, 0);
+ const stitchStart = Date.now();
await stitchFramesToVideo({
dir: outputDir,
width: config.width,
@@ -257,17 +277,30 @@ export const render = async () => {
assetsInfo,
parallelism,
onProgress: (frame: number) => {
- stitchingProgress.update(frame);
+ stitchingProgress.update(
+ makeStitchingProgres({
+ doneIn: null,
+ frames: frame,
+ steps,
+ totalFrames,
+ })
+ );
},
onDownload: (src: string) => {
- Log.Info('\n');
Log.Info('Downloading asset... ', src);
},
verbose: Internals.Logging.isEqualOrBelowLogLevel('verbose'),
});
- stitchingProgress.stop();
+ stitchingProgress.update(
+ makeStitchingProgres({
+ doneIn: Date.now() - stitchStart,
+ frames: totalFrames,
+ steps,
+ totalFrames,
+ }) + '\n'
+ );
- Log.Info('Cleaning up...');
+ Log.Verbose('Cleaning up...');
try {
await Promise.all([
fs.promises.rmdir(outputDir, {
@@ -290,7 +323,7 @@ export const render = async () => {
const seconds = Math.round((Date.now() - startTime) / 1000);
Log.Info(
[
- '\n- Total render time:',
+ '- Total render time:',
seconds,
seconds === 1 ? 'second' : 'seconds',
].join(' ')