🎥 Make videos programmatically with React
`@remotion/renderer`: Mux audio-only AAC into .m4a, .m4b and .3gp containers
Ahadi5f5a4a46e692
diff --git a/packages/renderer/src/aac-priming.ts b/packages/renderer/src/aac-priming.tsindex f08fb72379..30146731a3 100644--- a/packages/renderer/src/aac-priming.ts+++ b/packages/renderer/src/aac-priming.ts@@ -1,3 +1,4 @@+import {getMp4BrandForExtension} from './get-mp4-brand';import type {AudioCodec} from './options/audio-codec';// libfdk_aac (AAC-LC) prepends 2048 samples of encoder delay ("priming") to@@ -23,8 +24,12 @@ export const getAacPrimingInputArgs = ({}// Only containers with edit lists can carry the shift- const extension = outputExtension?.toLowerCase();- if (extension !== 'mp4' && extension !== 'mov') {+ const extension = outputExtension?.toLowerCase() ?? null;+ if (+ extension !== 'mp4' &&+ extension !== 'mov' &&+ getMp4BrandForExtension(extension) === null+ ) {return [];}diff --git a/packages/renderer/src/get-mp4-brand.ts b/packages/renderer/src/get-mp4-brand.tsnew file mode 100644index 0000000000..fb4461502d--- /dev/null+++ b/packages/renderer/src/get-mp4-brand.ts@@ -0,0 +1,20 @@+// Audio-only AAC renders may be saved as .m4a, .m4b or .3gp. These are+// ISO BMFF files, but the compressed audio is ADTS, so it must be remuxed+// rather than copied. Remotion's FFmpeg build has no `ipod` or `3gp` muxer,+// so the `mp4` muxer is used with the major brand that players use to+// identify these files.+const mp4Brands: Record<string, string> = {+ m4a: 'M4A ',+ m4b: 'M4B ',+ '3gp': '3gp4',+};++export const getMp4BrandForExtension = (+ extension: string | null,+): string | null => {+ if (extension === null) {+ return null;+ }++ return mp4Brands[extension.toLowerCase()] ?? null;+};diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.tsindex d500029b1c..22032166e2 100644--- a/packages/renderer/src/index.ts+++ b/packages/renderer/src/index.ts@@ -33,6 +33,7 @@ import {} from './get-extension-from-codec';import {getExtensionOfFilename} from './get-extension-of-filename';import {getRealFrameRange, getRealFrameRanges} from './get-frame-to-render';+import {getMp4BrandForExtension} from './get-mp4-brand';import {getDesiredPort} from './get-port';import {DEFAULT_STILL_IMAGE_FORMAT,@@ -217,6 +218,7 @@ export const RenderInternals = {SymbolicateableError,getFramesToRender,getExtensionOfFilename,+ getMp4BrandForExtension,getDesiredPort,isPathInside,execa,diff --git a/packages/renderer/src/mux-video-and-audio.ts b/packages/renderer/src/mux-video-and-audio.tsindex ad0d00ec69..4c44a599ee 100644--- a/packages/renderer/src/mux-video-and-audio.ts+++ b/packages/renderer/src/mux-video-and-audio.ts@@ -3,6 +3,7 @@ import {callFf} from './call-ffmpeg';import {convertNumberOfGifLoopsToFfmpegSyntax} from './convert-number-of-gif-loops-to-ffmpeg';import {getExtensionOfFilename} from './get-extension-of-filename';import {getFastStartMuxer} from './get-fast-start-muxer';+import {getMp4BrandForExtension} from './get-mp4-brand';import type {LogLevel} from './log-level';import {Log} from './logger';import type {CancelSignal} from './make-cancel-signal';@@ -46,6 +47,7 @@ export const muxVideoAndAudio = async ({const fastStartMuxer = outputExtension? getFastStartMuxer(outputExtension): null;+ const mp4Brand = getMp4BrandForExtension(outputExtension);const command = ['-hide_banner',@@ -73,8 +75,12 @@ export const muxVideoAndAudio = async ({numberOfGifLoops === null? null: convertNumberOfGifLoopsToFfmpegSyntax(numberOfGifLoops),- fastStartMuxer ? '-movflags' : null,- fastStartMuxer ? 'faststart' : null,+ mp4Brand ? '-f' : null,+ mp4Brand ? 'mp4' : null,+ mp4Brand ? '-brand' : null,+ mp4Brand,+ fastStartMuxer || mp4Brand ? '-movflags' : null,+ fastStartMuxer || mp4Brand ? 'faststart' : null,...makeMetadataArgs(metadata ?? {}),'-y',output,diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.tsindex 30a7eb51f2..c04e76450e 100644--- a/packages/renderer/src/stitch-frames-to-video.ts+++ b/packages/renderer/src/stitch-frames-to-video.ts@@ -23,6 +23,7 @@ import {getCpuCount} from './get-cpu-count';import {getFileExtensionFromCodec} from './get-extension-from-codec';import {getExtensionOfFilename} from './get-extension-of-filename';import {getFastStartMuxer} from './get-fast-start-muxer';+import {getMp4BrandForExtension} from './get-mp4-brand';import {getProResProfileName} from './get-prores-profile-name';import type {LogLevel} from './log-level';import {Log} from './logger';@@ -30,6 +31,7 @@ import type {CancelSignal} from './make-cancel-signal';import {cancelErrorMessages} from './make-cancel-signal';import {makeMetadataArgs} from './make-metadata-args';import {getMaxLambdaMemory} from './memory/from-lambda-env';+import {muxVideoAndAudio} from './mux-video-and-audio';import type {AudioCodec} from './options/audio-codec';import {resolveAudioCodec} from './options/audio-codec';import {DEFAULT_COLOR_SPACE, type ColorSpace} from './options/color-space';@@ -353,7 +355,27 @@ const innerStitchFramesToVideo = async ();}- cpSync(audio, outputLocation ?? (tempFile as string));+ const audioOnlyDestination = outputLocation ?? (tempFile as string);+ if (getMp4BrandForExtension(outputExtension)) {+ await muxVideoAndAudio({+ videoOutput: null,+ audioOutput: audio,+ output: path.resolve(remotionRoot, audioOnlyDestination),+ indent,+ logLevel,+ onProgress: () => undefined,+ binariesDirectory,+ fps,+ cancelSignal: cancelSignal ?? undefined,+ metadata,+ numberOfGifLoops: null,+ audioCodec: resolvedAudioCodec,+ sampleRate,+ });+ } else {+ cpSync(audio, audioOnlyDestination);+ }+onProgress?.(Math.round(assetsInfo.chunkLengthInSeconds * fps));deleteDirectory(path.dirname(audio));diff --git a/packages/serverless/src/concat-videos.ts b/packages/serverless/src/concat-videos.tsindex 19a2f60f7a..3c9151dd0e 100644--- a/packages/serverless/src/concat-videos.ts+++ b/packages/serverless/src/concat-videos.ts@@ -32,6 +32,7 @@ export const concatVideos = async <Provider extends CloudProvider>({frameRange,compositionDurationInFrames,sampleRate,+ outputExtension,}: {onProgress: CombineChunksOnProgress;codec: ServerlessCodec;@@ -52,10 +53,16 @@ export const concatVideos = async <Provider extends CloudProvider>({everyNthFrame: number;frameRange: SingleFrameRange | null;sampleRate: number;+ outputExtension: string | null;}) => {+ // .m4a, .m4b and .3gp need to be muxed into their container, otherwise the+ // ADTS stream would be uploaded as-is under that name.+ const extension = RenderInternals.getMp4BrandForExtension(outputExtension)+ ? (outputExtension as string)+ : RenderInternals.getFileExtensionFromCodec(codec, audioCodec);const outfile = join(RenderInternals.tmpDir(REMOTION_CONCATENATED_TOKEN),- `concat.${RenderInternals.getFileExtensionFromCodec(codec, audioCodec)}`,+ `concat.${extension}`,);const combine = insideFunctionSpecifics.timer('Combine chunks', logLevel);diff --git a/packages/serverless/src/merge-chunks.ts b/packages/serverless/src/merge-chunks.tsindex 5e8e402d63..8124131f3e 100644--- a/packages/serverless/src/merge-chunks.ts+++ b/packages/serverless/src/merge-chunks.ts@@ -4,6 +4,7 @@ import type {LogLevel,SingleFrameRange,} from '@remotion/renderer';+import {RenderInternals} from '@remotion/renderer';import type {DownloadBehavior} from '@remotion/serverless-client';import {type CloudProvider,@@ -92,6 +93,7 @@ export const mergeChunksAndFinishRender = async <everyNthFrame: options.everyNthFrame,frameRange: options.frameRange,sampleRate: options.sampleRate,+ outputExtension: RenderInternals.getExtensionOfFilename(options.key),});const encodingStop = Date.now();options.overallProgress.setTimeToCombine(encodingStop - encodingStart);