🎥 Make videos programmatically with React
allow to omit output name
Jonny Burger2efa7097bd15
diff --git a/packages/cli/src/compositions.ts b/packages/cli/src/compositions.tsindex 49101202a9..788cd172ce 100644--- a/packages/cli/src/compositions.ts+++ b/packages/cli/src/compositions.ts@@ -49,11 +49,9 @@ export const listCompositionsCommand = async () => {inputProps,puppeteerTimeout,port,- // TODO: Refactor compositionName out} = await getCliOptions({isLambda: false,type: 'get-compositions',- compositionName: 'abc',});const bundled = await bundleOnCli({fullPath, steps: ['bundling']});diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.tsindex 93e11719d1..079c03fea4 100644--- a/packages/cli/src/get-cli-options.ts+++ b/packages/cli/src/get-cli-options.ts@@ -5,7 +5,6 @@ import path from 'path';import type {BrowserExecutable, Codec, FrameRange, PixelFormat} from 'remotion';import {Internals} from 'remotion';import {getEnvironmentVariables} from './get-env';-import {getOutputFilename} from './get-filename';import {getInputProps} from './get-input-props';import {getImageFormat} from './image-formats';import {Log} from './log';@@ -24,25 +23,14 @@ const getAndValidateFrameRange = () => {return frameRange;};-const getFinalCodec = async (options: {- isLambda: boolean;- compositionName: string;-}) => {+const getFinalCodec = async (options: {isLambda: boolean}) => {const userCodec = Internals.getOutputCodecOrUndefined();const codec = Internals.getFinalOutputCodec({codec: userCodec,fileExtension: options.isLambda? null- : RenderInternals.getExtensionOfFilename(- getUserPassedOutputLocation(- RenderInternals.getFileExtensionFromCodec(- userCodec ?? 'h264',- 'final'- ),- options.compositionName- )- ),+ : RenderInternals.getExtensionOfFilename(getUserPassedOutputLocation()),emitWarning: true,});const ffmpegExecutable = Internals.getCustomFfmpegExecutable();@@ -99,11 +87,14 @@ const getFinalCodec = async (options: {const getBrowser = () => Internals.getBrowser() ?? Internals.DEFAULT_BROWSER;-const getAndValidateAbsoluteOutputFile = (- outputFile: string,+export const getAndValidateAbsoluteOutputFile = (+ relativeOutputLocation: string,overwrite: boolean) => {- const absoluteOutputFile = path.resolve(process.cwd(), outputFile);+ const absoluteOutputFile = path.resolve(+ process.cwd(),+ relativeOutputLocation+ );if (fs.existsSync(absoluteOutputFile) && !overwrite) {Log.error(`File at ${absoluteOutputFile} already exists. Use --overwrite to overwrite.`@@ -195,7 +186,6 @@ const getAndValidateBrowser = async (browserExecutable: BrowserExecutable) => {export const getCliOptions = async (options: {isLambda: boolean;type: 'still' | 'series' | 'get-compositions';- compositionName: string;}) => {const frameRange = getAndValidateFrameRange();@@ -204,7 +194,6 @@ export const getCliOptions = async (options: {? 'h264': await getFinalCodec({isLambda: options.isLambda,- compositionName: options.compositionName,});const shouldOutputImageSequence =options.type === 'still'@@ -213,15 +202,6 @@ export const getCliOptions = async (options: {frameRange,isLambda: options.isLambda,});- const outputFile =- options.isLambda || options.type === 'get-compositions'- ? null- : getOutputFilename({- codec,- imageSequence: shouldOutputImageSequence,- type: options.type,- compositionName: options.compositionName,- });const overwrite = Internals.getShouldOverwrite();const crf = getAndValidateCrf(shouldOutputImageSequence, codec);@@ -259,13 +239,9 @@ export const getCliOptions = async (options: {frameRange,shouldOutputImageSequence,codec,- overwrite: Internals.getShouldOverwrite(),inputProps: getInputProps(() => undefined),envVariables: await getEnvironmentVariables(),quality: Internals.getQuality(),- absoluteOutputFile: outputFile- ? getAndValidateAbsoluteOutputFile(outputFile, overwrite)- : null,browser: await getAndValidateBrowser(browserExecutable),crf,pixelFormat,@@ -280,6 +256,7 @@ export const getCliOptions = async (options: {logLevel: Internals.Logging.getLogLevel(),scale,chromiumOptions,+ overwrite,port: port ?? null,};};diff --git a/packages/cli/src/get-composition-id.ts b/packages/cli/src/get-composition-id.tsindex c08bd150bd..f27c31a1cd 100644--- a/packages/cli/src/get-composition-id.ts+++ b/packages/cli/src/get-composition-id.ts@@ -1,14 +1,10 @@-import type {TCompMetadata} from 'remotion';import {Log} from './log';import {parsedCli} from './parse-command-line';-export const getCompositionId = (comps: TCompMetadata[]) => {+export const getCompositionId = () => {if (!parsedCli._[2]) {Log.error('Composition ID not passed.');- Log.error(- 'Pass an extra argument <composition-id>. The following video names are available:'- );- Log.error(`${comps.map((c) => c.id).join(', ')}`);+ Log.error('Pass an extra argument <composition-id>.');process.exit(1);}diff --git a/packages/cli/src/get-filename.ts b/packages/cli/src/get-filename.tsindex 8a56a8f874..17d6d70654 100644--- a/packages/cli/src/get-filename.ts+++ b/packages/cli/src/get-filename.ts@@ -1,26 +1,23 @@import {RenderInternals} from '@remotion/renderer';import type {Codec} from 'remotion';import {Log} from './log';-import {getUserPassedOutputLocation} from './user-passed-output-location';+import {getOutputLocation} from './user-passed-output-location';export const getOutputFilename = ({codec,imageSequence,- type,compositionName,+ defaultExtension,}: {codec: Codec;imageSequence: boolean;- type: 'still' | 'series';compositionName: string;+ defaultExtension: string;}): string => {- let filename = getUserPassedOutputLocation(- RenderInternals.getFileExtensionFromCodec(codec, 'final'),- compositionName- );- if (type === 'still') {- return filename;- }+ let filename = getOutputLocation({+ compositionId: compositionName,+ defaultExtension,+ });let extension = RenderInternals.getExtensionOfFilename(filename);if (imageSequence) {diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsxindex 5f22fdf6d3..483bb7cefd 100644--- a/packages/cli/src/render.tsx+++ b/packages/cli/src/render.tsx@@ -11,8 +11,12 @@ import os from 'os';import path from 'path';import {Internals} from 'remotion';import {chalk} from './chalk';-import {getCliOptions} from './get-cli-options';+import {+ getAndValidateAbsoluteOutputFile,+ getCliOptions,+} from './get-cli-options';import {getCompositionId} from './get-composition-id';+import {getOutputFilename} from './get-filename';import {initializeRenderCli} from './initialize-render-cli';import {Log} from './log';import {parsedCli, quietFlagProvided} from './parse-command-line';@@ -49,7 +53,6 @@ export const render = async () => {parallelism,frameRange,shouldOutputImageSequence,- absoluteOutputFile,overwrite,inputProps,envVariables,@@ -70,14 +73,27 @@ export const render = async () => {} = await getCliOptions({isLambda: false,type: 'series',- compositionName: getCompositionId([]),});- if (!absoluteOutputFile) {- throw new Error(- 'assertion error - expected absoluteOutputFile to not be null'- );- }+ const relativeOutputLocation = getOutputFilename({+ codec,+ imageSequence: shouldOutputImageSequence,+ compositionName: getCompositionId(),+ defaultExtension: RenderInternals.getFileExtensionFromCodec(codec, 'final'),+ });++ const absoluteOutputFile = getAndValidateAbsoluteOutputFile(+ relativeOutputLocation,+ overwrite+ );++ const compositionId = getCompositionId();++ Log.info(+ chalk.gray(+ `Composition = ${compositionId}, Codec = ${codec}, Output = ${relativeOutputLocation}`+ )+ );Log.verbose('Browser executable: ', browserExecutable);@@ -134,7 +150,6 @@ export const render = async () => {chromiumOptions,browserExecutable,});- const compositionId = getCompositionId(comps);const config = comps.find((c) => c.id === compositionId);diff --git a/packages/cli/src/still.ts b/packages/cli/src/still.tsindex 43e489b6a1..d80a28bee0 100644--- a/packages/cli/src/still.ts+++ b/packages/cli/src/still.ts@@ -9,7 +9,10 @@ import {mkdirSync} from 'fs';import path from 'path';import {Config, Internals} from 'remotion';import {chalk} from './chalk';-import {getCliOptions} from './get-cli-options';+import {+ getAndValidateAbsoluteOutputFile,+ getCliOptions,+} from './get-cli-options';import {getCompositionId} from './get-composition-id';import {initializeRenderCli} from './initialize-render-cli';import {Log} from './log';@@ -21,7 +24,10 @@ import {} from './progress-bar';import {bundleOnCli} from './setup-cache';import type {RenderStep} from './step';-import {getUserPassedOutputLocation} from './user-passed-output-location';+import {+ getOutputLocation,+ getUserPassedOutputLocation,+} from './user-passed-output-location';export const still = async () => {const startTime = Date.now();@@ -32,6 +38,24 @@ export const still = async () => {await initializeRenderCli('still');+ const userPassedOutput = getUserPassedOutputLocation();+ if (+ userPassedOutput?.endsWith('.jpeg') ||+ userPassedOutput?.endsWith('.jpg')+ ) {+ Log.verbose(+ 'Output file has a JPEG extension, setting the image format to JPEG.'+ );+ Config.Rendering.setImageFormat('jpeg');+ }++ if (userPassedOutput?.endsWith('.png')) {+ Log.verbose(+ 'Output file has a PNG extension, setting the image format to PNG.'+ );+ Config.Rendering.setImageFormat('png');+ }+const {inputProps,envVariables,@@ -50,29 +74,27 @@ export const still = async () => {} = await getCliOptions({isLambda: false,type: 'still',- compositionName: getCompositionId([]),});- const userOutput = path.resolve(- process.cwd(),- getUserPassedOutputLocation(imageFormat, getCompositionId([]))- );+ Log.verbose('Browser executable: ', browserExecutable);- if (userOutput.endsWith('.jpeg') || userOutput.endsWith('.jpg')) {- Log.verbose(- 'Output file has a JPEG extension, therefore setting the image format to JPEG.'- );- Config.Rendering.setImageFormat('jpeg');- }+ const compositionId = getCompositionId();- if (userOutput.endsWith('.png')) {- Log.verbose(- 'Output file has a PNG extension, therefore setting the image format to PNG.'- );- Config.Rendering.setImageFormat('png');- }+ const relativeOutputLocation = getOutputLocation({+ compositionId,+ defaultExtension: imageFormat,+ });- Log.verbose('Browser executable: ', browserExecutable);+ const absoluteOutputLocation = getAndValidateAbsoluteOutputFile(+ relativeOutputLocation,+ overwrite+ );++ Log.info(+ chalk.gray(+ `Output = ${relativeOutputLocation}, Format = ${imageFormat}, Composition = ${compositionId}`+ )+ );if (imageFormat === 'none') {Log.error(@@ -81,19 +103,19 @@ export const still = async () => {process.exit(1);}- if (imageFormat === 'png' && !userOutput.endsWith('.png')) {+ if (imageFormat === 'png' && !absoluteOutputLocation.endsWith('.png')) {Log.warn(- `Rendering a PNG, expected a .png extension but got ${userOutput}`+ `Rendering a PNG, expected a .png extension but got ${absoluteOutputLocation}`);}if (imageFormat === 'jpeg' &&- !userOutput.endsWith('.jpg') &&- !userOutput.endsWith('.jpeg')+ !absoluteOutputLocation.endsWith('.jpg') &&+ !absoluteOutputLocation.endsWith('.jpeg')) {Log.warn(- `Rendering a JPEG, expected a .jpg or .jpeg extension but got ${userOutput}`+ `Rendering a JPEG, expected a .jpg or .jpeg extension but got ${absoluteOutputLocation}`);}@@ -108,7 +130,7 @@ export const still = async () => {forceDeviceScaleFactor: scale,});- mkdirSync(path.join(userOutput, '..'), {+ mkdirSync(path.join(absoluteOutputLocation, '..'), {recursive: true,});@@ -133,7 +155,6 @@ export const still = async () => {ffmpegExecutable,ffprobeExecutable,});- const compositionId = getCompositionId(comps);const composition = comps.find((c) => c.id === compositionId);if (!composition) {@@ -186,7 +207,7 @@ export const still = async () => {await renderStill({composition,frame: stillFrame,- output: userOutput,+ output: absoluteOutputLocation,serveUrl: await urlOrBundle,quality,dumpBrowserLogs: Internals.Logging.isEqualOrBelowLogLevel(@@ -222,6 +243,6 @@ export const still = async () => {].join(' '));Log.info('-', 'Output can be found at:');- Log.info(chalk.cyan(`▶️ ${userOutput}`));+ Log.info(chalk.cyan(`▶️ ${absoluteOutputLocation}`));await closeBrowserPromise;};diff --git a/packages/cli/src/user-passed-output-location.ts b/packages/cli/src/user-passed-output-location.tsindex b997176fe5..6b10fb2540 100644--- a/packages/cli/src/user-passed-output-location.ts+++ b/packages/cli/src/user-passed-output-location.ts@@ -1,15 +1,35 @@import {Internals} from 'remotion';import {parsedCli} from './parse-command-line';-export const getUserPassedOutputLocation = (- defaultExtension: string,- compositionName: string-) => {+export const getUserPassedOutputLocation = () => {const filename = parsedCli._[3]? parsedCli._[3]: Internals.getOutputLocation();- const defaultName = `${compositionName}.${defaultExtension}`;+ return filename;+};++const getDefaultOutLocation = ({+ compositionName,+ defaultExtension,+}: {+ compositionName: string;+ defaultExtension: string;+}) => {+ const defaultName = `out/${compositionName}.${defaultExtension}`;++ return defaultName;+};- return filename ?? defaultName;+export const getOutputLocation = ({+ compositionId,+ defaultExtension,+}: {+ compositionId: string;+ defaultExtension: string;+}) => {+ return (+ getUserPassedOutputLocation() ??+ getDefaultOutLocation({compositionName: compositionId, defaultExtension})+ );};diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.tsindex 73a0c6675b..a7396c3738 100644--- a/packages/lambda/src/cli/commands/render/render.ts+++ b/packages/lambda/src/cli/commands/render/render.ts@@ -70,7 +70,6 @@ export const renderCommand = async (args: string[]) => {} = await CliInternals.getCliOptions({type: 'series',isLambda: true,- compositionName: composition,});const functionName = await findFunctionName();diff --git a/packages/lambda/src/cli/commands/still.ts b/packages/lambda/src/cli/commands/still.tsindex 6b87a30c03..84405ac5c7 100644--- a/packages/lambda/src/cli/commands/still.ts+++ b/packages/lambda/src/cli/commands/still.ts@@ -61,7 +61,6 @@ export const stillCommand = async (args: string[]) => {} = await CliInternals.getCliOptions({type: 'still',isLambda: true,- compositionName: composition,});const functionName = await findFunctionName();diff --git a/packages/renderer/src/get-extension-of-filename.ts b/packages/renderer/src/get-extension-of-filename.tsindex bbc3c40bf5..c5d5740c47 100644--- a/packages/renderer/src/get-extension-of-filename.ts+++ b/packages/renderer/src/get-extension-of-filename.ts@@ -1,4 +1,10 @@-export const getExtensionOfFilename = (filename: string) => {+export const getExtensionOfFilename = (+ filename: string | null+): string | null => {+ if (filename === null) {+ return null;+ }+const filenameArr = filename.split('.');const hasExtension = filenameArr.length >= 2;