🎥 Make videos programmatically with React
more codecs
Jonny Burger0b047e3b1f87
diff --git a/packages/media-parser/src/get-audio-codec.ts b/packages/media-parser/src/get-audio-codec.tsindex 732615aed5..0396f50606 100644--- a/packages/media-parser/src/get-audio-codec.ts+++ b/packages/media-parser/src/get-audio-codec.ts@@ -3,7 +3,7 @@ import type {Sample} from './boxes/iso-base-media/stsd/samples';import {trakBoxContainsAudio} from './get-fps';import type {KnownAudioCodecs} from './options';import type {AnySegment} from './parse-result';-import {getMoovBox} from './traversal';+import {getMoovBox, getStsdBox} from './traversal';export const hasAudioCodec = (boxes: AnySegment[]): boolean => {try {@@ -51,60 +51,34 @@ const onSample = (sample: Sample): KnownAudioCodecs | null | undefined => {export const getAudioCodec = (boxes: AnySegment[]): KnownAudioCodecs | null => {const moovBox = getMoovBox(boxes);+if (moovBox) {const trakBox = moovBox.children.find((b) => b.type === 'trak-box' && trakBoxContainsAudio(b),);if (trakBox && trakBox.type === 'trak-box') {- const mdiaBox = trakBox.children.find(- (b) => b.type === 'regular-box' && b.boxType === 'mdia',- );- if (- mdiaBox &&- mdiaBox.type === 'regular-box' &&- mdiaBox.boxType === 'mdia'- ) {- const minfBox = mdiaBox?.children.find(- (b) => b.type === 'regular-box' && b.boxType === 'minf',- );- if (- minfBox &&- minfBox.type === 'regular-box' &&- minfBox.boxType === 'minf'- ) {- const stblBox = minfBox?.children.find(- (b) => b.type === 'regular-box' && b.boxType === 'stbl',+ const stsdBox = getStsdBox(trakBox);+ if (stsdBox) {+ const sample = stsdBox.samples.find((s) => s.type === 'audio');+ if (sample && sample.type === 'audio') {+ const ret = onSample(sample);+ if (ret) {+ return ret;+ }++ const waveBox = sample.children.find(+ (b) => b.type === 'regular-box' && b.boxType === 'wave',);- if (stblBox && stblBox.type === 'regular-box') {- const stsdBox = stblBox?.children.find(- (b) => b.type === 'stsd-box',- );- if (stsdBox && stsdBox.type === 'stsd-box') {- const sample = stsdBox.samples.find((s) => s.type === 'audio');- if (sample && sample.type === 'audio') {- const ret = onSample(sample);- if (ret) {- return ret;- }-- const waveBox = sample.children.find(- (b) => b.type === 'regular-box' && b.boxType === 'wave',- );- if (- waveBox &&- waveBox.type === 'regular-box' &&- waveBox.boxType === 'wave'- ) {- const esdsBox = waveBox.children.find(- (b) => b.type === 'esds-box',- );- if (esdsBox && esdsBox.type === 'esds-box') {- const ret2 = onEsdsBox(esdsBox);- if (ret2) {- return ret2;- }- }- }+ if (+ waveBox &&+ waveBox.type === 'regular-box' &&+ waveBox.boxType === 'wave'+ ) {+ const esdsBox = waveBox.children.find((b) => b.type === 'esds-box');+ if (esdsBox && esdsBox.type === 'esds-box') {+ const ret2 = onEsdsBox(esdsBox);+ if (ret2) {+ return ret2;}}}diff --git a/packages/media-parser/src/get-fps.ts b/packages/media-parser/src/get-fps.tsindex d70579e018..661450594f 100644--- a/packages/media-parser/src/get-fps.ts+++ b/packages/media-parser/src/get-fps.ts@@ -1,7 +1,7 @@import type {SttsBox} from './boxes/iso-base-media/stts/stts';import type {TrakBox} from './boxes/iso-base-media/trak/trak';import type {AnySegment} from './parse-result';-import {getMoovBox, getMvhdBox, getTraks} from './traversal';+import {getMoovBox, getMvhdBox, getStsdBox, getTraks} from './traversal';const calculateFps = ({sttsBox,@@ -30,38 +30,8 @@ type TimescaleAndDuration = {};export const trakBoxContainsAudio = (trakBox: TrakBox): boolean => {- const {children} = trakBox;- const mediaBoxes = children.filter(- (c) => c.type === 'regular-box' && c.boxType === 'mdia',- );- if (!mediaBoxes || mediaBoxes.length === 0) {- return false;- }-- const firstMediaBox = mediaBoxes[0];- if (- firstMediaBox.type !== 'regular-box' ||- firstMediaBox.boxType !== 'mdia'- ) {- return false;- }-- const minf = firstMediaBox.children.find(- (c) => c.type === 'regular-box' && c.boxType === 'minf',- );- if (!minf || minf.type !== 'regular-box' || minf.boxType !== 'minf') {- return false;- }-- const stbl = minf.children.find(- (c) => c.type === 'regular-box' && c.boxType === 'stbl',- );- if (!stbl || stbl.type !== 'regular-box' || stbl.boxType !== 'stbl') {- return false;- }-- const stsd = stbl.children.find((c) => c.type === 'stsd-box');- if (!stsd || stsd.type !== 'stsd-box') {+ const stsd = getStsdBox(trakBox);+ if (!stsd) {return false;}@@ -74,38 +44,8 @@ export const trakBoxContainsAudio = (trakBox: TrakBox): boolean => {};export const trakBoxContainsVideo = (trakBox: TrakBox): boolean => {- const {children} = trakBox;- const mediaBoxes = children.filter(- (c) => c.type === 'regular-box' && c.boxType === 'mdia',- );- if (!mediaBoxes || mediaBoxes.length === 0) {- return false;- }-- const firstMediaBox = mediaBoxes[0];- if (- firstMediaBox.type !== 'regular-box' ||- firstMediaBox.boxType !== 'mdia'- ) {- return false;- }-- const minf = firstMediaBox.children.find(- (c) => c.type === 'regular-box' && c.boxType === 'minf',- );- if (!minf || minf.type !== 'regular-box' || minf.boxType !== 'minf') {- return false;- }-- const stbl = minf.children.find(- (c) => c.type === 'regular-box' && c.boxType === 'stbl',- );- if (!stbl || stbl.type !== 'regular-box' || stbl.boxType !== 'stbl') {- return false;- }-- const stsd = stbl.children.find((c) => c.type === 'stsd-box');- if (!stsd || stsd.type !== 'stsd-box') {+ const stsd = getStsdBox(trakBox);+ if (!stsd) {return false;}diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.tsindex 7623701473..c9532fd1e4 100644--- a/packages/media-parser/src/get-tracks.ts+++ b/packages/media-parser/src/get-tracks.ts@@ -1,9 +1,21 @@+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 {AnySegment} from './parse-result';-import {getMoovBox, getMvhdBox, getTraks} from './traversal';+import {+ getMoovBox,+ getMvhdBox,+ getStcoBox,+ getStszBox,+ getTkhdBox,+ getTraks,+} from './traversal';type Track = {type: 'audio' | 'video';+ stcoBox: StcoBox | null;+ stszBox: StszBox | null;+ trackId: number;};// TODO: Use this to determine if all tracks are present@@ -31,12 +43,29 @@ export const getTracks = (segments: AnySegment[]): Track[] => {const tracks = getTraks(moovBox);for (const track of tracks) {+ const stszBox = getStszBox(track);+ const stcoBox = getStcoBox(track);+ const tkhdBox = getTkhdBox(track);+ if (!tkhdBox) {+ throw new Error('Expected tkhd box in trak box');+ }+if (trakBoxContainsAudio(track)) {- foundTracks.push({type: 'audio'});+ foundTracks.push({+ type: 'audio',+ stcoBox,+ stszBox,+ trackId: tkhdBox.trackId,+ });}if (trakBoxContainsVideo(track)) {- foundTracks.push({type: 'video'});+ foundTracks.push({+ type: 'video',+ stcoBox,+ stszBox,+ trackId: tkhdBox.trackId,+ });}}diff --git a/packages/media-parser/src/parse-result.ts b/packages/media-parser/src/parse-result.tsindex 300159f7ba..317ae6a879 100644--- a/packages/media-parser/src/parse-result.ts+++ b/packages/media-parser/src/parse-result.ts@@ -15,7 +15,7 @@ import type {TkhdBox} from './boxes/iso-base-media/tkhd';import type {TrakBox} from './boxes/iso-base-media/trak/trak';import type {MatroskaSegment} from './boxes/webm/segments';-interface RegularBox extends BaseBox {+export interface RegularBox extends BaseBox {boxType: string;boxSize: number;children: AnySegment[];diff --git a/packages/media-parser/src/test/stream-remote.test.ts b/packages/media-parser/src/test/stream-remote.test.tsindex adfdc28919..6bb6826453 100644--- a/packages/media-parser/src/test/stream-remote.test.ts+++ b/packages/media-parser/src/test/stream-remote.test.ts@@ -24,7 +24,6 @@ test('Should stream from Wikipedia', async () => {const result = await parseMedia({src: 'https://upload.wikimedia.org/wikipedia/commons/transcoded/c/c0/Big_Buck_Bunny_4K.webm/Big_Buck_Bunny_4K.webm.720p.vp9.webm',fields: {- boxes: true,durationInSeconds: true,fps: true,dimensions: true,diff --git a/packages/media-parser/src/test/stream-samples.test.ts b/packages/media-parser/src/test/stream-samples.test.tsindex acaa44c53e..ddb1475327 100644--- a/packages/media-parser/src/test/stream-samples.test.ts+++ b/packages/media-parser/src/test/stream-samples.test.ts@@ -15,7 +15,52 @@ test('Stream samples', async () => {});const tracks = getTracks(boxes);- expect(tracks).toEqual([{type: 'video'}, {type: 'audio'}]);-- await Bun.write('stream-samples.json', JSON.stringify(boxes, null, 2));+ expect(tracks).toEqual([+ {+ 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],+ },+ },+ {+ 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: [],+ },+ },+ ]);});diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.tsindex 8001701418..367556b2ff 100644--- a/packages/media-parser/src/traversal.ts+++ b/packages/media-parser/src/traversal.ts@@ -1,7 +1,11 @@import type {MoovBox} from './boxes/iso-base-media/moov/moov';import type {MvhdBox} from './boxes/iso-base-media/mvhd';+import type {StcoBox} from './boxes/iso-base-media/stsd/stco';+import type {StsdBox} from './boxes/iso-base-media/stsd/stsd';+import type {StszBox} from './boxes/iso-base-media/stsd/stsz';+import type {TkhdBox} from './boxes/iso-base-media/tkhd';import type {TrakBox} from './boxes/iso-base-media/trak/trak';-import type {AnySegment} from './parse-result';+import type {AnySegment, RegularBox} from './parse-result';export const getMoovBox = (segments: AnySegment[]): MoovBox | null => {const moovBox = segments.find((s) => s.type === 'moov-box');@@ -25,3 +29,81 @@ export const getMvhdBox = (moovBox: MoovBox): MvhdBox | null => {export const getTraks = (moovBox: MoovBox): TrakBox[] => {return moovBox.children.filter((s) => s.type === 'trak-box') as TrakBox[];};++export const getTkhdBox = (trakBox: TrakBox): TkhdBox | null => {+ const tkhdBox = trakBox.children.find(+ (s) => s.type === 'tkhd-box',+ ) as TkhdBox | null;++ return tkhdBox;+};++export const getStblBox = (trakBox: TrakBox): RegularBox | null => {+ const mdiaBox = trakBox.children.find(+ (s) => s.type === 'regular-box' && s.boxType === 'mdia',+ );++ if (!mdiaBox || mdiaBox.type !== 'regular-box') {+ return null;+ }++ const minfBox = mdiaBox.children.find(+ (s) => s.type === 'regular-box' && s.boxType === 'minf',+ );++ if (!minfBox || minfBox.type !== 'regular-box') {+ return null;+ }++ const stblBox = minfBox.children.find(+ (s) => s.type === 'regular-box' && s.boxType === 'stbl',+ );++ if (!stblBox || stblBox.type !== 'regular-box') {+ return null;+ }++ return stblBox;+};++export const getStsdBox = (trakBox: TrakBox): StsdBox | null => {+ const stblBox = getStblBox(trakBox);++ if (!stblBox || stblBox.type !== 'regular-box') {+ return null;+ }++ const stsdBox = stblBox.children.find(+ (s) => s.type === 'stsd-box',+ ) as StsdBox | null;++ return stsdBox;+};++export const getStcoBox = (trakBox: TrakBox): StcoBox | null => {+ const stblBox = getStblBox(trakBox);++ if (!stblBox || stblBox.type !== 'regular-box') {+ return null;+ }++ const stcoBox = stblBox.children.find(+ (s) => s.type === 'stco-box',+ ) as StcoBox | null;++ return stcoBox;+};++export const getStszBox = (trakBox: TrakBox): StszBox | null => {+ const stblBox = getStblBox(trakBox);++ if (!stblBox || stblBox.type !== 'regular-box') {+ return null;+ }++ const stszBox = stblBox.children.find(+ (s) => s.type === 'stsz-box',+ ) as StszBox | null;++ return stszBox;+};