Skip to content

🎥 Make videos programmatically with React

supported codec matrices

Jonny Burger04ac3ccd6e45
diff --git a/packages/lambda/src/functions/helpers/expected-out-name.ts b/packages/lambda/src/functions/helpers/expected-out-name.ts
index 1cfd877b97..99f96c2554 100644
--- a/packages/lambda/src/functions/helpers/expected-out-name.ts
+++ b/packages/lambda/src/functions/helpers/expected-out-name.ts
@@ -34,7 +34,11 @@ export const getExpectedOutName = (
renderMetadata,
});
if (outNameValue) {
- validateOutname(outNameValue);
+ validateOutname(
+ outNameValue,
+ renderMetadata.codec,
+ renderMetadata.audioCodec
+ );
return customOutName(renderMetadata.renderId, bucketName, outNameValue);
}
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 50b18ede31..4001dd6d21 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -227,7 +227,7 @@ const innerLaunchHandler = async (params: LambdaPayload, options: Options) => {
);
}
- validateOutname(params.outName, params.codec);
+ validateOutname(params.outName, params.codec, params.audioCodec);
validatePrivacy(params.privacy);
RenderInternals.validatePuppeteerTimeout(params.timeoutInMilliseconds);
diff --git a/packages/lambda/src/shared/validate-outname.ts b/packages/lambda/src/shared/validate-outname.ts
index 56d731bf44..1f52240e00 100644
--- a/packages/lambda/src/shared/validate-outname.ts
+++ b/packages/lambda/src/shared/validate-outname.ts
@@ -1,4 +1,4 @@
-import type {Codec} from '@remotion/renderer';
+import type {AudioCodec, Codec} from '@remotion/renderer';
import {RenderInternals, validateOutputFilename} from '@remotion/renderer';
import type {OutNameInputWithoutCredentials} from './constants';
import {validateBucketName} from './validate-bucketname';
@@ -21,7 +21,8 @@ const validateS3Key = (s3Key: string) => {
export const validateOutname = (
outName: OutNameInputWithoutCredentials | undefined | null,
- codec?: Codec
+ codec: Codec | null,
+ audioCodec: AudioCodec | null
) => {
if (typeof outName === 'undefined' || outName === null) {
return;
@@ -29,10 +30,12 @@ export const validateOutname = (
if (typeof outName === 'string') {
if (codec) {
- validateOutputFilename(
+ validateOutputFilename({
codec,
- RenderInternals.getExtensionOfFilename(outName ?? null)
- );
+ audioCodec,
+ extension: RenderInternals.getExtensionOfFilename(outName) as string,
+ preferLossless: false,
+ });
}
validateS3Key(outName);
diff --git a/packages/renderer/src/audio-codec.ts b/packages/renderer/src/audio-codec.ts
index 13a8e5e7b7..7f8fc467f8 100644
--- a/packages/renderer/src/audio-codec.ts
+++ b/packages/renderer/src/audio-codec.ts
@@ -4,18 +4,23 @@ export const validAudioCodecs = ['pcm-16', 'aac', 'mp3', 'opus'] as const;
export type AudioCodec = typeof validAudioCodecs[number];
-export const supportedAudioCodec: {[key in Codec]: AudioCodec[]} = {
- h264: ['aac'],
- 'h264-mkv': ['pcm-16'],
- aac: ['aac'],
- gif: [],
- h265: ['aac'],
- mp3: ['mp3'],
- prores: ['pcm-16', 'aac'],
- vp8: ['opus', 'pcm-16'],
- vp9: ['opus', 'pcm-16'],
- wav: ['pcm-16'],
-};
+export const supportedAudioCodec = {
+ h264: ['aac', 'pcm-16'] as const,
+ 'h264-mkv': ['pcm-16'] as const,
+ aac: ['aac'] as const,
+ gif: [] as const,
+ h265: ['aac'] as const,
+ mp3: ['mp3'] as const,
+ prores: ['pcm-16', 'aac'] as const,
+ vp8: ['opus', 'pcm-16'] as const,
+ vp9: ['opus', 'pcm-16'] as const,
+ wav: ['pcm-16'] as const,
+} as const;
+
+const _satisfies: {[key in Codec]: readonly AudioCodec[]} = supportedAudioCodec;
+if (_satisfies) {
+ // Just for type checking
+}
export const audioCodecNames = [
'pcm_s16le',
diff --git a/packages/renderer/src/get-extension-from-codec.ts b/packages/renderer/src/get-extension-from-codec.ts
index 6e8306a9c4..3f3a7d55af 100644
--- a/packages/renderer/src/get-extension-from-codec.ts
+++ b/packages/renderer/src/get-extension-from-codec.ts
@@ -1,9 +1,87 @@
-import type {AudioCodec} from './audio-codec';
+import type {AudioCodec, supportedAudioCodec} from './audio-codec';
import type {Codec} from './codec';
import {validCodecs} from './codec';
-export const getFileExtensionFromCodec = (
- codec: Codec,
+export const defaultFileExtensionMap: {
+ [key in Codec]: {
+ default: string;
+ forAudioCodec: {
+ [k in typeof supportedAudioCodec[key][number]]: {
+ possible: string[];
+ default: string;
+ };
+ };
+ };
+} = {
+ 'h264-mkv': {
+ default: 'mkv',
+ forAudioCodec: {
+ 'pcm-16': {possible: ['mkv'], default: 'mkv'},
+ },
+ },
+ aac: {
+ default: 'aac',
+ forAudioCodec: {
+ aac: {
+ possible: ['aac', '3gp', 'm4a', 'm4b', 'mpg', 'mpeg'],
+ default: 'aac',
+ },
+ },
+ },
+ gif: {
+ default: 'gif',
+ forAudioCodec: {},
+ },
+ h264: {
+ default: 'mp4',
+ forAudioCodec: {
+ 'pcm-16': {possible: ['mkv'], default: 'mkv'},
+ aac: {possible: ['mp4', 'mkv'], default: 'mp4'},
+ },
+ },
+ h265: {
+ default: 'mp4',
+ forAudioCodec: {
+ aac: {possible: ['mp4', 'mkv', 'hevc'], default: 'mp4'},
+ },
+ },
+ mp3: {
+ default: 'mp3',
+ forAudioCodec: {
+ mp3: {possible: ['mp3'], default: 'mp3'},
+ },
+ },
+ prores: {
+ default: 'mov',
+ forAudioCodec: {
+ aac: {possible: ['mov'], default: 'mov'},
+ 'pcm-16': {possible: ['mov', 'mkv', 'mxf'], default: 'mov'},
+ },
+ },
+ vp8: {
+ default: 'webm',
+ forAudioCodec: {
+ 'pcm-16': {possible: ['webm'], default: 'webm'},
+ opus: {possible: ['webm'], default: 'webm'},
+ },
+ },
+ vp9: {
+ default: 'webm',
+ forAudioCodec: {
+ 'pcm-16': {possible: ['webm'], default: 'webm'},
+ opus: {possible: ['webm'], default: 'webm'},
+ },
+ },
+ wav: {
+ default: 'wav',
+ forAudioCodec: {
+ 'pcm-16': {possible: ['wav'], default: 'wav'},
+ },
+ },
+};
+
+export const getFileExtensionFromCodec = <T extends Codec>(
+ codec: T,
audioCodec: AudioCodec | null
) => {
if (!validCodecs.includes(codec)) {
@@ -14,37 +92,22 @@ export const getFileExtensionFromCodec = (
);
}
- switch (codec) {
- case 'aac':
- return 'aac';
- case 'h264': {
- if (audioCodec === 'pcm-16') {
- return 'mkv';
- }
+ const map = defaultFileExtensionMap[
+ codec
+ ] as typeof defaultFileExtensionMap[T];
+ if (audioCodec === null) {
+ return map.default;
+ }
- return 'mp4';
- }
+ const typedAudioCodec =
+ audioCodec as keyof typeof defaultFileExtensionMap[Codec]['forAudioCodec'];
- // The chunks will be rendered as mkv, but the final output will still be MP4
- case 'h264-mkv':
- return 'mkv';
- case 'h265':
- return 'mp4';
- case 'mp3':
- return 'mp3';
- case 'prores':
- return 'mov';
- case 'vp8':
- return 'webm';
- case 'vp9':
- return 'webm';
- case 'gif':
- return 'gif';
- case 'wav':
- return 'wav';
- default:
- throw new Error(
- "Don't know which file extension to use for codec " + codec
- );
+ if (!(typedAudioCodec in map.forAudioCodec)) {
+ throw new Error(
+ `Audio codec ${typedAudioCodec} is not supported for codec ${codec}`
+ );
}
+
+ return map.forAudioCodec[audioCodec as typeof supportedAudioCodec[T][number]]
+ .default;
};
diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.ts
index fe089a9c91..2878ca7400 100644
--- a/packages/renderer/src/render-media.ts
+++ b/packages/renderer/src/render-media.ts
@@ -196,7 +196,12 @@ export const renderMedia = ({
});
validateSelectedPixelFormatAndCodecCombination(pixelFormat, codec);
if (outputLocation) {
- validateOutputFilename(codec, getExtensionOfFilename(outputLocation));
+ validateOutputFilename({
+ codec,
+ audioCodec: audioCodec ?? null,
+ extension: getExtensionOfFilename(outputLocation) as string,
+ preferLossless: options.preferLossless ?? false,
+ });
}
const absoluteOutputLocation = outputLocation
diff --git a/packages/renderer/src/validate-output-filename.ts b/packages/renderer/src/validate-output-filename.ts
index e485caa7f5..bcc44d60aa 100644
--- a/packages/renderer/src/validate-output-filename.ts
+++ b/packages/renderer/src/validate-output-filename.ts
@@ -1,84 +1,57 @@
+import type {AudioCodec, supportedAudioCodec} from './audio-codec';
+import {getDefaultAudioCodec} from './audio-codec';
import type {Codec} from './codec';
+import {defaultFileExtensionMap} from './get-extension-from-codec';
-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.'
- );
- }
+export const validateOutputFilename = <T extends Codec>({
+ codec,
+ audioCodec,
+ extension,
+ preferLossless,
+}: {
+ codec: T;
+ audioCodec: AudioCodec | null;
+ extension: string;
+ preferLossless: boolean;
+}) => {
+ if (!defaultFileExtensionMap[codec]) {
+ throw new TypeError(
+ `The codec "${codec}" is not supported. Supported codecs are: ${Object.keys(
+ defaultFileExtensionMap
+ ).join(', ')}`
+ );
}
- if (codec === 'h264-mkv') {
- if (extension !== 'mkv') {
- throw new TypeError(
- 'When using the "h264-mkv" codec, the output filename must end in ".mkv".'
- );
- }
- }
+ const map = defaultFileExtensionMap[codec];
+ const resolvedAudioCodec =
+ audioCodec ?? getDefaultAudioCodec({codec, preferLossless});
- if (codec === 'h265') {
- if (extension !== 'mp4' && extension !== 'hevc') {
+ if (resolvedAudioCodec === null) {
+ if (extension !== map.default) {
throw new TypeError(
- 'When using H265 codec, the output filename must end in .mp4 or .hevc.'
+ `When using the ${codec} codec, the output filename must end in .${map.default}.`
);
}
- }
- if (codec === 'vp8' || codec === 'vp9') {
- if (extension !== 'webm') {
- throw new TypeError(
- `When using the ${codec.toUpperCase()} codec, the output filename must end in .webm.`
- );
- }
+ return;
}
- 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 (!(resolvedAudioCodec in map.forAudioCodec)) {
+ throw new Error(
+ `Audio codec ${resolvedAudioCodec} is not supported for codec ${codec}`
+ );
}
- 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(', ')}`
- );
- }
- }
+ const acceptableExtensions =
+ map.forAudioCodec[
+ resolvedAudioCodec as typeof supportedAudioCodec[T][number]
+ ].possible;
- if (codec === 'wav') {
- if (extension !== 'wav') {
- throw new TypeError(
- "When using the 'wav' codec, the output location must end in .wav."
- );
- }
- }
-
- if (codec === 'gif') {
- if (extension !== 'gif') {
- throw new TypeError(
- 'When using the GIF codec, the output filename must end in .gif.'
- );
- }
+ if (!acceptableExtensions.includes(extension)) {
+ throw new TypeError(
+ `When using the ${codec} codec with the ${resolvedAudioCodec} audio codec, the output filename must end in one of the following: ${acceptableExtensions.join(
+ ', '
+ )}.`
+ );
}
};