Skip to content

🎥 Make videos programmatically with React

refactor

Jonny Burger3ed7efa13a52
diff --git a/packages/media-parser/src/containers/iso-base-media/collect-sample-positions-from-moof-boxes.ts b/packages/media-parser/src/containers/iso-base-media/collect-sample-positions-from-moof-boxes.ts
new file mode 100644
index 0000000000..ac5e0356ce
--- /dev/null
+++ b/packages/media-parser/src/containers/iso-base-media/collect-sample-positions-from-moof-boxes.ts
@@ -0,0 +1,26 @@
+import {getSamplesFromMoof} from '../../samples-from-moof';
+import type {IsoBaseMediaBox} from './base-media-box';
+import type {TfraBox} from './mfra/tfra';
+import type {TkhdBox} from './tkhd';
+
+export const collectSamplePositionsFromMoofBoxes = ({
+ moofBoxes,
+ tfraBoxes,
+ tkhdBox,
+}: {
+ moofBoxes: IsoBaseMediaBox[];
+ tfraBoxes: TfraBox[];
+ tkhdBox: TkhdBox;
+}) => {
+ const isComplete =
+ tfraBoxes.length > 0 &&
+ tfraBoxes.every((t) => t.entries.length === moofBoxes.length);
+
+ const samplePositions_ = moofBoxes
+ .map((m) => {
+ return getSamplesFromMoof({moofBox: m, trackId: tkhdBox.trackId});
+ })
+ .flat(1);
+
+ return {samplePositions: samplePositions_, isComplete};
+};
diff --git a/packages/media-parser/src/containers/iso-base-media/collect-sample-positions-from-trak.ts b/packages/media-parser/src/containers/iso-base-media/collect-sample-positions-from-trak.ts
new file mode 100644
index 0000000000..fb30c3f366
--- /dev/null
+++ b/packages/media-parser/src/containers/iso-base-media/collect-sample-positions-from-trak.ts
@@ -0,0 +1,60 @@
+import {isLpcmAudioCodec} from '../../get-audio-codec';
+import {getTimescaleAndDuration} from '../../get-fps';
+import {getSamplePositions} from '../../get-sample-positions';
+import {getSamplePositionsFromLpcm} from '../../get-sample-positions-from-lpcm';
+import type {TrakBox} from './trak/trak';
+import {
+ getCttsBox,
+ getStcoBox,
+ getStscBox,
+ getStssBox,
+ getStszBox,
+ getSttsBox,
+} from './traversal';
+
+export const collectSamplePositionsFromTrak = (trakBox: TrakBox) => {
+ const isLpcm = isLpcmAudioCodec(trakBox);
+ const timescaleAndDuration = getTimescaleAndDuration(trakBox);
+
+ if (isLpcm) {
+ return getSamplePositionsFromLpcm(trakBox);
+ }
+
+ const stszBox = getStszBox(trakBox);
+ const stcoBox = getStcoBox(trakBox);
+ const stscBox = getStscBox(trakBox);
+ const stssBox = getStssBox(trakBox);
+ const sttsBox = getSttsBox(trakBox);
+ const cttsBox = getCttsBox(trakBox);
+
+ if (!stszBox) {
+ throw new Error('Expected stsz box in trak box');
+ }
+
+ if (!stcoBox) {
+ throw new Error('Expected stco box in trak box');
+ }
+
+ if (!stscBox) {
+ throw new Error('Expected stsc box in trak box');
+ }
+
+ if (!sttsBox) {
+ throw new Error('Expected stts box in trak box');
+ }
+
+ if (!timescaleAndDuration) {
+ throw new Error('Expected timescale and duration in trak box');
+ }
+
+ const samplePositions = getSamplePositions({
+ stcoBox,
+ stscBox,
+ stszBox,
+ stssBox,
+ sttsBox,
+ cttsBox,
+ });
+
+ return samplePositions;
+};
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 ebbd0df21b..fd690f54ae 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
@@ -1,21 +1,10 @@
-import {isLpcmAudioCodec} from '../../get-audio-codec';
-import {getTimescaleAndDuration} from '../../get-fps';
import type {SamplePosition} from '../../get-sample-positions';
-import {getSamplePositions} from '../../get-sample-positions';
-import {getSamplePositionsFromLpcm} from '../../get-sample-positions-from-lpcm';
-import {getSamplesFromMoof} from '../../samples-from-moof';
import type {IsoBaseMediaBox} from './base-media-box';
+import {collectSamplePositionsFromMoofBoxes} from './collect-sample-positions-from-moof-boxes';
+import {collectSamplePositionsFromTrak} from './collect-sample-positions-from-trak';
import type {TfraBox} from './mfra/tfra';
import type {TrakBox} from './trak/trak';
-import {
- getCttsBox,
- getStcoBox,
- getStscBox,
- getStssBox,
- getStszBox,
- getSttsBox,
- getTkhdBox,
-} from './traversal';
+import {getTkhdBox} from './traversal';
export const getSamplePositionsFromTrack = ({
trakBox,
@@ -32,67 +21,20 @@ export const getSamplePositionsFromTrack = ({
}
if (moofBoxes.length > 0) {
- const isComplete =
- tfraBoxes.length > 0 &&
- tfraBoxes.every((t) => t.entries.length === moofBoxes.length);
+ const {isComplete, samplePositions} = collectSamplePositionsFromMoofBoxes({
+ moofBoxes,
+ tfraBoxes,
+ tkhdBox,
+ });
- 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 {
- samplePositions: getSamplePositionsFromLpcm(trakBox),
- isComplete: true,
+ samplePositions,
+ isComplete,
};
}
- const stszBox = getStszBox(trakBox);
- const stcoBox = getStcoBox(trakBox);
- const stscBox = getStscBox(trakBox);
- const stssBox = getStssBox(trakBox);
- const sttsBox = getSttsBox(trakBox);
- const cttsBox = getCttsBox(trakBox);
-
- if (!stszBox) {
- throw new Error('Expected stsz box in trak box');
- }
-
- if (!stcoBox) {
- throw new Error('Expected stco box in trak box');
- }
-
- if (!stscBox) {
- throw new Error('Expected stsc box in trak box');
- }
-
- if (!sttsBox) {
- throw new Error('Expected stts box in trak box');
- }
-
- if (!timescaleAndDuration) {
- throw new Error('Expected timescale and duration in trak box');
- }
-
- const samplePositions = getSamplePositions({
- stcoBox,
- stscBox,
- stszBox,
- stssBox,
- sttsBox,
- cttsBox,
- });
- if (samplePositions.length === 0) {
- return {samplePositions: [], isComplete: false};
- }
-
- return {samplePositions, isComplete: true};
+ return {
+ samplePositions: collectSamplePositionsFromTrak(trakBox),
+ isComplete: true,
+ };
};
diff --git a/packages/media-parser/src/get-duration.ts b/packages/media-parser/src/get-duration.ts
index 1bfe4280de..f5a0124ecd 100644
--- a/packages/media-parser/src/get-duration.ts
+++ b/packages/media-parser/src/get-duration.ts
@@ -96,6 +96,10 @@ const getDurationFromIsoBaseMedia = (parserState: ParserState) => {
return null;
}
+ if (samplePositions.length === 0) {
+ return null;
+ }
+
const highest = samplePositions
?.map((sp) => (sp.cts + sp.duration) / ts)
.reduce((a, b) => Math.max(a, b), 0);
@@ -103,7 +107,7 @@ const getDurationFromIsoBaseMedia = (parserState: ParserState) => {
return highest ?? 0;
});
- if (allSamples.some((s) => s === null)) {
+ if (allSamples.every((s) => s === null)) {
return null;
}
diff --git a/packages/media-parser/src/test/tricky-moof-duration.test.ts b/packages/media-parser/src/test/tricky-moof-duration.test.ts
index 61d681ab9e..0ab0eb24e7 100644
--- a/packages/media-parser/src/test/tricky-moof-duration.test.ts
+++ b/packages/media-parser/src/test/tricky-moof-duration.test.ts
@@ -20,13 +20,13 @@ test('non-zero duration', async () => {
acknowledgeRemotionLicense: true,
});
+ expect(slowDurationInSeconds).toBe(57.001995464852605);
+ expect(durationInSeconds).toBe(null);
+
expect(internalStats).toEqual({
skippedBytes: 0,
finalCursorOffset: 8734219,
});
-
- expect(slowDurationInSeconds).toBe(57.001995464852605);
- expect(durationInSeconds).toBe(null);
});
test('no fast duration', async () => {