Skip to content

🎥 Make videos programmatically with React

reorganize render process

Jonny Burger36c5ab05a017
diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.ts
new file mode 100644
index 0000000000..55f873cd71
--- /dev/null
+++ b/packages/cli/src/get-cli-options.ts
@@ -0,0 +1,169 @@
+import {RenderInternals} from '@remotion/renderer';
+import fs from 'fs';
+import path from 'path';
+import {Codec, Config, getInputProps, Internals, PixelFormat} from 'remotion';
+import {getOutputFilename} from './get-filename';
+import {getImageFormat} from './image-formats';
+import {Log} from './log';
+import {getUserPassedFileExtension} from './user-passed-output-location';
+
+const getAndValidateFrameRange = () => {
+ const frameRange = Internals.getRange();
+ if (typeof frameRange === 'number') {
+ Log.Warn('Selected a single frame. Assuming you want to output an image.');
+ Log.Warn(
+ `If you want to render a video, pass a range: '--frames=${frameRange}-${frameRange}'.`
+ );
+ Log.Warn("To dismiss this message, add the '--sequence' flag explicitly.");
+ Config.Output.setImageSequence(true);
+ }
+ return frameRange;
+};
+
+const getFinalCodec = async () => {
+ const userCodec = Internals.getOutputCodecOrUndefined();
+
+ const codec = Internals.getFinalOutputCodec({
+ codec: userCodec,
+ fileExtension: getUserPassedFileExtension(),
+ emitWarning: true,
+ });
+ if (
+ codec === 'vp8' &&
+ !(await RenderInternals.ffmpegHasFeature('enable-libvpx'))
+ ) {
+ Log.Error(
+ "The Vp8 codec has been selected, but your FFMPEG binary wasn't compiled with the --enable-lipvpx flag."
+ );
+ Log.Error(
+ 'This does not work, please switch out your FFMPEG binary or choose a different codec.'
+ );
+ }
+ if (
+ codec === 'h265' &&
+ !(await RenderInternals.ffmpegHasFeature('enable-gpl'))
+ ) {
+ Log.Error(
+ "The H265 codec has been selected, but your FFMPEG binary wasn't compiled with the --enable-gpl flag."
+ );
+ Log.Error(
+ 'This does not work, please recompile your FFMPEG binary with --enable-gpl --enable-libx265 or choose a different codec.'
+ );
+ }
+ if (
+ codec === 'h265' &&
+ !(await RenderInternals.ffmpegHasFeature('enable-libx265'))
+ ) {
+ Log.Error(
+ "The H265 codec has been selected, but your FFMPEG binary wasn't compiled with the --enable-libx265 flag."
+ );
+ Log.Error(
+ 'This does not work, please recompile your FFMPEG binary with --enable-gpl --enable-libx265 or choose a different codec.'
+ );
+ }
+ return codec;
+};
+
+const getBrowser = () => {
+ return Internals.getBrowser() ?? Internals.DEFAULT_BROWSER;
+};
+
+const getAndValidateAbsoluteOutputFile = (
+ outputFile: string,
+ overwrite: boolean
+) => {
+ const absoluteOutputFile = path.resolve(process.cwd(), outputFile);
+ if (fs.existsSync(absoluteOutputFile) && !overwrite) {
+ Log.Error(
+ `File at ${absoluteOutputFile} already exists. Use --overwrite to overwrite.`
+ );
+ process.exit(1);
+ }
+ return absoluteOutputFile;
+};
+
+const getAndValidateShouldOutputImageSequence = async () => {
+ const shouldOutputImageSequence = Internals.getShouldOutputImageSequence();
+ if (!shouldOutputImageSequence) {
+ await RenderInternals.validateFfmpeg();
+ }
+ return shouldOutputImageSequence;
+};
+
+const getAndValidateCrf = (
+ shouldOutputImageSequence: boolean,
+ codec: Codec
+) => {
+ const crf = shouldOutputImageSequence ? null : Internals.getActualCrf(codec);
+ if (crf !== null) {
+ Internals.validateSelectedCrfAndCodecCombination(crf, codec);
+ }
+ return crf;
+};
+
+const getAndValidatePixelFormat = (codec: Codec) => {
+ const pixelFormat = Internals.getPixelFormat();
+
+ Internals.validateSelectedPixelFormatAndCodecCombination(pixelFormat, codec);
+ return pixelFormat;
+};
+
+const getAndValidateImageFormat = ({
+ shouldOutputImageSequence,
+ codec,
+ pixelFormat,
+}: {
+ shouldOutputImageSequence: boolean;
+ codec: Codec;
+ pixelFormat: PixelFormat;
+}) => {
+ const imageFormat = getImageFormat(
+ shouldOutputImageSequence ? undefined : codec
+ );
+ Internals.validateSelectedPixelFormatAndImageFormatCombination(
+ pixelFormat,
+ imageFormat
+ );
+ return imageFormat;
+};
+
+const getAndValidateBrowser = async () => {
+ const browser = getBrowser();
+ try {
+ await RenderInternals.ensureLocalBrowser(browser);
+ } catch (err) {
+ Log.Error('Could not download a browser for rendering frames.');
+ Log.Error(err);
+ process.exit(1);
+ }
+ return browser;
+};
+
+export const getCliOptions = async () => {
+ const shouldOutputImageSequence = await getAndValidateShouldOutputImageSequence();
+ const codec = await getFinalCodec();
+ const outputFile = getOutputFilename(codec, shouldOutputImageSequence);
+ const overwrite = Internals.getShouldOverwrite();
+ const crf = getAndValidateCrf(shouldOutputImageSequence, codec);
+ const pixelFormat = getAndValidatePixelFormat(codec);
+ const imageFormat = getAndValidateImageFormat({
+ shouldOutputImageSequence,
+ codec,
+ pixelFormat,
+ });
+
+ return {
+ parallelism: Internals.getConcurrency(),
+ frameRange: getAndValidateFrameRange(),
+ shouldOutputImageSequence,
+ codec,
+ overwrite: Internals.getShouldOverwrite(),
+ inputProps: getInputProps(),
+ quality: Internals.getQuality(),
+ browser: await getAndValidateBrowser(),
+ absoluteOutputFile: getAndValidateAbsoluteOutputFile(outputFile, overwrite),
+ crf,
+ pixelFormat,
+ imageFormat,
+ };
+};
diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsx
index 82bca1995d..51af719114 100644
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@ -10,12 +10,10 @@ import chalk from 'chalk';
import fs from 'fs';
import os from 'os';
import path from 'path';
-import {Config, Internals} from 'remotion';
+import {Internals} from 'remotion';
+import {getCliOptions} from './get-cli-options';
import {getCompositionId} from './get-composition-id';
import {getConfigFileName} from './get-config-file-name';
-import {getOutputFilename} from './get-filename';
-import {getInputProps} from './get-input-props';
-import {getImageFormat} from './image-formats';
import {loadConfigFile} from './load-config';
import {Log} from './log';
import {parseCommandLine, parsedCli} from './parse-command-line';
@@ -25,8 +23,7 @@ import {
makeRenderingProgress,
makeStitchingProgres,
} from './progress-bar';
-import {getUserPassedFileExtension} from './user-passed-output-location';
-import {warnAboutFfmpegVersion} from './warn-about-ffmpeg-version';
+import {checkAndValidateFfmpegVersion} from './validate-ffmpeg-version';
export const render = async () => {
const startTime = Date.now();
@@ -39,105 +36,26 @@ export const render = async () => {
if (appliedName) {
Log.Verbose(`Applied configuration from ${appliedName}.`);
}
- const parallelism = Internals.getConcurrency();
- const frameRange = Internals.getRange();
- if (typeof frameRange === 'number') {
- Log.Warn('Selected a single frame. Assuming you want to output an image.');
- Log.Warn(
- `If you want to render a video, pass a range: '--frames=${frameRange}-${frameRange}'.`
- );
- Log.Warn("To dismiss this message, add the '--sequence' flag explicitly.");
- Config.Output.setImageSequence(true);
- }
- const shouldOutputImageSequence = Internals.getShouldOutputImageSequence();
- const userCodec = Internals.getOutputCodecOrUndefined();
-
- const codec = Internals.getFinalOutputCodec({
- codec: userCodec,
- fileExtension: getUserPassedFileExtension(),
- emitWarning: true,
- });
+ const {
+ codec,
+ parallelism,
+ frameRange,
+ shouldOutputImageSequence,
+ absoluteOutputFile,
+ overwrite,
+ inputProps,
+ quality,
+ browser,
+ crf,
+ pixelFormat,
+ imageFormat,
+ } = await getCliOptions();
- const ffmpegVersion = await RenderInternals.getFfmpegVersion();
- Log.Verbose(
- 'Your FFMPEG version:',
- ffmpegVersion ? ffmpegVersion.join('.') : 'Built from source'
- );
- warnAboutFfmpegVersion(ffmpegVersion);
- if (
- codec === 'vp8' &&
- !(await RenderInternals.ffmpegHasFeature('enable-libvpx'))
- ) {
- Log.Error(
- "The Vp8 codec has been selected, but your FFMPEG binary wasn't compiled with the --enable-lipvpx flag."
- );
- Log.Error(
- 'This does not work, please switch out your FFMPEG binary or choose a different codec.'
- );
- }
- if (
- codec === 'h265' &&
- !(await RenderInternals.ffmpegHasFeature('enable-gpl'))
- ) {
- Log.Error(
- "The H265 codec has been selected, but your FFMPEG binary wasn't compiled with the --enable-gpl flag."
- );
- Log.Error(
- 'This does not work, please recompile your FFMPEG binary with --enable-gpl --enable-libx265 or choose a different codec.'
- );
- }
- if (
- codec === 'h265' &&
- !(await RenderInternals.ffmpegHasFeature('enable-libx265'))
- ) {
- Log.Error(
- "The H265 codec has been selected, but your FFMPEG binary wasn't compiled with the --enable-libx265 flag."
- );
- Log.Error(
- 'This does not work, please recompile your FFMPEG binary with --enable-gpl --enable-libx265 or choose a different codec.'
- );
- }
+ checkAndValidateFfmpegVersion();
- const outputFile = getOutputFilename(codec, shouldOutputImageSequence);
- const overwrite = Internals.getShouldOverwrite();
- const inputProps = getInputProps();
- const quality = Internals.getQuality();
- const browser = Internals.getBrowser() ?? Internals.DEFAULT_BROWSER;
const browserInstance = RenderInternals.openBrowser(browser, {
shouldDumpIo: Internals.Logging.isEqualOrBelowLogLevel('verbose'),
});
-
- const absoluteOutputFile = path.resolve(process.cwd(), outputFile);
- if (fs.existsSync(absoluteOutputFile) && !overwrite) {
- Log.Error(
- `File at ${absoluteOutputFile} already exists. Use --overwrite to overwrite.`
- );
- process.exit(1);
- }
- if (!shouldOutputImageSequence) {
- await RenderInternals.validateFfmpeg();
- }
- const crf = shouldOutputImageSequence ? null : Internals.getActualCrf(codec);
- if (crf !== null) {
- Internals.validateSelectedCrfAndCodecCombination(crf, codec);
- }
- const pixelFormat = Internals.getPixelFormat();
- const imageFormat = getImageFormat(
- shouldOutputImageSequence ? undefined : codec
- );
-
- Internals.validateSelectedPixelFormatAndCodecCombination(pixelFormat, codec);
- Internals.validateSelectedPixelFormatAndImageFormatCombination(
- pixelFormat,
- imageFormat
- );
- try {
- await RenderInternals.ensureLocalBrowser(browser);
- } catch (err) {
- Log.Error('Could not download a browser for rendering frames.');
- Log.Error(err);
- process.exit(1);
- }
if (shouldOutputImageSequence) {
fs.mkdirSync(absoluteOutputFile, {
recursive: true,
@@ -179,7 +97,7 @@ export const render = async () => {
}
const openedBrowser = await browserInstance;
const comps = await getCompositions(bundled, {
- browser: Internals.getBrowser() || Internals.DEFAULT_BROWSER,
+ browser,
inputProps,
browserInstance: openedBrowser,
});
diff --git a/packages/cli/src/validate-ffmpeg-version.ts b/packages/cli/src/validate-ffmpeg-version.ts
new file mode 100644
index 0000000000..cd806ca40e
--- /dev/null
+++ b/packages/cli/src/validate-ffmpeg-version.ts
@@ -0,0 +1,12 @@
+import {RenderInternals} from '@remotion/renderer';
+import {Log} from './log';
+import {warnAboutFfmpegVersion} from './warn-about-ffmpeg-version';
+
+export const checkAndValidateFfmpegVersion = async () => {
+ const ffmpegVersion = await RenderInternals.getFfmpegVersion();
+ Log.Verbose(
+ 'Your FFMPEG version:',
+ ffmpegVersion ? ffmpegVersion.join('.') : 'Built from source'
+ );
+ warnAboutFfmpegVersion(ffmpegVersion);
+};