🎥 Make videos programmatically with React
validate outfilename
Jonny Burger874b2bc4139c
diff --git a/packages/cli/src/get-cli-options.ts b/packages/cli/src/get-cli-options.tsindex a411cc189b..388738bee2 100644--- a/packages/cli/src/get-cli-options.ts+++ b/packages/cli/src/get-cli-options.ts@@ -13,7 +13,7 @@ import {getOutputFilename} from './get-filename';import {getInputProps} from './get-input-props';import {getImageFormat} from './image-formats';import {Log} from './log';-import {getUserPassedFileExtension} from './user-passed-output-location';+import {getUserPassedOutputLocation} from './user-passed-output-location';const getAndValidateFrameRange = () => {const frameRange = Internals.getRange();@@ -33,7 +33,9 @@ const getFinalCodec = async (options: {isLambda: boolean}) => {const codec = Internals.getFinalOutputCodec({codec: userCodec,- fileExtension: options.isLambda ? null : getUserPassedFileExtension(),+ fileExtension: options.isLambda+ ? null+ : RenderInternals.getExtensionOfFilename(getUserPassedOutputLocation()),emitWarning: true,isLambda: options.isLambda,});diff --git a/packages/cli/src/get-filename.ts b/packages/cli/src/get-filename.tsindex 8988675ad2..b236d3e28f 100644--- a/packages/cli/src/get-filename.ts+++ b/packages/cli/src/get-filename.ts@@ -1,11 +1,8 @@+import {RenderInternals} from '@remotion/renderer';import {Codec} from 'remotion';import {Log} from './log';-import {- getUserPassedFileExtension,- getUserPassedOutputLocation,-} from './user-passed-output-location';+import {getUserPassedOutputLocation} from './user-passed-output-location';-// eslint-disable-next-line complexityexport const getOutputFilename = ({codec,imageSequence,@@ -20,7 +17,7 @@ export const getOutputFilename = ({return filename;}- let extension = getUserPassedFileExtension();+ let extension = RenderInternals.getExtensionOfFilename(filename);if (imageSequence) {if (extension !== null) {Log.error(@@ -59,81 +56,5 @@ export const getOutputFilename = ({}}- if (codec === 'h264') {- if (extension !== 'mp4' && extension !== 'mkv') {- Log.error(- 'When using the H264 codec, the output filename must end in .mp4 or .mkv.'- );- process.exit(1);- }- }-- if (codec === 'h264-mkv') {- if (extension !== 'mkv') {- Log.error(- 'When using the "h264-mkv" codec, the output filename must end in ".mkv".'- );- process.exit(1);- }- }-- if (codec === 'h265') {- if (extension !== 'mp4' && extension !== 'hevc') {- Log.error(- 'When using H265 codec, the output filename must end in .mp4 or .hevc.'- );- process.exit(1);- }- }-- if (codec === 'vp8' || codec === 'vp9') {- if (extension !== 'webm') {- Log.error(- `When using the ${codec.toUpperCase()} codec, the output filename must end in .webm.`- );- process.exit(1);- }- }-- if (codec === 'prores') {- const allowedProResExtensions = ['mov', 'mkv', 'mxf'];- if (!extension || !allowedProResExtensions.includes(extension)) {- Log.error(- `When using the 'prores' codec, the output must end in one of those extensions: ${allowedProResExtensions- .map((a) => `.${a}`)- .join(', ')}`- );- process.exit(1);- }- }-- if (codec === 'mp3') {- if (extension !== 'mp3') {- Log.error("When using the 'mp3' codec, the output must end in .mp3");- process.exit(1);- }- }-- if (codec === 'aac') {- const allowedAacExtensions = ['aac', '3gp', 'm4a', 'm4b', 'mpg', 'mpeg'];- if (!extension || !allowedAacExtensions.includes(extension)) {- Log.error(- `When using the 'aac' codec, the output must end in one of those extensions: ${allowedAacExtensions- .map((a) => `.${a}`)- .join(', ')}`- );- process.exit(1);- }- }-- if (codec === 'wav') {- if (extension !== 'wav') {- Log.error(- "When using the 'wav' codec, the output location must end in .wav."- );- process.exit(1);- }- }-return filename;};diff --git a/packages/cli/src/user-passed-output-location.ts b/packages/cli/src/user-passed-output-location.tsindex d5b58dcd38..a4bebfcf7a 100644--- a/packages/cli/src/user-passed-output-location.ts+++ b/packages/cli/src/user-passed-output-location.ts@@ -10,13 +10,3 @@ export const getUserPassedOutputLocation = () => {const filename = parsedCli._[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/renderer/src/get-extension-of-filename.ts b/packages/renderer/src/get-extension-of-filename.tsnew file mode 100644index 0000000000..bbc3c40bf5--- /dev/null+++ b/packages/renderer/src/get-extension-of-filename.ts@@ -0,0 +1,8 @@+export const getExtensionOfFilename = (filename: string) => {+ 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/renderer/src/index.ts b/packages/renderer/src/index.tsindex 79f63887d4..fa6d6bd5e4 100644--- a/packages/renderer/src/index.ts+++ b/packages/renderer/src/index.ts@@ -11,6 +11,7 @@ import {import {getActualConcurrency} from './get-concurrency';import {getDurationFromFrameRange} from './get-duration-from-frame-range';import {getFileExtensionFromCodec} from './get-extension-from-codec';+import {getExtensionOfFilename} from './get-extension-of-filename';import {getRealFrameRange} from './get-frame-to-render';import {ensureLocalBrowser} from './get-local-browser-executable';import {isServeUrl} from './is-serve-url';@@ -74,4 +75,5 @@ export const RenderInternals = {symbolicateError,SymbolicateableError,getDurationFromFrameRange,+ getExtensionOfFilename,};diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.tsindex 75bb0bf624..561df88dfc 100644--- a/packages/renderer/src/render-media.ts+++ b/packages/renderer/src/render-media.ts@@ -19,6 +19,7 @@ import {ensureFramesInOrder} from './ensure-frames-in-order';import {ensureOutputDirectory} from './ensure-output-directory';import {getDurationFromFrameRange} from './get-duration-from-frame-range';import {getFileExtensionFromCodec} from './get-extension-from-codec';+import {getExtensionOfFilename} from './get-extension-of-filename';import {getRealFrameRange} from './get-frame-to-render';import {getServeUrlWithFallback,@@ -31,6 +32,7 @@ import {stitchFramesToVideo} from './stitch-frames-to-video';import {tmpDir} from './tmp-dir';import {OnStartData} from './types';import {validateEvenDimensionsWithCodec} from './validate-even-dimensions-with-codec';+import {validateOutputFilename} from './validate-output-filename';import {validateScale} from './validate-scale';export type StitchingState = 'encoding' | 'muxing';@@ -102,6 +104,8 @@ export const renderMedia = async ({Internals.validateSelectedCrfAndCodecCombination(crf, codec);}+ validateOutputFilename(codec, getExtensionOfFilename(outputLocation));+validateScale(scale);const serveUrl = getServeUrlWithFallback(options);diff --git a/packages/renderer/src/validate-output-filename.ts b/packages/renderer/src/validate-output-filename.tsnew file mode 100644index 0000000000..4863b0f0a1--- /dev/null+++ b/packages/renderer/src/validate-output-filename.ts@@ -0,0 +1,76 @@+import {Codec} from 'remotion';++export const validateOutputFilename = (+ codec: Codec,+ extension: string | null+) => {+ if (codec === 'h264') {+ if (extension !== 'mp4' && extension !== 'mkv') {+ throw new TypeError(+ 'When using the H264 codec, the output filename must end in .mp4 or .mkv.'+ );+ }+ }++ if (codec === 'h264-mkv') {+ if (extension !== 'mkv') {+ throw new TypeError(+ 'When using the "h264-mkv" codec, the output filename must end in ".mkv".'+ );+ }+ }++ if (codec === 'h265') {+ if (extension !== 'mp4' && extension !== 'hevc') {+ throw new TypeError(+ 'When using H265 codec, the output filename must end in .mp4 or .hevc.'+ );+ }+ }++ if (codec === 'vp8' || codec === 'vp9') {+ if (extension !== 'webm') {+ throw new TypeError(+ `When using the ${codec.toUpperCase()} codec, the output filename must end in .webm.`+ );+ }+ }++ if (codec === 'prores') {+ const allowedProResExtensions = ['mov', 'mkv', 'mxf'];+ if (!extension || !allowedProResExtensions.includes(extension)) {+ throw new TypeError(+ `When using the 'prores' codec, the output must end in one of those extensions: ${allowedProResExtensions+ .map((a) => `.${a}`)+ .join(', ')}`+ );+ }+ }++ if (codec === 'mp3') {+ if (extension !== 'mp3') {+ throw new TypeError(+ "When using the 'mp3' codec, the output must end in .mp3"+ );+ }+ }++ if (codec === 'aac') {+ const allowedAacExtensions = ['aac', '3gp', 'm4a', 'm4b', 'mpg', 'mpeg'];+ if (!extension || !allowedAacExtensions.includes(extension)) {+ throw new TypeError(+ `When using the 'aac' codec, the output must end in one of those extensions: ${allowedAacExtensions+ .map((a) => `.${a}`)+ .join(', ')}`+ );+ }+ }++ if (codec === 'wav') {+ if (extension !== 'wav') {+ throw new TypeError(+ "When using the 'wav' codec, the output location must end in .wav."+ );+ }+ }+};