Skip to content

🎥 Make videos programmatically with React

fix(media): fall back to decodable audio tracks for automatic selection

Alec Watts983a2d293083
diff --git a/packages/docs/docs/media/audio.mdx b/packages/docs/docs/media/audio.mdx
index d194a79b56..efae3ccb0c 100644
--- a/packages/docs/docs/media/audio.mdx
+++ b/packages/docs/docs/media/audio.mdx
@@ -273,7 +273,7 @@ Customize the [number of retries](/docs/delay-render#retrying) of the [`delayRen
### `audioStreamIndex?`
-Select the audio stream to use. The default is `0`.
+Select the audio stream to use by its zero-based index. If omitted, Remotion automatically selects a decodable audio track paired with the video, or the first decodable audio track for an audio-only file. An explicit index is always honored, even if that track cannot be decoded.
```tsx twoslash
import {Audio} from '@remotion/media';
diff --git a/packages/docs/docs/media/video.mdx b/packages/docs/docs/media/video.mdx
index e5225e961e..facc17a72f 100644
--- a/packages/docs/docs/media/video.mdx
+++ b/packages/docs/docs/media/video.mdx
@@ -417,7 +417,7 @@ The callback is called with a [`CanvasImageSource`](https://developer.mozilla.or
### `audioStreamIndex?`
-Select the audio stream to use. The default is `0`.
+Select the audio stream to use by its zero-based index. If omitted, Remotion automatically selects a decodable audio track paired with the video, preferring the primary track when supported. An explicit index is always honored, even if that track cannot be decoded.
```tsx twoslash
import {Video} from '@remotion/media';
diff --git a/packages/media/src/helpers/resolve-audio-track.ts b/packages/media/src/helpers/resolve-audio-track.ts
index 5be0a577e3..0460cd892d 100644
--- a/packages/media/src/helpers/resolve-audio-track.ts
+++ b/packages/media/src/helpers/resolve-audio-track.ts
@@ -6,6 +6,8 @@ import type {InputAudioTrack, InputVideoTrack} from 'mediabunny';
// track (preserves multi-track behavior introduced in #7094).
// - When unset and the file is audio-only, fall back to the first audio track
// so that audio-only files (e.g. .m4a) keep working (regression fix for #7210).
+// - If the automatic choice cannot decode, try a decodable alternative. Keep
+// the original choice when none can decode so callers still report the error.
export const resolveAudioTrack = async ({
videoTrack,
audioTracks,
@@ -20,7 +22,22 @@ export const resolveAudioTrack = async ({
}
if (videoTrack) {
- return (await videoTrack.getPrimaryPairableAudioTrack()) ?? null;
+ const primary = await videoTrack.getPrimaryPairableAudioTrack();
+ if (!primary || (await primary.canDecode())) {
+ return primary;
+ }
+
+ return (
+ (await videoTrack.getPrimaryPairableAudioTrack({
+ filter: (track) => track.canDecode(),
+ })) ?? primary
+ );
+ }
+
+ for (const track of audioTracks) {
+ if (await track.canDecode()) {
+ return track;
+ }
}
return audioTracks[0] ?? null;
diff --git a/packages/media/src/test/resolve-audio-track.test.ts b/packages/media/src/test/resolve-audio-track.test.ts
new file mode 100644
index 0000000000..1e50260127
--- /dev/null
+++ b/packages/media/src/test/resolve-audio-track.test.ts
@@ -0,0 +1,119 @@
+import type {
+ InputAudioTrack,
+ InputTrackQuery,
+ InputVideoTrack,
+} from 'mediabunny';
+import {expect, test, vi} from 'vitest';
+import {resolveAudioTrack} from '../helpers/resolve-audio-track';
+
+const audioTrack = (decodable: boolean) =>
+ ({canDecode: vi.fn(async () => decodable)}) as unknown as InputAudioTrack;
+
+const videoTrack = (pairable: InputAudioTrack[]) =>
+ ({
+ getPrimaryPairableAudioTrack: vi.fn(
+ async (query?: InputTrackQuery<InputAudioTrack>) => {
+ for (const track of pairable) {
+ if (!query?.filter || (await query.filter(track))) {
+ return track;
+ }
+ }
+ return null;
+ },
+ ),
+ }) as unknown as InputVideoTrack;
+
+test('falls back from unsupported default APAC to a decodable paired audio track', async () => {
+ const apac = audioTrack(false);
+ const aac = audioTrack(true);
+ expect(
+ await resolveAudioTrack({
+ videoTrack: videoTrack([apac, aac]),
+ audioTracks: [apac, aac],
+ audioStreamIndex: null,
+ }),
+ ).toBe(aac);
+});
+
+test('keeps the supported default without probing alternatives', async () => {
+ const primary = audioTrack(true);
+ const alternative = audioTrack(true);
+ const video = videoTrack([primary, alternative]);
+ expect(
+ await resolveAudioTrack({
+ videoTrack: video,
+ audioTracks: [primary, alternative],
+ audioStreamIndex: null,
+ }),
+ ).toBe(primary);
+ expect(video.getPrimaryPairableAudioTrack).toHaveBeenCalledTimes(1);
+ expect(alternative.canDecode).not.toHaveBeenCalled();
+});
+
+test('does not replace an explicitly selected unsupported track', async () => {
+ const apac = audioTrack(false);
+ const aac = audioTrack(true);
+ const video = videoTrack([apac, aac]);
+ expect(
+ await resolveAudioTrack({
+ videoTrack: video,
+ audioTracks: [apac, aac],
+ audioStreamIndex: 0,
+ }),
+ ).toBe(apac);
+ expect(video.getPrimaryPairableAudioTrack).not.toHaveBeenCalled();
+ expect(apac.canDecode).not.toHaveBeenCalled();
+});
+
+test('does not fall back to audio that cannot pair with the video', async () => {
+ const unsupported = audioTrack(false);
+ const unrelated = audioTrack(true);
+ expect(
+ await resolveAudioTrack({
+ videoTrack: videoTrack([unsupported]),
+ audioTracks: [unsupported, unrelated],
+ audioStreamIndex: null,
+ }),
+ ).toBe(unsupported);
+ expect(unrelated.canDecode).not.toHaveBeenCalled();
+});
+
+test('keeps the original unsupported track so callers can report failure', async () => {
+ const unsupported = audioTrack(false);
+ expect(
+ await resolveAudioTrack({
+ videoTrack: videoTrack([unsupported]),
+ audioTracks: [unsupported],
+ audioStreamIndex: null,
+ }),
+ ).toBe(unsupported);
+});
+
+test('finds a decodable alternative for an audio-only file', async () => {
+ const unsupported = audioTrack(false);
+ const aac = audioTrack(true);
+ expect(
+ await resolveAudioTrack({
+ videoTrack: null,
+ audioTracks: [unsupported, aac],
+ audioStreamIndex: null,
+ }),
+ ).toBe(aac);
+});
+
+test('returns null when no audio is present', async () => {
+ expect(
+ await resolveAudioTrack({
+ videoTrack: videoTrack([]),
+ audioTracks: [],
+ audioStreamIndex: null,
+ }),
+ ).toBeNull();
+ expect(
+ await resolveAudioTrack({
+ videoTrack: null,
+ audioTracks: [],
+ audioStreamIndex: null,
+ }),
+ ).toBeNull();
+});
diff --git a/packages/media/src/video/props.ts b/packages/media/src/video/props.ts
index 5b2aec0727..eb962b6866 100644
--- a/packages/media/src/video/props.ts
+++ b/packages/media/src/video/props.ts
@@ -104,9 +104,10 @@ export type NativeVideoProps = Omit<
export type InnerVideoProps = MandatoryVideoProps &
OuterVideoProps &
- Omit<OptionalVideoProps, 'effects' | 'preservePitch'> &
+ Omit<OptionalVideoProps, 'effects' | 'preservePitch' | 'audioStreamIndex'> &
NativeVideoProps & {
preservePitch: boolean | null;
+ audioStreamIndex: number | null;
effects: EffectDefinitionAndStack<unknown>[];
_remotionInternalStack: string | undefined;
};
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index 33277de8de..14eefdab93 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -72,7 +72,7 @@ type VideoForPreviewProps = NativeVideoProps & {
readonly _remotionInternalStack: string | null;
readonly disallowFallbackToOffthreadVideo: boolean;
readonly fallbackOffthreadVideoProps: FallbackOffthreadVideoProps;
- readonly audioStreamIndex: number;
+ readonly audioStreamIndex: number | null;
readonly debugOverlay: boolean;
readonly headless: boolean;
readonly onError: MediaOnError | undefined;
diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsx
index 4620b8b787..859c3a8577 100644
--- a/packages/media/src/video/video-for-rendering.tsx
+++ b/packages/media/src/video/video-for-rendering.tsx
@@ -50,7 +50,7 @@ type InnerVideoProps = NativeVideoProps & {
readonly delayRenderRetries: number | null;
readonly delayRenderTimeoutInMilliseconds: number | null;
readonly fallbackOffthreadVideoProps: FallbackOffthreadVideoProps;
- readonly audioStreamIndex: number;
+ readonly audioStreamIndex: number | null;
readonly disallowFallbackToOffthreadVideo: boolean;
readonly _remotionInternalStack: string | undefined;
readonly toneFrequency: number;
diff --git a/packages/media/src/video/video.tsx b/packages/media/src/video/video.tsx
index 97aef749f8..7c8e42c70b 100644
--- a/packages/media/src/video/video.tsx
+++ b/packages/media/src/video/video.tsx
@@ -152,7 +152,7 @@ const InnerVideo: React.FC<
return (
<VideoForRendering
{...props}
- audioStreamIndex={audioStreamIndex ?? 0}
+ audioStreamIndex={audioStreamIndex ?? null}
className={className}
delayRenderRetries={delayRenderRetries ?? null}
delayRenderTimeoutInMilliseconds={
@@ -207,7 +207,7 @@ const InnerVideo: React.FC<
<VideoForPreview
{...props}
setMediaDurationInSeconds={setMediaDurationInSeconds}
- audioStreamIndex={audioStreamIndex ?? 0}
+ audioStreamIndex={audioStreamIndex ?? null}
className={className}
logLevel={logLevel}
loop={loop}
@@ -430,7 +430,7 @@ const VideoInner: React.FC<
<Internals.SequenceContent>
<InnerVideo
{...props}
- audioStreamIndex={audioStreamIndex ?? 0}
+ audioStreamIndex={audioStreamIndex ?? null}
className={className}
delayRenderRetries={delayRenderRetries ?? null}
delayRenderTimeoutInMilliseconds={