🎥 Make videos programmatically with React
make it parse
Jonny Burger5f8105a23725
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.tsindex 293acd3f7f..c553dad15b 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@@ -91,13 +91,15 @@ const processBoxAndSubtract = ({};}+ const childArray = sub.subarray(8, boxSize);+const children =boxType === 'mdia' ||boxType === 'minf' ||boxType === 'stbl' ||boxType === 'dims' ||boxType === 'stsb'- ? parseBoxes(sub.subarray(8), fileOffset)+ ? parseBoxes(childArray, fileOffset): [];return {diff --git a/packages/video-parser/src/boxes/iso-base-media/stsd/keys.ts b/packages/video-parser/src/boxes/iso-base-media/stsd/keys.tsindex 8d9693db2b..9ae6d27c57 100644--- a/packages/video-parser/src/boxes/iso-base-media/stsd/keys.ts+++ b/packages/video-parser/src/boxes/iso-base-media/stsd/keys.ts@@ -19,16 +19,6 @@ export const parseKeys = (data: Buffer, offset: number): KeysBox => {throw new Error(`Expected keys type of keys, got ${type}`);}- const rest = data.subarray(chunkOffset);- const restSize = rest.readInt32BE(0);- chunkOffset += 4;- const restType = rest- .subarray(chunkOffset, chunkOffset + 4)- .toString('utf-8');- console.log(restType);-- console.log(restSize);-return {type: 'keys-box',boxSize: data.length,diff --git a/packages/video-parser/src/boxes/iso-base-media/stsd/samples.ts b/packages/video-parser/src/boxes/iso-base-media/stsd/samples.tsnew file mode 100644index 0000000000..e642811f00--- /dev/null+++ b/packages/video-parser/src/boxes/iso-base-media/stsd/samples.ts@@ -0,0 +1,290 @@+type SampleBase = {+ format: string;+ offset: number;+ dataReferenceIndex: number;+ version: number;+ revisionLevel: number;+ vendor: number[];+ size: number;+};++type AudioSample = SampleBase & {+ type: 'audio';+ numberOfChannels: number;+ sampleSize: number;+ compressionId: number;+ packetSize: number;+ sampleRate: number;+ samplesPerPacket: number;+ bytesPerPacket: number;+ bytesPerFrame: number;+ bitsPerSample: number;+};++type VideoSample = SampleBase & {+ type: 'video';+ temporalQuality: number;+ spacialQuality: number;+ width: number;+ height: number;+ compressorName: number[];+ horizontalResolutionPpi: number;+ verticalResolutionPpi: number;+ dataSize: number;+ frameCountPerSample: number;+ depth: number;+ colorTableId: number;+};++export type Sample = AudioSample | VideoSample;++type SampleAndNext = {+ sample: Sample;+ next: Buffer;+ size: number;+};++// https://developer.apple.com/documentation/quicktime-file-format/video_sample_description+const videoTags = [+ 'cvid',+ 'jpeg',+ 'smc ',+ 'rle ',+ 'rpza',+ 'kpcd',+ 'png ',+ 'mjpa',+ 'mjpb',+ 'SVQ1',+ 'SVQ3',+ 'mp4v',+ 'avc1',+ 'dvc ',+ 'dvcp',+ 'gif ',+ 'h263',+ 'tiff',+ 'raw ',+ '2vuY',+ 'yuv2',+ 'v308',+ 'v408',+ 'v216',+ 'v410',+ 'v210',+];++// https://developer.apple.com/documentation/quicktime-file-format/sound_sample_descriptions+const audioTags = [+ 0x00000000,+ 'NONE',+ 'raw ',+ 'twos',+ 'sowt',+ 'MAC3 ',+ 'MAC6 ',+ 'ima4',+ 'fl32',+ 'fl64',+ 'in24',+ 'in32',+ 'ulaw',+ 'alaw',+ 0x6d730002,+ 0x6d730011,+ 'dvca',+ 'QDMC',+ 'QDM2',+ 'Qclp',+ 0x6d730055,+ '.mp3',+ 'mp4a',+ 'ac-3',+];++export const processSampleAndSubtract = ({+ data,+ fileOffset,+}: {+ data: Buffer;+ fileOffset: number;+}): SampleAndNext => {+ let chunkOffset = 0;++ const boxSize = data.readUint32BE(chunkOffset);+ chunkOffset += 4;+ if (boxSize === 0) {+ throw new Error(`Expected box size of ${data.length}, got ${boxSize}`);+ }++ const boxTypeBuffer = data.subarray(chunkOffset, chunkOffset + 4);+ chunkOffset += 4;+ const boxType = boxTypeBuffer.toString('utf-8');+ const isVideo = videoTags.includes(boxType);+ const isAudio =+ audioTags.includes(boxType) || audioTags.includes(Number(boxTypeBuffer));++ if (!isVideo && !isAudio) {+ throw new Error(`Unknown sample format ${boxType}`);+ }++ // 6 reserved bytes+ chunkOffset += 6;++ const dataReferenceIndex = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const version = data.readInt16BE(chunkOffset);+ chunkOffset += 2;++ const revisionLevel = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const vendor = data.subarray(chunkOffset, chunkOffset + 4);+ chunkOffset += 4;++ if (isAudio) {+ if (version !== 1) {+ throw new Error(`Unsupported version ${version}`);+ }++ const numberOfChannels = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const sampleSize = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const compressionId = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const packetSize = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const sampleRate = data.readUint32BE(chunkOffset) / 2 ** 16;+ chunkOffset += 4;++ const samplesPerPacket = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const bytesPerPacket = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const bytesPerFrame = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const bitsPerSample = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const next = data.subarray(boxSize);++ return {+ sample: {+ format: boxType,+ offset: fileOffset,+ dataReferenceIndex,+ version,+ revisionLevel,+ vendor: [...vendor],+ size: boxSize,+ type: 'audio',+ numberOfChannels,+ sampleSize,+ compressionId,+ packetSize,+ sampleRate,+ samplesPerPacket,+ bytesPerPacket,+ bytesPerFrame,+ bitsPerSample,+ },+ next,+ size: boxSize,+ };+ }++ if (isVideo) {+ const temporalQuality = data.readUint32BE(chunkOffset);+ chunkOffset += 4;++ const spacialQuality = data.readUint32BE(chunkOffset);+ chunkOffset += 4;++ const width = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const height = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const horizontalResolution = data.readUint32BE(chunkOffset) / 2 ** 16;+ chunkOffset += 4;++ const verticalResolution = data.readUint32BE(chunkOffset) / 2 ** 16;+ chunkOffset += 4;++ const dataSize = data.readUint32BE(chunkOffset);+ chunkOffset += 4;++ const frameCountPerSample = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const pascalString = data.subarray(chunkOffset, chunkOffset + 32);+ chunkOffset += 32;++ const depth = data.readUint16BE(chunkOffset);+ chunkOffset += 2;++ const colorTableId = data.readInt16BE(chunkOffset);+ chunkOffset += 2;++ const next = data.subarray(boxSize);++ chunkOffset += 4;++ return {+ sample: {+ format: boxType,+ offset: fileOffset,+ dataReferenceIndex,+ version,+ revisionLevel,+ vendor: [...vendor],+ size: boxSize,+ type: 'video',+ width,+ height,+ horizontalResolutionPpi: horizontalResolution,+ verticalResolutionPpi: verticalResolution,+ spacialQuality,+ temporalQuality,+ dataSize,+ frameCountPerSample,+ compressorName: [...pascalString],+ depth,+ colorTableId,+ },+ next,+ size: boxSize,+ };+ }++ throw new Error(`Unknown sample format ${boxType}`);+};++export const parseSamples = (data: Buffer, fileOffset: number): Sample[] => {+ const samples: Sample[] = [];+ let remaining = data;+ let bytesConsumed = fileOffset;++ while (remaining.length > 0) {+ const {next, sample, size} = processSampleAndSubtract({+ data: remaining,+ fileOffset: bytesConsumed,+ });++ remaining = next;+ samples.push(sample);+ bytesConsumed = sample.offset + size;+ }++ return samples;+};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.tsindex 9daca17993..e98d1492f3 100644--- a/packages/video-parser/src/boxes/iso-base-media/stsd/stsd.ts+++ b/packages/video-parser/src/boxes/iso-base-media/stsd/stsd.ts@@ -1,11 +1,11 @@-import type {Box} from '../../../parse-video';import type {BaseBox} from '../base-type';-import {parseBoxes} from '../process-box';+import type {Sample} from './samples';+import {parseSamples} from './samples';export interface StsdBox extends BaseBox {type: 'stsd-box';numberOfEntries: number;- children: Box[];+ samples: Sample[];}export const parseStsd = (data: Buffer, offset: number): StsdBox => {@@ -38,7 +38,7 @@ export const parseStsd = (data: Buffer, offset: number): StsdBox => {const numberOfEntries = data.readUInt32BE(chunkOffset);chunkOffset += 4;- const boxes = parseBoxes(data.subarray(chunkOffset), offset + chunkOffset);+ const boxes = parseSamples(data.subarray(chunkOffset), offset + chunkOffset);if (boxes.length !== numberOfEntries) {throw new Error(@@ -51,6 +51,6 @@ export const parseStsd = (data: Buffer, offset: number): StsdBox => {boxSize: data.length,offset,numberOfEntries,- children: boxes,+ samples: boxes,};};diff --git a/packages/video-parser/src/test/duration.test.ts b/packages/video-parser/src/test/duration.test.tsindex 866327bdc9..6b95f78dc5 100644--- a/packages/video-parser/src/test/duration.test.ts+++ b/packages/video-parser/src/test/duration.test.ts@@ -13,7 +13,7 @@ test('Should get duration of video', async () => {expect(duration).toBe(4.167);});-test.only('Should get duration of HEVC video', async () => {+test('Should get duration of HEVC video', async () => {const parsed = await parseVideo(exampleVideos.iphonehevc, Infinity);if (!parsed) {throw new Error('No parsed data');diff --git a/packages/video-parser/src/test/stsd.test.ts b/packages/video-parser/src/test/stsd.test.tsindex 193f0116f0..393c3ba69e 100644--- a/packages/video-parser/src/test/stsd.test.ts+++ b/packages/video-parser/src/test/stsd.test.ts@@ -1,68 +1,152 @@import {expect, test} from 'bun:test';+import {processSampleAndSubtract} from '../boxes/iso-base-media/stsd/samples';import {parseStsd} from '../boxes/iso-base-media/stsd/stsd';-test('Should be able to parse a STSD box correctly', () => {+test('Should be able to parse a STSD audio 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,+ // box size+ 0, 0, 0, 159,+ // data format "stsd"+ 115, 116, 115, 100,+ // reserved+ 0, 0, 0, 0, 0, 0,+ // number of entries+ 0, 1,+ // size+ 0, 0, 0, 143,+ // "mp4a"+ 109, 112, 52, 97,+ // reserved+ 0, 0, 0, 0, 0, 0,+ // data reference index+ 0, 1,+ // version+ 0, 1,+ // revisioon level+ 0, 0, 0, 0, 0, 0,+ // revision level+ 0, 2, 0, 16, 255, 254, 0, 0, 172, 68, 0, 0, 0, 0, 4, 0, 0, 0, 0, 1, 0, 0, 0,+ 2, 0, 0, 0, 2, 0, 0, 0, 91, 119, 97, 118, 101, 0, 0, 0, 12, 102, 114, 109,+ 97, 109, 112, 52, 97, 0, 0, 0, 12, 109, 112, 52, 97, 0, 0, 0, 0, 0, 0, 0,+ 51, 101, 115, 100, 115, 0, 0, 0, 0, 3, 128, 128, 128, 34, 0, 0, 0, 4, 128,+ 128, 128, 20, 64, 20, 0, 24, 0, 0, 2, 238, 0, 0, 0, 0, 0, 5, 128, 128, 128,+ 2, 18, 16, 6, 128, 128, 128, 1, 2, 0, 0, 0, 8, 0, 0, 0, 0,]);const parsed = parseStsd(buffer, 0);expect(parsed).toEqual({offset: 0,- boxSize: 604,+ boxSize: 159,type: 'stsd-box',numberOfEntries: 1,- descriptions: [+ samples: [{- boxSize: 588,- dataReferenceIndex: 1,- format: 'mebx',+ size: 143,offset: 16,- type: 'mebx-box',- children: [- {- boxSize: 572,- boxType: 'keys',- children: [],- offset: 32,- type: 'regular-box',- },- ],+ format: 'mp4a',+ dataReferenceIndex: 1,+ version: 1,+ revisionLevel: 0,+ vendor: [0, 0, 0, 0],+ numberOfChannels: 2,+ type: 'audio',+ sampleSize: 16,+ compressionId: 65534,+ packetSize: 0,+ samplesPerPacket: 0,+ bitsPerSample: 1,+ bytesPerFrame: 0,+ bytesPerPacket: 1024,+ sampleRate: 44100,},],});});++test('Should be able to parse a STSD video box correctly', () => {+ const buffer = Buffer.from([+ // box size+ 0, 0, 0, 158,+ // data format "avc1"+ 97, 118, 99, 49,+ // padding+ 0, 0, 0, 0, 0, 0,+ // data reference index+ 0, 1,+ // version+ 0, 0,+ // revision level+ 0, 0,+ // vendor+ 0, 0, 0, 0,+ // temporal quality+ 0, 0, 0, 0,+ // spatial quality+ 0, 0, 0, 0,+ // width+ 4, 56,+ // height+ 4, 56,+ // horiz resolution+ 0, 72, 0, 0,+ // vert resolution+ 0, 72, 0, 0,+ // frame count+ 0, 0,+ // compressor name+ // 0+ 0, 0, 0, 1,+ // 1+ 0, 0, 0, 0,+ // 2+ 0, 0, 0, 0,+ // 3+ 0, 0, 0, 0,+ // 4+ 0, 0, 0, 0,+ // 5+ 0, 0, 0, 0,+ // 6+ 0, 0, 0, 0,+ // 7+ 0, 0, 0, 0,+ // 8+ 0, 0, 0, 0,+ // depth+ 0, 24,+ // color space+ 255, 255,+ // avcC info (not parsed)+ 0, 0, 0, 56, 97, 118, 99, 67, 1, 100, 0, 32, 255, 225, 0, 27, 103, 100, 0,+ 32, 172, 217, 64, 68, 2, 39, 150, 92, 4, 64, 0, 0, 3, 0, 64, 0, 0, 12, 3,+ 198, 12, 101, 128, 1, 0, 6, 104, 235, 224, 140, 178, 44, 253, 248, 248, 0,+ 0, 0, 0, 16, 112, 97, 115, 112, 0, 0, 0, 1, 0, 0, 0, 1,+ ]);++ const parsed = processSampleAndSubtract({data: buffer, fileOffset: 0});+ expect(parsed.sample).toEqual({+ size: 158,+ format: 'avc1',+ dataReferenceIndex: 1,+ version: 0,+ revisionLevel: 0,+ vendor: [0, 0, 0, 0],+ type: 'video',+ temporalQuality: 0,+ spacialQuality: 0,+ width: 1080,+ height: 1080,+ horizontalResolutionPpi: 72,+ verticalResolutionPpi: 72,+ dataSize: 0,+ frameCountPerSample: 1,+ compressorName: [+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,+ 0, 0, 0, 0, 0, 0, 0,+ ],+ offset: 0,+ depth: 24,+ colorTableId: -1,+ });+});