🎥 Make videos programmatically with React
more progress
Jonny Burger46fde01defb4
diff --git a/packages/media-parser/src/boxes/webm/parse-ebml.ts b/packages/media-parser/src/boxes/webm/parse-ebml.tsindex 84f4603061..2aaea6343e 100644--- a/packages/media-parser/src/boxes/webm/parse-ebml.ts+++ b/packages/media-parser/src/boxes/webm/parse-ebml.ts@@ -97,7 +97,7 @@ export const parseEbml = (iterator: BufferIterator): Prettify<PossibleEbml> => {}}- return {type: hasInMap.name, value: children as EbmlParsed<Ebml>[], hex};+ return {type: hasInMap.name, value: children as PossibleEbml[], hex};}// @ts-expect-errordiff --git a/packages/media-parser/src/boxes/webm/segments.ts b/packages/media-parser/src/boxes/webm/segments.tsindex 017b299661..fb147ff54e 100644--- a/packages/media-parser/src/boxes/webm/segments.ts+++ b/packages/media-parser/src/boxes/webm/segments.ts@@ -7,12 +7,8 @@ import {getTrack} from './get-track';import {parseEbml} from './parse-ebml';import type {PossibleEbml} from './segments/all-segments';import {matroskaElements} from './segments/all-segments';-import type {InfoSegment} from './segments/info';-import {parseInfoSegment} from './segments/info';import {type MainSegment} from './segments/main';import {expectChildren} from './segments/parse-children';-import type {TimestampScaleSegment} from './segments/timestamp-scale';-import {parseTimestampScaleSegment} from './segments/timestamp-scale';import type {AlphaModeSegment,AudioSegment,@@ -38,7 +34,6 @@ import type {TagSegment,TagsSegment,TimestampSegment,- TitleSegment,TrackEntrySegment,TrackNumberSegment,TrackUIDSegment,@@ -68,7 +63,6 @@ import {parseTagSegment,parseTagsSegment,parseTimestampSegment,- parseTitleSegment,parseTrackEntry,parseTrackNumber,parseTrackUID,@@ -79,8 +73,6 @@ import {parseTracksSegment} from './segments/tracks';export type MatroskaSegment =| MainSegment- | InfoSegment- | TimestampScaleSegment| TracksSegment| TrackEntrySegment| TrackNumberSegment@@ -94,7 +86,6 @@ export type MatroskaSegment =| AlphaModeSegment| MaxBlockAdditionId| ColorSegment- | TitleSegment| InterlacedSegment| CodecPrivateSegment| DefaultFlagSegment@@ -132,16 +123,6 @@ const parseSegment = async ({throw new Error(`Expected length of ${segmentId} to be greater than 0`);}- if (segmentId === '0x1549a966') {- return parseInfoSegment(iterator, length, parserContext);- }-- if (segmentId === matroskaElements.TimestampScale) {- const timestampScale = parseTimestampScaleSegment(iterator);- parserContext.parserState.setTimescale(timestampScale.timestampScale);- return timestampScale;- }-if (segmentId === '0x1654ae6b') {return parseTracksSegment(iterator, length, parserContext);}@@ -223,10 +204,6 @@ const parseSegment = async ({return parseCodecPrivateSegment(iterator, length);}- if (segmentId === '0x7ba9') {- return parseTitleSegment(iterator, length);- }-if (segmentId === '0x88') {return parseDefaultFlagSegment(iterator, length);}@@ -328,7 +305,12 @@ const parseSegment = async ({iterator.counter.decrement(headerReadSoFar);- return parseEbml(iterator);+ const ebml = parseEbml(iterator);+ if (ebml.type === 'TimestampScale') {+ parserContext.parserState.setTimescale(ebml.value);+ }++ return ebml;};export const expectSegment = async (diff --git a/packages/media-parser/src/boxes/webm/segments/all-segments.ts b/packages/media-parser/src/boxes/webm/segments/all-segments.tsindex a9ba08e3c2..c128337df2 100644--- a/packages/media-parser/src/boxes/webm/segments/all-segments.ts+++ b/packages/media-parser/src/boxes/webm/segments/all-segments.ts@@ -1,3 +1,5 @@+import type {Prettify} from '../parse-ebml';+export const matroskaElements = {Header: '0x1a45dfa3',EBMLMaxIDLength: '0x42f2',@@ -460,10 +462,43 @@ export const heightType = {type: 'uint',} as const satisfies Ebml;+export const muxingApp = {+ name: 'MuxingApp',+ type: 'string',+} as const satisfies Ebml;++export const duration = {+ name: 'Duration',+ type: 'float',+} as const satisfies Ebml;++export const timestampScale = {+ name: 'TimestampScale',+ type: 'uint',+} as const satisfies Ebml;++export const writingApp = {+ name: 'WritingApp',+ type: 'string',+} as const satisfies Ebml;++export const infoType = {+ name: 'Info',+ type: 'children',+ children: [muxingApp, timestampScale, writingApp, duration],+} as const satisfies Ebml;++export const titleType = {+ name: 'Title',+ type: 'string',+} as const satisfies Ebml;+export type CodecIdSegment = EbmlParsed<typeof codecID>;export type TrackTypeSegment = EbmlParsed<typeof trackType>;export type WidthSegment = EbmlParsed<typeof widthType>;export type HeightSegment = EbmlParsed<typeof heightType>;+export type TimestampScaleSegment = EbmlParsed<typeof timestampScale>;+export type DurationSegment = EbmlParsed<typeof duration>;export type EbmlValue<T extends Ebml> = T extends EbmlWithUint8? number@@ -475,7 +510,7 @@ export type EbmlValue<T extends Ebml> = T extends EbmlWithUint8? number: T extends EbmlWithHexString? string- : EbmlParsed<Ebml>[];+ : PossibleEbml[];export type EbmlParsed<T extends Ebml> = {type: T['name'];@@ -541,10 +576,7 @@ export const ebmlMap = {name: 'Crc32',type: 'void',},- [matroskaElements.MuxingApp]: {- name: 'MuxingApp',- type: 'string',- },+ [matroskaElements.MuxingApp]: muxingApp,[matroskaElements.WritingApp]: {name: 'WritingApp',type: 'string',@@ -553,10 +585,7 @@ export const ebmlMap = {name: 'SegmentUUID',type: 'string',},- [matroskaElements.Duration]: {- name: 'Duration',- type: 'float',- },+ [matroskaElements.Duration]: duration,[matroskaElements.CodecID]: {name: 'CodecID',type: 'string',@@ -564,12 +593,13 @@ export const ebmlMap = {[matroskaElements.TrackType]: trackType,[matroskaElements.PixelWidth]: widthType,[matroskaElements.PixelHeight]: heightType,+ [matroskaElements.TimestampScale]: timestampScale,+ [matroskaElements.Info]: infoType,+ [matroskaElements.Title]: titleType,} as const satisfies Partial<Record<MatroskaElement, Ebml>>;-export type PossibleEbml = {- [key in keyof typeof ebmlMap]: {- type: (typeof ebmlMap)[key]['name'];- value: EbmlValue<(typeof ebmlMap)[key]>;- hex: string;- };-}[keyof typeof ebmlMap];+export type PossibleEbml = Prettify<+ {+ [key in keyof typeof ebmlMap]: EbmlParsed<(typeof ebmlMap)[key]>;+ }[keyof typeof ebmlMap]+>;diff --git a/packages/media-parser/src/boxes/webm/segments/info.ts b/packages/media-parser/src/boxes/webm/segments/info.tsdeleted file mode 100644index 43fa1e4936..0000000000--- a/packages/media-parser/src/boxes/webm/segments/info.ts+++ /dev/null@@ -1,34 +0,0 @@-import type {BufferIterator} from '../../../buffer-iterator';-import type {ParserContext} from '../../../parser-context';-import type {MatroskaSegment} from '../segments';-import {expectChildren} from './parse-children';--export type InfoSegment = {- type: 'info-segment';- length: number;- children: MatroskaSegment[];-};--export const parseInfoSegment = async (- iterator: BufferIterator,- length: number,- parserContext: ParserContext,-): Promise<InfoSegment> => {- const children = await expectChildren({- iterator,- length,- initialChildren: [],- wrap: null,- parserContext,- });-- if (children.status === 'incomplete') {- throw new Error('Incomplete children');- }-- return {- type: 'info-segment',- length,- children: children.segments as MatroskaSegment[],- };-};diff --git a/packages/media-parser/src/boxes/webm/segments/timestamp-scale.ts b/packages/media-parser/src/boxes/webm/segments/timestamp-scale.tsdeleted file mode 100644index f129093be6..0000000000--- a/packages/media-parser/src/boxes/webm/segments/timestamp-scale.ts+++ /dev/null@@ -1,17 +0,0 @@-import type {BufferIterator} from '../../../buffer-iterator';--export type TimestampScaleSegment = {- type: 'timestamp-scale-segment';- timestampScale: number;-};--export const parseTimestampScaleSegment = (- iterator: BufferIterator,-): TimestampScaleSegment => {- const timestampScale = iterator.getUint(3);-- return {- type: 'timestamp-scale-segment',- timestampScale,- };-};diff --git a/packages/media-parser/src/boxes/webm/segments/track-entry.ts b/packages/media-parser/src/boxes/webm/segments/track-entry.tsindex 3213a0f11a..31efb0bf34 100644--- a/packages/media-parser/src/boxes/webm/segments/track-entry.ts+++ b/packages/media-parser/src/boxes/webm/segments/track-entry.ts@@ -317,23 +317,6 @@ export const parseColorSegment = (};};-export type TitleSegment = {- type: 'title-segment';- title: string;-};--export const parseTitleSegment = (- iterator: BufferIterator,- length: number,-): TitleSegment => {- const title = iterator.getByteString(length);-- return {- type: 'title-segment',- title,- };-};-export type InterlacedSegment = {type: 'interlaced-segment';interlaced: boolean;diff --git a/packages/media-parser/src/get-duration.ts b/packages/media-parser/src/get-duration.tsindex fe476e2c20..78069820eb 100644--- a/packages/media-parser/src/get-duration.ts+++ b/packages/media-parser/src/get-duration.ts@@ -1,3 +1,4 @@+import type {DurationSegment} from './boxes/webm/segments/all-segments';import type {AnySegment} from './parse-result';import {getMoovBox, getMvhdBox} from './traversal';@@ -12,28 +13,26 @@ const getDurationFromMatroska = (segments: AnySegment[]): number | null => {return null;}- const infoSegment = children.find((s) => s.type === 'info-segment');+ const infoSegment = children.find((s) => s.type === 'Info');const relevantBoxes = [...mainSegment.children,- ...(infoSegment && infoSegment.type === 'info-segment'- ? infoSegment.children- : []),+ ...(infoSegment && infoSegment.type === 'Info' ? infoSegment.value : []),];- const timestampScale = relevantBoxes.find(- (s) => s.type === 'timestamp-scale-segment',- );- if (!timestampScale || timestampScale.type !== 'timestamp-scale-segment') {+ const timestampScale = relevantBoxes.find((s) => s.type === 'TimestampScale');+ if (!timestampScale || timestampScale.type !== 'TimestampScale') {return null;}- const duration = relevantBoxes.find((s) => s.type === 'Duration');+ const duration = relevantBoxes.find(+ (s) => s.type === 'Duration',+ ) as DurationSegment;if (!duration || duration.type !== 'Duration') {return null;}- return (duration.value / timestampScale.timestampScale) * 1000;+ return (duration.value / timestampScale.value) * 1000;};export const getDuration = (boxes: AnySegment[]): number | null => {diff --git a/packages/media-parser/src/test/matroska.test.ts b/packages/media-parser/src/test/matroska.test.tsindex beba3cd678..d3dd238e12 100644--- a/packages/media-parser/src/test/matroska.test.ts+++ b/packages/media-parser/src/test/matroska.test.ts@@ -144,12 +144,13 @@ test('Should get duration of AV1 video', async () => {hex: matroskaElements.Void,},{- type: 'info-segment',- length: 48,- children: [+ type: 'Info',+ hex: matroskaElements.Info,+ value: [{- type: 'timestamp-scale-segment',- timestampScale: 1000000,+ type: 'TimestampScale',+ value: 1000000,+ hex: matroskaElements.TimestampScale,},{type: 'MuxingApp',diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.tsindex 1a98825f05..0285f93760 100644--- a/packages/media-parser/src/traversal.ts+++ b/packages/media-parser/src/traversal.ts@@ -14,11 +14,11 @@ import type {TrakBox} from './boxes/iso-base-media/trak/trak';import type {CodecIdSegment,HeightSegment,+ TimestampScaleSegment,TrackTypeSegment,WidthSegment,} from './boxes/webm/segments/all-segments';import type {MainSegment} from './boxes/webm/segments/main';-import type {TimestampScaleSegment} from './boxes/webm/segments/timestamp-scale';import type {AudioSegment,ClusterSegment,@@ -265,21 +265,19 @@ export const getTracksSegment = (segment: MainSegment) => {export const getTimescaleSegment = (segment: MainSegment,): TimestampScaleSegment | null => {- const infoSegment = segment.children.find((b) => b.type === 'info-segment');+ const infoSegment = segment.children.find((b) => b.type === 'Info');- if (!infoSegment || infoSegment.type !== 'info-segment') {+ if (!infoSegment || infoSegment.type !== 'Info') {return null;}- const timescale = infoSegment.children.find(- (b) => b.type === 'timestamp-scale-segment',- );+ const timescale = infoSegment.value.find((b) => b.type === 'TimestampScale');- if (!timescale || timescale.type !== 'timestamp-scale-segment') {+ if (!timescale || timescale.type !== 'TimestampScale') {return null;}- return timescale;+ return timescale as TimestampScaleSegment;};export const getVideoSegment = (