Skip to content

🎥 Make videos programmatically with React

more stuff goes into generic EBML parser

Jonny Burgerc5fd0e74c753
diff --git a/packages/media-parser/src/boxes/webm/get-track.ts b/packages/media-parser/src/boxes/webm/get-track.ts
index 71e90a096b..4846adbcbf 100644
--- a/packages/media-parser/src/boxes/webm/get-track.ts
+++ b/packages/media-parser/src/boxes/webm/get-track.ts
@@ -211,8 +211,8 @@ export const getTrack = ({
trackId,
codec: codecString,
description: getDescription(track),
- height: displayHeight ? displayHeight.displayHeight : height.value,
- width: displayWidth ? displayWidth.displayWidth : width.value,
+ height: displayHeight ? displayHeight.value : height.value,
+ width: displayWidth ? displayWidth.value : width.value,
sampleAspectRatio: {
numerator: 1,
denominator: 1,
@@ -221,12 +221,8 @@ export const getTrack = ({
samplePositions: [],
codedHeight: height.value,
codedWidth: width.value,
- displayAspectHeight: displayHeight
- ? displayHeight.displayHeight
- : height.value,
- displayAspectWidth: displayWidth
- ? displayWidth.displayWidth
- : width.value,
+ displayAspectHeight: displayHeight ? displayHeight.value : height.value,
+ displayAspectWidth: displayWidth ? displayWidth.value : width.value,
rotation: 0,
};
}
diff --git a/packages/media-parser/src/boxes/webm/segments.ts b/packages/media-parser/src/boxes/webm/segments.ts
index d355d2b369..932c9b7d56 100644
--- a/packages/media-parser/src/boxes/webm/segments.ts
+++ b/packages/media-parser/src/boxes/webm/segments.ts
@@ -11,7 +11,6 @@ import {type MainSegment} from './segments/main';
import {expectChildren} from './segments/parse-children';
import type {
AudioSegment,
- BitDepthSegment,
BlockAdditionsSegment,
BlockElement,
BlockGroupSegment,
@@ -20,8 +19,6 @@ import type {
ColorSegment,
DefaultDurationSegment,
DefaultFlagSegment,
- DisplayHeightSegment,
- DisplayWidthSegment,
FlagLacingSegment,
LanguageSegment,
MaxBlockAdditionId,
@@ -37,7 +34,6 @@ import type {
} from './segments/track-entry';
import {
parseAudioSegment,
- parseBitDepthSegment,
parseBlockAdditionsSegment,
parseBlockElementSegment,
parseBlockGroupSegment,
@@ -45,8 +41,6 @@ import {
parseColorSegment,
parseDefaultDurationSegment,
parseDefaultFlagSegment,
- parseDisplayHeightSegment,
- parseDisplayWidthSegment,
parseFlagLacing,
parseLanguageSegment,
parseMaxBlockAdditionId,
@@ -73,8 +67,6 @@ export type MatroskaSegment =
| LanguageSegment
| DefaultDurationSegment
| VideoSegment
- | DisplayWidthSegment
- | DisplayHeightSegment
| MaxBlockAdditionId
| ColorSegment
| CodecPrivateSegment
@@ -87,7 +79,6 @@ export type MatroskaSegment =
| BlockGroupSegment
| BlockElement
| AudioSegment
- | BitDepthSegment
| ReferenceBlockSegment
| BlockAdditionsSegment
| PossibleEbml;
@@ -172,14 +163,6 @@ const parseSegment = async ({
return parseAudioSegment(iterator, length, parserContext);
}
- if (segmentId === matroskaElements.DisplayWidth) {
- return parseDisplayWidthSegment(iterator, length);
- }
-
- if (segmentId === matroskaElements.DisplayHeight) {
- return parseDisplayHeightSegment(iterator, length);
- }
-
if (segmentId === '0x63a2') {
return parseCodecPrivateSegment(iterator, length);
}
@@ -196,10 +179,6 @@ const parseSegment = async ({
return parseTagSegment(iterator, length);
}
- if (segmentId === matroskaElements.BitDepth) {
- return parseBitDepthSegment(iterator, length);
- }
-
if (segmentId === matroskaElements.Timestamp) {
const offset = iterator.counter.getOffset();
const timestampSegment = parseTimestampSegment(iterator, length);
diff --git a/packages/media-parser/src/boxes/webm/segments/all-segments.ts b/packages/media-parser/src/boxes/webm/segments/all-segments.ts
index a10938e790..302d6b6189 100644
--- a/packages/media-parser/src/boxes/webm/segments/all-segments.ts
+++ b/packages/media-parser/src/boxes/webm/segments/all-segments.ts
@@ -513,12 +513,29 @@ export const interlaced = {
type: 'uint',
} as const satisfies Ebml;
+export const bitDepth = {
+ name: 'BitDepth',
+ type: 'uint',
+} as const satisfies Ebml;
+
+export const displayWidth = {
+ name: 'DisplayWidth',
+ type: 'uint',
+} as const satisfies Ebml;
+
+export const displayHeight = {
+ name: 'DisplayHeight',
+ type: 'uint',
+} 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 DisplayWidthSegment = EbmlParsed<typeof displayWidth>;
+export type DisplayHeightSegment = EbmlParsed<typeof displayHeight>;
export type EbmlValue<T extends Ebml> = T extends EbmlWithUint8
? number
@@ -620,6 +637,9 @@ export const ebmlMap = {
[matroskaElements.Channels]: channels,
[matroskaElements.AlphaMode]: alphaMode,
[matroskaElements.FlagInterlaced]: interlaced,
+ [matroskaElements.BitDepth]: bitDepth,
+ [matroskaElements.DisplayHeight]: displayHeight,
+ [matroskaElements.DisplayWidth]: displayWidth,
} 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.ts
index 675da9a7d5..7682ef6640 100644
--- a/packages/media-parser/src/boxes/webm/segments/track-entry.ts
+++ b/packages/media-parser/src/boxes/webm/segments/track-entry.ts
@@ -212,52 +212,6 @@ export const parseAudioSegment = async (
};
};
-export type DisplayWidthSegment = {
- type: 'display-width-segment';
- displayWidth: number;
-};
-
-export const parseDisplayWidthSegment = (
- iterator: BufferIterator,
- length: number,
-): DisplayWidthSegment => {
- if (length !== 2) {
- throw new Error(
- `Expected display width segment to be 2 bytes, got ${length}`,
- );
- }
-
- const displayWidth = iterator.getUint16();
-
- return {
- type: 'display-width-segment',
- displayWidth,
- };
-};
-
-export type DisplayHeightSegment = {
- type: 'display-height-segment';
- displayHeight: number;
-};
-
-export const parseDisplayHeightSegment = (
- iterator: BufferIterator,
- length: number,
-): DisplayHeightSegment => {
- if (length !== 2) {
- throw new Error(
- `Expected display height segment to be 2 bytes, got ${length}`,
- );
- }
-
- const displayHeight = iterator.getUint16();
-
- return {
- type: 'display-height-segment',
- displayHeight,
- };
-};
-
export type MaxBlockAdditionId = {
type: 'max-block-addition-id-segment';
maxBlockAdditionId: number;
@@ -620,26 +574,3 @@ export const parseBlockElementSegment = (
length,
};
};
-
-export type BitDepthSegment = {
- type: 'bit-depth-segment';
- bitDepth: number;
-};
-
-export const parseBitDepthSegment = (
- iterator: BufferIterator,
- length: number,
-): BitDepthSegment => {
- if (length !== 1) {
- throw new Error(
- `Expected length of bit depth segment to be 1, got ${length}`,
- );
- }
-
- const bitDepth = iterator.getUint8();
-
- return {
- type: 'bit-depth-segment',
- bitDepth,
- };
-};
diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.ts
index 5bab913d14..ffa2091523 100644
--- a/packages/media-parser/src/traversal.ts
+++ b/packages/media-parser/src/traversal.ts
@@ -13,6 +13,8 @@ import type {TkhdBox} from './boxes/iso-base-media/tkhd';
import type {TrakBox} from './boxes/iso-base-media/trak/trak';
import type {
CodecIdSegment,
+ DisplayHeightSegment,
+ DisplayWidthSegment,
HeightSegment,
TimestampScaleSegment,
TrackTypeSegment,
@@ -22,8 +24,6 @@ import type {MainSegment} from './boxes/webm/segments/main';
import type {
AudioSegment,
ClusterSegment,
- DisplayHeightSegment,
- DisplayWidthSegment,
TrackEntrySegment,
VideoSegment,
} from './boxes/webm/segments/track-entry';
@@ -340,15 +340,13 @@ export const getBitDepth = (track: TrackEntrySegment): number | null => {
return null;
}
- const bitDepth = audioSegment.children.find(
- (b) => b.type === 'bit-depth-segment',
- );
+ const bitDepth = audioSegment.children.find((b) => b.type === 'BitDepth');
- if (!bitDepth || bitDepth.type !== 'bit-depth-segment') {
+ if (!bitDepth || bitDepth.type !== 'BitDepth') {
return null;
}
- return bitDepth.bitDepth;
+ return bitDepth.value;
};
export const getPrivateData = (track: TrackEntrySegment): Uint8Array | null => {
@@ -406,10 +404,10 @@ export const getDisplayWidthSegment = (
}
const displayWidth = videoSegment.children.find(
- (b) => b.type === 'display-width-segment',
+ (b) => b.type === 'DisplayWidth',
);
- if (!displayWidth || displayWidth.type !== 'display-width-segment') {
+ if (!displayWidth || displayWidth.type !== 'DisplayWidth') {
return null;
}
@@ -425,10 +423,10 @@ export const getDisplayHeightSegment = (
}
const displayHeight = videoSegment.children.find(
- (b) => b.type === 'display-height-segment',
+ (b) => b.type === 'DisplayHeight',
);
- if (!displayHeight || displayHeight.type !== 'display-height-segment') {
+ if (!displayHeight || displayHeight.type !== 'DisplayHeight') {
return null;
}