Skip to content

🎥 Make videos programmatically with React

parse boxes

Jonny Burger7622ee5c6c62
diff --git a/packages/video-parser/src/boxes/iso-base-media/dims.ts b/packages/video-parser/src/boxes/iso-base-media/dims.ts
new file mode 100644
index 0000000000..23e2d421e1
--- /dev/null
+++ b/packages/video-parser/src/boxes/iso-base-media/dims.ts
@@ -0,0 +1,21 @@
+import type {BaseBox} from './base-type';
+
+export interface DimensionsBox extends BaseBox {
+ width: number;
+ height: number;
+ type: 'dims-box';
+}
+
+export const parseDims = (data: Buffer, offset: number): DimensionsBox => {
+ console.log({data});
+ const width = data.readUInt16BE(offset);
+ const height = data.readUInt16BE(offset + 2);
+
+ return {
+ width,
+ height,
+ boxSize: data.length,
+ offset,
+ type: 'dims-box',
+ };
+};
diff --git a/packages/video-parser/src/boxes/iso-base-media/process-box.ts b/packages/video-parser/src/boxes/iso-base-media/process-box.ts
index f8fc1085ea..57c5a40b62 100644
--- a/packages/video-parser/src/boxes/iso-base-media/process-box.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/process-box.ts
@@ -1,6 +1,8 @@
import type {Box} from '../../parse-video';
+import {parseDims} from './dims';
import {fourByteToNumber, parseFtyp} from './ftype';
import {parseMvhd} from './mvhd';
+import {parseStsd} from './stsd/stsd';
import {parseTkhd} from './tkhd';
type BoxAndNext = {
@@ -17,7 +19,13 @@ const processBoxAndSubtract = ({
fileOffset: number;
}): BoxAndNext => {
const boxSize = fourByteToNumber(data, 0);
- const boxType = data.subarray(4, 8).toString('utf-8');
+ if (boxSize === 0) {
+ throw new Error(`Expected box size of ${data.length}, got ${boxSize}`);
+ }
+
+ const boxTypeBuffer = data.subarray(4, 8);
+
+ const boxType = boxTypeBuffer.toString('utf-8');
const sub = data.subarray(0, boxSize);
const next = data.subarray(boxSize);
@@ -46,12 +54,29 @@ const processBoxAndSubtract = ({
};
}
+ if (boxType === 'dims') {
+ return {
+ box: parseDims(sub, fileOffset),
+ next,
+ size: boxSize,
+ };
+ }
+
+ if (boxType === 'stsd') {
+ return {
+ box: parseStsd(sub, fileOffset),
+ next,
+ size: boxSize,
+ };
+ }
+
const children =
boxType === 'moov' ||
boxType === 'trak' ||
boxType === 'mdia' ||
boxType === 'minf' ||
boxType === 'stbl' ||
+ boxType === 'dims' ||
boxType === 'stsb'
? parseBoxes(sub.subarray(8), fileOffset)
: [];
diff --git a/packages/video-parser/src/boxes/iso-base-media/stsd/mebx.ts b/packages/video-parser/src/boxes/iso-base-media/stsd/mebx.ts
new file mode 100644
index 0000000000..b44f03352c
--- /dev/null
+++ b/packages/video-parser/src/boxes/iso-base-media/stsd/mebx.ts
@@ -0,0 +1,37 @@
+import type {BaseBox} from '../base-type';
+
+export interface MebxBox extends BaseBox {
+ type: 'mebx-box';
+ dataReferenceIndex: number;
+ format: string;
+}
+
+export const parseMebx = (data: Buffer, offset: number): MebxBox => {
+ let chunkOffset = 0;
+
+ const size = data.readUInt32BE(chunkOffset);
+ if (size !== data.length) {
+ throw new Error(`Expected mebx size of ${data.length}, got ${size}`);
+ }
+
+ chunkOffset += 4;
+ const type = data.subarray(chunkOffset, chunkOffset + 4).toString('utf-8');
+ if (type !== 'mebx') {
+ throw new Error(`Expected mebx type of mebx, got ${type}`);
+ }
+
+ chunkOffset += 4;
+
+ // reserved, 6 bit
+ chunkOffset += 6;
+
+ const dataReferenceIndex = data.readUInt16BE(chunkOffset);
+
+ return {
+ type: 'mebx-box',
+ boxSize: data.length,
+ offset,
+ dataReferenceIndex,
+ format: 'mebx',
+ };
+};
diff --git a/packages/video-parser/src/boxes/iso-base-media/stsd/stsd.ts b/packages/video-parser/src/boxes/iso-base-media/stsd/stsd.ts
new file mode 100644
index 0000000000..7b5f203e90
--- /dev/null
+++ b/packages/video-parser/src/boxes/iso-base-media/stsd/stsd.ts
@@ -0,0 +1,107 @@
+import type {BaseBox} from '../base-type';
+import type {MebxBox} from './mebx';
+import {parseMebx} from './mebx';
+
+export interface StsdBox extends BaseBox {
+ type: 'stsd-box';
+ numberOfEntries: number;
+ descriptions: StsdDescription[];
+}
+
+type UnknownBox = {
+ type: 'unknown-box';
+};
+
+type StsdDescription = MebxBox | UnknownBox;
+
+const consumeSampleDescriptions = (
+ data: Buffer,
+ offset: number,
+): {
+ description: StsdDescription;
+ consumed: number;
+} => {
+ let chunkOffset = 0;
+
+ const size = data.readUInt32BE(chunkOffset);
+ chunkOffset += 4;
+ const type = data.subarray(chunkOffset, chunkOffset + 4).toString('utf-8');
+ chunkOffset += 4;
+
+ if (type === 'mebx') {
+ return {
+ description: parseMebx(data.subarray(0, size), offset),
+ consumed: size,
+ };
+ }
+
+ return {
+ consumed: size,
+ description: {
+ type: 'unknown-box',
+ },
+ };
+};
+
+export const parseStsd = (data: Buffer, offset: number): StsdBox => {
+ let chunkOffset = 0;
+
+ const size = data.readUInt32BE(chunkOffset);
+ if (size !== data.length) {
+ throw new Error(`Expected stsd size of ${data.length}, got ${size}`);
+ }
+
+ chunkOffset += 4;
+
+ const type = data.subarray(chunkOffset, chunkOffset + 4).toString('utf-8');
+ if (type !== 'stsd') {
+ throw new Error(`Expected stsd type of stsd, got ${type}`);
+ }
+
+ chunkOffset += 4;
+
+ const version = data.readUInt8(chunkOffset);
+ chunkOffset += 1;
+ if (version !== 0) {
+ throw new Error(`Unsupported STSD version ${version}`);
+ }
+
+ // flags, we discard them
+ data.subarray(chunkOffset, chunkOffset + 3);
+ chunkOffset += 3;
+
+ const numberOfEntries = data.readUInt32BE(chunkOffset);
+ chunkOffset += 4;
+
+ let remaining = size - chunkOffset;
+
+ const descriptions: StsdDescription[] = [];
+
+ for (let i = 0; i < numberOfEntries; i++) {
+ const result = consumeSampleDescriptions(
+ data.subarray(chunkOffset),
+ offset + chunkOffset,
+ );
+ remaining -= result.consumed;
+ chunkOffset += result.consumed;
+ descriptions.push(result.description);
+ }
+
+ if (descriptions.length !== numberOfEntries) {
+ throw new Error(
+ `Expected ${numberOfEntries} sample descriptions, got ${descriptions.length}`,
+ );
+ }
+
+ if (remaining !== 0) {
+ throw new Error(`Expected 0 bytes remaining, got ${remaining}`);
+ }
+
+ return {
+ type: 'stsd-box',
+ boxSize: data.length,
+ offset,
+ numberOfEntries,
+ descriptions,
+ };
+};
diff --git a/packages/video-parser/src/parse-video.ts b/packages/video-parser/src/parse-video.ts
index 6762af97d1..44ae8115ee 100644
--- a/packages/video-parser/src/parse-video.ts
+++ b/packages/video-parser/src/parse-video.ts
@@ -1,8 +1,10 @@
import {createReadStream} from 'fs';
import type {BaseBox} from './boxes/iso-base-media/base-type';
+import type {DimensionsBox} from './boxes/iso-base-media/dims';
import type {FtypBox} from './boxes/iso-base-media/ftype';
import type {MvhdBox} from './boxes/iso-base-media/mvhd';
import {parseBoxes} from './boxes/iso-base-media/process-box';
+import type {StsdBox} from './boxes/iso-base-media/stsd/stsd';
import type {TkhdBox} from './boxes/iso-base-media/tkhd';
interface RegularBox extends BaseBox {
@@ -13,7 +15,13 @@ interface RegularBox extends BaseBox {
type: 'regular-box';
}
-export type Box = RegularBox | FtypBox | MvhdBox | TkhdBox;
+export type Box =
+ | RegularBox
+ | FtypBox
+ | MvhdBox
+ | TkhdBox
+ | DimensionsBox
+ | StsdBox;
const isoBaseMediaMp4Pattern = Buffer.from('ftyp');
diff --git a/packages/video-parser/src/test/dimensions.test.ts b/packages/video-parser/src/test/dimensions.test.ts
new file mode 100644
index 0000000000..7f4f7cd558
--- /dev/null
+++ b/packages/video-parser/src/test/dimensions.test.ts
@@ -0,0 +1,3 @@
+import {test} from 'bun:test';
+
+test('Get dimensions of a video', () => {});
diff --git a/packages/video-parser/src/test/stsd.test.ts b/packages/video-parser/src/test/stsd.test.ts
new file mode 100644
index 0000000000..537df87bf3
--- /dev/null
+++ b/packages/video-parser/src/test/stsd.test.ts
@@ -0,0 +1,59 @@
+import {expect, test} from 'bun:test';
+import {parseStsd} from '../boxes/iso-base-media/stsd/stsd';
+
+test('Should be able to parse a STSD box correctly', () => {
+ const buffer = Buffer.from([
+ 0, 0, 2, 92, 115, 116, 115, 100, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 76, 109,
+ 101, 98, 120, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 60, 107, 101, 121, 115, 0, 0,
+ 2, 52, 0, 0, 0, 1, 0, 0, 0, 47, 107, 101, 121, 100, 109, 100, 116, 97, 99,
+ 111, 109, 46, 97, 112, 112, 108, 101, 46, 113, 117, 105, 99, 107, 116, 105,
+ 109, 101, 46, 108, 105, 118, 101, 45, 112, 104, 111, 116, 111, 45, 105, 110,
+ 102, 111, 0, 0, 0, 67, 100, 116, 121, 112, 0, 0, 0, 1, 99, 111, 109, 46, 97,
+ 112, 112, 108, 101, 46, 113, 117, 105, 99, 107, 116, 105, 109, 101, 46, 99,
+ 111, 109, 46, 97, 112, 112, 108, 101, 46, 113, 117, 105, 99, 107, 116, 105,
+ 109, 101, 46, 108, 105, 118, 101, 45, 112, 104, 111, 116, 111, 45, 105, 110,
+ 102, 111, 0, 0, 0, 20, 115, 100, 112, 100, 0, 0, 0, 12, 115, 100, 112, 105,
+ 0, 0, 0, 0, 0, 0, 1, 142, 115, 101, 116, 117, 0, 0, 1, 118, 99, 102, 103,
+ 118, 98, 112, 108, 105, 115, 116, 48, 48, 211, 1, 2, 3, 4, 5, 12, 95, 16,
+ 33, 76, 105, 118, 101, 80, 104, 111, 116, 111, 77, 101, 116, 97, 100, 97,
+ 116, 97, 83, 101, 116, 117, 112, 68, 97, 116, 97, 86, 101, 114, 115, 105,
+ 111, 110, 93, 83, 121, 115, 116, 101, 109, 86, 101, 114, 115, 105, 111, 110,
+ 95, 16, 17, 70, 114, 97, 109, 101, 119, 111, 114, 107, 86, 101, 114, 115,
+ 105, 111, 110, 115, 16, 1, 211, 6, 7, 8, 9, 10, 11, 95, 16, 19, 80, 114,
+ 111, 100, 117, 99, 116, 66, 117, 105, 108, 100, 86, 101, 114, 115, 105, 111,
+ 110, 91, 80, 114, 111, 100, 117, 99, 116, 78, 97, 109, 101, 94, 80, 114,
+ 111, 100, 117, 99, 116, 86, 101, 114, 115, 105, 111, 110, 88, 50, 49, 70,
+ 53, 48, 52, 56, 102, 89, 105, 80, 104, 111, 110, 101, 32, 79, 83, 84, 49,
+ 55, 46, 53, 213, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 94, 72, 49, 51, 73,
+ 83, 80, 83, 101, 114, 118, 105, 99, 101, 115, 90, 67, 111, 114, 101, 77,
+ 111, 116, 105, 111, 110, 93, 67, 77, 67, 97, 112, 116, 117, 114, 101, 67,
+ 111, 114, 101, 94, 72, 49, 48, 73, 83, 80, 83, 101, 114, 118, 105, 99, 101,
+ 115, 89, 67, 111, 114, 101, 77, 101, 100, 105, 97, 85, 56, 46, 53, 48, 50,
+ 94, 50, 56, 57, 48, 46, 49, 54, 46, 49, 51, 46, 48, 46, 49, 87, 52, 55, 55,
+ 46, 50, 46, 49, 86, 50, 48, 46, 53, 48, 48, 88, 51, 49, 49, 48, 46, 52, 46,
+ 49, 0, 8, 0, 15, 0, 51, 0, 65, 0, 85, 0, 87, 0, 94, 0, 116, 0, 128, 0, 143,
+ 0, 152, 0, 162, 0, 167, 0, 178, 0, 193, 0, 204, 0, 218, 0, 233, 0, 243, 0,
+ 249, 1, 8, 1, 16, 1, 23, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 23, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 32, 0, 0, 0, 16, 100, 105, 109,
+ 115, 0, 0, 7, 128, 0, 0, 4, 56, 0, 0, 0, 24, 99, 116, 112, 115, 0, 0, 0, 16,
+ 100, 116, 121, 112, 0, 0, 0, 0, 0, 0, 0, 0,
+ ]);
+
+ const parsed = parseStsd(buffer, 0);
+
+ expect(parsed).toEqual({
+ offset: 0,
+ boxSize: 604,
+ type: 'stsd-box',
+ numberOfEntries: 1,
+ descriptions: [
+ {
+ boxSize: 588,
+ dataReferenceIndex: 1,
+ format: 'mebx',
+ offset: 16,
+ type: 'mebx-box',
+ },
+ ],
+ });
+});
diff --git a/packages/video-parser/src/test/tkhd.test.ts b/packages/video-parser/src/test/tkhd.test.ts
index 7722fb281d..cd7d8bc029 100644
--- a/packages/video-parser/src/test/tkhd.test.ts
+++ b/packages/video-parser/src/test/tkhd.test.ts
@@ -1,7 +1,7 @@
import {expect, test} from 'bun:test';
import {parseTkhd} from '../boxes/iso-base-media/tkhd';
-test('Should be able to parse a TKHD box correctly', () => {
+test('Should be able to parse a TKHD box', () => {
const buffer = Buffer.from([
// size, 32 bit
0, 0, 0, 92,