🎥 Make videos programmatically with React
video segment
Jonny Burger3a803a830590
diff --git a/packages/media-parser/src/boxes/webm/segments.ts b/packages/media-parser/src/boxes/webm/segments.tsindex 1c4df8a08a..5e553b76fa 100644--- a/packages/media-parser/src/boxes/webm/segments.ts+++ b/packages/media-parser/src/boxes/webm/segments.ts@@ -10,31 +10,23 @@ import {matroskaElements} from './segments/all-segments';import {type MainSegment} from './segments/main';import {expectChildren} from './segments/parse-children';import type {- AudioSegment,- BlockAdditionsSegment,BlockElement,BlockGroupSegment,ClusterSegment,DefaultFlagSegment,- MaxBlockAdditionId,ReferenceBlockSegment,SimpleBlockOrBlockSegment,TimestampSegment,TrackEntrySegment,- VideoSegment,} from './segments/track-entry';import {- parseAudioSegment,- parseBlockAdditionsSegment,parseBlockElementSegment,parseBlockGroupSegment,parseDefaultFlagSegment,- parseMaxBlockAdditionId,parseReferenceBlockSegment,parseSimpleBlockOrBlockSegment,parseTimestampSegment,parseTrackEntry,- parseVideoSegment,} from './segments/track-entry';import type {TracksSegment} from './segments/tracks';import {parseTracksSegment} from './segments/tracks';@@ -43,17 +35,13 @@ export type MatroskaSegment =| MainSegment| TracksSegment| TrackEntrySegment- | VideoSegment- | MaxBlockAdditionId| DefaultFlagSegment| ClusterSegment| TimestampSegment| SimpleBlockOrBlockSegment| BlockGroupSegment| BlockElement- | AudioSegment| ReferenceBlockSegment- | BlockAdditionsSegment| PossibleEbml;export type OnTrackEntrySegment = (trackEntry: TrackEntrySegment) => void;@@ -100,18 +88,6 @@ const parseSegment = async ({return trackEntry;}- if (segmentId === '0x55ee') {- return parseMaxBlockAdditionId(iterator, length);- }-- if (segmentId === '0xe0') {- return parseVideoSegment(iterator, length, parserContext);- }-- if (segmentId === '0xe1') {- return parseAudioSegment(iterator, length, parserContext);- }-if (segmentId === '0x88') {return parseDefaultFlagSegment(iterator, length);}@@ -144,10 +120,6 @@ const parseSegment = async ({return parseReferenceBlockSegment(iterator, length);}- if (segmentId === matroskaElements.BlockAdditions) {- return parseBlockAdditionsSegment(iterator, length);- }-if (segmentId === '0xa0') {const blockGroup = await parseBlockGroupSegment(iterator,diff --git a/packages/media-parser/src/boxes/webm/segments/all-segments.ts b/packages/media-parser/src/boxes/webm/segments/all-segments.tsindex 248b70916d..d2a6946182 100644--- a/packages/media-parser/src/boxes/webm/segments/all-segments.ts+++ b/packages/media-parser/src/boxes/webm/segments/all-segments.ts@@ -581,6 +581,28 @@ export const codecPrivate = {type: 'uint8array',} as const satisfies Ebml;+export const blockAdditionsSegment = {+ name: 'BlockAdditions',+ type: 'uint8array',+} as const satisfies Ebml;++export const maxBlockAdditionIdSegment = {+ name: 'MaxBlockAdditionID',+ type: 'uint',+} as const satisfies Ebml;++export const audioSegment = {+ name: 'Audio',+ type: 'children',+ children: [samplingFrequency, channels, bitDepth],+} as const satisfies Ebml;++export const videoSegment = {+ name: 'Video',+ type: 'children',+ children: [heightType, displayHeight, displayWidth, widthType],+} as const satisfies Ebml;+export type CodecIdSegment = EbmlParsed<typeof codecID>;export type TrackTypeSegment = EbmlParsed<typeof trackType>;export type WidthSegment = EbmlParsed<typeof widthType>;@@ -590,6 +612,8 @@ export type DurationSegment = EbmlParsed<typeof duration>;export type DisplayWidthSegment = EbmlParsed<typeof displayWidth>;export type DisplayHeightSegment = EbmlParsed<typeof displayHeight>;export type TrackNumberSegment = EbmlParsed<typeof trackNumber>;+export type AudioSegment = EbmlParsed<typeof audioSegment>;+export type VideoSegment = EbmlParsed<typeof videoSegment>;export type EbmlValue<T extends Ebml> = T extends EbmlWithUint8? number@@ -704,6 +728,10 @@ export const ebmlMap = {[matroskaElements.Language]: language,[matroskaElements.DefaultDuration]: defaultDuration,[matroskaElements.CodecPrivate]: codecPrivate,+ [matroskaElements.BlockAdditions]: blockAdditionsSegment,+ [matroskaElements.MaxBlockAdditionID]: maxBlockAdditionIdSegment,+ [matroskaElements.Audio]: audioSegment,+ [matroskaElements.Video]: videoSegment,} as const satisfies Partial<Record<MatroskaElement, Ebml>>;export type PossibleEbml = Prettify<diff --git a/packages/media-parser/src/boxes/webm/segments/track-entry.ts b/packages/media-parser/src/boxes/webm/segments/track-entry.tsindex b3f901b5d6..5370f3f475 100644--- a/packages/media-parser/src/boxes/webm/segments/track-entry.ts+++ b/packages/media-parser/src/boxes/webm/segments/track-entry.ts@@ -68,83 +68,6 @@ export const trackTypeToString = (trackType: number): TrackType => {}};-export type VideoSegment = {- type: 'video-segment';- children: MatroskaSegment[];-};--export const parseVideoSegment = async (- iterator: BufferIterator,- length: number,- parserContext: ParserContext,-): Promise<VideoSegment> => {- const children = await expectChildren({- iterator,- length,- initialChildren: [],- wrap: null,- parserContext,- });-- if (children.status === 'incomplete') {- throw new Error('Incomplete children');- }-- return {- type: 'video-segment',- children: children.segments as MatroskaSegment[],- };-};--export type AudioSegment = {- type: 'audio-segment';- children: MatroskaSegment[];-};--export const parseAudioSegment = async (- iterator: BufferIterator,- length: number,- parserContext: ParserContext,-): Promise<AudioSegment> => {- const children = await expectChildren({- iterator,- length,- initialChildren: [],- wrap: null,- parserContext,- });-- if (children.status === 'incomplete') {- throw new Error('Incomplete children');- }-- return {- type: 'audio-segment',- children: children.segments as MatroskaSegment[],- };-};--export type MaxBlockAdditionId = {- type: 'max-block-addition-id-segment';- maxBlockAdditionId: number;-};--export const parseMaxBlockAdditionId = (- iterator: BufferIterator,- length: number,-): MaxBlockAdditionId => {- if (length !== 1) {- throw new Error('Expected alpha mode segment to be 1 byte');- }-- const maxBlockAdditionId = iterator.getUint8();-- return {- type: 'max-block-addition-id-segment',- maxBlockAdditionId,- };-};-export type DefaultFlagSegment = {type: 'default-flag-segment';defaultFlag: boolean;@@ -360,23 +283,6 @@ export const parseReferenceBlockSegment = (};};-export type BlockAdditionsSegment = {- type: 'block-additions-segment';- blockAdditions: Uint8Array;-};--export const parseBlockAdditionsSegment = (- iterator: BufferIterator,- length: number,-): BlockAdditionsSegment => {- const blockAdditions = iterator.getSlice(length);-- return {- type: 'block-additions-segment',- blockAdditions,- };-};-export type BlockElement = {type: 'block-element-segment';length: number;diff --git a/packages/media-parser/src/test/matroska.test.ts b/packages/media-parser/src/test/matroska.test.tsindex 786a441a00..5d69dab2b7 100644--- a/packages/media-parser/src/test/matroska.test.ts+++ b/packages/media-parser/src/test/matroska.test.ts@@ -176,8 +176,8 @@ test('Should get duration of AV1 video', async () => {value: 40000000,},{- type: 'video-segment',- children: [+ type: 'Video',+ value: [{type: 'PixelWidth',value: 1920,diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.tsindex c21e213a8f..8318996f1d 100644--- a/packages/media-parser/src/traversal.ts+++ b/packages/media-parser/src/traversal.ts@@ -12,20 +12,20 @@ import type {SttsBox} from './boxes/iso-base-media/stsd/stts';import type {TkhdBox} from './boxes/iso-base-media/tkhd';import type {TrakBox} from './boxes/iso-base-media/trak/trak';import type {+ AudioSegment,CodecIdSegment,DisplayHeightSegment,DisplayWidthSegment,HeightSegment,TimestampScaleSegment,TrackTypeSegment,+ VideoSegment,WidthSegment,} from './boxes/webm/segments/all-segments';import type {MainSegment} from './boxes/webm/segments/main';import type {- AudioSegment,ClusterSegment,TrackEntrySegment,- VideoSegment,} from './boxes/webm/segments/track-entry';import type {AnySegment, RegularBox} from './parse-result';@@ -283,8 +283,8 @@ export const getTimescaleSegment = (export const getVideoSegment = (track: TrackEntrySegment,): VideoSegment | null => {- const videoSegment = track.children.find((b) => b.type === 'video-segment');- if (!videoSegment || videoSegment.type !== 'video-segment') {+ const videoSegment = track.children.find((b) => b.type === 'Video');+ if (!videoSegment || videoSegment.type !== 'Video') {return null;}@@ -294,8 +294,8 @@ export const getVideoSegment = (export const getAudioSegment = (track: TrackEntrySegment,): AudioSegment | null => {- const audioSegment = track.children.find((b) => b.type === 'audio-segment');- if (!audioSegment || audioSegment.type !== 'audio-segment') {+ const audioSegment = track.children.find((b) => b.type === 'Audio');+ if (!audioSegment || audioSegment.type !== 'Audio') {return null;}@@ -308,7 +308,7 @@ export const getSampleRate = (track: TrackEntrySegment): number | null => {return null;}- const samplingFrequency = audioSegment.children.find(+ const samplingFrequency = audioSegment.value.find((b) => b.type === 'SamplingFrequency',);@@ -325,7 +325,7 @@ export const getNumberOfChannels = (track: TrackEntrySegment): number => {throw new Error('Could not find audio segment');}- const channels = audioSegment.children.find((b) => b.type === 'Channels');+ const channels = audioSegment.value.find((b) => b.type === 'Channels');if (!channels || channels.type !== 'Channels') {return 1;@@ -340,7 +340,7 @@ export const getBitDepth = (track: TrackEntrySegment): number | null => {return null;}- const bitDepth = audioSegment.children.find((b) => b.type === 'BitDepth');+ const bitDepth = audioSegment.value.find((b) => b.type === 'BitDepth');if (!bitDepth || bitDepth.type !== 'BitDepth') {return null;@@ -367,7 +367,7 @@ export const getWidthSegment = (return null;}- const width = videoSegment.children.find((b) => b.type === 'PixelWidth');+ const width = videoSegment.value.find((b) => b.type === 'PixelWidth');if (!width || width.type !== 'PixelWidth') {return null;@@ -384,7 +384,7 @@ export const getHeightSegment = (return null;}- const height = videoSegment.children.find((b) => b.type === 'PixelHeight');+ const height = videoSegment.value.find((b) => b.type === 'PixelHeight');if (!height || height.type !== 'PixelHeight') {return null;@@ -401,7 +401,7 @@ export const getDisplayWidthSegment = (return null;}- const displayWidth = videoSegment.children.find(+ const displayWidth = videoSegment.value.find((b) => b.type === 'DisplayWidth',);@@ -420,7 +420,7 @@ export const getDisplayHeightSegment = (return null;}- const displayHeight = videoSegment.children.find(+ const displayHeight = videoSegment.value.find((b) => b.type === 'DisplayHeight',);