🎥 Make videos programmatically with React
some progress
Jonny Burger30be9dafa12d
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 57c5a40b62..666bf86f32 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@@ -2,6 +2,7 @@ import type {Box} from '../../parse-video';import {parseDims} from './dims';import {fourByteToNumber, parseFtyp} from './ftype';import {parseMvhd} from './mvhd';+import {parseMebx} from './stsd/mebx';import {parseStsd} from './stsd/stsd';import {parseTkhd} from './tkhd';@@ -70,6 +71,14 @@ const processBoxAndSubtract = ({};}+ if (boxType === 'mebx') {+ return {+ box: parseMebx(sub, fileOffset),+ next,+ size: boxSize,+ };+ }+const children =boxType === 'moov' ||boxType === 'trak' ||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.tsnew file mode 100644index 0000000000..8d9693db2b--- /dev/null+++ b/packages/video-parser/src/boxes/iso-base-media/stsd/keys.ts@@ -0,0 +1,37 @@+import type {BaseBox} from '../base-type';++export interface KeysBox extends BaseBox {+ type: 'keys-box';+}++export const parseKeys = (data: Buffer, offset: number): KeysBox => {+ let chunkOffset = 0;++ const size = data.readUInt32BE(chunkOffset);+ chunkOffset += 4;+ if (size !== data.length) {+ throw new Error(`Expected keys size of ${data.length}, got ${size}`);+ }++ const type = data.subarray(chunkOffset, chunkOffset + 4).toString('utf-8');+ chunkOffset += 4;+ if (type !== 'keys') {+ 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,+ offset,+ };+};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.tsindex b44f03352c..16731e11a9 100644--- a/packages/video-parser/src/boxes/iso-base-media/stsd/mebx.ts+++ b/packages/video-parser/src/boxes/iso-base-media/stsd/mebx.ts@@ -1,9 +1,12 @@+import type {Box} from '../../../parse-video';import type {BaseBox} from '../base-type';+import {parseBoxes} from '../process-box';export interface MebxBox extends BaseBox {type: 'mebx-box';dataReferenceIndex: number;format: string;+ children: Box[];}export const parseMebx = (data: Buffer, offset: number): MebxBox => {@@ -26,6 +29,11 @@ export const parseMebx = (data: Buffer, offset: number): MebxBox => {chunkOffset += 6;const dataReferenceIndex = data.readUInt16BE(chunkOffset);+ chunkOffset += 2;++ const rest = data.subarray(chunkOffset);++ const children = parseBoxes(rest, offset + chunkOffset);return {type: 'mebx-box',@@ -33,5 +41,6 @@ export const parseMebx = (data: Buffer, offset: number): MebxBox => {offset,dataReferenceIndex,format: 'mebx',+ children,};};diff --git a/packages/video-parser/src/parse-video.ts b/packages/video-parser/src/parse-video.tsindex 44ae8115ee..5f286cea2f 100644--- a/packages/video-parser/src/parse-video.ts+++ b/packages/video-parser/src/parse-video.ts@@ -4,6 +4,7 @@ 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 {MebxBox} from './boxes/iso-base-media/stsd/mebx';import type {StsdBox} from './boxes/iso-base-media/stsd/stsd';import type {TkhdBox} from './boxes/iso-base-media/tkhd';@@ -21,7 +22,8 @@ export type Box =| MvhdBox| TkhdBox| DimensionsBox- | StsdBox;+ | StsdBox+ | MebxBox;const isoBaseMediaMp4Pattern = Buffer.from('ftyp');diff --git a/packages/video-parser/src/test/duration.test.ts b/packages/video-parser/src/test/duration.test.tsindex 2b3e3d2646..0678b6b6fd 100644--- a/packages/video-parser/src/test/duration.test.ts+++ b/packages/video-parser/src/test/duration.test.ts@@ -21,6 +21,11 @@ test('Should get duration of HEVC video', async () => {throw new Error('No parsed data');}+ const t = parsed[3];+ if (t.type !== 'regular-box') {+ throw new Error('Expected regular box');+ }+const duration = getDuration(parsed);expect(duration).toBe(3.4);});diff --git a/packages/video-parser/src/test/keys.test.ts b/packages/video-parser/src/test/keys.test.tsnew file mode 100644index 0000000000..cebcc6fd29--- /dev/null+++ b/packages/video-parser/src/test/keys.test.ts@@ -0,0 +1,47 @@+import {expect, test} from 'bun:test';+import {parseKeys} from '../boxes/iso-base-media/stsd/keys';++test('Should parse keyd box', () => {+ const buf = Buffer.from([+ 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 data = parseKeys(buf, 0);++ expect(data).toEqual({+ type: 'keys-box',+ boxSize: 572,+ offset: 0,+ });+});diff --git a/packages/video-parser/src/test/stsd.test.ts b/packages/video-parser/src/test/stsd.test.tsindex 537df87bf3..193f0116f0 100644--- a/packages/video-parser/src/test/stsd.test.ts+++ b/packages/video-parser/src/test/stsd.test.ts@@ -53,6 +53,15 @@ test('Should be able to parse a STSD box correctly', () => {format: 'mebx',offset: 16,type: 'mebx-box',+ children: [+ {+ boxSize: 572,+ boxType: 'keys',+ children: [],+ offset: 32,+ type: 'regular-box',+ },+ ],},],});