Skip to content

🎥 Make videos programmatically with React

set default codecs for file extensions

Jonny Burger5610ddd92e39
diff --git a/packages/cli/src/get-final-output-codec.ts b/packages/cli/src/get-final-output-codec.ts
index 4387fb09da..94223e4d18 100644
--- a/packages/cli/src/get-final-output-codec.ts
+++ b/packages/cli/src/get-final-output-codec.ts
@@ -1,12 +1,23 @@
-import type {Codec, CodecOrUndefined} from '@remotion/renderer';
+import type {Codec, CodecOrUndefined, FileExtension} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
-const deriveExtensionFromFilename = (extension: string | null) => {
+const deriveCodecsFromFilename = (
+ extension: string | null
+): {
+ possible: Codec[];
+ default: Codec | null;
+} => {
if (extension === null) {
- return [];
+ return {possible: [], default: null};
}
- return RenderInternals.makeFileExtensionMap()[extension] ?? [];
+ return {
+ default:
+ RenderInternals.defaultCodecsForFileExtension[
+ extension as FileExtension
+ ] ?? null,
+ possible: RenderInternals.makeFileExtensionMap()[extension] ?? [],
+ };
};
export const getFinalOutputCodec = ({
@@ -24,15 +35,14 @@ export const getFinalOutputCodec = ({
RenderInternals.getExtensionOfFilename(downloadName);
const outNameExtension = RenderInternals.getExtensionOfFilename(outName);
- const derivedDownloadCodecs = deriveExtensionFromFilename(
- downloadNameExtension
- );
- const derivedOutNameCodecs = deriveExtensionFromFilename(outNameExtension);
+ const derivedDownloadCodecs = deriveCodecsFromFilename(downloadNameExtension);
+ const derivedOutNameCodecs = deriveCodecsFromFilename(outNameExtension);
if (
- derivedDownloadCodecs.length > 0 &&
- derivedOutNameCodecs.length > 0 &&
- derivedDownloadCodecs.join('') !== derivedOutNameCodecs.join('')
+ derivedDownloadCodecs.possible.length > 0 &&
+ derivedOutNameCodecs.possible.length > 0 &&
+ derivedDownloadCodecs.possible.join('') !==
+ derivedOutNameCodecs.possible.join('')
) {
throw new TypeError(
`The download name is ${downloadName} but the output name is ${outName}. The file extensions must match`
@@ -41,22 +51,22 @@ export const getFinalOutputCodec = ({
if (cliFlag) {
if (
- derivedDownloadCodecs.length > 0 &&
- derivedDownloadCodecs.indexOf(cliFlag) === -1
+ derivedDownloadCodecs.possible.length > 0 &&
+ derivedDownloadCodecs.possible.indexOf(cliFlag) === -1
) {
throw new TypeError(
- `The download name is ${downloadName} but --codec=${cliFlag} was passed. The download name implies a codec of ${derivedDownloadCodecs.join(
+ `The download name is ${downloadName} but --codec=${cliFlag} was passed. The download name implies a codec of ${derivedDownloadCodecs.possible.join(
' or '
)} which does not align with the --codec flag.`
);
}
if (
- derivedOutNameCodecs.length > 0 &&
- derivedOutNameCodecs.indexOf(cliFlag) === -1
+ derivedOutNameCodecs.possible.length > 0 &&
+ derivedOutNameCodecs.possible.indexOf(cliFlag) === -1
) {
throw new TypeError(
- `The out name is ${outName} but --codec=${cliFlag} was passed. The out name implies a codec of ${derivedOutNameCodecs.join(
+ `The out name is ${outName} but --codec=${cliFlag} was passed. The out name implies a codec of ${derivedOutNameCodecs.possible.join(
' or '
)} which does not align with the --codec flag.`
);
@@ -65,16 +75,16 @@ export const getFinalOutputCodec = ({
return {codec: cliFlag, reason: 'from --codec flag'};
}
- if (derivedDownloadCodecs.length > 0) {
+ if (derivedDownloadCodecs.possible.length > 0) {
return {
- codec: derivedDownloadCodecs[0],
+ codec: derivedDownloadCodecs.default as Codec,
reason: 'derived from download name',
};
}
- if (derivedOutNameCodecs.length > 0) {
+ if (derivedOutNameCodecs.possible.length > 0) {
return {
- codec: derivedOutNameCodecs[0],
+ codec: derivedOutNameCodecs.default as Codec,
reason: 'derived from out name',
};
}
diff --git a/packages/renderer/src/get-extension-from-codec.ts b/packages/renderer/src/get-extension-from-codec.ts
index 3adc4be5b1..8f8cc928d9 100644
--- a/packages/renderer/src/get-extension-from-codec.ts
+++ b/packages/renderer/src/get-extension-from-codec.ts
@@ -1,7 +1,7 @@
import type {AudioCodec} from './audio-codec';
import type {Codec} from './codec';
import {validCodecs} from './codec';
-import type {supportedAudioCodecs} from './file-extensions';
+import type {FileExtension, supportedAudioCodecs} from './file-extensions';
import {defaultFileExtensionMap} from './file-extensions';
export const getFileExtensionFromCodec = <T extends Codec>(
@@ -36,14 +36,6 @@ export const getFileExtensionFromCodec = <T extends Codec>(
.default;
};
-export const deriveExtensionFromFilename = (extension: string | null) => {
- if (extension === null) {
- return [];
- }
-
- return makeFileExtensionMap()[extension] ?? [];
-};
-
export const makeFileExtensionMap = () => {
const map: Record<string, Codec[]> = {};
Object.keys(defaultFileExtensionMap).forEach(
@@ -80,3 +72,21 @@ export const makeFileExtensionMap = () => {
return map;
};
+
+export const defaultCodecsForFileExtension: Record<FileExtension, Codec> = {
+ '3gp': 'aac',
+ aac: 'aac',
+ gif: 'gif',
+ hevc: 'h265',
+ m4a: 'aac',
+ m4b: 'aac',
+ mkv: 'h264-mkv',
+ mov: 'prores',
+ mp3: 'mp3',
+ mp4: 'h264',
+ mpeg: 'aac',
+ mpg: 'aac',
+ mxf: 'prores',
+ wav: 'wav',
+ webm: 'vp8',
+};
diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts
index 955c63fd3a..82ca0497c0 100644
--- a/packages/renderer/src/index.ts
+++ b/packages/renderer/src/index.ts
@@ -24,7 +24,7 @@ import {validateFrameRange} from './frame-range';
import {getActualConcurrency} from './get-concurrency';
import {getFramesToRender} from './get-duration-from-frame-range';
import {
- deriveExtensionFromFilename,
+ defaultCodecsForFileExtension,
getFileExtensionFromCodec,
makeFileExtensionMap,
} from './get-extension-from-codec';
@@ -167,8 +167,8 @@ export const RenderInternals = {
validAudioCodecs,
defaultFileExtensionMap,
supportedAudioCodecs,
- deriveExtensionFromFilename,
makeFileExtensionMap,
+ defaultCodecsForFileExtension,
};
// Warn of potential performance issues with Apple Silicon (M1 chip under Rosetta)