Skip to content

🎥 Make videos programmatically with React

`@remotion/renderer`: Mux separateAudioTo AAC into .m4a, .m4b and .3gp containers

heojunfo63d2e8e10ec5
diff --git a/packages/renderer/src/options/audio-codec.tsx b/packages/renderer/src/options/audio-codec.tsx
index 49317c2fca..9fcf082304 100644
--- a/packages/renderer/src/options/audio-codec.tsx
+++ b/packages/renderer/src/options/audio-codec.tsx
@@ -1,4 +1,5 @@
import type {Codec} from '../codec';
+import {getMp4BrandForExtension} from '../get-mp4-brand';
import type {AnyRemotionOption} from './option';
import {separateAudioOption} from './separate-audio';
@@ -149,26 +150,32 @@ export const resolveAudioCodec = ({
let derivedFromSeparateAudioToExtension: AudioCodec | null = null;
if (separateAudioTo) {
- const extension = separateAudioTo.split('.').pop();
+ const extension = separateAudioTo.split('.').pop() ?? null;
for (const [key, value] of Object.entries(extensionMap)) {
if (value === extension) {
derivedFromSeparateAudioToExtension = key as AudioCodec;
- if (
- !(supportedAudioCodecs[codec] as readonly string[]).includes(
- derivedFromSeparateAudioToExtension as string,
- ) &&
- derivedFromSeparateAudioToExtension
- ) {
- throw new Error(
- `The codec is ${codec} but the audio codec derived from --${
- separateAudioOption.cliFlag
- } is ${derivedFromSeparateAudioToExtension}. The only supported codecs are: ${supportedAudioCodecs[
- codec
- ].join(', ')}`,
- );
- }
}
}
+
+ // .m4a, .m4b and .3gp are AAC in an MP4 container
+ if (getMp4BrandForExtension(extension)) {
+ derivedFromSeparateAudioToExtension = 'aac';
+ }
+
+ if (
+ derivedFromSeparateAudioToExtension &&
+ !(supportedAudioCodecs[codec] as readonly string[]).includes(
+ derivedFromSeparateAudioToExtension,
+ )
+ ) {
+ throw new Error(
+ `The codec is ${codec} but the audio codec derived from --${
+ separateAudioOption.cliFlag
+ } is ${derivedFromSeparateAudioToExtension}. The only supported codecs are: ${supportedAudioCodecs[
+ codec
+ ].join(', ')}`,
+ );
+ }
}
// Explanation: https://github.com/remotion-dev/remotion/issues/1647
diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.ts
index 43db83eafe..9f98151f35 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -552,12 +552,6 @@ const innerStitchFramesToVideo = async (
}
});
- if (separateAudioTo && audio) {
- const finalDestination = path.resolve(remotionRoot, separateAudioTo);
- cpSync(audio, finalDestination);
- rmSync(audio);
- }
-
await new Promise<void>((resolve, reject) => {
task.once('close', (code, signal) => {
if (code === 0) {
@@ -574,6 +568,39 @@ const innerStitchFramesToVideo = async (
});
});
+ if (separateAudioTo && audio) {
+ const finalDestination = path.resolve(remotionRoot, separateAudioTo);
+ const separateAudioExtension =
+ getExtensionOfFilename(finalDestination)?.toLowerCase() ?? null;
+ let separateAudioFile = audio;
+ if (getMp4BrandForExtension(separateAudioExtension)) {
+ // Like audio-only renders: the ADTS stream is remuxed into the
+ // container, away from the public path because of Fast Start.
+ separateAudioFile = path.join(
+ assetsInfo.downloadMap.stitchFrames,
+ `separate-audio.${separateAudioExtension}`,
+ );
+ await muxVideoAndAudio({
+ videoOutput: null,
+ audioOutput: audio,
+ output: separateAudioFile,
+ indent,
+ logLevel,
+ onProgress: () => undefined,
+ binariesDirectory,
+ fps,
+ cancelSignal: cancelSignal ?? undefined,
+ metadata: null,
+ numberOfGifLoops: null,
+ audioCodec: resolvedAudioCodec,
+ sampleRate,
+ });
+ }
+
+ cpSync(separateAudioFile, finalDestination);
+ rmSync(audio);
+ }
+
if (fastStartIntermediate && fastStartMuxer) {
const destination = outputLocation ?? tempFile;
if (destination === null) {