🎥 Make videos programmatically with React
samples
Jonny Burger9bcdf79607cd
diff --git a/packages/media-parser/boxes.json b/packages/media-parser/boxes.jsondeleted file mode 100644index f269042bfc..0000000000--- a/packages/media-parser/boxes.json+++ /dev/null@@ -1 +0,0 @@-[{"type":"ftyp-box","majorBrand":"qt","minorVersion":0,"compatibleBrands":["qt"],"offset":0,"boxSize":20},{"type":"regular-box","boxType":"wide","boxSize":8,"children":[],"offset":20},{"type":"regular-box","boxType":"mdat","children":[],"boxSize":39048800,"offset":28},{"offset":39048828,"boxSize":14100,"type":"moov-box","children":[{"creationTime":1686832959000,"modificationTime":1686832973000,"timeScale":600,"durationInUnits":7541,"durationInSeconds":12.568333333333333,"rate":1,"volume":1,"matrix":[65536,0,0,0,65536,0,0,0,1073741824],"nextTrackId":6,"type":"mvhd-box","boxSize":108,"offset":39048836},{"offset":39048944,"boxSize":7341,"type":"trak-box","children":[{"offset":39048952,"boxSize":92,"type":"tkhd-box","creationTime":1686832959000,"modificationTime":1686832973000,"trackId":1,"duration":7541,"layer":0,"alternateGroup":0,"volume":0,"matrix":[0,65536,0,4294901760,0,0,141557760,0,1073741824],"width":3840,"height":2160,"version":0},{"type":"regular-box","boxType":"tapt","boxSize":68,"children":[],"offset":39049044},{"type":"regular-box","boxType":"edts","boxSize":36,"children":[],"offset":39049112},{"type":"regular-box","boxType":"mdia","boxSize":7137,"children":[{"type":"mdhd-box","duration":7541,"timescale":600,"version":0,"language":21956,"quality":0},{"type":"regular-box","boxType":"hdlr","boxSize":49,"children":[],"offset":39049188},{"type":"regular-box","boxType":"minf","boxSize":7048,"children":[{"type":"regular-box","boxType":"vmhd","boxSize":20,"children":[],"offset":39049245},{"type":"regular-box","boxType":"hdlr","boxSize":56,"children":[],"offset":39049265},{"type":"regular-box","boxType":"dinf","boxSize":36,"children":[],"offset":39049321},{"type":"regular-box","boxType":"stbl","boxSize":6928,"children":[{"type":"stsd-box","boxSize":309,"offset":39049365,"numberOfEntries":1,"samples":[{"format":"hvc1","offset":39049381,"dataReferenceIndex":1,"version":0,"revisionLevel":0,"vendor":[0,0,0,0],"size":293,"type":"video","width":3840,"height":2160,"horizontalResolutionPpi":72,"verticalResolutionPpi":72,"spacialQuality":512,"temporalQuality":512,"dataSize":0,"frameCountPerSample":1,"compressorName":[4,72,69,86,67,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],"depth":24,"colorTableId":-1}]},{"type":"regular-box","boxType":"sgpd","boxSize":104,"children":[],"offset":39049674},{"type":"regular-box","boxType":"sgpd","boxSize":26,"children":[],"offset":39049778},{"type":"regular-box","boxType":"sgpd","boxSize":28,"children":[],"offset":39049804},{"type":"regular-box","boxType":"sbgp","boxSize":212,"children":[],"offset":39049832},{"type":"regular-box","boxType":"csgm","boxSize":71,"children":[],"offset":39050044},{"type":"regular-box","boxType":"csgm","boxSize":249,"children":[],"offset":39050115},{"type":"stts-box","sampleDistribution":[{"sampleCount":269,"sampleDelta":20},{"sampleCount":1,"sampleDelta":21},{"sampleCount":107,"sampleDelta":20}]},{"type":"regular-box","boxType":"ctts","boxSize":3032,"children":[],"offset":39050404},{"type":"regular-box","boxType":"cslg","boxSize":32,"children":[],"offset":39053436},{"type":"regular-box","boxType":"stss","boxSize":64,"children":[],"offset":39053468},{"type":"regular-box","boxType":"sdtp","boxSize":389,"children":[],"offset":39053532},{"type":"regular-box","boxType":"stsc","boxSize":616,"children":[],"offset":39053921},{"type":"regular-box","boxType":"stsz","boxSize":1528,"children":[],"offset":39054537},{"type":"regular-box","boxType":"stco","boxSize":220,"children":[],"offset":39056065}],"offset":39049357}],"offset":39049237}],"offset":39049148}]},{"offset":39056285,"boxSize":3190,"type":"trak-box","children":[{"offset":39056293,"boxSize":92,"type":"tkhd-box","creationTime":1686832959000,"modificationTime":1686832973000,"trackId":2,"duration":7540,"layer":0,"alternateGroup":0,"volume":256,"matrix":[65536,0,0,0,65536,0,0,0,1073741824],"width":0,"height":0,"version":0},{"type":"regular-box","boxType":"edts","boxSize":36,"children":[],"offset":39056385},{"type":"regular-box","boxType":"mdia","boxSize":3054,"children":[{"type":"mdhd-box","duration":557056,"timescale":44100,"version":0,"language":21956,"quality":0},{"type":"regular-box","boxType":"hdlr","boxSize":49,"children":[],"offset":39056461},{"type":"regular-box","boxType":"minf","boxSize":2965,"children":[{"type":"regular-box","boxType":"smhd","boxSize":16,"children":[],"offset":39056518},{"type":"regular-box","boxType":"hdlr","boxSize":56,"children":[],"offset":39056534},{"type":"regular-box","boxType":"dinf","boxSize":36,"children":[],"offset":39056590},{"type":"regular-box","boxType":"stbl","boxSize":2849,"children":[{"type":"stsd-box","boxSize":159,"offset":39056634,"numberOfEntries":1,"samples":[{"format":"mp4a","offset":39056650,"dataReferenceIndex":1,"version":1,"revisionLevel":0,"vendor":[0,0,0,0],"size":143,"type":"audio","numberOfChannels":2,"sampleSize":16,"compressionId":-2,"packetSize":0,"sampleRate":44100,"samplesPerPacket":1024,"bytesPerPacket":1,"bytesPerFrame":2,"bitsPerSample":2,"children":[{"type":"regular-box","boxType":"wave","boxSize":91,"children":[{"type":"regular-box","boxType":"frma","boxSize":12,"children":[],"offset":39056710},{"type":"regular-box","boxType":"mp4a","boxSize":12,"children":[],"offset":39056722},{"type":"esds-box","version":0,"tag":3,"sizeOfInstance":34,"esId":0,"descriptors":[{"type":"decoder-config-descriptor","objectTypeIndication":"aac"},{"type":"sl-config-descriptor"}]},{"type":"regular-box","boxType":"\u0000\u0000\u0000\u0000","boxSize":8,"children":[],"offset":39056785}],"offset":39056702}]}]},{"type":"regular-box","boxType":"sgpd","boxSize":26,"children":[],"offset":39056793},{"type":"regular-box","boxType":"sbgp","boxSize":28,"children":[],"offset":39056819},{"type":"stts-box","sampleDistribution":[{"sampleCount":544,"sampleDelta":1024}]},{"type":"regular-box","boxType":"stsc","boxSize":292,"children":[],"offset":39056871},{"type":"regular-box","boxType":"stsz","boxSize":2196,"children":[],"offset":39057163},{"type":"regular-box","boxType":"stco","boxSize":116,"children":[],"offset":39059359}],"offset":39056626}],"offset":39056510}],"offset":39056421}]},{"offset":39059475,"boxSize":629,"type":"trak-box","children":[{"offset":39059483,"boxSize":92,"type":"tkhd-box","creationTime":1686832959000,"modificationTime":1686832973000,"trackId":3,"duration":7541,"layer":0,"alternateGroup":0,"volume":0,"matrix":[65536,0,0,0,65536,0,0,0,1073741824],"width":0,"height":0,"version":0},{"type":"regular-box","boxType":"edts","boxSize":36,"children":[],"offset":39059575},{"type":"regular-box","boxType":"tref","boxSize":32,"children":[],"offset":39059611},{"type":"regular-box","boxType":"mdia","boxSize":461,"children":[{"type":"mdhd-box","duration":7541,"timescale":600,"version":0,"language":21956,"quality":0},{"type":"regular-box","boxType":"hdlr","boxSize":52,"children":[],"offset":39059683},{"type":"regular-box","boxType":"minf","boxSize":369,"children":[{"type":"regular-box","boxType":"gmhd","boxSize":32,"children":[],"offset":39059743},{"type":"regular-box","boxType":"hdlr","boxSize":56,"children":[],"offset":39059775},{"type":"regular-box","boxType":"dinf","boxSize":36,"children":[],"offset":39059831},{"type":"regular-box","boxType":"stbl","boxSize":237,"children":[{"type":"stsd-box","boxSize":133,"offset":39059875,"numberOfEntries":1,"samples":[{"type":"unknown","offset":39059891,"dataReferenceIndex":1,"version":0,"revisionLevel":101,"vendor":[107,101,121,115],"size":117,"format":"mebx"}]},{"type":"stts-box","sampleDistribution":[{"sampleCount":1,"sampleDelta":7541}]},{"type":"regular-box","boxType":"stsc","boxSize":28,"children":[],"offset":39060032},{"type":"regular-box","boxType":"stsz","boxSize":24,"children":[],"offset":39060060},{"type":"regular-box","boxType":"stco","boxSize":20,"children":[],"offset":39060084}],"offset":39059867}],"offset":39059735}],"offset":39059643}]},{"offset":39060104,"boxSize":1074,"type":"trak-box","children":[{"offset":39060112,"boxSize":92,"type":"tkhd-box","creationTime":1686832959000,"modificationTime":1686832973000,"trackId":4,"duration":7541,"layer":0,"alternateGroup":0,"volume":0,"matrix":[65536,0,0,0,65536,0,0,0,1073741824],"width":0,"height":0,"version":0},{"type":"regular-box","boxType":"edts","boxSize":36,"children":[],"offset":39060204},{"type":"regular-box","boxType":"tref","boxSize":32,"children":[],"offset":39060240},{"type":"regular-box","boxType":"mdia","boxSize":906,"children":[{"type":"mdhd-box","duration":7541,"timescale":600,"version":0,"language":21956,"quality":0},{"type":"regular-box","boxType":"hdlr","boxSize":52,"children":[],"offset":39060312},{"type":"regular-box","boxType":"minf","boxSize":814,"children":[{"type":"regular-box","boxType":"gmhd","boxSize":32,"children":[],"offset":39060372},{"type":"regular-box","boxType":"hdlr","boxSize":56,"children":[],"offset":39060404},{"type":"regular-box","boxType":"dinf","boxSize":36,"children":[],"offset":39060460},{"type":"regular-box","boxType":"stbl","boxSize":682,"children":[{"type":"stsd-box","boxSize":578,"offset":39060504,"numberOfEntries":1,"samples":[{"type":"unknown","offset":39060520,"dataReferenceIndex":1,"version":0,"revisionLevel":502,"vendor":[107,101,121,115],"size":562,"format":"mebx"}]},{"type":"stts-box","sampleDistribution":[{"sampleCount":1,"sampleDelta":7541}]},{"type":"regular-box","boxType":"stsc","boxSize":28,"children":[],"offset":39061106},{"type":"regular-box","boxType":"stsz","boxSize":24,"children":[],"offset":39061134},{"type":"regular-box","boxType":"stco","boxSize":20,"children":[],"offset":39061158}],"offset":39060496}],"offset":39060364}],"offset":39060272}]},{"offset":39061178,"boxSize":1218,"type":"trak-box","children":[{"offset":39061186,"boxSize":92,"type":"tkhd-box","creationTime":1686832959000,"modificationTime":1686832973000,"trackId":5,"duration":7541,"layer":0,"alternateGroup":0,"volume":0,"matrix":[65536,0,0,0,65536,0,0,0,1073741824],"width":0,"height":0,"version":0},{"type":"regular-box","boxType":"edts","boxSize":36,"children":[],"offset":39061278},{"type":"regular-box","boxType":"tref","boxSize":32,"children":[],"offset":39061314},{"type":"regular-box","boxType":"mdia","boxSize":1050,"children":[{"type":"mdhd-box","duration":7541,"timescale":600,"version":0,"language":21956,"quality":0},{"type":"regular-box","boxType":"hdlr","boxSize":52,"children":[],"offset":39061386},{"type":"regular-box","boxType":"minf","boxSize":958,"children":[{"type":"regular-box","boxType":"gmhd","boxSize":32,"children":[],"offset":39061446},{"type":"regular-box","boxType":"hdlr","boxSize":56,"children":[],"offset":39061478},{"type":"regular-box","boxType":"dinf","boxSize":36,"children":[],"offset":39061534},{"type":"regular-box","boxType":"stbl","boxSize":826,"children":[{"type":"stsd-box","boxSize":598,"offset":39061578,"numberOfEntries":1,"samples":[{"type":"unknown","offset":39061594,"dataReferenceIndex":1,"version":0,"revisionLevel":566,"vendor":[107,101,121,115],"size":582,"format":"mebx"}]},{"type":"stts-box","sampleDistribution":[{"sampleCount":269,"sampleDelta":20},{"sampleCount":1,"sampleDelta":21},{"sampleCount":107,"sampleDelta":20}]},{"type":"regular-box","boxType":"stsc","boxSize":40,"children":[],"offset":39062216},{"type":"regular-box","boxType":"stsz","boxSize":20,"children":[],"offset":39062256},{"type":"regular-box","boxType":"stco","boxSize":120,"children":[],"offset":39062276}],"offset":39061570}],"offset":39061438}],"offset":39061346}]},{"type":"regular-box","boxType":"meta","boxSize":532,"children":[],"offset":39062396}]}]\ No newline at end of filediff --git a/packages/media-parser/src/boxes/iso-base-media/stsd/stsz.ts b/packages/media-parser/src/boxes/iso-base-media/stsd/stsz.tsindex befff52ff0..55102d554a 100644--- a/packages/media-parser/src/boxes/iso-base-media/stsd/stsz.ts+++ b/packages/media-parser/src/boxes/iso-base-media/stsd/stsz.ts@@ -1,14 +1,22 @@import type {BufferIterator} from '../../../buffer-iterator';import type {BaseBox} from '../base-type';-export interface StszBox extends BaseBox {+type Discriminated =+ | {+ countType: 'fixed';+ sampleSize: number;+ }+ | {+ countType: 'variable';+ entries: number[];+ };++export type StszBox = BaseBox & {type: 'stsz-box';version: number;flags: number[];- sampleSize: number;sampleCount: number;- samples: number[];-}+} & Discriminated;export const parseStsz = ({iterator,@@ -29,6 +37,19 @@ export const parseStsz = ({const sampleSize = iterator.getUint32();const sampleCount = iterator.getUint32();+ if (sampleSize !== 0) {+ return {+ type: 'stsz-box',+ boxSize: size,+ offset,+ version,+ flags: [...flags],+ sampleCount,+ countType: 'fixed',+ sampleSize,+ };+ }+const samples: number[] = [];for (let i = 0; i < sampleCount; i++) {const bytesRemaining = size - (iterator.counter.getOffset() - offset);@@ -47,8 +68,8 @@ export const parseStsz = ({offset,version,flags: [...flags],- sampleSize,sampleCount,- samples,+ countType: 'variable',+ entries: samples,};};diff --git a/packages/media-parser/src/get-sample-positions.ts b/packages/media-parser/src/get-sample-positions.tsindex 0c6804dfe5..b92866da98 100644--- a/packages/media-parser/src/get-sample-positions.ts+++ b/packages/media-parser/src/get-sample-positions.ts@@ -2,6 +2,11 @@ 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 type SamplePosition = {+ offset: number;+ size: number;+};+export const getSamplePositions = ({stcoBox,stszBox,@@ -10,4 +15,35 @@ export const getSamplePositions = ({stcoBox: StcoBox;stszBox: StszBox;stscBox: StscBox;-}) => {};+}) => {+ const chunks = stcoBox.entries;+ const samples: SamplePosition[] = [];++ let samplesPerChunk = 1;++ for (let i = 0; i < chunks.length; i++) {+ const hasEntry = stscBox.entries.find(+ (entry) => entry.firstChunk === i + 1,+ );+ if (hasEntry) {+ samplesPerChunk = hasEntry.samplesPerChunk;+ }++ let offsetInThisChunk = 0;++ for (let j = 0; j < samplesPerChunk; j++) {+ const size =+ stszBox.countType === 'fixed'+ ? stszBox.sampleSize+ : stszBox.entries[samples.length];++ samples.push({+ offset: chunks[i] + offsetInThisChunk,+ size,+ });+ offsetInThisChunk += size;+ }+ }++ return samples;+};diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.tsindex d87d38921a..2b42ddf299 100644--- a/packages/media-parser/src/get-tracks.ts+++ b/packages/media-parser/src/get-tracks.ts@@ -1,6 +1,6 @@-import type {StcoBox} from './boxes/iso-base-media/stsd/stco';-import type {StszBox} from './boxes/iso-base-media/stsd/stsz';import {trakBoxContainsAudio, trakBoxContainsVideo} from './get-fps';+import type {SamplePosition} from './get-sample-positions';+import {getSamplePositions} from './get-sample-positions';import type {AnySegment} from './parse-result';import {getMoovBox,@@ -14,8 +14,7 @@ import {type Track = {type: 'audio' | 'video';- stcoBox: StcoBox | null;- stszBox: StszBox | null;+ samplePositions: SamplePosition[];trackId: number;};@@ -53,11 +52,28 @@ export const getTracks = (segments: AnySegment[]): Track[] => {throw new Error('Expected tkhd box in trak box');}+ if (!stszBox) {+ throw new Error('Expected stsz box in trak box');+ }++ if (!stcoBox) {+ throw new Error('Expected stco box in trak box');+ }++ if (!stscBox) {+ throw new Error('Expected stsc box in trak box');+ }++ const samplePositions = getSamplePositions({+ stcoBox,+ stscBox,+ stszBox,+ });+if (trakBoxContainsAudio(track)) {foundTracks.push({type: 'audio',- stcoBox,- stszBox,+ samplePositions,trackId: tkhdBox.trackId,});}@@ -65,8 +81,7 @@ export const getTracks = (segments: AnySegment[]): Track[] => {if (trakBoxContainsVideo(track)) {foundTracks.push({type: 'video',- stcoBox,- stszBox,+ samplePositions,trackId: tkhdBox.trackId,});}diff --git a/packages/media-parser/src/test/stream-samples.test.ts b/packages/media-parser/src/test/stream-samples.test.tsindex ddb1475327..5aeb7a707e 100644--- a/packages/media-parser/src/test/stream-samples.test.ts+++ b/packages/media-parser/src/test/stream-samples.test.ts@@ -19,48 +19,114 @@ test('Stream samples', async () => {{type: 'video',trackId: 1,- stcoBox: {- type: 'stco-box',- boxSize: 52,- offset: 808,- version: 0,- flags: [0, 0, 0],- entries: [1637, 7868, 10486, 12041, 14509, 16080, 17741, 20196, 21932],- entryCount: 9,- },- stszBox: {- type: 'stsz-box',- boxSize: 60,- offset: 748,- version: 0,- flags: [0, 0, 0],- sampleSize: 0,- sampleCount: 10,- samples: [4834, 437, 698, 595, 548, 611, 701, 535, 776, 586],- },+ samplePositions: [+ {+ offset: 1637,+ size: 4834,+ },+ {+ offset: 6471,+ size: 437,+ },+ {+ offset: 7868,+ size: 698,+ },+ {+ offset: 10486,+ size: 595,+ },+ {+ offset: 12041,+ size: 548,+ },+ {+ offset: 14509,+ size: 611,+ },+ {+ offset: 16080,+ size: 701,+ },+ {+ offset: 17741,+ size: 535,+ },+ {+ offset: 20196,+ size: 776,+ },+ {+ offset: 21932,+ size: 586,+ },+ ],},{type: 'audio',trackId: 2,- stcoBox: {- type: 'stco-box',- boxSize: 52,- offset: 1421,- version: 0,- flags: [0, 0, 0],- entries: [6908, 8566, 11081, 12589, 15120, 16781, 18276, 20972, 22518],- entryCount: 9,- },- stszBox: {- type: 'stsz-box',- boxSize: 20,- offset: 1401,- version: 0,- flags: [0, 0, 0],- sampleSize: 960,- sampleCount: 15,- samples: [],- },+ samplePositions: [+ {+ offset: 6908,+ size: 960,+ },+ {+ offset: 8566,+ size: 960,+ },+ {+ offset: 9526,+ size: 960,+ },+ {+ offset: 11081,+ size: 960,+ },+ {+ offset: 12589,+ size: 960,+ },+ {+ offset: 13549,+ size: 960,+ },+ {+ offset: 15120,+ size: 960,+ },+ {+ offset: 16781,+ size: 960,+ },+ {+ offset: 18276,+ size: 960,+ },+ {+ offset: 19236,+ size: 960,+ },+ {+ offset: 20972,+ size: 960,+ },+ {+ offset: 22518,+ size: 960,+ },+ {+ offset: 23478,+ size: 960,+ },+ {+ offset: 24438,+ size: 960,+ },+ {+ offset: 25398,+ size: 960,+ },+ ],},]);});diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.tsindex b71749770c..c5e423e7ae 100644--- a/packages/media-parser/src/traversal.ts+++ b/packages/media-parser/src/traversal.ts@@ -120,7 +120,13 @@ export const getStszBox = (trakBox: TrakBox): StszBox | null => {};export const getStscBox = (trakBox: TrakBox): StscBox | null => {- const stcoBox = trakBox.children.find(+ const stblBox = getStblBox(trakBox);++ if (!stblBox || stblBox.type !== 'regular-box') {+ return null;+ }++ const stcoBox = stblBox.children.find((b) => b.type === 'stsc-box',) as StscBox | null;