π₯ Make videos programmatically with React
codec
Jonny Burger0bd006c97ca6
diff --git a/packages/cli/src/get-filename.ts b/packages/cli/src/get-filename.tsindex 8a8d53eb3b..3fdb9f1102 100644--- a/packages/cli/src/get-filename.ts+++ b/packages/cli/src/get-filename.ts@@ -1,62 +1,44 @@-import minimist from 'minimist';-import {Config, OutputFormat} from 'remotion';+import {Codec} from 'remotion';+import {+ getUserPassedFileExtension,+ getUserPassedOutputLocation,+} from './user-passed-output-location';-export const getOutputFilename = (renderMode: OutputFormat): string => {- const args = minimist(process.argv.slice(2));- if (!args._[3]) {- console.log('Pass an extra argument <output-filename>.');- process.exit(1);- }- let filename = args._[3];- const filenameArr = filename.split('.');- const hasExtension = filenameArr.length >= 2;- const filenameArrLength = filenameArr.length;- const extension = hasExtension ? filenameArr[filenameArrLength - 1] : null;- if (filenameArrLength === 1 && renderMode !== 'png') {- console.info(- 'User has given a file without extension , adding the extension automatically.'- );- if (- renderMode === 'mp4' ||- renderMode === 'h264' ||- renderMode === 'h265'- ) {+export const getOutputFilename = (codec: Codec): string => {+ let filename = getUserPassedOutputLocation();+ let extension = getUserPassedFileExtension();+ if (extension === null && codec !== 'png') {+ if (codec === 'h264' || codec === 'h265') {+ console.info('No file extension specified, adding .mp4 automatically.');filename += '.mp4';+ extension = 'mp4';}- if (renderMode === 'vp8' || renderMode === 'vp9') {+ if (codec === 'vp8' || codec === 'vp9') {+ console.log('No file extension specified, adding .webm automatically');filename += '.webm';+ extension = 'webm';}}- if (extension === 'mp4') {- if (renderMode === 'mp4') {- console.info(- 'You have specified a .mp4 extension, encoding it using the default H264 codec. To use H265 codec use --format=h265 flag.'- );- }- }- if (extension === 'webm') {- if (renderMode === 'mp4') {- console.info(- 'You have specified a .webm extension, encoding it using the default VP8 codec. To use VP9 codec use --format=vp9 flag.'- );- Config.Output.setOutputFormat('vp8');- renderMode = 'vp8';+ if (codec === 'h264') {+ if (extension !== 'mp4') {+ console.error('The output filename must end in .mp4.');+ process.exit(1);}}- if (renderMode === 'mp4' || renderMode === 'h264' || renderMode === 'h265') {- if (hasExtension && extension !== 'mp4') {- console.error('The output filename must end in .mp4.');+ if (codec === 'h265') {+ if (extension !== 'mp4' && extension !== 'hevc') {+ console.error('The output filename must end in .mp4 or .hevc');process.exit(1);}}- if (renderMode === 'vp8' || renderMode === 'vp9') {- if (hasExtension && extension !== 'webm') {+ if (codec === 'vp8' || codec === 'vp9') {+ if (extension !== 'webm') {console.error('The output filename must end in .webm.');process.exit(1);}}- if (renderMode === 'png') {- if (hasExtension) {+ if (codec === 'png') {+ if (extension !== null) {console.error('The output directory cannot have an extension.');process.exit(1);}diff --git a/packages/cli/src/image-formats.ts b/packages/cli/src/image-formats.tsindex 9bcb74bf4c..535b7cc580 100644--- a/packages/cli/src/image-formats.ts+++ b/packages/cli/src/image-formats.ts@@ -1,10 +1,9 @@-import {OutputFormat} from 'remotion';+import {Codec} from 'remotion';export type FrameFormat = 'png' | 'jpeg';-export const getFrameFormat = (format: OutputFormat): FrameFormat => {+export const getFrameFormat = (format: Codec): FrameFormat => {if (- format === 'mp4' ||format === 'h264' ||format === 'h265' ||format === 'vp8' ||diff --git a/packages/cli/src/parse-command-line.ts b/packages/cli/src/parse-command-line.tsindex 7ae5a397d3..9b41ba1a3c 100644--- a/packages/cli/src/parse-command-line.ts+++ b/packages/cli/src/parse-command-line.ts@@ -1,5 +1,6 @@import minimist from 'minimist';-import {Config, OutputFormat, PixelFormat} from 'remotion';+import {Config, PixelFormat} from 'remotion';+import {CodecOrUndefined} from 'remotion/dist/config/codec';export type CommandLineOptions = {pixelFormat: PixelFormat;@@ -9,7 +10,7 @@ export type CommandLineOptions = {png: boolean;quality: number | undefined;force: boolean;- format: OutputFormat;+ format: CodecOrUndefined;props: string;};diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsxindex e22dd2c5ba..0353bf112e 100644--- a/packages/cli/src/render.tsx+++ b/packages/cli/src/render.tsx@@ -11,6 +11,7 @@ import fs from 'fs';import os from 'os';import path from 'path';import {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';@@ -18,6 +19,7 @@ import {getUserProps} from './get-user-props';import {getFrameFormat} from './image-formats';import {loadConfigFile} from './load-config';import {parseCommandLine} from './parse-command-line';+import {getUserPassedFileExtension} from './user-passed-output-location';export const render = async () => {const args = process.argv;@@ -28,8 +30,12 @@ export const render = async () => {loadConfigFile(configFileName);parseCommandLine();const parallelism = Internals.getConcurrency();- const renderMode = Internals.getOutputFormat();- const outputFile = getOutputFilename(renderMode);+ const codec = getFinalOutputCodec({+ codec: Internals.getOutputCodecOrUndefined(),+ fileExtension: getUserPassedFileExtension(),+ emitWarning: true,+ });+ const outputFile = getOutputFilename(codec);const overwrite = Internals.getShouldOverwrite();const userProps = getUserProps();const quality = Internals.getQuality();@@ -42,20 +48,19 @@ export const render = async () => {process.exit(1);}if (- renderMode === 'mp4' ||- renderMode === 'h264' ||- renderMode === 'h265' ||- renderMode === 'vp8' ||- renderMode === 'vp9'+ codec === 'h264' ||+ codec === 'h265' ||+ codec === 'vp8' ||+ codec === 'vp9') {await validateFfmpeg();}- if (renderMode === 'png') {+ if (codec === 'png') {fs.mkdirSync(absoluteOutputFile, {recursive: true,});}- const steps = renderMode === 'png' ? 2 : 3;+ const steps = codec === 'png' ? 2 : 3;process.stdout.write(`π¦ (1/${steps}) Bundling video...\n`);const bundlingProgress = new cliProgress.Bar(@@ -83,7 +88,7 @@ export const render = async () => {const {durationInFrames: frames} = config;const outputDir =- renderMode === 'png'+ codec === 'png'? absoluteOutputFile: await fs.promises.mkdtemp(path.join(os.tmpdir(), 'react-motion-render')@@ -113,7 +118,7 @@ export const render = async () => {},userProps,webpackBundle: bundled,- imageFormat: getFrameFormat(renderMode),+ imageFormat: getFrameFormat(codec),quality,});renderProgress.stop();@@ -121,11 +126,10 @@ export const render = async () => {Internals.perf.logPerf();}if (- renderMode === 'mp4' ||- renderMode === 'h264' ||- renderMode === 'h265' ||- renderMode === 'vp8' ||- renderMode === 'vp9'+ codec === 'h264' ||+ codec === 'h265' ||+ codec === 'vp8' ||+ codec === 'vp9') {process.stdout.write(`π§΅ (3/${steps}) Stitching frames together...\n`);await stitchFramesToVideo({@@ -135,9 +139,9 @@ export const render = async () => {fps: config.fps,outputLocation: absoluteOutputFile,force: overwrite,- imageFormat: getFrameFormat(renderMode),+ imageFormat: getFrameFormat(codec),pixelFormat: Internals.getPixelFormat(),- outputFormat: Internals.getOutputFormat(),+ outputFormat: codec,});console.log('Cleaning up...');await fs.promises.rmdir(outputDir, {diff --git a/packages/cli/src/user-passed-output-location.ts b/packages/cli/src/user-passed-output-location.tsnew file mode 100644index 0000000000..a93d3683d1--- /dev/null+++ b/packages/cli/src/user-passed-output-location.ts@@ -0,0 +1,21 @@+import minimist from 'minimist';++export const getUserPassedOutputLocation = () => {+ const args = minimist(process.argv.slice(2));+ if (!args._[3]) {+ console.log('Pass an extra argument <output-filename>.');+ process.exit(1);+ }+ const filename = args._[3];+ return filename;+};++export const getUserPassedFileExtension = () => {+ const filename = getUserPassedOutputLocation();+ const filenameArr = filename.split('.');++ const hasExtension = filenameArr.length >= 2;+ const filenameArrLength = filenameArr.length;+ const extension = hasExtension ? filenameArr[filenameArrLength - 1] : null;+ return extension;+};diff --git a/packages/core/src/config/codec.ts b/packages/core/src/config/codec.tsnew file mode 100644index 0000000000..fc8a79e11b--- /dev/null+++ b/packages/core/src/config/codec.ts@@ -0,0 +1,53 @@+const validRenderModes = ['png', 'h264', 'h265', 'vp8', 'vp9'] as const;++export type Codec = typeof validRenderModes[number];+export type CodecOrUndefined = Codec | undefined;++let format: CodecOrUndefined = undefined;++export const getOutputCodecOrUndefined = (): CodecOrUndefined => {+ return format;+};++export const getFinalOutputCodec = ({+ codec,+ fileExtension,+ emitWarning,+}: {+ codec: CodecOrUndefined;+ fileExtension: string | null;+ emitWarning: boolean;+}): Codec => {+ if (codec === undefined && fileExtension === 'webm') {+ if (emitWarning) {+ console.info(+ 'You have specified a .webm extension, using the VP8 encoder. Use --codec=vp9 to use the Vp9 encoder.'+ );+ }+ return 'vp8';+ }+ if (codec === undefined && fileExtension === 'hevc') {+ if (emitWarning) {+ console.info(+ 'You have specified a .hevc extension, using it using H265 encoder.'+ );+ }+ return 'h265';+ }+ return codec ?? 'h264';+};++export const setOutputFormat = (newRenderMode: CodecOrUndefined) => {+ if (newRenderMode === undefined) {+ format = undefined;+ return;+ }+ if (!validRenderModes.includes(newRenderMode)) {+ throw new Error(+ `Render mode must be one of the following: ${validRenderModes.join(+ ', '+ )}, but got ${newRenderMode}`+ );+ }+ format = newRenderMode;+};diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.tsindex 716acf7a49..21f7e5d38a 100644--- a/packages/core/src/config/index.ts+++ b/packages/core/src/config/index.ts@@ -1,3 +1,4 @@+import {setOutputFormat} from './codec';import {Concurrency, setConcurrency} from './concurrency';import {overrideWebpackConfig,@@ -7,7 +8,6 @@ import {import {setOverwriteOutput} from './overwrite';import {PixelFormat, setPixelFormat} from './pixel-format';import {setQuality} from './quality';-import {OutputFormat, setOutputFormat} from './render-mode';export const Config = {Bundling: {@@ -52,10 +52,5 @@ export const Config = {},} as const;-export type {- PixelFormat,- Concurrency,- OutputFormat,- WebpackConfiguration,- WebpackOverrideFn,-};+export {Codec, getFinalOutputCodec} from './codec';+export type {PixelFormat, Concurrency, WebpackConfiguration, WebpackOverrideFn};diff --git a/packages/core/src/config/render-mode.ts b/packages/core/src/config/render-mode.tsdeleted file mode 100644index 14ebd8d349..0000000000--- a/packages/core/src/config/render-mode.ts+++ /dev/null@@ -1,20 +0,0 @@-const validRenderModes = ['png', 'mp4', 'h264', 'h265', 'vp8', 'vp9'] as const;--export type OutputFormat = typeof validRenderModes[number];--let format: OutputFormat = 'mp4';--export const getOutputFormat = (): OutputFormat => {- return format;-};--export const setOutputFormat = (newRenderMode: OutputFormat) => {- if (!validRenderModes.includes(newRenderMode)) {- throw new Error(- `Render mode must be one of the following: ${validRenderModes.join(- ', '- )}, but got ${newRenderMode}`- );- }- format = newRenderMode;-};diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex 2bfb00f36f..cec346d640 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -4,6 +4,7 @@ import {TComposition,TSequence,} from './CompositionManager';+import {getOutputCodecOrUndefined} from './config/codec';import {getConcurrency} from './config/concurrency';import {getWebpackOverrideFn,@@ -12,7 +13,6 @@ import {import {getShouldOverwrite} from './config/overwrite';import {getPixelFormat} from './config/pixel-format';import {getQuality} from './config/quality';-import {getOutputFormat} from './config/render-mode';import * as perf from './perf';import {getCompositionName, getIsEvaluation, getRoot} from './register-root';import {RemotionRoot} from './RemotionRoot';@@ -35,7 +35,7 @@ export const Internals = {getPixelFormat,getConcurrency,getShouldOverwrite,- getOutputFormat,+ getOutputCodecOrUndefined,getWebpackOverrideFn,getQuality,};diff --git a/packages/renderer/src/stitcher.ts b/packages/renderer/src/stitcher.tsindex f6a82708a5..1b921a1c33 100644--- a/packages/renderer/src/stitcher.ts+++ b/packages/renderer/src/stitcher.ts@@ -1,18 +1,23 @@import execa from 'execa';import fs from 'fs';-import {OutputFormat, PixelFormat} from 'remotion';+import {Codec, PixelFormat} from 'remotion';import {DEFAULT_IMAGE_FORMAT, ImageFormat} from './image-format';import {validateFfmpeg} from './validate-ffmpeg';-const getCodecName = (outputFormat: OutputFormat): string => {- if (outputFormat === 'mp4' || outputFormat === 'h264') {+const getCodecName = (codec: Codec): string => {+ if (codec === 'h264') {return 'libx264';}- if (outputFormat === 'h265') {+ if (codec === 'h265') {return 'libx265';}-- return outputFormat === 'vp8' ? 'libvpx' : 'libvpx-vp9';+ if (codec === 'vp8') {+ return 'libvpx';+ }+ if (codec === 'vp9') {+ return 'libvpx-vp9';+ }+ throw new TypeError(`Cannot find FFMPEG codec for ${codec}`);};export const stitchFramesToVideo = async (options: {@@ -24,7 +29,7 @@ export const stitchFramesToVideo = async (options: {force: boolean;imageFormat?: ImageFormat;pixelFormat?: PixelFormat;- outputFormat: OutputFormat;+ outputFormat: Codec;}): Promise<void> => {const format = options.imageFormat ?? DEFAULT_IMAGE_FORMAT;await validateFfmpeg();