Skip to content

🎥 Make videos programmatically with React

alright

Jonny Burgerb4c3d05101ac
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 145508915b..1537f81767 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
@@ -13,6 +13,7 @@ import {parseMoov} from './moov/moov';
import {parseMvhd} from './mvhd';
import {parseMebx} from './stsd/mebx';
import {parseStco} from './stsd/stco';
+import {parseStsc} from './stsd/stsc';
import {parseStsd} from './stsd/stsd';
import {parseStsz} from './stsd/stsz';
import {parseStts} from './stts/stts';
@@ -200,6 +201,21 @@ const processBox = ({
};
}
+ if (boxType === 'stsc') {
+ const box = parseStsc({
+ iterator,
+ offset: fileOffset,
+ size: boxSize,
+ });
+
+ return {
+ type: 'complete',
+ box,
+ size: boxSize,
+ skipTo: null,
+ };
+ }
+
if (boxType === 'mebx') {
const box = parseMebx({
iterator,
diff --git a/packages/media-parser/src/boxes/iso-base-media/stsd/stsc.ts b/packages/media-parser/src/boxes/iso-base-media/stsd/stsc.ts
new file mode 100644
index 0000000000..175ad968ca
--- /dev/null
+++ b/packages/media-parser/src/boxes/iso-base-media/stsd/stsc.ts
@@ -0,0 +1,61 @@
+import type {BufferIterator} from '../../../buffer-iterator';
+import type {BaseBox} from '../base-type';
+
+type StscEntry = {
+ firstChunk: number;
+ samplesPerChunk: number;
+};
+
+export interface StscBox extends BaseBox {
+ type: 'stsc-box';
+ version: number;
+ flags: number[];
+ entryCount: number;
+ entries: StscEntry[];
+}
+
+export const parseStsc = ({
+ iterator,
+ offset,
+ size,
+}: {
+ iterator: BufferIterator;
+ offset: number;
+ size: number;
+}): StscBox => {
+ const version = iterator.getUint8();
+ if (version !== 0) {
+ throw new Error(`Unsupported STSD version ${version}`);
+ }
+
+ const flags = iterator.getSlice(3);
+ const entryCount = iterator.getUint32();
+
+ const entries: StscEntry[] = [];
+
+ for (let i = 0; i < entryCount; i++) {
+ const firstChunk = iterator.getUint32();
+ const samplesPerChunk = iterator.getUint32();
+ const sampleDescriptionIndex = iterator.getUint32();
+ if (sampleDescriptionIndex !== 1) {
+ throw new Error(
+ `Expected sampleDescriptionIndex to be 1, but got ${sampleDescriptionIndex}`,
+ );
+ }
+
+ entries.push({
+ firstChunk,
+ samplesPerChunk,
+ });
+ }
+
+ return {
+ type: 'stsc-box',
+ boxSize: size,
+ offset,
+ version,
+ flags: [...flags],
+ entryCount,
+ entries,
+ };
+};
diff --git a/packages/media-parser/src/get-sample-positions.ts b/packages/media-parser/src/get-sample-positions.ts
new file mode 100644
index 0000000000..0c6804dfe5
--- /dev/null
+++ b/packages/media-parser/src/get-sample-positions.ts
@@ -0,0 +1,13 @@
+import type {StcoBox} from './boxes/iso-base-media/stsd/stco';
+import type {StscBox} from './boxes/iso-base-media/stsd/stsc';
+import type {StszBox} from './boxes/iso-base-media/stsd/stsz';
+
+export const getSamplePositions = ({
+ stcoBox,
+ stszBox,
+ stscBox,
+}: {
+ stcoBox: StcoBox;
+ stszBox: StszBox;
+ stscBox: StscBox;
+}) => {};
diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.ts
index c9532fd1e4..d87d38921a 100644
--- a/packages/media-parser/src/get-tracks.ts
+++ b/packages/media-parser/src/get-tracks.ts
@@ -6,6 +6,7 @@ import {
getMoovBox,
getMvhdBox,
getStcoBox,
+ getStscBox,
getStszBox,
getTkhdBox,
getTraks,
@@ -45,7 +46,9 @@ export const getTracks = (segments: AnySegment[]): Track[] => {
for (const track of tracks) {
const stszBox = getStszBox(track);
const stcoBox = getStcoBox(track);
+ const stscBox = getStscBox(track);
const tkhdBox = getTkhdBox(track);
+
if (!tkhdBox) {
throw new Error('Expected tkhd box in trak box');
}
diff --git a/packages/media-parser/src/parse-result.ts b/packages/media-parser/src/parse-result.ts
index 317ae6a879..1f20fc98b6 100644
--- a/packages/media-parser/src/parse-result.ts
+++ b/packages/media-parser/src/parse-result.ts
@@ -8,6 +8,7 @@ import type {MvhdBox} from './boxes/iso-base-media/mvhd';
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';
+import type {StscBox} from './boxes/iso-base-media/stsd/stsc';
import type {StsdBox} from './boxes/iso-base-media/stsd/stsd';
import type {StszBox} from './boxes/iso-base-media/stsd/stsz';
import type {SttsBox} from './boxes/iso-base-media/stts/stts';
@@ -38,7 +39,8 @@ export type IsoBaseMediaBox =
| EsdsBox
| MdatBox
| StszBox
- | StcoBox;
+ | StcoBox
+ | StscBox;
export type AnySegment = MatroskaSegment | IsoBaseMediaBox;
diff --git a/packages/media-parser/src/test/parse-stsc.test.ts b/packages/media-parser/src/test/parse-stsc.test.ts
new file mode 100644
index 0000000000..a1191ba2fb
--- /dev/null
+++ b/packages/media-parser/src/test/parse-stsc.test.ts
@@ -0,0 +1,104 @@
+import {expect, test} from 'bun:test';
+import {parseStsc} from '../boxes/iso-base-media/stsd/stsc';
+import {getArrayBufferIterator} from '../buffer-iterator';
+
+test('Parse stsc box', () => {
+ const buffer = Uint8Array.from([
+ // mock header
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ // actual box
+ 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0,
+ 0, 0, 1, 0, 0, 0, 1,
+ ]);
+
+ const iterator = getArrayBufferIterator(buffer);
+ iterator.counter.increment(8);
+ const result = parseStsc({
+ iterator,
+ offset: 0,
+ size: buffer.length - 8,
+ });
+
+ expect(result).toEqual({
+ boxSize: 32,
+ flags: [0, 0, 0],
+ offset: 0,
+ type: 'stsc-box',
+ version: 0,
+ entryCount: 2,
+ entries: [
+ {
+ firstChunk: 1,
+ samplesPerChunk: 2,
+ },
+ {
+ firstChunk: 2,
+ samplesPerChunk: 1,
+ },
+ ],
+ });
+});
+
+test('Parse stsc box 2', () => {
+ const buffer = Uint8Array.from([
+ // mock header
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ // actual box
+ 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0,
+ 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0,
+ 0, 2, 0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 7, 0, 0, 0,
+ 2, 0, 0, 0, 1, 0, 0, 0, 8, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 9, 0, 0, 0, 4,
+ 0, 0, 0, 1,
+ ]);
+
+ const iterator = getArrayBufferIterator(buffer);
+ iterator.counter.increment(8);
+ const result = parseStsc({
+ iterator,
+ offset: 0,
+ size: buffer.length - 8,
+ });
+
+ expect(result).toEqual({
+ boxSize: 104,
+ flags: [0, 0, 0],
+ offset: 0,
+ type: 'stsc-box',
+ version: 0,
+ entryCount: 8,
+ entries: [
+ {
+ firstChunk: 1,
+ samplesPerChunk: 1,
+ },
+ {
+ firstChunk: 2,
+ samplesPerChunk: 2,
+ },
+ {
+ firstChunk: 3,
+ samplesPerChunk: 1,
+ },
+ {
+ firstChunk: 4,
+ samplesPerChunk: 2,
+ },
+ {
+ firstChunk: 5,
+ samplesPerChunk: 1,
+ },
+ {
+ firstChunk: 7,
+ samplesPerChunk: 2,
+ },
+ {
+ firstChunk: 8,
+ samplesPerChunk: 1,
+ },
+ {
+ firstChunk: 9,
+ samplesPerChunk: 4,
+ },
+ ],
+ });
+});
diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.ts
index d93d470adb..b71749770c 100644
--- a/packages/media-parser/src/traversal.ts
+++ b/packages/media-parser/src/traversal.ts
@@ -1,6 +1,7 @@
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';
+import type {StscBox} from './boxes/iso-base-media/stsd/stsc';
import type {StsdBox} from './boxes/iso-base-media/stsd/stsd';
import type {StszBox} from './boxes/iso-base-media/stsd/stsz';
import type {TkhdBox} from './boxes/iso-base-media/tkhd';
@@ -117,3 +118,11 @@ export const getStszBox = (trakBox: TrakBox): StszBox | null => {
return stszBox;
};
+
+export const getStscBox = (trakBox: TrakBox): StscBox | null => {
+ const stcoBox = trakBox.children.find(
+ (b) => b.type === 'stsc-box',
+ ) as StscBox | null;
+
+ return stcoBox;
+};