Skip to content

🎥 Make videos programmatically with React

tighter types

Jonny Burger0c0d84e45886
diff --git a/packages/media-parser/src/boxes/webm/parse-ebml.ts b/packages/media-parser/src/boxes/webm/parse-ebml.ts
index 9042c92d82..c4343b9f83 100644
--- a/packages/media-parser/src/boxes/webm/parse-ebml.ts
+++ b/packages/media-parser/src/boxes/webm/parse-ebml.ts
@@ -1,19 +1,12 @@
import type {BufferIterator} from '../../buffer-iterator';
-import type {
- EbmlWithFloat,
- EbmlWithString,
- EbmlWithUint8,
- EbmlWithVoid,
-} from './segments/all-segments';
+import type {PossibleEbml} from './segments/all-segments';
import {ebmlMap, type Ebml, type EbmlParsed} from './segments/all-segments';
type Prettify<T> = {
[K in keyof T]: T[K];
} & {};
-export const parseEbml = (
- iterator: BufferIterator,
-): Prettify<EbmlParsed<Ebml>> => {
+export const parseEbml = (iterator: BufferIterator): Prettify<PossibleEbml> => {
const hex = iterator.getMatroskaSegmentId();
if (hex === null) {
throw new Error(
@@ -38,7 +31,7 @@ export const parseEbml = (
if (hasInMap.type === 'uint-8') {
const value = iterator.getUint8();
- return {type: hasInMap.name, value, hex} as EbmlParsed<EbmlWithUint8>;
+ return {type: hasInMap.name, value, hex};
}
if (hasInMap.type === 'string') {
@@ -48,7 +41,7 @@ export const parseEbml = (
type: hasInMap.name,
value,
hex,
- } as EbmlParsed<EbmlWithString>;
+ };
}
if (hasInMap.type === 'float') {
@@ -58,7 +51,7 @@ export const parseEbml = (
type: hasInMap.name,
value,
hex,
- } as EbmlParsed<EbmlWithFloat>;
+ };
}
if (hasInMap.type === 'void') {
@@ -68,7 +61,7 @@ export const parseEbml = (
type: hasInMap.name,
value: undefined,
hex,
- } as EbmlParsed<EbmlWithVoid>;
+ };
}
if (hasInMap.type === 'children') {
diff --git a/packages/media-parser/src/boxes/webm/segments.ts b/packages/media-parser/src/boxes/webm/segments.ts
index 7347871b38..07b100e43c 100644
--- a/packages/media-parser/src/boxes/webm/segments.ts
+++ b/packages/media-parser/src/boxes/webm/segments.ts
@@ -5,7 +5,7 @@ import type {ParserContext} from '../../parser-context';
import type {VideoSample} from '../../webcodec-sample-types';
import {getTrack} from './get-track';
import {parseEbml} from './parse-ebml';
-import type {Ebml, EbmlParsed} from './segments/all-segments';
+import type {PossibleEbml} from './segments/all-segments';
import {matroskaElements} from './segments/all-segments';
import type {DurationSegment} from './segments/duration';
import {parseDurationSegment} from './segments/duration';
@@ -156,7 +156,7 @@ export type MatroskaSegment =
| BitDepthSegment
| ReferenceBlockSegment
| BlockAdditionsSegment
- | EbmlParsed<Ebml>;
+ | PossibleEbml;
export type OnTrackEntrySegment = (trackEntry: TrackEntrySegment) => void;
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 376d57dcee..1495f043d1 100644
--- a/packages/media-parser/src/boxes/webm/segments/all-segments.ts
+++ b/packages/media-parser/src/boxes/webm/segments/all-segments.ts
@@ -427,7 +427,7 @@ export type EbmlParsed<T extends Ebml> = {
hex: string;
};
-export const ebmlMap: Partial<Record<MatroskaElement, Ebml>> = {
+export const ebmlMap = {
[matroskaElements.Header]: matroskaHeader,
[matroskaElements.DocType]: docType,
[matroskaElements.DocTypeVersion]: docTypeVersion,
@@ -477,4 +477,12 @@ export const ebmlMap: Partial<Record<MatroskaElement, Ebml>> = {
name: 'SliceDuration' as const,
type: 'void',
},
-};
+} 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];
diff --git a/packages/media-parser/src/boxes/webm/traversal.ts b/packages/media-parser/src/boxes/webm/traversal.ts
index 14f4abf1a3..7e478ea102 100644
--- a/packages/media-parser/src/boxes/webm/traversal.ts
+++ b/packages/media-parser/src/boxes/webm/traversal.ts
@@ -34,7 +34,7 @@ export const getTrackTimestampScale = (track: TrackEntrySegment) => {
throw new Error('Expected TrackTimestampScale');
}
- return child.value as number;
+ return child.value;
};
export const getTrackByNumber = (tracks: TrackEntrySegment[], id: number) => {