🎥 Make videos programmatically with React
tkhd
Jonny Burger454ba82ee6a6
diff --git a/packages/video-parser/src/boxes/iso-base-media/base-type.ts b/packages/video-parser/src/boxes/iso-base-media/base-type.tsindex 7600321dbc..099db95d30 100644--- a/packages/video-parser/src/boxes/iso-base-media/base-type.ts+++ b/packages/video-parser/src/boxes/iso-base-media/base-type.ts@@ -1,5 +1,4 @@export interface BaseBox {- boxType: string;boxSize: number;offset: number;}diff --git a/packages/video-parser/src/boxes/iso-base-media/ftype.ts b/packages/video-parser/src/boxes/iso-base-media/ftype.tsindex 59f3134a94..c1ef7572ac 100644--- a/packages/video-parser/src/boxes/iso-base-media/ftype.ts+++ b/packages/video-parser/src/boxes/iso-base-media/ftype.ts@@ -36,6 +36,5 @@ export const parseFtyp = (data: Buffer, offset: number): FtypBox => {compatibleBrands,offset,boxSize: data.length,- boxType: 'ftyp',};};diff --git a/packages/video-parser/src/boxes/iso-base-media/mvhd.ts b/packages/video-parser/src/boxes/iso-base-media/mvhd.tsindex 23b3588410..6386796bbc 100644--- a/packages/video-parser/src/boxes/iso-base-media/mvhd.ts+++ b/packages/video-parser/src/boxes/iso-base-media/mvhd.ts@@ -1,6 +1,6 @@import type {BaseBox} from './base-type';-type ThreeDMatrix = [+export type ThreeDMatrix = [number,number,number,@@ -106,6 +106,5 @@ export const parseMvhd = (data: Buffer, offset: number): MvhdBox => {type: 'mvhd-box',boxSize: data.length,offset,- boxType: 'mvhd',};};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.tsnew file mode 100644index 0000000000..f8fc1085ea--- /dev/null+++ b/packages/video-parser/src/boxes/iso-base-media/process-box.ts@@ -0,0 +1,89 @@+import type {Box} from '../../parse-video';+import {fourByteToNumber, parseFtyp} from './ftype';+import {parseMvhd} from './mvhd';+import {parseTkhd} from './tkhd';++type BoxAndNext = {+ box: Box;+ next: Buffer;+ size: number;+};++const processBoxAndSubtract = ({+ data,+ fileOffset,+}: {+ data: Buffer;+ fileOffset: number;+}): BoxAndNext => {+ const boxSize = fourByteToNumber(data, 0);+ const boxType = data.subarray(4, 8).toString('utf-8');++ const sub = data.subarray(0, boxSize);+ const next = data.subarray(boxSize);++ if (boxType === 'ftyp') {+ return {+ box: parseFtyp(sub, fileOffset),+ next,+ size: boxSize,+ };+ }++ if (boxType === 'mvhd') {+ return {+ box: parseMvhd(sub, fileOffset),+ next,+ size: boxSize,+ };+ }++ if (boxType === 'tkhd') {+ return {+ box: parseTkhd(sub, fileOffset),+ next,+ size: boxSize,+ };+ }++ const children =+ boxType === 'moov' ||+ boxType === 'trak' ||+ boxType === 'mdia' ||+ boxType === 'minf' ||+ boxType === 'stbl' ||+ boxType === 'stsb'+ ? parseBoxes(sub.subarray(8), fileOffset)+ : [];++ return {+ box: {+ type: 'regular-box',+ boxType,+ boxSize,+ children,+ offset: fileOffset,+ },+ next,+ size: boxSize,+ };+};++export const parseBoxes = (data: Buffer, fileOffset: number): Box[] => {+ const boxes: Box[] = [];+ let remaining = data;+ let bytesConsumed = fileOffset;++ while (remaining.length > 0) {+ const {next, box, size} = processBoxAndSubtract({+ data: remaining,+ fileOffset: bytesConsumed,+ });++ remaining = next;+ boxes.push(box);+ bytesConsumed = box.offset + size;+ }++ return boxes;+};diff --git a/packages/video-parser/src/boxes/iso-base-media/tkhd.ts b/packages/video-parser/src/boxes/iso-base-media/tkhd.tsnew file mode 100644index 0000000000..71e748c0c5--- /dev/null+++ b/packages/video-parser/src/boxes/iso-base-media/tkhd.ts@@ -0,0 +1,114 @@+import type {BaseBox} from './base-type';+import type {ThreeDMatrix} from './mvhd';++export interface TkhdBox extends BaseBox {+ type: 'tkhd-box';+ alternateGroup: number;+ creationTime: number | null;+ duration: number;+ modificationTime: number | null;+ trackId: number;+ version: number;+ layer: number;+ volume: number;+ matrix: ThreeDMatrix;+ width: number;+ height: number;+}++export const parseTkhd = (data: Buffer, fileOffset: number): TkhdBox => {+ let chunkOffset = 0;++ const size = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;+ const atom = data.subarray(chunkOffset, chunkOffset + 4);+ if (atom.toString() !== 'tkhd') {+ throw new Error(`Expected tkhd atom, got ${atom.toString('utf-8')}`);+ }++ chunkOffset += 4;+ const version = data.readUInt8(chunkOffset);+ if (version !== 0) {+ throw new Error(`Unsupported TKHD version ${version}`);+ }++ chunkOffset += 1;++ if (size !== 92) {+ throw new Error(`Expected tkhd size of version 0 to be 92, got ${size}`);+ }++ // Flags, we discard them+ data.subarray(chunkOffset, chunkOffset + 3);+ chunkOffset += 3;++ const creationTime = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;++ const modificationTime = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;++ const trackId = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;++ // reserved+ chunkOffset += 4;++ const duration = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;++ // reserved 2+ chunkOffset += 4;++ // reserved 3+ chunkOffset += 4;++ const layer = data.readUInt16BE(chunkOffset);+ chunkOffset += 2;++ const alternateGroup = data.readUInt16BE(chunkOffset);+ chunkOffset += 2;++ const volume = data.readUInt16BE(chunkOffset);+ chunkOffset += 2;++ // reserved 4+ chunkOffset += 2;++ const matrix = [+ data.readUInt32BE(chunkOffset),+ data.readUInt32BE(chunkOffset + 4),+ data.readUInt32BE(chunkOffset + 8),+ data.readUInt32BE(chunkOffset + 12),+ data.readUInt32BE(chunkOffset + 16),+ data.readUInt32BE(chunkOffset + 20),+ data.readUInt32BE(chunkOffset + 24),+ data.readUInt32BE(chunkOffset + 28),+ data.readUInt32BE(chunkOffset + 32),+ ];++ chunkOffset += 36;++ const width = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;++ const height = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;++ return {+ offset: fileOffset,+ boxSize: data.length,+ type: 'tkhd-box',+ creationTime,+ modificationTime,+ trackId,+ duration,+ layer,+ alternateGroup,+ volume,+ matrix: matrix as ThreeDMatrix,+ width,+ height,+ version,+ };+};diff --git a/packages/video-parser/src/get-duration.ts b/packages/video-parser/src/get-duration.tsindex af283f547e..98b069573b 100644--- a/packages/video-parser/src/get-duration.ts+++ b/packages/video-parser/src/get-duration.ts@@ -2,9 +2,9 @@ import type {Box} from './parse-video';export const getDuration = (boxes: Box[]): number | null => {const moovBox = boxes.find(- (b) => b.boxType === 'moov' && b.type === 'regular-box',+ (b) => b.type === 'regular-box' && b.boxType === 'moov',);- if (moovBox === undefined || moovBox.type !== 'regular-box') {+ if (!moovBox || moovBox.type !== 'regular-box') {return null;}@@ -13,7 +13,7 @@ export const getDuration = (boxes: Box[]): number | null => {return null;}- const mvhdBox = children.find((b) => b.boxType === 'mvhd');+ const mvhdBox = children.find((b) => b.type === 'mvhd-box');if (!mvhdBox) {return null;diff --git a/packages/video-parser/src/parse-video.ts b/packages/video-parser/src/parse-video.tsindex 59a85e5993..6762af97d1 100644--- a/packages/video-parser/src/parse-video.ts+++ b/packages/video-parser/src/parse-video.ts@@ -1,9 +1,9 @@import {createReadStream} from 'fs';import type {BaseBox} from './boxes/iso-base-media/base-type';import type {FtypBox} from './boxes/iso-base-media/ftype';-import {fourByteToNumber, parseFtyp} from './boxes/iso-base-media/ftype';import type {MvhdBox} from './boxes/iso-base-media/mvhd';-import {parseMvhd} from './boxes/iso-base-media/mvhd';+import {parseBoxes} from './boxes/iso-base-media/process-box';+import type {TkhdBox} from './boxes/iso-base-media/tkhd';interface RegularBox extends BaseBox {boxType: string;@@ -13,87 +13,10 @@ interface RegularBox extends BaseBox {type: 'regular-box';}-export type Box = RegularBox | FtypBox | MvhdBox;--const processBoxAndSubtract = ({- data,- fileOffset,-}: {- data: Buffer;- fileOffset: number;-}): BoxAndNext => {- const boxSize = fourByteToNumber(data, 0);- const boxType = data.subarray(4, 8).toString('utf-8');-- const sub = data.subarray(0, boxSize);- const next = data.subarray(boxSize);-- if (boxType === 'ftyp') {- return {- box: parseFtyp(sub, fileOffset),- next,- size: boxSize,- };- }-- if (boxType === 'mvhd') {- return {- box: parseMvhd(sub, fileOffset),- next,- size: boxSize,- };- }-- const children =- boxType === 'moov' ||- boxType === 'trak' ||- boxType === 'mdia' ||- boxType === 'minf' ||- boxType === 'stbl' ||- boxType === 'stsb'- ? parseBoxes(sub.subarray(8), fileOffset)- : [];-- return {- box: {- type: 'regular-box',- boxType,- boxSize,- children,- offset: fileOffset,- },- next,- size: boxSize,- };-};--type BoxAndNext = {- box: Box;- next: Buffer;- size: number;-};+export type Box = RegularBox | FtypBox | MvhdBox | TkhdBox;const isoBaseMediaMp4Pattern = Buffer.from('ftyp');-const parseBoxes = (data: Buffer, fileOffset: number): Box[] => {- const boxes: Box[] = [];- let remaining = data;- let bytesConsumed = fileOffset;-- while (remaining.length > 0) {- const {next, box, size} = processBoxAndSubtract({- data: remaining,- fileOffset: bytesConsumed,- });-- remaining = next;- boxes.push(box);- bytesConsumed = box.offset + size;- }-- return boxes;-};-const matchesPattern = (pattern: Buffer) => {return (data: Buffer) => {return pattern.every((value, index) => data[index] === value);diff --git a/packages/video-parser/src/test/mvhd.test.ts b/packages/video-parser/src/test/mvhd.test.tsindex e9ca9b2709..7c12ffab2f 100644--- a/packages/video-parser/src/test/mvhd.test.ts+++ b/packages/video-parser/src/test/mvhd.test.ts@@ -9,11 +9,11 @@ test('Should be able to parse a MVHD box correctly', () => {109, 118, 104, 100,// version, 8 unsigned bit0,- // flags+ // flags, 24 bit0, 0, 0,- // creation time+ // creation time, 32 bit0, 0, 0, 0,- // modification time+ // modification time, 32 bit0, 0, 0, 0,// time scale0, 0, 3, 232,@@ -77,6 +77,5 @@ test('Should be able to parse a MVHD box correctly', () => {type: 'mvhd-box',boxSize: 108,offset: 0,- boxType: 'mvhd',});});diff --git a/packages/video-parser/src/test/parse-video.test.ts b/packages/video-parser/src/test/parse-video.test.tsindex 483fff8cd6..ef4ee4e80e 100644--- a/packages/video-parser/src/test/parse-video.test.ts+++ b/packages/video-parser/src/test/parse-video.test.ts@@ -10,7 +10,6 @@ test('Parse Big Buck bunny', async () => {{offset: 0,boxSize: 32,- boxType: 'ftyp',type: 'ftyp-box',majorBrand: 'isom',minorVersion: 512,@@ -38,7 +37,6 @@ test('Parse an iPhone video', async () => {expect(data).toEqual([{boxSize: 20,- boxType: 'ftyp',type: 'ftyp-box',majorBrand: 'qt',minorVersion: 0,@@ -71,7 +69,6 @@ test('Parse framer', async () => {{offset: 0,boxSize: 32,- boxType: 'ftyp',compatibleBrands: ['isom', 'iso2', 'avc1', 'mp41'],majorBrand: 'isom',minorVersion: 512,@@ -107,7 +104,6 @@ test('Parse a full video', async () => {expect(first).toEqual({offset: 0,boxSize: 32,- boxType: 'ftyp',type: 'ftyp-box',majorBrand: 'isom',minorVersion: 512,diff --git a/packages/video-parser/src/test/tkhd.test.ts b/packages/video-parser/src/test/tkhd.test.tsnew file mode 100644index 0000000000..c44162c0fc--- /dev/null+++ b/packages/video-parser/src/test/tkhd.test.ts@@ -0,0 +1,77 @@+import {expect, test} from 'bun:test';+import {parseTkhd} from '../boxes/iso-base-media/tkhd';++test('Should be able to parse a TKHD box correctly', () => {+ const buffer = Buffer.from([+ // size, 32 bit+ 0, 0, 0, 92,+ // mvhd atom, 32 bit+ 116, 107, 104, 100,+ // version, 8 unsigned bit+ 0,+ // flags, 24 bit+ 0, 0, 15,+ // creation time, 64 bit+ 226, 94, 106, 178,+ // modification time, 64 bit+ 226, 94, 106, 182,+ // track id, 32 bit+ 0, 0, 0, 5,+ // reserved, 32 bit+ 0, 0, 0, 0,+ // duration, 32 bit+ 0, 0, 7, 248,+ // reserved, 32 bit+ 0, 0, 0, 0,+ // reserved, 32 bit+ 0, 0, 0, 0,+ // layer, 16 bit+ 0, 0,+ // alternate group, 16 bit+ 0, 0,+ // volume, 16 bit+ 0, 0,+ // reserved, 16 bit+ 0, 0,+ // matrix[0], 32 bit+ 0, 1, 0, 0,+ // matrix[1], 32 bit+ 0, 0, 0, 0,+ // matrix[2], 32 bit+ 0, 0, 0, 0,+ // matrix[3], 32 bit+ 0, 0, 0, 0,+ // matrix[4], 32 bit+ 0, 1, 0, 0,+ // matrix[5], 32 bit+ 0, 0, 0, 0,+ // matrix[6], 32 bit+ 0, 0, 0, 0,+ // matrix[7], 32 bit+ 0, 0, 0, 0,+ // matrix[8], 32 bit+ 64, 0, 0, 0,+ // width, 32 bit+ 0, 0, 0, 0,+ // height, 32 bit+ 0, 0, 0, 0,+ ]);++ const mvhd = parseTkhd(buffer, 0);+ expect(mvhd).toEqual({+ type: 'tkhd-box',+ boxSize: 92,+ offset: 0,+ alternateGroup: 0,+ creationTime: 3797838514,+ duration: 2040,+ modificationTime: 3797838518,+ trackId: 5,+ version: 0,+ layer: 0,+ volume: 0,+ matrix: [65536, 0, 0, 0, 65536, 0, 0, 0, 1073741824],+ width: 0,+ height: 0,+ });+});