Skip to content

🎥 Make videos programmatically with React

`@remotion/media-parser`: Handle audio track coming before video track

Jonny Burgera7ab299c2c8b
diff --git a/packages/media-parser/src/get-fps.ts b/packages/media-parser/src/get-fps.ts
index cb79465f55..b4eb754451 100644
--- a/packages/media-parser/src/get-fps.ts
+++ b/packages/media-parser/src/get-fps.ts
@@ -27,6 +27,54 @@ type TimescaleAndDuration = {
duration: number;
};
+export const trakBoxContainsVideo = (trakBox: AnySegment): boolean => {
+ if (trakBox.type !== 'trak-box') {
+ return false;
+ }
+
+ const {children} = trakBox;
+ const mediaBoxes = children.filter(
+ (c) => c.type === 'regular-box' && c.boxType === 'mdia',
+ );
+ if (!mediaBoxes || mediaBoxes.length === 0) {
+ return false;
+ }
+
+ const firstMediaBox = mediaBoxes[0];
+ if (
+ firstMediaBox.type !== 'regular-box' ||
+ firstMediaBox.boxType !== 'mdia'
+ ) {
+ return false;
+ }
+
+ const minf = firstMediaBox.children.find(
+ (c) => c.type === 'regular-box' && c.boxType === 'minf',
+ );
+ if (!minf || minf.type !== 'regular-box' || minf.boxType !== 'minf') {
+ return false;
+ }
+
+ const stbl = minf.children.find(
+ (c) => c.type === 'regular-box' && c.boxType === 'stbl',
+ );
+ if (!stbl || stbl.type !== 'regular-box' || stbl.boxType !== 'stbl') {
+ return false;
+ }
+
+ const stsd = stbl.children.find((c) => c.type === 'stsd-box');
+ if (!stsd || stsd.type !== 'stsd-box') {
+ return false;
+ }
+
+ const videoSample = stsd.samples.find((s) => s.type === 'video');
+ if (!videoSample || videoSample.type !== 'video') {
+ return false;
+ }
+
+ return true;
+};
+
export const getTimescaleAndDuration = (
boxes: AnySegment[],
): TimescaleAndDuration | null => {
@@ -42,7 +90,7 @@ export const getTimescaleAndDuration = (
}
// TODO: What if the video track is not the first track?
- const trackBox = trackBoxes[0];
+ const trackBox = trackBoxes.find(trakBoxContainsVideo);
if (!trackBox || trackBox.type !== 'trak-box') {
return null;
}
@@ -99,8 +147,7 @@ export const getFps = (segments: AnySegment[]) => {
return null;
}
- // TODO: What if the video track is not the first track?
- const trackBox = trackBoxes[0];
+ const trackBox = trackBoxes.find(trakBoxContainsVideo);
if (!trackBox || trackBox.type !== 'trak-box') {
return null;
}
diff --git a/packages/media-parser/src/get-video-codec.ts b/packages/media-parser/src/get-video-codec.ts
index b2ef73d763..c948acaf04 100644
--- a/packages/media-parser/src/get-video-codec.ts
+++ b/packages/media-parser/src/get-video-codec.ts
@@ -1,4 +1,5 @@
/* eslint-disable max-depth */
+import {trakBoxContainsVideo} from './get-fps';
import type {KnownVideoCodecs} from './options';
import type {AnySegment} from './parse-result';
@@ -13,7 +14,9 @@ export const hasVideoCodec = (boxes: AnySegment[]): boolean => {
export const getVideoCodec = (boxes: AnySegment[]): KnownVideoCodecs | null => {
const moovBox = boxes.find((b) => b.type === 'moov-box');
if (moovBox && moovBox.type === 'moov-box') {
- const trakBox = moovBox.children.find((b) => b.type === 'trak-box');
+ const trakBox = moovBox.children.find(
+ (b) => b.type === 'trak-box' && trakBoxContainsVideo(b),
+ );
if (trakBox && trakBox.type === 'trak-box') {
const mdiaBox = trakBox.children.find(
(b) => b.type === 'regular-box' && b.boxType === 'mdia',
diff --git a/packages/media-parser/src/test/stream-remote.test.ts b/packages/media-parser/src/test/stream-remote.test.ts
index 903ece4db2..496ebb257f 100644
--- a/packages/media-parser/src/test/stream-remote.test.ts
+++ b/packages/media-parser/src/test/stream-remote.test.ts
@@ -40,3 +40,14 @@ test('Should stream from Wikipedia', async () => {
});
expect(result.videoCodec).toEqual('vp9');
});
+
+test('Should handle redirect', async () => {
+ const result = await parseMedia('https://remotion.dev/bbb.mp4', {
+ fps: true,
+ videoCodec: true,
+ durationInSeconds: true,
+ });
+ expect(result.videoCodec).toEqual('h264');
+ expect(result.fps).toEqual(24);
+ expect(result.durationInSeconds).toEqual(596.4733333333334);
+});