Skip to content

🎥 Make videos programmatically with React

get timescale

Jonny Burger9e6287a2af75
diff --git a/packages/media-parser/src/get-fps.ts b/packages/media-parser/src/get-fps.ts
index 661450594f..3e03576b5f 100644
--- a/packages/media-parser/src/get-fps.ts
+++ b/packages/media-parser/src/get-fps.ts
@@ -1,7 +1,13 @@
import type {SttsBox} from './boxes/iso-base-media/stts/stts';
import type {TrakBox} from './boxes/iso-base-media/trak/trak';
import type {AnySegment} from './parse-result';
-import {getMoovBox, getMvhdBox, getStsdBox, getTraks} from './traversal';
+import {
+ getMdhdBox,
+ getMoovBox,
+ getStsdBox,
+ getSttsBox,
+ getTraks,
+} from './traversal';
const calculateFps = ({
sttsBox,
@@ -58,113 +64,36 @@ export const trakBoxContainsVideo = (trakBox: TrakBox): boolean => {
};
export const getTimescaleAndDuration = (
- boxes: AnySegment[],
+ trakBox: TrakBox,
): TimescaleAndDuration | null => {
- const moovBox = getMoovBox(boxes);
- if (!moovBox) {
- return null;
- }
-
- const trackBoxes = getTraks(moovBox);
-
- const trackBox = trackBoxes.find(trakBoxContainsVideo);
- if (!trackBox || trackBox.type !== 'trak-box') {
- return null;
- }
-
- const trackBoxChildren = trackBox.children;
- if (!trackBoxChildren || trackBoxChildren.length === 0) {
- return null;
- }
-
- const mdiaBox = trackBoxChildren.find(
- (c) => c.type === 'regular-box' && c.boxType === 'mdia',
- );
- if (
- !mdiaBox ||
- mdiaBox.type !== 'regular-box' ||
- mdiaBox.boxType !== 'mdia'
- ) {
- return null;
- }
-
- const mdhdBox = mdiaBox?.children.find((c) => c.type === 'mdhd-box');
- if (mdhdBox && mdhdBox.type === 'mdhd-box') {
+ const mdhdBox = getMdhdBox(trakBox);
+ if (mdhdBox) {
return {timescale: mdhdBox.timescale, duration: mdhdBox.duration};
}
- const mvhdBox = getMvhdBox(moovBox);
- if (!mvhdBox) {
- return null;
- }
-
- const {timeScale, durationInUnits} = mvhdBox;
- return {timescale: timeScale, duration: durationInUnits};
+ return null;
};
export const getFps = (segments: AnySegment[]) => {
- const timescaleAndDuration = getTimescaleAndDuration(segments);
- if (!timescaleAndDuration) {
- return null;
- }
-
const moovBox = getMoovBox(segments);
if (!moovBox) {
return null;
}
- const mvhdBox = getMvhdBox(moovBox);
- if (!mvhdBox) {
- return null;
- }
-
const trackBoxes = getTraks(moovBox);
const trackBox = trackBoxes.find(trakBoxContainsVideo);
- if (!trackBox || trackBox.type !== 'trak-box') {
- return null;
- }
-
- const trackBoxChildren = trackBox.children;
- if (!trackBoxChildren || trackBoxChildren.length === 0) {
- return null;
- }
-
- const mdiaBox = trackBoxChildren.find(
- (c) => c.type === 'regular-box' && c.boxType === 'mdia',
- );
- if (
- !mdiaBox ||
- mdiaBox.type !== 'regular-box' ||
- mdiaBox.boxType !== 'mdia'
- ) {
+ if (!trackBox) {
return null;
}
- const minfBox = mdiaBox.children.find(
- (c) => c.type === 'regular-box' && c.boxType === 'minf',
- );
- if (
- !minfBox ||
- minfBox.type !== 'regular-box' ||
- minfBox.boxType !== 'minf'
- ) {
- return null;
- }
-
- const stblBox = minfBox.children.find(
- (c) => c.type === 'regular-box' && c.boxType === 'stbl',
- );
- if (
- !stblBox ||
- stblBox.type !== 'regular-box' ||
- stblBox.boxType !== 'stbl'
- ) {
+ const timescaleAndDuration = getTimescaleAndDuration(trackBox);
+ if (!timescaleAndDuration) {
return null;
}
- const sttsBox = stblBox.children.find((c) => c.type === 'stts-box');
- if (!sttsBox || sttsBox.type !== 'stts-box') {
+ const sttsBox = getSttsBox(trackBox);
+ if (!sttsBox) {
return null;
}
diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.ts
index 0c50edf03e..95bbc3ddf0 100644
--- a/packages/media-parser/src/get-tracks.ts
+++ b/packages/media-parser/src/get-tracks.ts
@@ -1,5 +1,9 @@
import type {MoovBox} from './boxes/iso-base-media/moov/moov';
-import {trakBoxContainsAudio, trakBoxContainsVideo} from './get-fps';
+import {
+ getTimescaleAndDuration,
+ trakBoxContainsAudio,
+ trakBoxContainsVideo,
+} from './get-fps';
import type {SamplePosition} from './get-sample-positions';
import {getSamplePositions} from './get-sample-positions';
import type {AnySegment} from './parse-result';
@@ -21,6 +25,7 @@ export type Track = {
samplePositions: SamplePosition[];
trackId: number;
description: Uint8Array | null;
+ timescale: number;
};
// TODO: Use this to determine if all tracks are present
@@ -77,6 +82,7 @@ export const getTracks = (
const stssBox = getStssBox(track);
const tkhdBox = getTkhdBox(track);
const videoDescriptors = getVideoDescriptors(track);
+ const timescaleAndDuration = getTimescaleAndDuration(track);
if (!tkhdBox) {
throw new Error('Expected tkhd box in trak box');
@@ -94,6 +100,10 @@ export const getTracks = (
throw new Error('Expected stsc box in trak box');
}
+ if (!timescaleAndDuration) {
+ throw new Error('Expected timescale and duration in trak box');
+ }
+
const samplePositions = getSamplePositions({
stcoBox,
stscBox,
@@ -107,6 +117,7 @@ export const getTracks = (
samplePositions,
trackId: tkhdBox.trackId,
description: null,
+ timescale: timescaleAndDuration.timescale,
});
}
@@ -116,6 +127,7 @@ export const getTracks = (
samplePositions,
trackId: tkhdBox.trackId,
description: videoDescriptors,
+ timescale: timescaleAndDuration.timescale,
});
}
}
diff --git a/packages/media-parser/src/test/stream-samples.test.ts b/packages/media-parser/src/test/stream-samples.test.ts
index c20a5ac42a..3c6448f9de 100644
--- a/packages/media-parser/src/test/stream-samples.test.ts
+++ b/packages/media-parser/src/test/stream-samples.test.ts
@@ -75,6 +75,7 @@ test('Stream samples', async () => {
},
],
description,
+ timescale: 15360,
},
]);
@@ -160,6 +161,7 @@ test('Stream samples', async () => {
},
],
description: null,
+ timescale: 48000,
},
]);
});
diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.ts
index a9353b89d3..44b47858cf 100644
--- a/packages/media-parser/src/traversal.ts
+++ b/packages/media-parser/src/traversal.ts
@@ -1,4 +1,5 @@
import type {FtypBox} from './boxes/iso-base-media/ftyp';
+import type {MdhdBox} from './boxes/iso-base-media/mdhd';
import type {MoovBox} from './boxes/iso-base-media/moov/moov';
import type {MvhdBox} from './boxes/iso-base-media/mvhd';
import type {StcoBox} from './boxes/iso-base-media/stsd/stco';
@@ -6,6 +7,7 @@ import type {StscBox} from './boxes/iso-base-media/stsd/stsc';
import type {StsdBox} from './boxes/iso-base-media/stsd/stsd';
import type {StssBox} from './boxes/iso-base-media/stsd/stss';
import type {StszBox} from './boxes/iso-base-media/stsd/stsz';
+import type {SttsBox} from './boxes/iso-base-media/stts/stts';
import type {TkhdBox} from './boxes/iso-base-media/tkhd';
import type {TrakBox} from './boxes/iso-base-media/trak/trak';
import type {AnySegment, RegularBox} from './parse-result';
@@ -62,6 +64,20 @@ export const getMdiaBox = (trakBox: TrakBox): RegularBox | null => {
return mdiaBox;
};
+export const getMdhdBox = (trakBox: TrakBox): MdhdBox | null => {
+ const mdiaBox = getMdiaBox(trakBox);
+
+ if (!mdiaBox) {
+ return null;
+ }
+
+ const mdhdBox = mdiaBox.children.find(
+ (c) => c.type === 'mdhd-box',
+ ) as MdhdBox | null;
+
+ return mdhdBox;
+};
+
export const getStblBox = (trakBox: TrakBox): RegularBox | null => {
const mdiaBox = getMdiaBox(trakBox);
@@ -138,6 +154,20 @@ export const getStcoBox = (trakBox: TrakBox): StcoBox | null => {
return stcoBox;
};
+export const getSttsBox = (trakBox: TrakBox): SttsBox | null => {
+ const stblBox = getStblBox(trakBox);
+
+ if (!stblBox || stblBox.type !== 'regular-box') {
+ return null;
+ }
+
+ const sttsBox = stblBox.children.find(
+ (s) => s.type === 'stts-box',
+ ) as SttsBox | null;
+
+ return sttsBox;
+};
+
export const getStszBox = (trakBox: TrakBox): StszBox | null => {
const stblBox = getStblBox(trakBox);