Skip to content

🎥 Make videos programmatically with React

move render still flow into separate function

Jonny Burger9ce55c282690
diff --git a/packages/cli/src/render-flows/still.ts b/packages/cli/src/render-flows/still.ts
new file mode 100644
index 0000000000..5155c880b6
--- /dev/null
+++ b/packages/cli/src/render-flows/still.ts
@@ -0,0 +1,238 @@
+// Prints to CLI and also reports back to browser
+
+import type {RenderMediaOnDownload} from '@remotion/renderer';
+import {
+ getCompositions,
+ openBrowser,
+ RenderInternals,
+ renderStill,
+} from '@remotion/renderer';
+import {mkdirSync} from 'fs';
+import path from 'path';
+import {chalk} from '../chalk';
+import {ConfigInternals} from '../config';
+import {determineFinalImageFormat} from '../determine-image-format';
+import {
+ getAndValidateAbsoluteOutputFile,
+ getCliOptions,
+} from '../get-cli-options';
+import {getCompositionWithDimensionOverride} from '../get-composition-with-dimension-override';
+import {Log} from '../log';
+import {parsedCli, quietFlagProvided} from '../parse-command-line';
+import type {DownloadProgress} from '../progress-bar';
+import {
+ createOverwriteableCliOutput,
+ makeRenderingAndStitchingProgress,
+} from '../progress-bar';
+import {bundleOnCliOrTakeServeUrl} from '../setup-cache';
+import type {RenderStep} from '../step';
+import {truthy} from '../truthy';
+import {
+ getOutputLocation,
+ getUserPassedOutputLocation,
+} from '../user-passed-output-location';
+
+export const renderStillFlow = async ({
+ remotionRoot,
+ fullPath,
+ entryPointReason,
+ file,
+ remainingArgs,
+}: {
+ remotionRoot: string;
+ fullPath: string;
+ file: string | null;
+ entryPointReason: string;
+ remainingArgs: string[];
+}) => {
+ const startTime = Date.now();
+
+ const {
+ inputProps,
+ envVariables,
+ quality,
+ browser,
+ stillFrame,
+ browserExecutable,
+ chromiumOptions,
+ scale,
+ ffmpegExecutable,
+ ffprobeExecutable,
+ overwrite,
+ puppeteerTimeout,
+ port,
+ publicDir,
+ height,
+ width,
+ } = await getCliOptions({
+ isLambda: false,
+ type: 'still',
+ remotionRoot,
+ });
+
+ Log.verbose('Browser executable: ', browserExecutable);
+
+ const browserInstance = openBrowser(browser, {
+ browserExecutable,
+ chromiumOptions,
+ shouldDumpIo: RenderInternals.isEqualOrBelowLogLevel(
+ ConfigInternals.Logging.getLogLevel(),
+ 'verbose'
+ ),
+ forceDeviceScaleFactor: scale,
+ });
+
+ const steps: RenderStep[] = [
+ RenderInternals.isServeUrl(fullPath) ? null : ('bundling' as const),
+ 'rendering' as const,
+ ].filter(truthy);
+
+ const {cleanup: cleanupBundle, urlOrBundle} = await bundleOnCliOrTakeServeUrl(
+ {fullPath, remotionRoot, steps, publicDir}
+ );
+
+ const puppeteerInstance = await browserInstance;
+
+ const downloadMap = RenderInternals.makeDownloadMap();
+
+ const comps = await getCompositions(urlOrBundle, {
+ inputProps,
+ puppeteerInstance,
+ envVariables,
+ timeoutInMilliseconds: puppeteerTimeout,
+ chromiumOptions,
+ port,
+ browserExecutable,
+ ffmpegExecutable,
+ ffprobeExecutable,
+ downloadMap,
+ });
+
+ const {compositionId, config, reason, argsAfterComposition} =
+ await getCompositionWithDimensionOverride({
+ validCompositions: comps,
+ height,
+ width,
+ args: remainingArgs,
+ });
+ const {format: imageFormat, source} = determineFinalImageFormat({
+ // TODO: Should factor in browser
+ cliFlag: parsedCli['image-format'] ?? null,
+ configImageFormat: ConfigInternals.getUserPreferredImageFormat() ?? null,
+ downloadName: null,
+ outName: getUserPassedOutputLocation(argsAfterComposition),
+ isLambda: false,
+ });
+
+ const relativeOutputLocation = getOutputLocation({
+ compositionId,
+ defaultExtension: imageFormat,
+ args: argsAfterComposition,
+ });
+
+ const absoluteOutputLocation = getAndValidateAbsoluteOutputFile(
+ relativeOutputLocation,
+ overwrite
+ );
+
+ mkdirSync(path.join(absoluteOutputLocation, '..'), {
+ recursive: true,
+ });
+
+ Log.info(
+ chalk.gray(
+ `Entry point = ${file} (${entryPointReason}), Output = ${relativeOutputLocation}, Format = ${imageFormat} (${source}), Composition = ${compositionId} (${reason})`
+ )
+ );
+
+ const renderProgress = createOverwriteableCliOutput(quietFlagProvided());
+ const renderStart = Date.now();
+
+ const downloads: DownloadProgress[] = [];
+ let frames = 0;
+ const totalFrames = 1;
+
+ const updateProgress = () => {
+ renderProgress.update(
+ makeRenderingAndStitchingProgress({
+ rendering: {
+ frames,
+ concurrency: 1,
+ doneIn: frames === totalFrames ? Date.now() - renderStart : null,
+ steps,
+ totalFrames,
+ },
+ downloads,
+ stitching: null,
+ })
+ );
+ };
+
+ updateProgress();
+
+ const onDownload: RenderMediaOnDownload = (src) => {
+ const id = Math.random();
+ const download: DownloadProgress = {
+ id,
+ name: src,
+ progress: 0,
+ downloaded: 0,
+ totalBytes: null,
+ };
+ downloads.push(download);
+ updateProgress();
+
+ return ({percent}) => {
+ download.progress = percent;
+ updateProgress();
+ };
+ };
+
+ await renderStill({
+ composition: config,
+ frame: stillFrame,
+ output: absoluteOutputLocation,
+ serveUrl: urlOrBundle,
+ quality,
+ dumpBrowserLogs: RenderInternals.isEqualOrBelowLogLevel(
+ ConfigInternals.Logging.getLogLevel(),
+ 'verbose'
+ ),
+ envVariables,
+ imageFormat,
+ inputProps,
+ chromiumOptions,
+ timeoutInMilliseconds: ConfigInternals.getCurrentPuppeteerTimeout(),
+ scale,
+ ffmpegExecutable,
+ browserExecutable,
+ overwrite,
+ onDownload,
+ port,
+ downloadMap,
+ });
+
+ frames = 1;
+ updateProgress();
+ Log.info();
+
+ const closeBrowserPromise = puppeteerInstance.close(false);
+
+ Log.info(chalk.green('\nYour still frame is ready!'));
+
+ const seconds = Math.round((Date.now() - startTime) / 1000);
+ Log.info(
+ [
+ '- Total render time:',
+ seconds,
+ seconds === 1 ? 'second' : 'seconds',
+ ].join(' ')
+ );
+ Log.info('-', 'Output can be found at:');
+ Log.info(chalk.cyan(`▶️ ${absoluteOutputLocation}`));
+ await closeBrowserPromise;
+ await RenderInternals.cleanDownloadMap(downloadMap);
+ await cleanupBundle();
+
+ Log.verbose('Cleaned up', downloadMap.assetDir);
+};
diff --git a/packages/cli/src/still.ts b/packages/cli/src/still.ts
index 103a906cf6..df4ebe597d 100644
--- a/packages/cli/src/still.ts
+++ b/packages/cli/src/still.ts
@@ -1,38 +1,11 @@
-import type {RenderMediaOnDownload} from '@remotion/renderer';
-import {
- getCompositions,
- openBrowser,
- RenderInternals,
- renderStill,
-} from '@remotion/renderer';
-import {mkdirSync} from 'fs';
+import {RenderInternals} from '@remotion/renderer';
import path from 'path';
-import {chalk} from './chalk';
-import {ConfigInternals} from './config';
-import {determineFinalImageFormat} from './determine-image-format';
import {findEntryPoint} from './entry-point';
-import {
- getAndValidateAbsoluteOutputFile,
- getCliOptions,
-} from './get-cli-options';
-import {getCompositionWithDimensionOverride} from './get-composition-with-dimension-override';
import {Log} from './log';
-import {parsedCli, quietFlagProvided} from './parse-command-line';
-import type {DownloadProgress} from './progress-bar';
-import {
- createOverwriteableCliOutput,
- makeRenderingAndStitchingProgress,
-} from './progress-bar';
-import {bundleOnCliOrTakeServeUrl} from './setup-cache';
-import type {RenderStep} from './step';
-import {truthy} from './truthy';
-import {
- getOutputLocation,
- getUserPassedOutputLocation,
-} from './user-passed-output-location';
+import {parsedCli} from './parse-command-line';
+import {renderStillFlow} from './render-flows/still';
export const still = async (remotionRoot: string, args: string[]) => {
- const startTime = Date.now();
const {
file,
remainingArgs,
@@ -59,191 +32,11 @@ export const still = async (remotionRoot: string, args: string[]) => {
process.exit(1);
}
- const {
- inputProps,
- envVariables,
- quality,
- browser,
- stillFrame,
- browserExecutable,
- chromiumOptions,
- scale,
- ffmpegExecutable,
- ffprobeExecutable,
- overwrite,
- puppeteerTimeout,
- port,
- publicDir,
- height,
- width,
- } = await getCliOptions({
- isLambda: false,
- type: 'still',
+ await renderStillFlow({
remotionRoot,
+ entryPointReason,
+ fullPath,
+ file,
+ remainingArgs,
});
-
- Log.verbose('Browser executable: ', browserExecutable);
-
- const browserInstance = openBrowser(browser, {
- browserExecutable,
- chromiumOptions,
- shouldDumpIo: RenderInternals.isEqualOrBelowLogLevel(
- ConfigInternals.Logging.getLogLevel(),
- 'verbose'
- ),
- forceDeviceScaleFactor: scale,
- });
-
- const steps: RenderStep[] = [
- RenderInternals.isServeUrl(fullPath) ? null : ('bundling' as const),
- 'rendering' as const,
- ].filter(truthy);
-
- const {cleanup: cleanupBundle, urlOrBundle} = await bundleOnCliOrTakeServeUrl(
- {fullPath, remotionRoot, steps, publicDir}
- );
-
- const puppeteerInstance = await browserInstance;
-
- const downloadMap = RenderInternals.makeDownloadMap();
-
- const comps = await getCompositions(urlOrBundle, {
- inputProps,
- puppeteerInstance,
- envVariables,
- timeoutInMilliseconds: puppeteerTimeout,
- chromiumOptions,
- port,
- browserExecutable,
- ffmpegExecutable,
- ffprobeExecutable,
- downloadMap,
- });
-
- const {compositionId, config, reason, argsAfterComposition} =
- await getCompositionWithDimensionOverride({
- validCompositions: comps,
- height,
- width,
- args: remainingArgs,
- });
- const {format: imageFormat, source} = determineFinalImageFormat({
- cliFlag: parsedCli['image-format'] ?? null,
- configImageFormat: ConfigInternals.getUserPreferredImageFormat() ?? null,
- downloadName: null,
- outName: getUserPassedOutputLocation(argsAfterComposition),
- isLambda: false,
- });
-
- const relativeOutputLocation = getOutputLocation({
- compositionId,
- defaultExtension: imageFormat,
- args: argsAfterComposition,
- });
-
- const absoluteOutputLocation = getAndValidateAbsoluteOutputFile(
- relativeOutputLocation,
- overwrite
- );
-
- mkdirSync(path.join(absoluteOutputLocation, '..'), {
- recursive: true,
- });
-
- Log.info(
- chalk.gray(
- `Entry point = ${file} (${entryPointReason}), Output = ${relativeOutputLocation}, Format = ${imageFormat} (${source}), Composition = ${compositionId} (${reason})`
- )
- );
-
- const renderProgress = createOverwriteableCliOutput(quietFlagProvided());
- const renderStart = Date.now();
-
- const downloads: DownloadProgress[] = [];
- let frames = 0;
- const totalFrames = 1;
-
- const updateProgress = () => {
- renderProgress.update(
- makeRenderingAndStitchingProgress({
- rendering: {
- frames,
- concurrency: 1,
- doneIn: frames === totalFrames ? Date.now() - renderStart : null,
- steps,
- totalFrames,
- },
- downloads,
- stitching: null,
- })
- );
- };
-
- updateProgress();
-
- const onDownload: RenderMediaOnDownload = (src) => {
- const id = Math.random();
- const download: DownloadProgress = {
- id,
- name: src,
- progress: 0,
- downloaded: 0,
- totalBytes: null,
- };
- downloads.push(download);
- updateProgress();
-
- return ({percent}) => {
- download.progress = percent;
- updateProgress();
- };
- };
-
- await renderStill({
- composition: config,
- frame: stillFrame,
- output: absoluteOutputLocation,
- serveUrl: urlOrBundle,
- quality,
- dumpBrowserLogs: RenderInternals.isEqualOrBelowLogLevel(
- ConfigInternals.Logging.getLogLevel(),
- 'verbose'
- ),
- envVariables,
- imageFormat,
- inputProps,
- chromiumOptions,
- timeoutInMilliseconds: ConfigInternals.getCurrentPuppeteerTimeout(),
- scale,
- ffmpegExecutable,
- browserExecutable,
- overwrite,
- onDownload,
- port,
- downloadMap,
- });
-
- frames = 1;
- updateProgress();
- Log.info();
-
- const closeBrowserPromise = puppeteerInstance.close(false);
-
- Log.info(chalk.green('\nYour still frame is ready!'));
-
- const seconds = Math.round((Date.now() - startTime) / 1000);
- Log.info(
- [
- '- Total render time:',
- seconds,
- seconds === 1 ? 'second' : 'seconds',
- ].join(' ')
- );
- Log.info('-', 'Output can be found at:');
- Log.info(chalk.cyan(`▶️ ${absoluteOutputLocation}`));
- await closeBrowserPromise;
- await RenderInternals.cleanDownloadMap(downloadMap);
- await cleanupBundle();
-
- Log.verbose('Cleaned up', downloadMap.assetDir);
};