Skip to content

🎥 Make videos programmatically with React

MKV support

Jonny Burger310bddce3b5a
diff --git a/packages/cli/src/get-filename.ts b/packages/cli/src/get-filename.ts
index 00b73a4842..8b8601636c 100644
--- a/packages/cli/src/get-filename.ts
+++ b/packages/cli/src/get-filename.ts
@@ -31,6 +31,12 @@ export const getOutputFilename = (
extension = 'mp4';
}
+ if (codec === 'h264-mkv') {
+ Log.info('No file extension specified, adding .mkv automatically.');
+ filename += '.mkv';
+ extension = 'mkv';
+ }
+
if (codec === 'vp8' || codec === 'vp9') {
Log.info('No file extension specified, adding .webm automatically.');
filename += '.webm';
@@ -45,9 +51,9 @@ export const getOutputFilename = (
}
if (codec === 'h264') {
- if (extension !== 'mp4') {
+ if (extension !== 'mp4' && extension !== 'mkv') {
Log.error(
- 'When using the H264 codec, the output filename must end in .mp4.'
+ 'When using the H264 codec, the output filename must end in .mp4 or .mkv.'
);
process.exit(1);
}
diff --git a/packages/cli/src/image-formats.ts b/packages/cli/src/image-formats.ts
index f01b66d62d..2dbbd256f8 100644
--- a/packages/cli/src/image-formats.ts
+++ b/packages/cli/src/image-formats.ts
@@ -15,6 +15,7 @@ export const getImageFormat = (
if (
codec === 'h264' ||
+ codec === 'h264-mkv' ||
codec === 'h265' ||
codec === 'vp8' ||
codec === 'vp9' ||
diff --git a/packages/core/src/config/codec.ts b/packages/core/src/config/codec.ts
index 384a580352..e12075692a 100644
--- a/packages/core/src/config/codec.ts
+++ b/packages/core/src/config/codec.ts
@@ -7,6 +7,7 @@ const validCodecs = [
'aac',
'wav',
'prores',
+ 'h264-mkv',
] as const;
export type Codec = typeof validCodecs[number];
@@ -102,6 +103,16 @@ export const getFinalOutputCodec = ({
return 'aac';
}
+ if (inputCodec === undefined && fileExtension === 'mkv') {
+ if (emitWarning) {
+ console.info(
+ 'You have specified a .mkv extension, using the H264 encoder and WAV audio format.'
+ );
+ }
+
+ return 'h264-mkv';
+ }
+
return inputCodec ?? DEFAULT_CODEC;
};
diff --git a/packages/core/src/config/crf.ts b/packages/core/src/config/crf.ts
index 3ece1d34a3..3bc17c0142 100644
--- a/packages/core/src/config/crf.ts
+++ b/packages/core/src/config/crf.ts
@@ -22,7 +22,7 @@ export const getDefaultCrfForCodec = (codec: Codec): number => {
return 0;
}
- if (codec === 'h264') {
+ if (codec === 'h264' || codec === 'h264-mkv') {
return 18; // FFMPEG default 23
}
@@ -54,7 +54,7 @@ export const getValidCrfRanges = (codec: Codec): [number, number] => {
return [0, 0];
}
- if (codec === 'h264') {
+ if (codec === 'h264' || codec === 'h264-mkv') {
return [1, 51];
}
@@ -78,7 +78,7 @@ export const validateSelectedCrfAndCodecCombination = (
codec: Codec
) => {
const range = getValidCrfRanges(codec);
- if (crf === 0 && codec === 'h264') {
+ if (crf === 0 && (codec === 'h264' || codec === 'h264-mkv')) {
throw new TypeError(
"Setting the CRF to 0 with a H264 codec is not supported anymore because of it's inconsistencies between platforms. Videos with CRF 0 cannot be played on iOS/macOS. 0 is a extreme value with inefficient settings which you probably want. Set CRF to a higher value to fix this error."
);
diff --git a/packages/core/src/test/codec.test.ts b/packages/core/src/test/codec.test.ts
index 9e1af7db9c..a3ffddfb9a 100644
--- a/packages/core/src/test/codec.test.ts
+++ b/packages/core/src/test/codec.test.ts
@@ -4,7 +4,7 @@ import {
getOutputCodecOrUndefined,
setCodec,
} from '../config/codec';
-import {expectToThrow} from "./expect-to-throw";
+import {expectToThrow} from './expect-to-throw';
// getFinalOutputCodec
@@ -17,8 +17,9 @@ describe('Codec tests valid codec input', () => {
'mp3',
'aac',
'wav',
+ 'h264-mkv',
];
- validCodecInput.forEach(entry =>
+ validCodecInput.forEach((entry) =>
test(`codec ${entry}`, () =>
expect(
getFinalOutputCodec({
@@ -61,7 +62,7 @@ describe('Codec tests undefined codec input with known extension', () => {
describe('Codec tests undefined codec input with unknown extension', () => {
const unknownExtensions = ['', 'abc'];
- unknownExtensions.forEach(entry =>
+ unknownExtensions.forEach((entry) =>
test(`testing with "${entry}" as extension`, () =>
expect(
getFinalOutputCodec({
@@ -83,7 +84,7 @@ describe('Codec tests setOutputFormat', () => {
'vp9',
undefined,
];
- validCodecInputs.forEach(entry =>
+ validCodecInputs.forEach((entry) =>
test(`testing with ${entry}`, () => {
setCodec(entry);
expect(getOutputCodecOrUndefined()).toEqual(entry);
@@ -91,6 +92,9 @@ describe('Codec tests setOutputFormat', () => {
);
test('setCodec with invalid coded', () => {
// @ts-expect-error
- expectToThrow(() => setCodec('invalid'), /Codec must be one of the following:/)
- })
+ expectToThrow(
+ () => setCodec('invalid'),
+ /Codec must be one of the following:/
+ );
+ });
});
diff --git a/packages/docs/docs/cli.md b/packages/docs/docs/cli.md
index 21701a747e..55be0a5c86 100644
--- a/packages/docs/docs/cli.md
+++ b/packages/docs/docs/cli.md
@@ -55,7 +55,7 @@ Specify a location for a dotenv file. Default `.env`. Available in v2.2 and late
### `--codec`
-[`h264` or `h265` or `png` or `vp8` or `vp9` or `mp3` or `aac` or `wav` or `prores`](/docs/config#setcodec). If you don't supply `--codec`, it will use the H.264 encoder. Available since v1.4.
+[`h264` or `h265` or `png` or `vp8` or `vp9` or `mp3` or `aac` or `wav` or `prores` or `h264-mkv`](/docs/config#setcodec). If you don't supply `--codec`, it will use the H.264 encoder. Available since v1.4.
### `--prores-profile`
diff --git a/packages/docs/docs/config.mdx b/packages/docs/docs/config.mdx
index 047549988a..99700d4dc9 100644
--- a/packages/docs/docs/config.mdx
+++ b/packages/docs/docs/config.mdx
@@ -213,6 +213,7 @@ Choose one of the supported codecs: `h264` _(default)_, `h265`, `vp8`, `vp9`.
- `mp3` will export audio only as an MP3 file _(available from v2.0)_
- `wav` will export audio only as an WAV file _(available from v2.0)_
- `aac` will export audio only as an AAC file _(available from v2.0)_
+- `mkv` will export using H264 codec, MKV container format and WAV audio codec. _(available from v2.1.12)_
```tsx
Config.Output.setCodec('h265');
diff --git a/packages/docs/docs/encoding.md b/packages/docs/docs/encoding.md
index 0aae16ad57..a35894a930 100644
--- a/packages/docs/docs/encoding.md
+++ b/packages/docs/docs/encoding.md
@@ -19,7 +19,7 @@ Remotion supports 5 video codecs: `h264` (_default_), `h265`, `vp8`, `vp9`, `pro
</tr>
<tr>
<td>H.264 <sub>also known as MPEG-4</sub></td>
- <td>.mp4</td>
+ <td>.mp4 or .mkv</td>
<td style={{color: 'red'}}>Large</td>
<td style={{color: 'green', fontWeight: 'bold'}}>Very fast</td>
<td><a href="https://caniuse.com/mpeg4" style={{color: 'green', fontWeight: 'bold'}}>Very good</a></td>
@@ -227,6 +227,14 @@ Higher bitrate means higher quality and higher file size.
You can pass `mp3`, `wav` or `aac` as a codec. If you do it, an audio file will be output in the corresponding format. Quality settings will be ignored.
+## Use .mkv container format
+
+You can set the codec to `h264-mkv` to use the mkv container format together with the H264 codec. If you select this option, the audio will be encoded losslessly using the WAV codec.
+
+:::info
+This preset was created for Remotion Lambda, and is optimal for when concatenating multiple video clips into one.
+:::
+
## What other settings do you need?
Which of the dozens of options that FFMPEG supports would you like to see exposed in Remotion? Let us know by opening an [issue on our issue tracker!](https://github.com/remotion-dev/remotion/issues)
diff --git a/packages/renderer/src/get-audio-codec-name.ts b/packages/renderer/src/get-audio-codec-name.ts
index 14aa7d44f5..099db83d4e 100644
--- a/packages/renderer/src/get-audio-codec-name.ts
+++ b/packages/renderer/src/get-audio-codec-name.ts
@@ -2,6 +2,12 @@ import {Codec, Internals} from 'remotion';
export const getAudioCodecName = (codec: Codec): string | null => {
if (!Internals.isAudioCodec(codec)) {
+ // The mkv container supports WAV, but MP4 does only support
+ // AAC. Choose MKV codec for better quality because we can put in lossless audio
+ if (codec === 'h264-mkv') {
+ return 'pcm_s16le';
+ }
+
return 'aac';
}
diff --git a/packages/renderer/src/get-codec-name.ts b/packages/renderer/src/get-codec-name.ts
index d6b1da92b3..5ee433c4c6 100644
--- a/packages/renderer/src/get-codec-name.ts
+++ b/packages/renderer/src/get-codec-name.ts
@@ -5,7 +5,7 @@ export const getCodecName = (codec: Codec): string | null => {
return null;
}
- if (codec === 'h264') {
+ if (codec === 'h264' || codec === 'h264-mkv') {
return 'libx264';
}