Skip to content

🎥 Make videos programmatically with React

`@remotion/lambda`: Fix muted renders

Jonny Burger53cfbba87b51
diff --git a/packages/lambda/src/functions/helpers/concat-videos.ts b/packages/lambda/src/functions/helpers/concat-videos.ts
index 07d4845325..bb9bd46c3a 100644
--- a/packages/lambda/src/functions/helpers/concat-videos.ts
+++ b/packages/lambda/src/functions/helpers/concat-videos.ts
@@ -206,6 +206,7 @@ export const concatVideosS3 = async ({
binariesDirectory,
cancelSignal,
preferLossless,
+ muted,
}: {
onProgress: (frames: number) => void;
numberOfFrames: number;
@@ -221,6 +222,7 @@ export const concatVideosS3 = async ({
binariesDirectory: string | null;
cancelSignal: CancelSignal | undefined;
preferLossless: boolean;
+ muted: boolean;
}) => {
const outfile = join(
RenderInternals.tmpDir(REMOTION_CONCATED_TOKEN),
@@ -244,7 +246,7 @@ export const concatVideosS3 = async ({
);
const seamlessVideo = canConcatVideoSeamlessly(codec);
- await RenderInternals.combineVideos({
+ await RenderInternals.combineChunks({
files,
filelistDir,
output: outfile,
@@ -262,6 +264,7 @@ export const concatVideosS3 = async ({
cancelSignal,
seamlessAudio,
seamlessVideo,
+ muted,
});
combine.end();
diff --git a/packages/lambda/src/functions/helpers/merge-chunks.ts b/packages/lambda/src/functions/helpers/merge-chunks.ts
index d2a95efda1..8da1c20e99 100644
--- a/packages/lambda/src/functions/helpers/merge-chunks.ts
+++ b/packages/lambda/src/functions/helpers/merge-chunks.ts
@@ -171,6 +171,10 @@ export const mergeChunksAndFinishRender = async (options: {
compositionStart: options.compositionStart,
});
const encodingStart = Date.now();
+ if (options.renderMetadata.type === 'still') {
+ throw new Error('Cannot merge stills');
+ }
+
const {outfile, cleanupChunksProm} = await concatVideosS3({
onProgress,
numberOfFrames: options.frameCountLength,
@@ -186,6 +190,7 @@ export const mergeChunksAndFinishRender = async (options: {
binariesDirectory: options.binariesDirectory,
cancelSignal: undefined,
preferLossless: options.preferLossless,
+ muted: options.renderMetadata.muted,
});
const encodingStop = Date.now();
diff --git a/packages/lambda/src/functions/renderer.ts b/packages/lambda/src/functions/renderer.ts
index 99f1c72542..56c9ae4876 100644
--- a/packages/lambda/src/functions/renderer.ts
+++ b/packages/lambda/src/functions/renderer.ts
@@ -145,11 +145,26 @@ const renderHandler = async (
: null;
const videoOutputLocation = path.join(outdir, `${chunk}.${videoExtension}`);
- const audioOutputLocation = RenderInternals.isAudioCodec(params.codec)
- ? null
- : audioExtension
- ? path.join(outdir, `${chunk}.${audioExtension}`)
- : null;
+
+ const willRenderAudioEval = RenderInternals.getShouldRenderAudio({
+ assetsInfo: null,
+ codec: params.codec,
+ enforceAudioTrack: true,
+ muted: params.muted,
+ });
+
+ if (willRenderAudioEval === 'maybe') {
+ throw new Error('Cannot determine whether to render audio or not');
+ }
+
+ const audioOutputLocation =
+ willRenderAudioEval === 'no'
+ ? null
+ : RenderInternals.isAudioCodec(params.codec)
+ ? null
+ : audioExtension
+ ? path.join(outdir, `${chunk}.${audioExtension}`)
+ : null;
const resolvedProps = await resolvedPropsPromise;
const serializedInputPropsWithCustomSchema = await inputPropsPromise;
diff --git a/packages/lambda/src/test/integration/renders/muted-render.test.ts b/packages/lambda/src/test/integration/renders/muted-render.test.ts
new file mode 100644
index 0000000000..8988eb202f
--- /dev/null
+++ b/packages/lambda/src/test/integration/renders/muted-render.test.ts
@@ -0,0 +1,64 @@
+import {RenderInternals} from '@remotion/renderer';
+import {afterAll, expect, test} from 'vitest';
+import {deleteRender} from '../../../api/delete-render';
+import {rendersPrefix} from '../../../defaults';
+import {lambdaLs} from '../../../functions/helpers/io';
+import {simulateLambdaRender} from '../simulate-lambda-render';
+
+afterAll(async () => {
+ await RenderInternals.killAllBrowsers();
+});
+
+test('Should make muted render audio', async () => {
+ const {close, file, progress, renderId} = await simulateLambdaRender({
+ codec: 'h264',
+ composition: 'framer',
+ frameRange: [100, 110],
+ imageFormat: 'jpeg',
+ logLevel: 'error',
+ region: 'eu-central-1',
+ inputProps: {},
+ muted: true,
+ });
+
+ const out = await RenderInternals.callFf({
+ bin: 'ffprobe',
+ args: ['-'],
+ options: {
+ stdin: file,
+ },
+ indent: false,
+ binariesDirectory: null,
+ cancelSignal: undefined,
+ logLevel: 'error',
+ });
+
+ expect(out.stdout).not.toContain('Audio');
+ console.log(out);
+
+ const files = await lambdaLs({
+ bucketName: progress.outBucket as string,
+ region: 'eu-central-1',
+ expectedBucketOwner: 'abc',
+ prefix: rendersPrefix(renderId),
+ });
+
+ expect(files.length).toBe(3);
+
+ await deleteRender({
+ bucketName: progress.outBucket as string,
+ region: 'eu-central-1',
+ renderId,
+ });
+
+ const expectFiles = await lambdaLs({
+ bucketName: progress.outBucket as string,
+ region: 'eu-central-1',
+ expectedBucketOwner: 'abc',
+ prefix: rendersPrefix(renderId),
+ });
+
+ expect(expectFiles.length).toBe(0);
+
+ await close();
+});
diff --git a/packages/renderer/src/combine-videos.ts b/packages/renderer/src/combine-videos.ts
index 5253b5d41e..1e46801444 100644
--- a/packages/renderer/src/combine-videos.ts
+++ b/packages/renderer/src/combine-videos.ts
@@ -33,9 +33,10 @@ type Options = {
cancelSignal: CancelSignal | undefined;
seamlessAudio: boolean;
seamlessVideo: boolean;
+ muted: boolean;
};
-export const combineVideos = async ({
+export const combineChunks = async ({
files,
filelistDir,
output,
@@ -53,8 +54,9 @@ export const combineVideos = async ({
cancelSignal,
seamlessAudio,
seamlessVideo,
+ muted,
}: Options) => {
- const shouldCreateAudio = resolvedAudioCodec !== null;
+ const shouldCreateAudio = resolvedAudioCodec !== null && !muted;
const shouldCreateVideo = !isAudioCodec(codec);
const videoOutput = join(
@@ -89,8 +91,18 @@ export const combineVideos = async ({
Log.verbose(
{indent, logLevel},
`Combining chunks, audio = ${
- seamlessAudio ? 'seamlessly' : 'normally'
- }, video = ${seamlessVideo ? 'seamlessly' : 'normally'}`,
+ shouldCreateAudio === false
+ ? 'no'
+ : seamlessAudio
+ ? 'seamlessly'
+ : 'normally'
+ }, video = ${
+ shouldCreateVideo === false
+ ? 'no'
+ : seamlessVideo
+ ? 'seamlessly'
+ : 'normally'
+ }`,
);
await Promise.all(
[
@@ -138,12 +150,6 @@ export const combineVideos = async ({
].filter(truthy),
);
- // Either only audio or only video
- if (!(audioOutput && shouldCreateVideo)) {
- rmSync(filelistDir, {recursive: true});
- return;
- }
-
try {
await muxVideoAndAudio({
audioOutput,
diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts
index aa22497b65..94f14bb250 100644
--- a/packages/renderer/src/index.ts
+++ b/packages/renderer/src/index.ts
@@ -9,7 +9,7 @@ import {chalk} from './chalk';
import {isColorSupported} from './chalk/is-color-supported';
import {checkNodeVersionAndWarnAboutRosetta} from './check-version-requirements';
import {DEFAULT_CODEC, validCodecs} from './codec';
-import {combineVideos} from './combine-videos';
+import {combineChunks} from './combine-videos';
import {getExecutablePath} from './compositor/get-executable-path';
import {convertToPositiveFrameIndex} from './convert-to-positive-frame-index';
import {copyImageToClipboard} from './copy-to-clipboard';
@@ -137,6 +137,7 @@ import {
resolveAudioCodec,
supportedAudioCodecs,
} from './options/audio-codec';
+import {getShouldRenderAudio} from './render-has-audio';
import {validatePuppeteerTimeout} from './validate-puppeteer-timeout';
import {validateBitrate} from './validate-videobitrate';
import {
@@ -190,7 +191,7 @@ export const RenderInternals = {
convertToPositiveFrameIndex,
findRemotionRoot,
validateBitrate,
- combineVideos,
+ combineChunks,
getMinConcurrency,
getMaxConcurrency,
getDefaultAudioCodec,
@@ -227,6 +228,7 @@ export const RenderInternals = {
getExtensionFromAudioCodec,
makeFileExecutableIfItIsNot,
resolveAudioCodec,
+ getShouldRenderAudio,
};
// Warn of potential performance issues with Apple Silicon (M1 chip under Rosetta)
diff --git a/packages/renderer/src/mux-video-and-audio.ts b/packages/renderer/src/mux-video-and-audio.ts
index a9e415285a..9ae33baaf2 100644
--- a/packages/renderer/src/mux-video-and-audio.ts
+++ b/packages/renderer/src/mux-video-and-audio.ts
@@ -18,7 +18,7 @@ export const muxVideoAndAudio = async ({
cancelSignal,
}: {
videoOutput: string;
- audioOutput: string;
+ audioOutput: string | null;
output: string;
indent: boolean;
logLevel: LogLevel;
@@ -33,12 +33,12 @@ export const muxVideoAndAudio = async ({
'-hide_banner',
'-i',
videoOutput,
- '-i',
+ audioOutput ? '-i' : null,
audioOutput,
'-c:v',
'copy',
- '-c:a',
- 'copy',
+ audioOutput ? '-c:a' : null,
+ audioOutput ? 'copy' : null,
`-metadata`,
`comment=Made with Remotion ${VERSION}`,
'-y',
diff --git a/packages/renderer/src/render-has-audio.ts b/packages/renderer/src/render-has-audio.ts
new file mode 100644
index 0000000000..6b48a5af26
--- /dev/null
+++ b/packages/renderer/src/render-has-audio.ts
@@ -0,0 +1,33 @@
+import type {RenderAssetInfo} from './assets/download-map';
+import type {Codec} from './codec';
+import {codecSupportsMedia} from './codec-supports-media';
+
+export const getShouldRenderAudio = ({
+ codec,
+ assetsInfo,
+ enforceAudioTrack,
+ muted,
+}: {
+ codec: Codec;
+ assetsInfo: RenderAssetInfo | null;
+ enforceAudioTrack: boolean;
+ muted: boolean;
+}): 'yes' | 'maybe' | 'no' => {
+ if (muted) {
+ return 'no';
+ }
+
+ if (!codecSupportsMedia(codec).audio) {
+ return 'no';
+ }
+
+ if (enforceAudioTrack) {
+ return 'yes';
+ }
+
+ if (assetsInfo === null) {
+ return 'maybe';
+ }
+
+ return assetsInfo.assets.flat(1).length > 0 ? 'yes' : 'no';
+};
diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.ts
index be748ee160..a05478d4d5 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -35,6 +35,7 @@ import {
} from './pixel-format';
import type {ProResProfile} from './prores-profile';
import {validateSelectedCodecAndProResCombination} from './prores-profile';
+import {getShouldRenderAudio} from './render-has-audio';
import {validateDimension, validateFps} from './validate';
import {validateEvenDimensionsWithCodec} from './validate-even-dimensions-with-codec';
import {validateBitrate} from './validate-videobitrate';
@@ -163,11 +164,19 @@ const innerStitchFramesToVideo = async (
const mediaSupport = codecSupportsMedia(codec);
- const shouldRenderAudio =
- mediaSupport.audio &&
- (assetsInfo.assets.flat(1).length > 0 || enforceAudioTrack) &&
- !muted;
+ const renderAudioEvaluation = getShouldRenderAudio({
+ assetsInfo,
+ codec,
+ enforceAudioTrack,
+ muted,
+ });
+ if (renderAudioEvaluation === 'maybe') {
+ throw new Error(
+ 'Remotion is not sure whether to render audio. Please report this in the Remotion repo.',
+ );
+ }
+ const shouldRenderAudio = renderAudioEvaluation === 'yes';
const shouldRenderVideo = mediaSupport.video;
if (!shouldRenderAudio && !shouldRenderVideo) {