Skip to content

🎥 Make videos programmatically with React

Fix empty audio detection behind the v5 flag

JonnyBurgere07b9f4ab190
diff --git a/packages/docs/docs/5-0-migration.mdx b/packages/docs/docs/5-0-migration.mdx
index 2027848add..6dfd912547 100644
--- a/packages/docs/docs/5-0-migration.mdx
+++ b/packages/docs/docs/5-0-migration.mdx
@@ -31,6 +31,13 @@ Upgrade `remotion` and all packages starting with `@remotion` to the latest vers
Run `npm i `, `yarn`, `pnpm i` or `bun i` respectively afterwards.
+## Videos without audio no longer include a silent audio track
+
+In Remotion 4, video renders could include a silent audio track even when [`enforceAudioTrack`](/docs/renderer/render-media#enforceaudiotrack) was `false`.
+Remotion 5 omits the track when there are no audio assets, including renders whose only media sources are videos without audio streams. Audio-only exports still produce an audio file, even when silent.
+
+**Required action**: If you need a silent track, set `enforceAudioTrack: true` or pass [`--enforce-audio-track`](/docs/cli/render#--enforce-audio-track). This is also required when using `separateAudioTo` without audio or [rendering chunks for concatenation](/docs/distributed-rendering). Remotion Lambda already enforces audio tracks for its chunks. `muted: true` still takes precedence.
+
## Runtime requirements
The minimum Node version is now <MinNodeVersion />. The minimum Bun version is <MinBunVersion />.
@@ -40,7 +47,7 @@ If you use `@remotion/eslint-config`, the minimum ESLint version is now <MinEsli
## Telemetry is now mandatory for Company License customers
Previously, we had no idea how many renders our company license customers are making, leading to a lack of accountability.
-If you upgrade to Remotion 5.0, you must [enable telemetry](/docs/telemetry) if you are a company license holder or contact Remotion to arrange a plausible delivery of render counts.
+If you upgrade to Remotion 5.0, you must [enable telemetry](/docs/telemetry) if you are a company license holder or contact Remotion to arrange a plausible delivery of render counts.
**Required action**: Pass license key to rendering APIs and in the [Remotion config file](/docs/config). If you are eligible for the free license, pass `"free-license"` as the license key.
@@ -163,8 +170,8 @@ When using `loadFonts()` from `@remotion/google-fonts`, you must now specify whi
import {loadFont} from '@remotion/google-fonts/Roboto';
loadFont('normal', {
- weights: ['400', '700'],
- subsets: ['latin'],
+ weights: ['400', '700'],
+ subsets: ['latin'],
});
```
diff --git a/packages/renderer/src/create-audio.ts b/packages/renderer/src/create-audio.ts
index d8b96e59b6..d0251ae9c0 100644
--- a/packages/renderer/src/create-audio.ts
+++ b/packages/renderer/src/create-audio.ts
@@ -1,4 +1,5 @@
import path from 'path';
+import {NoReactInternals} from 'remotion/no-react';
import {calculateAssetPositions} from './assets/calculate-asset-positions';
import {convertAssetsToFileUrls} from './assets/convert-assets-to-file-urls';
import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';
@@ -46,6 +47,7 @@ export const createAudio = async ({
trimRightOffset,
forSeamlessAacConcatenation,
sampleRate,
+ enforceAudioTrack,
}: {
assets: FrameAndAssets[];
onDownload: RenderMediaOnDownload | undefined;
@@ -64,7 +66,8 @@ export const createAudio = async ({
trimRightOffset: number;
forSeamlessAacConcatenation: boolean;
sampleRate: number;
-}): Promise<string> => {
+ enforceAudioTrack: boolean;
+}): Promise<string | null> => {
const fileUrlAssets = await convertAssetsToFileUrls({
assets,
onDownload: onDownload ?? (() => () => undefined),
@@ -169,6 +172,16 @@ export const createAudio = async ({
]
: []),
];
+ if (
+ NoReactInternals.ENABLE_V5_BREAKING_CHANGES &&
+ !enforceAudioTrack &&
+ preprocessed.length === 0
+ ) {
+ deleteDirectory(downloadMap.audioMixing);
+ onProgress(1);
+ return null;
+ }
+
const merged = path.join(downloadMap.audioPreprocessing, 'merged.wav');
const extension = getExtensionFromAudioCodec(audioCodec);
const outName = path.join(
diff --git a/packages/renderer/src/options/enforce-audio.tsx b/packages/renderer/src/options/enforce-audio.tsx
index 079b458154..0839e930b0 100644
--- a/packages/renderer/src/options/enforce-audio.tsx
+++ b/packages/renderer/src/options/enforce-audio.tsx
@@ -14,7 +14,8 @@ export const enforceAudioOption = {
<p>
Defaults to <code>false</code>. If <code>true</code>, include a silent
audio track when the render contains no audio. If <code>false</code>,
- omit the audio track when no audio is present.
+ omit the audio track when no audio is present. Audio-only exports still
+ produce an audio file, even when silent.
</p>
<p>
{mode === 'cli' ? (
@@ -34,6 +35,10 @@ export const enforceAudioOption = {
<code>{mode === 'cli' ? '--muted' : 'muted: true'}</code> takes
precedence.
</p>
+ <p>
+ In Remotion 4, video renders may still include a silent audio track when
+ this option is <code>false</code>. This is fixed in Remotion 5.
+ </p>
</>
),
ssrName: 'enforceAudioTrack',
diff --git a/packages/renderer/src/render-has-audio.ts b/packages/renderer/src/render-has-audio.ts
index 6b48a5af26..a63e1732fa 100644
--- a/packages/renderer/src/render-has-audio.ts
+++ b/packages/renderer/src/render-has-audio.ts
@@ -1,3 +1,4 @@
+import {NoReactInternals} from 'remotion/no-react';
import type {RenderAssetInfo} from './assets/download-map';
import type {Codec} from './codec';
import {codecSupportsMedia} from './codec-supports-media';
@@ -29,5 +30,19 @@ export const getShouldRenderAudio = ({
return 'maybe';
}
+ if (NoReactInternals.ENABLE_V5_BREAKING_CHANGES) {
+ if (!codecSupportsMedia(codec).video) {
+ return 'yes';
+ }
+
+ return assetsInfo.assets.some(
+ (frame) =>
+ frame.audioAndVideoAssets.length > 0 ||
+ frame.inlineAudioAssets.length > 0,
+ )
+ ? 'yes'
+ : 'no';
+ }
+
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 cd8290c54f..cdfe963b29 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -325,6 +325,7 @@ const innerStitchFramesToVideo = async (
trimRightOffset: assetsInfo.trimRightOffset,
forSeamlessAacConcatenation: assetsInfo.forSeamlessAacConcatenation,
sampleRate,
+ enforceAudioTrack: enforceAudioTrack || !shouldRenderVideo,
})
: null;
@@ -369,6 +370,14 @@ const innerStitchFramesToVideo = async (
return Promise.resolve(file);
}
+ if (separateAudioTo && !audio) {
+ throw new Error(
+ `\`separateAudioTo\` was set to ${JSON.stringify(
+ separateAudioTo,
+ )}, but this render included no audio. Set \`enforceAudioTrack: true\` to render a silent audio file.`,
+ );
+ }
+
// Parallel encoding already resolved the encoder in the pre-stitcher.
const resolvedHardwareAcceleration = preEncodedFileLocation
? 'disable'
@@ -480,15 +489,7 @@ const innerStitchFramesToVideo = async (
}
});
- if (separateAudioTo) {
- if (!audio) {
- throw new Error(
- `\`separateAudioTo\` was set to ${JSON.stringify(
- separateAudioTo,
- )}, but this render included no audio`,
- );
- }
-
+ if (separateAudioTo && audio) {
const finalDestination = path.resolve(remotionRoot, separateAudioTo);
cpSync(audio, finalDestination);
rmSync(audio);
diff --git a/packages/renderer/src/test/empty-audio.test.ts b/packages/renderer/src/test/empty-audio.test.ts
new file mode 100644
index 0000000000..e2ecad4699
--- /dev/null
+++ b/packages/renderer/src/test/empty-audio.test.ts
@@ -0,0 +1,307 @@
+import {expect, test} from 'bun:test';
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import type {AudioOrVideoAsset, InlineAudioAsset} from 'remotion/no-react';
+import {NoReactInternals} from 'remotion/no-react';
+import {cleanDownloadMap, makeDownloadMap} from '../assets/download-map';
+import {callFf} from '../call-ffmpeg';
+import type {Codec} from '../codec';
+import {stitchFramesToVideo} from '../stitch-frames-to-video';
+
+test(
+ 'Video exports omit absent audio unless an audio track is enforced',
+ async () => {
+ const directory = await fs.promises.mkdtemp(
+ path.join(os.tmpdir(), 'remotion-empty-audio-'),
+ );
+ const sourceVideo = path.join(directory, 'source.mp4');
+ const examplePublic = path.join(__dirname, '../../../example/public');
+ const ffmpegOptions = {
+ indent: false,
+ logLevel: 'error',
+ binariesDirectory: null,
+ cancelSignal: undefined,
+ } as const;
+
+ try {
+ await fs.promises.copyFile(
+ path.join(examplePublic, 'stuttgart-pin.png'),
+ path.join(directory, 'frame-0.png'),
+ );
+ await callFf({
+ ...ffmpegOptions,
+ bin: 'ffmpeg',
+ args: [
+ '-framerate',
+ '1',
+ '-i',
+ path.join(directory, 'frame-0.png'),
+ '-an',
+ '-c:v',
+ 'libx264',
+ '-pix_fmt',
+ 'yuv420p',
+ sourceVideo,
+ ],
+ });
+
+ for (const scenario of [
+ {
+ name: 'empty',
+ source: null,
+ enforced: false,
+ muted: false,
+ codec: 'h264',
+ separate: false,
+ },
+ {
+ name: 'artifact',
+ source: null,
+ enforced: false,
+ muted: false,
+ codec: 'h264',
+ separate: false,
+ },
+ {
+ name: 'video-without-audio',
+ source: sourceVideo,
+ enforced: false,
+ muted: false,
+ codec: 'h264',
+ separate: false,
+ },
+ {
+ name: 'audio',
+ source: path.join(examplePublic, 'audio-48000hz.wav'),
+ enforced: false,
+ muted: false,
+ codec: 'h264',
+ separate: false,
+ },
+ {
+ name: 'inline',
+ source: null,
+ enforced: false,
+ muted: false,
+ codec: 'h264',
+ separate: false,
+ },
+ {
+ name: 'enforced-empty',
+ source: null,
+ enforced: true,
+ muted: false,
+ codec: 'h264',
+ separate: false,
+ },
+ {
+ name: 'enforced-video',
+ source: sourceVideo,
+ enforced: true,
+ muted: false,
+ codec: 'h264',
+ separate: false,
+ },
+ {
+ name: 'muted',
+ source: null,
+ enforced: true,
+ muted: true,
+ codec: 'h264',
+ separate: false,
+ },
+ {
+ name: 'audio-only',
+ source: null,
+ enforced: false,
+ muted: false,
+ codec: 'wav',
+ separate: false,
+ },
+ {
+ name: 'separate-empty',
+ source: null,
+ enforced: false,
+ muted: false,
+ codec: 'h264',
+ separate: true,
+ },
+ {
+ name: 'separate-enforced',
+ source: null,
+ enforced: true,
+ muted: false,
+ codec: 'h264',
+ separate: true,
+ },
+ ] satisfies {
+ name: string;
+ source: string | null;
+ enforced: boolean;
+ muted: boolean;
+ codec: Codec;
+ separate: boolean;
+ }[]) {
+ const downloadMap = makeDownloadMap(48000);
+ const output = path.join(
+ directory,
+ `${scenario.name}.${scenario.codec === 'wav' ? 'wav' : 'mp4'}`,
+ );
+ const separateAudioTo = scenario.separate
+ ? path.join(directory, `${scenario.name}.wav`)
+ : null;
+ const audioAndVideoAssets: AudioOrVideoAsset[] =
+ scenario.source === null
+ ? []
+ : [
+ {
+ type: scenario.source === sourceVideo ? 'video' : 'audio',
+ src: `data:${scenario.source === sourceVideo ? 'video/mp4' : 'audio/wav'};base64,${fs.readFileSync(scenario.source).toString('base64')}`,
+ id: 'source',
+ frame: 0,
+ mediaFrame: 0,
+ volume: 1,
+ playbackRate: 1,
+ toneFrequency: 1,
+ audioStartFrame: 0,
+ audioStreamIndex: 0,
+ },
+ ];
+ const inlineAudioAssets: InlineAudioAsset[] =
+ scenario.name === 'inline'
+ ? [
+ {
+ type: 'inline-audio',
+ id: 'inline',
+ frame: 0,
+ startInVideo: 0,
+ timestamp: 0,
+ duration: 1,
+ toneFrequency: 1,
+ audio: new Int16Array(96000).fill(1000),
+ },
+ ]
+ : [];
+ try {
+ for (const asset of inlineAudioAssets) {
+ downloadMap.inlineAudioMixing.addAsset({
+ asset,
+ fps: 1,
+ totalNumberOfFrames: 1,
+ firstFrame: 0,
+ trimLeftOffset: 0,
+ trimRightOffset: 0,
+ });
+ }
+
+ const render = stitchFramesToVideo({
+ assetsInfo: {
+ assets: [
+ {
+ frame: 0,
+ audioAndVideoAssets,
+ inlineAudioAssets,
+ artifactAssets:
+ scenario.name === 'artifact'
+ ? [{frame: 0, filename: 'report.txt'}]
+ : [],
+ },
+ ],
+ chunkLengthInSeconds: 1,
+ downloadMap,
+ firstFrameIndex: 0,
+ forSeamlessAacConcatenation: false,
+ imageSequenceName: path.join(directory, 'frame-%d.png'),
+ trimLeftOffset: 0,
+ trimRightOffset: 0,
+ },
+ codec: scenario.codec,
+ fps: 1,
+ height: 512,
+ width: 512,
+ enforceAudioTrack: scenario.enforced,
+ muted: scenario.muted,
+ outputLocation: output,
+ separateAudioTo,
+ audioCodec:
+ scenario.separate || scenario.codec === 'wav' ? 'pcm-16' : 'aac',
+ });
+
+ if (
+ NoReactInternals.ENABLE_V5_BREAKING_CHANGES &&
+ scenario.name === 'separate-empty'
+ ) {
+ await expect(render).rejects.toThrow('enforceAudioTrack: true');
+ expect(fs.existsSync(output)).toBe(false);
+ continue;
+ }
+
+ await render;
+ const probe = await callFf({
+ ...ffmpegOptions,
+ bin: 'ffprobe',
+ args: [
+ '-v',
+ 'error',
+ '-show_entries',
+ 'stream=codec_type',
+ '-of',
+ 'json',
+ output,
+ ],
+ });
+ const {streams} = JSON.parse(probe.stdout) as {
+ streams: {codec_type: string}[];
+ };
+ const hasSourceAudio =
+ scenario.name === 'audio' || scenario.name === 'inline';
+ const hasAudio =
+ !scenario.muted &&
+ (scenario.enforced ||
+ hasSourceAudio ||
+ scenario.codec === 'wav' ||
+ !NoReactInternals.ENABLE_V5_BREAKING_CHANGES);
+ expect(
+ streams.map((stream) => stream.codec_type),
+ scenario.name,
+ ).toEqual([
+ ...(scenario.codec === 'wav' ? [] : ['video']),
+ ...(hasAudio && !scenario.separate ? ['audio'] : []),
+ ]);
+ if (hasAudio) {
+ const pcm = path.join(directory, `${scenario.name}-decoded.wav`);
+ await callFf({
+ ...ffmpegOptions,
+ bin: 'ffmpeg',
+ args: [
+ '-i',
+ separateAudioTo ?? output,
+ '-map',
+ '0:a:0',
+ '-c:a',
+ 'pcm_s16le',
+ pcm,
+ ],
+ });
+ const wav = await fs.promises.readFile(pcm);
+ const dataOffset = wav.indexOf('data');
+ expect(dataOffset).toBeGreaterThan(0);
+ const samples = wav.subarray(dataOffset + 8);
+ expect(samples.length).toBeGreaterThan(0);
+ expect(
+ samples.some((sample) => sample !== 0),
+ scenario.name,
+ ).toBe(hasSourceAudio);
+ }
+ } finally {
+ downloadMap.allowCleanup();
+ cleanDownloadMap(downloadMap);
+ }
+ }
+ } finally {
+ await fs.promises.rm(directory, {recursive: true, force: true});
+ }
+ },
+ {timeout: 30000},
+);