Skip to content

🎥 Make videos programmatically with React

parametrize audio bitrate

Jonny Burger72ca6079b417
diff --git a/packages/lambda/src/functions/helpers/concat-videos.ts b/packages/lambda/src/functions/helpers/concat-videos.ts
index 736d06ab3a..db30d5482b 100644
--- a/packages/lambda/src/functions/helpers/concat-videos.ts
+++ b/packages/lambda/src/functions/helpers/concat-videos.ts
@@ -182,6 +182,7 @@ export const concatVideosS3 = async ({
files,
outdir,
audioCodec,
+ audioBitrate,
}: {
onProgress: (frames: number) => void;
numberOfFrames: number;
@@ -191,6 +192,7 @@ export const concatVideosS3 = async ({
files: string[];
outdir: string;
audioCodec: AudioCodec | null;
+ audioBitrate: string | null;
}) => {
const outfile = join(
RenderInternals.tmpDir(REMOTION_CONCATED_TOKEN),
@@ -209,6 +211,7 @@ export const concatVideosS3 = async ({
fps,
numberOfGifLoops,
audioCodec,
+ audioBitrate,
});
combine.end();
diff --git a/packages/lambda/src/functions/helpers/merge-chunks.ts b/packages/lambda/src/functions/helpers/merge-chunks.ts
index f5087cb727..11912b998e 100644
--- a/packages/lambda/src/functions/helpers/merge-chunks.ts
+++ b/packages/lambda/src/functions/helpers/merge-chunks.ts
@@ -60,6 +60,7 @@ export const mergeChunksAndFinishRender = async (options: {
serializedResolvedProps: SerializedInputProps;
renderMetadata: RenderMetadata;
onAllChunks: OnAllChunksAvailable;
+ audioBitrate: string | null;
}): Promise<PostRenderData> => {
let lastProgressUploaded = 0;
@@ -159,6 +160,7 @@ export const mergeChunksAndFinishRender = async (options: {
files,
outdir,
audioCodec: options.audioCodec,
+ audioBitrate: options.audioBitrate,
});
const encodingStop = Date.now();
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 2fa75ccc65..a4a9d86674 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -298,6 +298,7 @@ const innerLaunchHandler = async ({
deleteAfter: params.deleteAfter,
numberOfGifLoops: params.numberOfGifLoops,
downloadBehavior: params.downloadBehavior,
+ audioBitrate: params.audioBitrate,
};
const {key, renderBucketName, customCredentials} = getExpectedOutName(
@@ -375,6 +376,7 @@ const innerLaunchHandler = async ({
verbose,
renderMetadata,
onAllChunks: onAllChunksAvailable,
+ audioBitrate: params.audioBitrate,
});
return postRenderData;
diff --git a/packages/lambda/src/functions/merge.ts b/packages/lambda/src/functions/merge.ts
index 10b6cdfd09..43db6d2abc 100644
--- a/packages/lambda/src/functions/merge.ts
+++ b/packages/lambda/src/functions/merge.ts
@@ -71,6 +71,7 @@ export const mergeHandler = async (
onAllChunks: () => {
RenderInternals.Log.info('All chunks have been downloaded now.');
},
+ audioBitrate: renderMetadata.audioBitrate,
});
return {type: 'success' as const, postRenderData};
diff --git a/packages/lambda/src/functions/still.ts b/packages/lambda/src/functions/still.ts
index 58b83887b6..23dd17eccf 100644
--- a/packages/lambda/src/functions/still.ts
+++ b/packages/lambda/src/functions/still.ts
@@ -159,6 +159,7 @@ const innerStillHandler = async ({
deleteAfter: lambdaParams.deleteAfter,
numberOfGifLoops: null,
downloadBehavior: lambdaParams.downloadBehavior,
+ audioBitrate: null,
};
await lambdaWriteFile({
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index 44610c838c..072a7368d3 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -460,6 +460,7 @@ export type RenderMetadata = Discriminated & {
everyNthFrame: number;
deleteAfter: DeleteAfter | null;
numberOfGifLoops: number | null;
+ audioBitrate: string | null;
downloadBehavior: DownloadBehavior;
};
diff --git a/packages/lambda/src/test/unit/expected-out-name.test.ts b/packages/lambda/src/test/unit/expected-out-name.test.ts
index cb20ef5885..68b032267a 100644
--- a/packages/lambda/src/test/unit/expected-out-name.test.ts
+++ b/packages/lambda/src/test/unit/expected-out-name.test.ts
@@ -40,6 +40,7 @@ const testRenderMetadata: RenderMetadata = {
deleteAfter: null,
numberOfGifLoops: null,
downloadBehavior: {type: 'play-in-browser'},
+ audioBitrate: null,
};
test('Should get a custom outname', () => {
diff --git a/packages/lambda/src/test/unit/price-calculation.test.ts b/packages/lambda/src/test/unit/price-calculation.test.ts
index 31a8e3c799..2594649ebc 100644
--- a/packages/lambda/src/test/unit/price-calculation.test.ts
+++ b/packages/lambda/src/test/unit/price-calculation.test.ts
@@ -16,6 +16,7 @@ test('Should not throw while calculating prices when time shifts occur', () => {
],
memorySizeInMb: 1024,
renderMetadata: {
+ audioBitrate: null,
codec: 'h264',
compositionId: 'react-svg',
estimatedRenderLambdaInvokations: 10,
diff --git a/packages/renderer/src/audio-codec.ts b/packages/renderer/src/audio-codec.ts
index a8cc9be51f..a75b625fca 100644
--- a/packages/renderer/src/audio-codec.ts
+++ b/packages/renderer/src/audio-codec.ts
@@ -23,7 +23,12 @@ if (_satisfies) {
// Just for type checking
}
-const audioCodecNames = ['pcm_s16le', 'aac', 'libmp3lame', 'libopus'] as const;
+const audioCodecNames = [
+ 'pcm_s16le',
+ 'libfdk_aac',
+ 'libmp3lame',
+ 'libopus',
+] as const;
type FfmpegAudioCodecName = (typeof audioCodecNames)[number];
@@ -31,7 +36,7 @@ export const mapAudioCodecToFfmpegAudioCodecName = (
audioCodec: AudioCodec,
): FfmpegAudioCodecName => {
if (audioCodec === 'aac') {
- return 'aac';
+ return 'libfdk_aac';
}
if (audioCodec === 'mp3') {
diff --git a/packages/renderer/src/combine-videos.ts b/packages/renderer/src/combine-videos.ts
index 840b6879cc..f77bb778a0 100644
--- a/packages/renderer/src/combine-videos.ts
+++ b/packages/renderer/src/combine-videos.ts
@@ -24,6 +24,7 @@ type Options = {
fps: number;
numberOfGifLoops: number | null;
audioCodec: AudioCodec | null;
+ audioBitrate: string | null;
};
export const combineVideos = async (options: Options) => {
@@ -37,6 +38,7 @@ export const combineVideos = async (options: Options) => {
fps,
numberOfGifLoops,
audioCodec,
+ audioBitrate,
} = options;
const fileList = files.map((p) => `file '${p}'`).join('\n');
@@ -67,9 +69,8 @@ export const combineVideos = async (options: Options) => {
resolvedAudioCodec
? mapAudioCodecToFfmpegAudioCodecName(resolvedAudioCodec)
: null,
- // Set max bitrate up to 512kbps, will choose lower if that's too much
- '-b:a',
- '512K',
+ audioBitrate ? '-b:a' : null,
+ audioBitrate ? audioBitrate : null,
codec === 'h264' ? '-movflags' : null,
codec === 'h264' ? 'faststart' : null,
'-y',
diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.ts
index 46e73d2cd0..7402c7fcec 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -366,9 +366,8 @@ const innerStitchFramesToVideo = async (
audio,
'-c:a',
mapAudioCodecToFfmpegAudioCodecName(resolvedAudioCodec),
- // Set bitrate up to 320k, for aac it might effectively be lower
- '-b:a',
- audioBitrate ?? '320k',
+ audioBitrate ? '-b:a' : undefined,
+ audioBitrate ? audioBitrate : undefined,
force ? '-y' : null,
outputLocation ?? tempFile,
].filter(Internals.truthy),