🎥 Make videos programmatically with React
progress with webm
Jonny Burger394c935bf529
diff --git a/packages/media-parser/src/boxes/webm/parse-ebml.ts b/packages/media-parser/src/boxes/webm/parse-ebml.tsindex 82d7785b86..c5d0a89429 100644--- a/packages/media-parser/src/boxes/webm/parse-ebml.ts+++ b/packages/media-parser/src/boxes/webm/parse-ebml.ts@@ -157,6 +157,10 @@ export const postprocessEbml = async ({state.webm.setTimescale(ebml.value.value);}+ if (ebml.type === 'Tracks') {+ state.callbacks.tracks.setIsDone();+ }+if (ebml.type === 'TrackEntry') {state.webm.onTrackEntrySegment(ebml);diff --git a/packages/media-parser/src/boxes/webm/parse-webm-header.ts b/packages/media-parser/src/boxes/webm/parse-webm-header.tsindex 8b337bfae9..3efff21ff4 100644--- a/packages/media-parser/src/boxes/webm/parse-webm-header.ts+++ b/packages/media-parser/src/boxes/webm/parse-webm-header.ts@@ -1,40 +1,16 @@import type {BufferIterator} from '../../buffer-iterator';import type {Options, ParseMediaFields} from '../../options';-import type {- MatroskaParseResult,- MatroskaStructure,- ParseResult,-} from '../../parse-result';+import type {ParseResult} from '../../parse-result';import type {ParserState} from '../../state/parser-state';-import {expectChildren} from './segments/parse-children';--const continueAfterMatroskaResult = (- result: MatroskaParseResult,- structure: MatroskaStructure,-): ParseResult => {- if (result.status === 'done') {- return {- status: 'done',- };- }-- return {- status: 'incomplete',- continueParsing: async () => {- const newResult = await result.continueParsing();- return continueAfterMatroskaResult(newResult, structure);- },- skipTo: null,- };-};+import {expectSegment} from './segments';// Parsing according to https://darkcoding.net/software/reading-mediarecorders-webm-opus-output/export const parseWebm = async ({- counter,+ iterator,state,fields,}: {- counter: BufferIterator;+ iterator: BufferIterator;state: ParserState;fields: Options<ParseMediaFields>;}): Promise<ParseResult> => {@@ -43,14 +19,26 @@ export const parseWebm = async ({throw new Error('Invalid structure type');}- const results = await expectChildren({- iterator: counter,- length: Infinity,- children: structure.boxes,+ const continueParsing = () => {+ return parseWebm({+ iterator,+ fields,+ state,+ });+ };++ const results = await expectSegment({+ iterator,state,- startOffset: counter.counter.getOffset(),fields,- topLevelStructure: structure,});- return continueAfterMatroskaResult(results, structure);+ if (results !== null) {+ structure.boxes.push(results);+ }++ return {+ status: 'incomplete',+ continueParsing,+ skipTo: null,+ };};diff --git a/packages/media-parser/src/boxes/webm/segments.ts b/packages/media-parser/src/boxes/webm/segments.tsindex cbe5454c90..d8e00cff24 100644--- a/packages/media-parser/src/boxes/webm/segments.ts+++ b/packages/media-parser/src/boxes/webm/segments.ts@@ -1,113 +1,42 @@/* eslint-disable @typescript-eslint/no-use-before-define */import type {BufferIterator} from '../../buffer-iterator';+import {Log} from '../../log';import type {Options, ParseMediaFields} from '../../options';-import type {- ExpectSegmentParseResult,- MatroskaParseResult,- MatroskaStructure,-} from '../../parse-result';import type {ParserState} from '../../state/parser-state';import {parseEbml, postprocessEbml} from './parse-ebml';import type {ClusterSegment, MainSegment} from './segments/all-segments';-import {type PossibleEbml, type TrackEntry} from './segments/all-segments';import {- expectAndProcessSegment,- expectChildren,-} from './segments/parse-children';+ ebmlMap,+ type PossibleEbml,+ type TrackEntry,+} from './segments/all-segments';+import {expectChildren} from './segments/parse-children';export type MatroskaSegment = PossibleEbml;export type OnTrackEntrySegment = (trackEntry: TrackEntry) => void;-const continueAfterMatroskaParseResult = async ({- result,- iterator,- state,- segment,-}: {- result: MatroskaParseResult;- iterator: BufferIterator;- state: ParserState;- segment: MatroskaSegment;-}): Promise<ExpectSegmentParseResult> => {- if (result.status === 'done') {- throw new Error('Should not continue after done');- }-- const proceeded = await result.continueParsing();- if (proceeded.status === 'done') {- return {- status: 'done',- segment,- };- }-- return {- continueParsing() {- return continueAfterMatroskaParseResult({- result: proceeded,- iterator,- state,- segment,- });- },- segment: null,- status: 'incomplete',- };-};-export const expectSegment = async ({iterator,state,- offset,- children,fields,- topLevelStructure,}: {iterator: BufferIterator;state: ParserState;- offset: number;- children: PossibleEbml[];fields: Options<ParseMediaFields>;- topLevelStructure: MatroskaStructure;-}): Promise<ExpectSegmentParseResult> => {- iterator.counter.decrement(iterator.counter.getOffset() - offset);-+}): Promise<MatroskaSegment | null> => {if (iterator.bytesRemaining() === 0) {- return {- status: 'incomplete',- continueParsing: () => {- return expectAndProcessSegment({- iterator,- state,- offset,- children,- fields,- topLevelStructure,- });- },- segment: null,- };+ throw new Error('has no bytes');}+ const offset = iterator.counter.getOffset();+ const {returnToCheckpoint} = iterator.startCheckpoint();const segmentId = iterator.getMatroskaSegmentId();+ Log.trace(state.logLevel, 'Segment ID:', ebmlMap[segmentId as string]?.name);if (segmentId === null) {- iterator.counter.decrement(iterator.counter.getOffset() - offset);- return {- status: 'incomplete',- continueParsing: () => {- return expectAndProcessSegment({- iterator,- state,- offset,- children,- fields,- topLevelStructure,- });- },- segment: null,- };+ returnToCheckpoint();+ return null;}const offsetBeforeVInt = iterator.counter.getOffset();@@ -115,81 +44,33 @@ export const expectSegment = async ({const offsetAfterVInt = iterator.counter.getOffset();if (length === null) {- iterator.counter.decrement(iterator.counter.getOffset() - offset);- return {- status: 'incomplete',- continueParsing: () => {- return expectSegment({- iterator,- state,- offset,- children,- fields,- topLevelStructure,- });- },- segment: null,- };+ returnToCheckpoint();+ return null;}const bytesRemainingNow =iterator.byteLength() - iterator.counter.getOffset();if (segmentId === '0x18538067' || segmentId === '0x1f43b675') {- const newSegment: ClusterSegment | MainSegment = {- type: segmentId === '0x18538067' ? 'Segment' : 'Cluster',- minVintWidth: offsetAfterVInt - offsetBeforeVInt,- value: [],- };-- const main = await expectChildren({+ const value = await expectChildren({iterator,length,- children: newSegment.value,state,startOffset: iterator.counter.getOffset(),fields,- topLevelStructure,});-- if (main.status === 'incomplete') {- return {- status: 'incomplete',- continueParsing: () => {- return continueAfterMatroskaParseResult({- iterator,- state,- result: main,- segment: newSegment,- });- },- segment: newSegment,- };- }-- return {- status: 'done',- segment: newSegment,+ const newSegment: ClusterSegment | MainSegment = {+ type: segmentId === '0x18538067' ? 'Segment' : 'Cluster',+ minVintWidth: offsetAfterVInt - offsetBeforeVInt,+ value,};++ return newSegment;}if (bytesRemainingNow < length) {- const bytesRead = iterator.counter.getOffset() - offset;- iterator.counter.decrement(bytesRead);- return {- status: 'incomplete',- segment: null,- continueParsing: () => {- return expectSegment({- iterator,- state,- offset,- children,- fields,- topLevelStructure,- });- },- };+ returnToCheckpoint();+ return null;}const segment = await parseSegment({@@ -200,10 +81,7 @@ export const expectSegment = async ({headerReadSoFar: iterator.counter.getOffset() - offset,});- return {- status: 'done',- segment,- };+ return segment;};const parseSegment = async ({diff --git a/packages/media-parser/src/boxes/webm/segments/parse-children.ts b/packages/media-parser/src/boxes/webm/segments/parse-children.tsindex c7bc82937c..3c20e0870a 100644--- a/packages/media-parser/src/boxes/webm/segments/parse-children.ts+++ b/packages/media-parser/src/boxes/webm/segments/parse-children.ts@@ -1,224 +1,37 @@import type {BufferIterator} from '../../../buffer-iterator';-import {hasAllInfo} from '../../../has-all-info';import type {Options, ParseMediaFields} from '../../../options';-import type {- ExpectSegmentParseResult,- MatroskaParseResult,- MatroskaStructure,-} from '../../../parse-result';import type {ParserState} from '../../../state/parser-state';-import type {MatroskaSegment} from '../segments';import {expectSegment} from '../segments';import type {PossibleEbml} from './all-segments';-const processParseResult = ({- parseResult,- children,- state,- fields,- topLevelStructure,-}: {- children: MatroskaSegment[];- parseResult: ExpectSegmentParseResult;- state: ParserState;- fields: Options<ParseMediaFields>;- topLevelStructure: MatroskaStructure;-}): ExpectSegmentParseResult => {- if (parseResult.segment && !children.includes(parseResult.segment)) {- children.push(parseResult.segment);- if (hasAllInfo({fields, state})) {- return {- status: 'done',- segment: parseResult.segment,- };- }-- if (parseResult.segment.type === 'Tracks') {- state.callbacks.tracks.setIsDone();- }- }-- if (parseResult.status === 'incomplete') {- // No need to decrement because expectSegment already does it- return {- status: 'incomplete',- segment: parseResult.segment,- continueParsing: async () => {- const newParseResult = await parseResult.continueParsing();- return processParseResult({- children,- parseResult: newParseResult,- state,- fields,- topLevelStructure,- });- },- };- }-- return {- status: 'done',- segment: parseResult.segment,- };-};--export const expectAndProcessSegment = async ({- iterator,- state,- offset,- children,- fields,- topLevelStructure,-}: {- iterator: BufferIterator;- state: ParserState;- offset: number;- children: PossibleEbml[];- fields: Options<ParseMediaFields>;- topLevelStructure: MatroskaStructure;-}) => {- const segment = await expectSegment({- iterator,- state,- offset,- children,- fields,- topLevelStructure,- });- return processParseResult({- children,- parseResult: segment,- state,- fields,- topLevelStructure,- });-};--const continueAfterSegmentResult = async ({- result,- length,- children,- state,- iterator,- startOffset,- fields,- topLevelStructure,-}: {- result: ExpectSegmentParseResult;- length: number;- children: MatroskaSegment[];- state: ParserState;- iterator: BufferIterator;- startOffset: number;- fields: Options<ParseMediaFields>;- topLevelStructure: MatroskaStructure;-}): Promise<MatroskaParseResult> => {- if (result.status === 'done') {- throw new Error('Should not continue after done');- }-- const segmentResult = await result.continueParsing();- if (segmentResult.status === 'done') {- return {- status: 'incomplete',- continueParsing: () => {- // eslint-disable-next-line @typescript-eslint/no-use-before-define- return expectChildren({- children,- iterator,- length,- state,- startOffset,- fields,- topLevelStructure,- });- },- skipTo: null,- };- }-- return {- status: 'incomplete',- continueParsing: () => {- return continueAfterSegmentResult({- result: segmentResult,- children,- iterator,- length,- state,- startOffset,- fields,- topLevelStructure,- });- },- skipTo: null,- };-};-export const expectChildren = async ({iterator,length,- children,state,startOffset,fields,- topLevelStructure,}: {iterator: BufferIterator;length: number;- children: MatroskaSegment[];state: ParserState;startOffset: number;fields: Options<ParseMediaFields>;- topLevelStructure: MatroskaStructure;-}): Promise<MatroskaParseResult> => {+}): Promise<PossibleEbml[]> => {+ const children: PossibleEbml[] = [];while (iterator.counter.getOffset() < startOffset + length) {if (iterator.bytesRemaining() === 0) {break;}- const currentOffset = iterator.counter.getOffset();- const child = await expectAndProcessSegment({+ const child = await expectSegment({iterator,state,- offset: currentOffset,- children,fields,- topLevelStructure,});-- if (- hasAllInfo({- fields,- state,- })- ) {- return {- status: 'done',- };- }-- if (child.status === 'incomplete') {- return {- status: 'incomplete',- continueParsing: () => {- return continueAfterSegmentResult({- result: child,- children,- iterator,- length: length - (currentOffset - startOffset),- state,- startOffset: currentOffset,- fields,- topLevelStructure,- });- },- skipTo: null,- };+ if (child) {+ children.push(child);}}- return {- status: 'done',- };+ return children;};diff --git a/packages/media-parser/src/parse-result.ts b/packages/media-parser/src/parse-result.tsindex ff10bc2cc7..0db64e8b5b 100644--- a/packages/media-parser/src/parse-result.ts+++ b/packages/media-parser/src/parse-result.ts@@ -128,23 +128,6 @@ export type ParseResult =continueParsing: () => Promise<ParseResult>;};-export type MatroskaParseResult =- | {- status: 'done';- }- | {- status: 'incomplete';- skipTo: number | null;- continueParsing: () => Promise<MatroskaParseResult>;- };--export type ExpectSegmentParseResult =- | {- status: 'done';- segment: MatroskaSegment;- }- | {- status: 'incomplete';- segment: MatroskaSegment | null;- continueParsing: () => Promise<ExpectSegmentParseResult>;- };+export type MatroskaParseResult = {+ skipTo: number | null;+};diff --git a/packages/media-parser/src/parse-video.ts b/packages/media-parser/src/parse-video.tsindex 37566b3f08..f8820aeae3 100644--- a/packages/media-parser/src/parse-video.ts+++ b/packages/media-parser/src/parse-video.ts@@ -73,7 +73,7 @@ export const parseVideo = ({boxes: [],type: 'matroska',});- return parseWebm({counter: iterator, state, fields});+ return parseWebm({iterator, state, fields});}if (fileType.type === 'transport-stream') {diff --git a/packages/media-parser/src/test/keyframes.test.ts b/packages/media-parser/src/test/keyframes.test.tsindex d377da65b7..30e6766ede 100644--- a/packages/media-parser/src/test/keyframes.test.ts+++ b/packages/media-parser/src/test/keyframes.test.ts@@ -13,6 +13,7 @@ test('should be able to get keyframes from webm', async () => {internalStats: true,},reader: nodeReader,+ logLevel: 'trace',});expect(slowKeyframes).toEqual([{