Skip to content

🎥 Make videos programmatically with React

`@remotion/media-parser`: Handle tricky fragmented MP4 video

Jonny Burgeraebca57cf472
diff --git a/packages/example-videos/src/remote-videos.ts b/packages/example-videos/src/remote-videos.ts
index 456db1133a..e302430a6a 100644
--- a/packages/example-videos/src/remote-videos.ts
+++ b/packages/example-videos/src/remote-videos.ts
@@ -6,6 +6,8 @@ import path from 'path';
export const remoteExampleVideos = {
mp4av1:
'https://remotion-assets.s3.eu-central-1.amazonaws.com/example-videos/mp4-av1.mp4',
+ fragmentedMoofTrickyDuration:
+ 'https://remotion-video-submissions.s3.ap-northeast-1.amazonaws.com/6048e3e4-ffbb-43a5-b6b5-75643365646a',
};
const rootDir = path.join(__dirname, '..');
diff --git a/packages/media-parser/src/get-duration.ts b/packages/media-parser/src/get-duration.ts
index 3a5f62c18b..0ac0afaa97 100644
--- a/packages/media-parser/src/get-duration.ts
+++ b/packages/media-parser/src/get-duration.ts
@@ -52,6 +52,37 @@ export const isMatroska = (boxes: AnySegment[]) => {
return matroskaBox;
};
+const isoHasDuration = (parserState: ParserState) => {
+ const structure = parserState.getIsoStructure();
+
+ const moovBox = getMoovBox(parserState);
+ if (!moovBox) {
+ return false;
+ }
+
+ const mvhdBox = getMvhdBox(moovBox);
+
+ if (!mvhdBox) {
+ return false;
+ }
+
+ if (mvhdBox.type !== 'mvhd-box') {
+ throw new Error('Expected mvhd-box');
+ }
+
+ if (mvhdBox.durationInSeconds > 0) {
+ return true;
+ }
+
+ // Checking if this is a fragmented mp4
+ const moofBoxes = getMoofBoxes(structure.boxes);
+ const hasMvex = moovBox.children.some(
+ (b) => b.type === 'regular-box' && b.boxType === 'mvex',
+ );
+ const isFragmented = moofBoxes.length > 0 || hasMvex;
+ return !isFragmented;
+};
+
const getDurationFromIsoBaseMedia = (parserState: ParserState) => {
const structure = parserState.getIsoStructure();
@@ -91,9 +122,12 @@ const getDurationFromIsoBaseMedia = (parserState: ParserState) => {
const highest = samplePositions
?.map((sp) => (sp.cts + sp.duration) / ts)
.reduce((a, b) => Math.max(a, b), 0);
+
return highest ?? 0;
});
+
const highestTimestamp = Math.max(...allSamples);
+
return highestTimestamp;
};
@@ -137,14 +171,23 @@ export const getDuration = (parserState: ParserState): number | null => {
// `duration` just grabs from metadata, and otherwise returns null
// Therefore just checking if we have tracks
export const hasDuration = (parserState: ParserState): boolean => {
+ const structure = parserState.getStructureOrNull();
+ if (structure === null) {
+ return false;
+ }
+
+ if (structure.type === 'iso-base-media') {
+ return isoHasDuration(parserState);
+ }
+
return getHasTracks(parserState);
};
-// `slowDuration` does through everything, and therefore is false
+// `slowDuration` goes through everything, and therefore is false
// Unless it it somewhere in the metadata and is non-null
export const hasSlowDuration = (parserState: ParserState): boolean => {
try {
- return getDuration(parserState) !== null;
+ return hasDuration(parserState) && getDuration(parserState) !== null;
} catch {
return false;
}
diff --git a/packages/media-parser/src/test/tricky-moof-duration.test.ts b/packages/media-parser/src/test/tricky-moof-duration.test.ts
new file mode 100644
index 0000000000..57a7d5bc85
--- /dev/null
+++ b/packages/media-parser/src/test/tricky-moof-duration.test.ts
@@ -0,0 +1,23 @@
+import {getRemoteExampleVideo} from '@remotion/example-videos';
+import {beforeAll, expect, test} from 'bun:test';
+import {parseMedia} from '../parse-media';
+import {nodeReader} from '../readers/from-node';
+
+beforeAll(async () => {
+ await getRemoteExampleVideo('fragmentedMoofTrickyDuration');
+});
+
+test('non-zero duration', async () => {
+ const {durationInSeconds, slowDurationInSeconds} = await parseMedia({
+ src: await getRemoteExampleVideo('fragmentedMoofTrickyDuration'),
+ fields: {
+ durationInSeconds: true,
+ slowDurationInSeconds: true,
+ },
+ reader: nodeReader,
+ acknowledgeRemotionLicense: true,
+ });
+
+ expect(slowDurationInSeconds).toBe(57.001995464852605);
+ expect(durationInSeconds).toBe(57.001995464852605);
+});