Skip to content

🎥 Make videos programmatically with React

parse ctts

Jonny Burger8a5cb751c06c
diff --git a/packages/media-parser/src/boxes/iso-base-media/process-box.ts b/packages/media-parser/src/boxes/iso-base-media/process-box.ts
index edee36ea03..722f1bf985 100644
--- a/packages/media-parser/src/boxes/iso-base-media/process-box.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/process-box.ts
@@ -15,13 +15,14 @@ import {parseMdat} from './mdat/mdat';
import {parseMdhd} from './mdhd';
import {parseMoov} from './moov/moov';
import {parseMvhd} from './mvhd';
+import {parseCtts} from './stsd/ctts';
import {parseMebx} from './stsd/mebx';
import {parseStco} from './stsd/stco';
import {parseStsc} from './stsd/stsc';
import {parseStsd} from './stsd/stsd';
import {parseStss} from './stsd/stss';
import {parseStsz} from './stsd/stsz';
-import {parseStts} from './stts/stts';
+import {parseStts} from './stsd/stts';
import {parseTkhd} from './tkhd';
import {parseTrak} from './trak/trak';
@@ -232,6 +233,21 @@ const processBox = ({
};
}
+ if (boxType === 'ctts') {
+ const box = parseCtts({
+ iterator,
+ offset: fileOffset,
+ size: boxSize,
+ });
+
+ return {
+ type: 'complete',
+ box,
+ size: boxSize,
+ skipTo: null,
+ };
+ }
+
if (boxType === 'stsc') {
const box = parseStsc({
iterator,
diff --git a/packages/media-parser/src/boxes/iso-base-media/stsd/ctts.ts b/packages/media-parser/src/boxes/iso-base-media/stsd/ctts.ts
new file mode 100644
index 0000000000..e397066a7b
--- /dev/null
+++ b/packages/media-parser/src/boxes/iso-base-media/stsd/ctts.ts
@@ -0,0 +1,54 @@
+import type {BufferIterator} from '../../../buffer-iterator';
+
+type CttsEntry = {
+ sampleCount: number;
+ sampleOffset: number;
+};
+
+export interface CttsBox extends BaseBox {
+ type: 'ctts-box';
+ version: number;
+ flags: number[];
+ entryCount: number;
+ entries: CttsEntry[];
+}
+
+export const parseCtts = ({
+ iterator,
+ offset,
+ size,
+}: {
+ iterator: BufferIterator;
+ offset: number;
+ size: number;
+}): CttsBox => {
+ const version = iterator.getUint8();
+ if (version !== 0) {
+ throw new Error(`Unsupported CTTS version ${version}`);
+ }
+
+ const flags = iterator.getSlice(3);
+ const entryCount = iterator.getUint32();
+
+ const entries: CttsEntry[] = [];
+
+ for (let i = 0; i < entryCount; i++) {
+ const sampleCount = iterator.getUint32();
+ const sampleOffset = iterator.getUint32();
+
+ entries.push({
+ sampleCount,
+ sampleOffset,
+ });
+ }
+
+ return {
+ type: 'ctts-box',
+ boxSize: size,
+ offset,
+ version,
+ flags: [...flags],
+ entryCount,
+ entries,
+ };
+};
diff --git a/packages/media-parser/src/boxes/iso-base-media/stts/stts.ts b/packages/media-parser/src/boxes/iso-base-media/stsd/stts.ts
similarity index 100%
rename from packages/media-parser/src/boxes/iso-base-media/stts/stts.ts
rename to packages/media-parser/src/boxes/iso-base-media/stsd/stts.ts
diff --git a/packages/media-parser/src/get-fps.ts b/packages/media-parser/src/get-fps.ts
index 3e03576b5f..fd098fb962 100644
--- a/packages/media-parser/src/get-fps.ts
+++ b/packages/media-parser/src/get-fps.ts
@@ -1,4 +1,4 @@
-import type {SttsBox} from './boxes/iso-base-media/stts/stts';
+import type {SttsBox} from './boxes/iso-base-media/stsd/stts';
import type {TrakBox} from './boxes/iso-base-media/trak/trak';
import type {AnySegment} from './parse-result';
import {
diff --git a/packages/media-parser/src/get-sample-positions.ts b/packages/media-parser/src/get-sample-positions.ts
index 16ecc23f14..69c526a935 100644
--- a/packages/media-parser/src/get-sample-positions.ts
+++ b/packages/media-parser/src/get-sample-positions.ts
@@ -1,14 +1,16 @@
+import type {CttsBox} from './boxes/iso-base-media/stsd/ctts';
import type {StcoBox} from './boxes/iso-base-media/stsd/stco';
import type {StscBox} from './boxes/iso-base-media/stsd/stsc';
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 {SttsBox} from './boxes/iso-base-media/stsd/stts';
export type SamplePosition = {
offset: number;
size: number;
isKeyframe: boolean;
dts: number;
+ cts: number;
duration: number;
};
@@ -18,17 +20,28 @@ export const getSamplePositions = ({
stscBox,
stssBox,
sttsBox,
+ cttsBox,
}: {
stcoBox: StcoBox;
stszBox: StszBox;
stscBox: StscBox;
stssBox: StssBox | null;
sttsBox: SttsBox;
+ cttsBox: CttsBox | null;
}) => {
- const deltas: number[] = [];
+ const sttsDeltas: number[] = [];
for (const distribution of sttsBox.sampleDistribution) {
for (let i = 0; i < distribution.sampleCount; i++) {
- deltas.push(distribution.sampleDelta);
+ sttsDeltas.push(distribution.sampleDelta);
+ }
+ }
+
+ const cttsEntries: number[] = [];
+ for (const entry of cttsBox?.entries ?? [
+ {sampleCount: sttsDeltas.length, sampleOffset: 0},
+ ]) {
+ for (let i = 0; i < entry.sampleCount; i++) {
+ cttsEntries.push(entry.sampleOffset);
}
}
@@ -59,13 +72,16 @@ export const getSamplePositions = ({
? stssBox.sampleNumber.includes(samples.length + 1)
: true;
- const delta = deltas[samples.length];
+ const delta = sttsDeltas[samples.length];
+ const ctsOffset = cttsEntries[samples.length];
+ const cts = dts + ctsOffset;
samples.push({
offset: chunks[i] + offsetInThisChunk,
size,
isKeyframe,
dts,
+ cts,
duration: delta,
});
dts += delta;
diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.ts
index 55f56fbda7..81d7f7f6fe 100644
--- a/packages/media-parser/src/get-tracks.ts
+++ b/packages/media-parser/src/get-tracks.ts
@@ -8,6 +8,7 @@ import type {SamplePosition} from './get-sample-positions';
import {getSamplePositions} from './get-sample-positions';
import type {AnySegment} from './parse-result';
import {
+ getCttsBox,
getFtypBox,
getMoovBox,
getMvhdBox,
@@ -83,6 +84,7 @@ export const getTracks = (
const stssBox = getStssBox(track);
const sttsBox = getSttsBox(track);
const tkhdBox = getTkhdBox(track);
+ const cttsBox = getCttsBox(track);
const videoDescriptors = getVideoDescriptors(track);
const timescaleAndDuration = getTimescaleAndDuration(track);
@@ -116,6 +118,7 @@ export const getTracks = (
stszBox,
stssBox,
sttsBox,
+ cttsBox,
});
if (trakBoxContainsAudio(track)) {
diff --git a/packages/media-parser/src/parse-result.ts b/packages/media-parser/src/parse-result.ts
index b5498d3204..de3f2a2b6f 100644
--- a/packages/media-parser/src/parse-result.ts
+++ b/packages/media-parser/src/parse-result.ts
@@ -6,6 +6,7 @@ import type {MdatBox} from './boxes/iso-base-media/mdat/mdat';
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 {CttsBox} from './boxes/iso-base-media/stsd/ctts';
import type {KeysBox} from './boxes/iso-base-media/stsd/keys';
import type {MebxBox} from './boxes/iso-base-media/stsd/mebx';
import type {StcoBox} from './boxes/iso-base-media/stsd/stco';
@@ -13,7 +14,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 {SttsBox} from './boxes/iso-base-media/stsd/stts';
import type {TkhdBox} from './boxes/iso-base-media/tkhd';
import type {TrakBox} from './boxes/iso-base-media/trak/trak';
import type {VoidBox} from './boxes/iso-base-media/void-box';
@@ -47,7 +48,8 @@ export type IsoBaseMediaBox =
| AvccBox
| HvccBox
| VoidBox
- | StssBox;
+ | StssBox
+ | CttsBox;
export type AnySegment = MatroskaSegment | IsoBaseMediaBox;
diff --git a/packages/media-parser/src/test/parse-stts.test.ts b/packages/media-parser/src/test/parse-stts.test.ts
index 7ef6472763..af5d02cb7f 100644
--- a/packages/media-parser/src/test/parse-stts.test.ts
+++ b/packages/media-parser/src/test/parse-stts.test.ts
@@ -1,5 +1,5 @@
import {expect, test} from 'bun:test';
-import {parseStts} from '../boxes/iso-base-media/stts/stts';
+import {parseStts} from '../boxes/iso-base-media/stsd/stts';
import {getArrayBufferIterator} from '../buffer-iterator';
const buffer = new Uint8Array([
diff --git a/packages/media-parser/src/test/stream-samples.test.ts b/packages/media-parser/src/test/stream-samples.test.ts
index 2fa3de7714..e878dcb1e5 100644
--- a/packages/media-parser/src/test/stream-samples.test.ts
+++ b/packages/media-parser/src/test/stream-samples.test.ts
@@ -29,6 +29,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 0,
duration: 512,
+ cts: 1024,
},
{
offset: 6471,
@@ -36,6 +37,7 @@ test('Stream samples', async () => {
isKeyframe: false,
dts: 512,
duration: 512,
+ cts: 1536,
},
{
offset: 7868,
@@ -43,6 +45,7 @@ test('Stream samples', async () => {
isKeyframe: false,
dts: 1024,
duration: 512,
+ cts: 2048,
},
{
offset: 10486,
@@ -50,6 +53,7 @@ test('Stream samples', async () => {
isKeyframe: false,
dts: 1536,
duration: 512,
+ cts: 2560,
},
{
offset: 12041,
@@ -57,6 +61,7 @@ test('Stream samples', async () => {
isKeyframe: false,
dts: 2048,
duration: 512,
+ cts: 3072,
},
{
offset: 14509,
@@ -64,6 +69,7 @@ test('Stream samples', async () => {
isKeyframe: false,
dts: 2560,
duration: 512,
+ cts: 3584,
},
{
offset: 16080,
@@ -71,6 +77,7 @@ test('Stream samples', async () => {
isKeyframe: false,
dts: 3072,
duration: 512,
+ cts: 4096,
},
{
offset: 17741,
@@ -78,6 +85,7 @@ test('Stream samples', async () => {
isKeyframe: false,
dts: 3584,
duration: 512,
+ cts: 4608,
},
{
offset: 20196,
@@ -85,6 +93,7 @@ test('Stream samples', async () => {
isKeyframe: false,
dts: 4096,
duration: 512,
+ cts: 5120,
},
{
offset: 21932,
@@ -92,6 +101,7 @@ test('Stream samples', async () => {
isKeyframe: false,
dts: 4608,
duration: 512,
+ cts: 5632,
},
],
description,
@@ -110,6 +120,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 0,
duration: 1152,
+ cts: 0,
},
{
offset: 8566,
@@ -117,6 +128,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 1152,
duration: 1152,
+ cts: 1152,
},
{
offset: 9526,
@@ -124,6 +136,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 2304,
duration: 1152,
+ cts: 2304,
},
{
offset: 11081,
@@ -131,6 +144,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 3456,
duration: 1152,
+ cts: 3456,
},
{
offset: 12589,
@@ -138,6 +152,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 4608,
duration: 1152,
+ cts: 4608,
},
{
offset: 13549,
@@ -145,6 +160,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 5760,
duration: 1152,
+ cts: 5760,
},
{
offset: 15120,
@@ -152,6 +168,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 6912,
duration: 1152,
+ cts: 6912,
},
{
offset: 16781,
@@ -159,6 +176,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 8064,
duration: 1152,
+ cts: 8064,
},
{
offset: 18276,
@@ -166,6 +184,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 9216,
duration: 1152,
+ cts: 9216,
},
{
offset: 19236,
@@ -173,6 +192,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 10368,
duration: 1152,
+ cts: 10368,
},
{
offset: 20972,
@@ -180,6 +200,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 11520,
duration: 1152,
+ cts: 11520,
},
{
offset: 22518,
@@ -187,6 +208,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 12672,
duration: 1152,
+ cts: 12672,
},
{
offset: 23478,
@@ -194,6 +216,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 13824,
duration: 1152,
+ cts: 13824,
},
{
offset: 24438,
@@ -201,6 +224,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 14976,
duration: 1152,
+ cts: 14976,
},
{
offset: 25398,
@@ -208,6 +232,7 @@ test('Stream samples', async () => {
isKeyframe: true,
dts: 16128,
duration: 1152,
+ cts: 16128,
},
],
description: null,
diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.ts
index 44b47858cf..01038cb46b 100644
--- a/packages/media-parser/src/traversal.ts
+++ b/packages/media-parser/src/traversal.ts
@@ -2,12 +2,13 @@ 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 {CttsBox} from './boxes/iso-base-media/stsd/ctts';
import type {StcoBox} from './boxes/iso-base-media/stsd/stco';
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 {SttsBox} from './boxes/iso-base-media/stsd/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';
@@ -168,6 +169,20 @@ export const getSttsBox = (trakBox: TrakBox): SttsBox | null => {
return sttsBox;
};
+export const getCttsBox = (trakBox: TrakBox): CttsBox | null => {
+ const stblBox = getStblBox(trakBox);
+
+ if (!stblBox || stblBox.type !== 'regular-box') {
+ return null;
+ }
+
+ const cttsBox = stblBox.children.find(
+ (s) => s.type === 'ctts-box',
+ ) as CttsBox | null;
+
+ return cttsBox;
+};
+
export const getStszBox = (trakBox: TrakBox): StszBox | null => {
const stblBox = getStblBox(trakBox);