Skip to content

🎥 Make videos programmatically with React

`@remotion/media`: support `audioStreamIndex` in preview

Igor Samokhovets89def6ecd760
diff --git a/packages/docs/docs/media/audio.mdx b/packages/docs/docs/media/audio.mdx
index 861d066b7d..9d9ef143d2 100644
--- a/packages/docs/docs/media/audio.mdx
+++ b/packages/docs/docs/media/audio.mdx
@@ -211,11 +211,6 @@ export const MyComposition = () => {
};
```
-:::note
-Currently only works during rendering.
-Planning to support it during preview as well.
-:::
-
### `toneFrequency?`
Accepts a number between `0.01` and `2`, where `1` represents the original pitch. Values less than `1` will decrease the pitch, while values greater than `1` will increase it.
diff --git a/packages/docs/docs/media/video.mdx b/packages/docs/docs/media/video.mdx
index ca35874def..48e7562e5b 100644
--- a/packages/docs/docs/media/video.mdx
+++ b/packages/docs/docs/media/video.mdx
@@ -243,11 +243,6 @@ export const MyComposition = () => {
};
```
-:::note
-Currently only works during rendering.
-Planning to support it during preview as well.
-:::
-
### `toneFrequency?`
Accepts a number between `0.01` and `2`, where `1` represents the original pitch. Values less than `1` will decrease the pitch, while values greater than `1` will increase it.
diff --git a/packages/example/public/matroska-two-audio-streams.mkv b/packages/example/public/matroska-two-audio-streams.mkv
new file mode 100644
index 0000000000..982f592a7d
Binary files /dev/null and b/packages/example/public/matroska-two-audio-streams.mkv differ
diff --git a/packages/media/src/audio/audio-for-preview.tsx b/packages/media/src/audio/audio-for-preview.tsx
index 552d564fb1..cc3cc7b7d7 100644
--- a/packages/media/src/audio/audio-for-preview.tsx
+++ b/packages/media/src/audio/audio-for-preview.tsx
@@ -30,6 +30,7 @@ type NewAudioForPreviewProps = {
readonly name: string | undefined;
readonly showInTimeline: boolean;
readonly stack: string | null;
+ readonly audioStreamIndex: number;
};
const NewAudioForPreview: React.FC<NewAudioForPreviewProps> = ({
@@ -45,6 +46,7 @@ const NewAudioForPreview: React.FC<NewAudioForPreviewProps> = ({
name,
showInTimeline,
stack,
+ audioStreamIndex,
}) => {
const videoConfig = useUnsafeVideoConfig();
const frame = useCurrentFrame();
@@ -123,6 +125,7 @@ const NewAudioForPreview: React.FC<NewAudioForPreviewProps> = ({
: undefined,
canvas: null,
playbackRate,
+ audioStreamIndex,
});
mediaPlayerRef.current = player;
@@ -178,6 +181,7 @@ const NewAudioForPreview: React.FC<NewAudioForPreviewProps> = ({
trimBefore,
playbackRate,
videoConfig.fps,
+ audioStreamIndex,
]);
useEffect(() => {
@@ -286,6 +290,7 @@ type InnerAudioProps = {
readonly trimAfter?: number | undefined;
readonly trimBefore?: number | undefined;
readonly stack: string | null;
+ readonly audioStreamIndex?: number;
};
export const AudioForPreview: React.FC<InnerAudioProps> = ({
@@ -301,11 +306,13 @@ export const AudioForPreview: React.FC<InnerAudioProps> = ({
trimBefore,
showInTimeline,
stack,
+ audioStreamIndex,
}) => {
const preloadedSrc = usePreload(src);
return (
<NewAudioForPreview
+ audioStreamIndex={audioStreamIndex ?? 0}
src={preloadedSrc}
playbackRate={playbackRate ?? 1}
logLevel={logLevel ?? window.remotion_logLevel}
diff --git a/packages/media/src/video/media-player.ts b/packages/media/src/video/media-player.ts
index eb6e57df74..24a187473e 100644
--- a/packages/media/src/video/media-player.ts
+++ b/packages/media/src/video/media-player.ts
@@ -20,6 +20,7 @@ export class MediaPlayer {
private src: string;
private logLevel: LogLevel;
private playbackRate: number;
+ private audioStreamIndex: number;
private canvasSink: CanvasSink | null = null;
private videoFrameIterator: AsyncGenerator<
@@ -80,6 +81,7 @@ export class MediaPlayer {
trimBeforeSeconds,
trimAfterSeconds,
playbackRate,
+ audioStreamIndex,
}: {
canvas: HTMLCanvasElement | null;
src: string;
@@ -89,6 +91,7 @@ export class MediaPlayer {
trimBeforeSeconds: number | undefined;
trimAfterSeconds: number | undefined;
playbackRate: number;
+ audioStreamIndex?: number;
}) {
this.canvas = canvas ?? null;
this.src = src;
@@ -98,6 +101,7 @@ export class MediaPlayer {
this.loop = loop;
this.trimBeforeSeconds = trimBeforeSeconds;
this.trimAfterSeconds = trimAfterSeconds;
+ this.audioStreamIndex = audioStreamIndex ?? 0;
if (canvas) {
const context = canvas.getContext('2d', {
@@ -140,13 +144,15 @@ export class MediaPlayer {
this.input = input;
- const [duration, videoTrack, audioTrack] = await Promise.all([
+ const [duration, videoTrack, audioTracks] = await Promise.all([
input.computeDuration(),
input.getPrimaryVideoTrack(),
- input.getPrimaryAudioTrack(),
+ input.getAudioTracks(),
]);
this.totalDuration = duration;
+ const audioTrack = audioTracks[this.audioStreamIndex] ?? null;
+
if (!videoTrack && !audioTrack) {
throw new Error(`No video or audio track found for ${this.src}`);
}
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index 47c64df91c..8d76b3c0ee 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -38,6 +38,7 @@ type NewVideoForPreviewProps = {
readonly trimAfter: number | undefined;
readonly trimBefore: number | undefined;
readonly stack: string | null;
+ readonly audioStreamIndex: number;
};
const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
@@ -56,6 +57,7 @@ const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
trimAfter,
trimBefore,
stack,
+ audioStreamIndex,
}) => {
const canvasRef = useRef<HTMLCanvasElement>(null);
const videoConfig = useUnsafeVideoConfig();
@@ -133,6 +135,7 @@ const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
? trimBefore / videoConfig.fps
: undefined,
playbackRate,
+ audioStreamIndex,
});
mediaPlayerRef.current = player;
@@ -178,6 +181,7 @@ const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
trimBefore,
videoConfig.fps,
playbackRate,
+ audioStreamIndex,
]);
const classNameValue = useMemo(() => {
@@ -329,6 +333,7 @@ type InnerVideoProps = {
readonly trimAfter: number | undefined;
readonly trimBefore: number | undefined;
readonly stack: string | null;
+ readonly audioStreamIndex: number;
};
export const VideoForPreview: React.FC<InnerVideoProps> = ({
@@ -347,6 +352,7 @@ export const VideoForPreview: React.FC<InnerVideoProps> = ({
trimAfter,
trimBefore,
stack,
+ audioStreamIndex,
}) => {
const preloadedSrc = usePreload(src);
@@ -367,6 +373,7 @@ export const VideoForPreview: React.FC<InnerVideoProps> = ({
loopVolumeCurveBehavior={loopVolumeCurveBehavior}
showInTimeline={showInTimeline}
stack={stack}
+ audioStreamIndex={audioStreamIndex}
/>
);
};
diff --git a/packages/media/src/video/video.tsx b/packages/media/src/video/video.tsx
index 3042fe1ece..a1c12045b9 100644
--- a/packages/media/src/video/video.tsx
+++ b/packages/media/src/video/video.tsx
@@ -89,6 +89,7 @@ const InnerVideo: React.FC<InnerVideoProps> = ({
return (
<VideoForPreview
+ audioStreamIndex={audioStreamIndex ?? 0}
className={className}
name={name}
logLevel={logLevel}