Skip to content

🎥 Make videos programmatically with React

`@remotion/media-parser`: Seek in moof segments

Jonny Burger9e7bcafa4c95
diff --git a/packages/media-parser/src/containers/iso-base-media/get-keyframes.ts b/packages/media-parser/src/containers/iso-base-media/get-keyframes.ts
index d133446620..d9657cb981 100644
--- a/packages/media-parser/src/containers/iso-base-media/get-keyframes.ts
+++ b/packages/media-parser/src/containers/iso-base-media/get-keyframes.ts
@@ -16,14 +16,13 @@ export const getKeyframesFromIsoBaseMedia = (
const allSamples = videoTracks.map((t): MediaParserKeyframe[] => {
const {timescale: ts} = t;
- const samplePositions = getSamplePositionsFromTrack({
+ const {samplePositions, isComplete} = getSamplePositionsFromTrack({
trakBox: t.trakBox as TrakBox,
moofBoxes,
tfraBoxes,
- needsToBeComplete: true,
});
- if (samplePositions === null) {
+ if (!isComplete) {
return [];
}
diff --git a/packages/media-parser/src/containers/iso-base-media/get-sample-positions-from-track.ts b/packages/media-parser/src/containers/iso-base-media/get-sample-positions-from-track.ts
index 07b0d4f9f7..ebbd0df21b 100644
--- a/packages/media-parser/src/containers/iso-base-media/get-sample-positions-from-track.ts
+++ b/packages/media-parser/src/containers/iso-base-media/get-sample-positions-from-track.ts
@@ -21,13 +21,11 @@ export const getSamplePositionsFromTrack = ({
trakBox,
moofBoxes,
tfraBoxes,
- needsToBeComplete,
}: {
trakBox: TrakBox;
moofBoxes: IsoBaseMediaBox[];
tfraBoxes: TfraBox[];
- needsToBeComplete: boolean;
-}): SamplePosition[] | null => {
+}): {samplePositions: SamplePosition[]; isComplete: boolean} => {
const tkhdBox = getTkhdBox(trakBox);
if (!tkhdBox) {
throw new Error('Expected tkhd box in trak box');
@@ -37,22 +35,24 @@ export const getSamplePositionsFromTrack = ({
const isComplete =
tfraBoxes.length > 0 &&
tfraBoxes.every((t) => t.entries.length === moofBoxes.length);
- if (needsToBeComplete && !isComplete) {
- return null;
- }
- return moofBoxes
+ const samplePositions_ = moofBoxes
.map((m) => {
return getSamplesFromMoof({moofBox: m, trackId: tkhdBox.trackId});
})
.flat(1);
+
+ return {samplePositions: samplePositions_, isComplete};
}
const isLpcm = isLpcmAudioCodec(trakBox);
const timescaleAndDuration = getTimescaleAndDuration(trakBox);
if (isLpcm) {
- return getSamplePositionsFromLpcm(trakBox);
+ return {
+ samplePositions: getSamplePositionsFromLpcm(trakBox),
+ isComplete: true,
+ };
}
const stszBox = getStszBox(trakBox);
@@ -91,8 +91,8 @@ export const getSamplePositionsFromTrack = ({
cttsBox,
});
if (samplePositions.length === 0) {
- return null;
+ return {samplePositions: [], isComplete: false};
}
- return samplePositions;
+ return {samplePositions, isComplete: true};
};
diff --git a/packages/media-parser/src/containers/iso-base-media/get-seeking-from-mp4.ts b/packages/media-parser/src/containers/iso-base-media/get-seeking-from-mp4.ts
index ca3fdc41c5..17be299397 100644
--- a/packages/media-parser/src/containers/iso-base-media/get-seeking-from-mp4.ts
+++ b/packages/media-parser/src/containers/iso-base-media/get-seeking-from-mp4.ts
@@ -46,14 +46,13 @@ export const getSeekingByteFromIsoBaseMedia = (
continue;
}
- const samplePositions = getSamplePositionsFromTrack({
+ const {samplePositions, isComplete} = getSamplePositionsFromTrack({
trakBox: t.trakBox as TrakBox,
moofBoxes: info.moofBoxes,
tfraBoxes: info.tfraBoxes,
- needsToBeComplete: true,
});
- if (samplePositions === null) {
+ if (!isComplete) {
continue;
}
diff --git a/packages/media-parser/src/get-duration.ts b/packages/media-parser/src/get-duration.ts
index 2eb166793e..1bfe4280de 100644
--- a/packages/media-parser/src/get-duration.ts
+++ b/packages/media-parser/src/get-duration.ts
@@ -86,14 +86,13 @@ const getDurationFromIsoBaseMedia = (parserState: ParserState) => {
];
const allSamples = allTracks.map((t) => {
const {timescale: ts} = t;
- const samplePositions = getSamplePositionsFromTrack({
+ const {samplePositions, isComplete} = getSamplePositionsFromTrack({
trakBox: t.trakBox as TrakBox,
moofBoxes,
tfraBoxes,
- needsToBeComplete: true,
});
- if (samplePositions === null) {
+ if (!isComplete) {
return null;
}
diff --git a/packages/media-parser/src/state/iso-base-media/cached-sample-positions.ts b/packages/media-parser/src/state/iso-base-media/cached-sample-positions.ts
index c08d1854cc..7a48983877 100644
--- a/packages/media-parser/src/state/iso-base-media/cached-sample-positions.ts
+++ b/packages/media-parser/src/state/iso-base-media/cached-sample-positions.ts
@@ -24,15 +24,11 @@ export const calculateFlatSamples = (state: ParserState) => {
const flatSamples = allTracks
.map((track) => {
- const samplePositions = getSamplePositionsFromTrack({
+ const {samplePositions} = getSamplePositionsFromTrack({
trakBox: track.trakBox as TrakBox,
moofBoxes: getMoofBoxes(state.getIsoStructure().boxes),
tfraBoxes: getTfraBoxes(state.getIsoStructure()),
- needsToBeComplete: false,
});
- if (!samplePositions) {
- throw new Error('No sample positions');
- }
return samplePositions.map((samplePosition) => {
return {
diff --git a/packages/media-parser/src/test/seeking/seek-moof.test.ts b/packages/media-parser/src/test/seeking/seek-moof.test.ts
index 46e678540e..d4a4c5a4f3 100644
--- a/packages/media-parser/src/test/seeking/seek-moof.test.ts
+++ b/packages/media-parser/src/test/seeking/seek-moof.test.ts
@@ -4,7 +4,7 @@ import {mediaParserController} from '../../media-parser-controller';
import {parseMedia} from '../../parse-media';
import {nodeReader} from '../../readers/from-node';
-test.skip('seek moof', async () => {
+test('seek moof', async () => {
const video = await getRemoteExampleVideo('fragmentedMoofTrickyDuration');
const controller = mediaParserController();
diff --git a/packages/media-parser/src/test/stream-samples.test.ts b/packages/media-parser/src/test/stream-samples.test.ts
index d0d4ae6cf7..5dee02adff 100644
--- a/packages/media-parser/src/test/stream-samples.test.ts
+++ b/packages/media-parser/src/test/stream-samples.test.ts
@@ -62,9 +62,102 @@ test('Stream samples', async () => {
trakBox: trakBox as TrakBox,
moofBoxes: [],
tfraBoxes: [],
- needsToBeComplete: true,
}),
- );
+ ).toEqual({
+ isComplete: true,
+ samplePositions: [
+ {
+ chunk: 0,
+ cts: 1024,
+ dts: 0,
+ duration: 512,
+ isKeyframe: true,
+ offset: 1637,
+ size: 4834,
+ },
+ {
+ chunk: 0,
+ cts: 1536,
+ dts: 512,
+ duration: 512,
+ isKeyframe: false,
+ offset: 6471,
+ size: 437,
+ },
+ {
+ chunk: 1,
+ cts: 2048,
+ dts: 1024,
+ duration: 512,
+ isKeyframe: false,
+ offset: 7868,
+ size: 698,
+ },
+ {
+ chunk: 2,
+ cts: 2560,
+ dts: 1536,
+ duration: 512,
+ isKeyframe: false,
+ offset: 10486,
+ size: 595,
+ },
+ {
+ chunk: 3,
+ cts: 3072,
+ dts: 2048,
+ duration: 512,
+ isKeyframe: false,
+ offset: 12041,
+ size: 548,
+ },
+ {
+ chunk: 4,
+ cts: 3584,
+ dts: 2560,
+ duration: 512,
+ isKeyframe: false,
+ offset: 14509,
+ size: 611,
+ },
+ {
+ chunk: 5,
+ cts: 4096,
+ dts: 3072,
+ duration: 512,
+ isKeyframe: false,
+ offset: 16080,
+ size: 701,
+ },
+ {
+ chunk: 6,
+ cts: 4608,
+ dts: 3584,
+ duration: 512,
+ isKeyframe: false,
+ offset: 17741,
+ size: 535,
+ },
+ {
+ chunk: 7,
+ cts: 5120,
+ dts: 4096,
+ duration: 512,
+ isKeyframe: false,
+ offset: 20196,
+ size: 776,
+ },
+ {
+ chunk: 8,
+ cts: 5632,
+ dts: 4608,
+ duration: 512,
+ isKeyframe: false,
+ offset: 21932,
+ size: 586,
+ },
+ ],
+ });
const [firstAudioTrack] = tracks.audioTracks;
const {trakBox: trakBox2, ...audioTrack} = firstAudioTrack;
@@ -85,143 +178,145 @@ test('Stream samples', async () => {
trakBox: trakBox2 as TrakBox,
moofBoxes: [],
tfraBoxes: [],
- needsToBeComplete: true,
}),
- ).toEqual([
- {
- offset: 6908,
- size: 960,
- isKeyframe: true,
- dts: 0,
- duration: 1152,
- cts: 0,
- chunk: 0,
- },
- {
- offset: 8566,
- size: 960,
- isKeyframe: true,
- dts: 1152,
- duration: 1152,
- cts: 1152,
- chunk: 1,
- },
- {
- offset: 9526,
- size: 960,
- isKeyframe: true,
- dts: 2304,
- duration: 1152,
- cts: 2304,
- chunk: 1,
- },
- {
- offset: 11081,
- size: 960,
- isKeyframe: true,
- dts: 3456,
- duration: 1152,
- cts: 3456,
- chunk: 2,
- },
- {
- offset: 12589,
- size: 960,
- isKeyframe: true,
- dts: 4608,
- duration: 1152,
- cts: 4608,
- chunk: 3,
- },
- {
- offset: 13549,
- size: 960,
- isKeyframe: true,
- dts: 5760,
- duration: 1152,
- cts: 5760,
- chunk: 3,
- },
- {
- offset: 15120,
- size: 960,
- isKeyframe: true,
- dts: 6912,
- duration: 1152,
- cts: 6912,
- chunk: 4,
- },
- {
- offset: 16781,
- size: 960,
- isKeyframe: true,
- dts: 8064,
- duration: 1152,
- cts: 8064,
- chunk: 5,
- },
- {
- offset: 18276,
- size: 960,
- isKeyframe: true,
- dts: 9216,
- duration: 1152,
- cts: 9216,
- chunk: 6,
- },
- {
- offset: 19236,
- size: 960,
- isKeyframe: true,
- dts: 10368,
- duration: 1152,
- cts: 10368,
- chunk: 6,
- },
- {
- offset: 20972,
- size: 960,
- isKeyframe: true,
- dts: 11520,
- duration: 1152,
- cts: 11520,
- chunk: 7,
- },
- {
- offset: 22518,
- size: 960,
- isKeyframe: true,
- dts: 12672,
- duration: 1152,
- cts: 12672,
- chunk: 8,
- },
- {
- offset: 23478,
- size: 960,
- isKeyframe: true,
- dts: 13824,
- duration: 1152,
- cts: 13824,
- chunk: 8,
- },
- {
- offset: 24438,
- size: 960,
- isKeyframe: true,
- dts: 14976,
- duration: 1152,
- cts: 14976,
- chunk: 8,
- },
- {
- offset: 25398,
- size: 960,
- isKeyframe: true,
- dts: 16128,
- duration: 1152,
- cts: 16128,
- chunk: 8,
- },
- ]);
+ ).toEqual({
+ samplePositions: [
+ {
+ offset: 6908,
+ size: 960,
+ isKeyframe: true,
+ dts: 0,
+ duration: 1152,
+ cts: 0,
+ chunk: 0,
+ },
+ {
+ offset: 8566,
+ size: 960,
+ isKeyframe: true,
+ dts: 1152,
+ duration: 1152,
+ cts: 1152,
+ chunk: 1,
+ },
+ {
+ offset: 9526,
+ size: 960,
+ isKeyframe: true,
+ dts: 2304,
+ duration: 1152,
+ cts: 2304,
+ chunk: 1,
+ },
+ {
+ offset: 11081,
+ size: 960,
+ isKeyframe: true,
+ dts: 3456,
+ duration: 1152,
+ cts: 3456,
+ chunk: 2,
+ },
+ {
+ offset: 12589,
+ size: 960,
+ isKeyframe: true,
+ dts: 4608,
+ duration: 1152,
+ cts: 4608,
+ chunk: 3,
+ },
+ {
+ offset: 13549,
+ size: 960,
+ isKeyframe: true,
+ dts: 5760,
+ duration: 1152,
+ cts: 5760,
+ chunk: 3,
+ },
+ {
+ offset: 15120,
+ size: 960,
+ isKeyframe: true,
+ dts: 6912,
+ duration: 1152,
+ cts: 6912,
+ chunk: 4,
+ },
+ {
+ offset: 16781,
+ size: 960,
+ isKeyframe: true,
+ dts: 8064,
+ duration: 1152,
+ cts: 8064,
+ chunk: 5,
+ },
+ {
+ offset: 18276,
+ size: 960,
+ isKeyframe: true,
+ dts: 9216,
+ duration: 1152,
+ cts: 9216,
+ chunk: 6,
+ },
+ {
+ offset: 19236,
+ size: 960,
+ isKeyframe: true,
+ dts: 10368,
+ duration: 1152,
+ cts: 10368,
+ chunk: 6,
+ },
+ {
+ offset: 20972,
+ size: 960,
+ isKeyframe: true,
+ dts: 11520,
+ duration: 1152,
+ cts: 11520,
+ chunk: 7,
+ },
+ {
+ offset: 22518,
+ size: 960,
+ isKeyframe: true,
+ dts: 12672,
+ duration: 1152,
+ cts: 12672,
+ chunk: 8,
+ },
+ {
+ offset: 23478,
+ size: 960,
+ isKeyframe: true,
+ dts: 13824,
+ duration: 1152,
+ cts: 13824,
+ chunk: 8,
+ },
+ {
+ offset: 24438,
+ size: 960,
+ isKeyframe: true,
+ dts: 14976,
+ duration: 1152,
+ cts: 14976,
+ chunk: 8,
+ },
+ {
+ offset: 25398,
+ size: 960,
+ isKeyframe: true,
+ dts: 16128,
+ duration: 1152,
+ cts: 16128,
+ chunk: 8,
+ },
+ ],
+ isComplete: true,
+ });
});