Skip to content

🎥 Make videos programmatically with React

av1 codec string

Jonny Burger786673483696
diff --git a/packages/media-parser/src/av1-codec-string.ts b/packages/media-parser/src/av1-codec-string.ts
index 929bddcbff..035fdd344d 100644
--- a/packages/media-parser/src/av1-codec-string.ts
+++ b/packages/media-parser/src/av1-codec-string.ts
@@ -5,6 +5,7 @@ import type {
TrackEntrySegment,
} from './boxes/webm/segments/track-entry';
import {getAv1BitstreamHeader} from './boxes/webm/traversal';
+import {getCodecSegment} from './traversal';
export const av1CodecStringToString = ({
track,
@@ -13,33 +14,22 @@ export const av1CodecStringToString = ({
track: TrackEntrySegment;
clusterSegment: ClusterSegment;
}): string => {
- const codecSegment = track.children.find((b) => b.type === 'codec-segment');
+ const codecSegment = getCodecSegment(track);
- if (!codecSegment || codecSegment.type !== 'codec-segment') {
+ if (!codecSegment) {
throw new Error('Expected codec segment');
}
if (codecSegment.codec !== 'V_AV1') {
- throw new Error(`Unknown codec: ${codecSegment.codec}`);
- }
-
- const simpleBlockSegment = clusterSegment.children.find((b) => {
- if (b.type !== 'simple-block-segment') {
- return false;
- }
-
- const child = b.children.find((c) => c.type === 'av1-bitstream-header');
- return child;
- });
-
- if (
- !simpleBlockSegment ||
- simpleBlockSegment.type !== 'simple-block-segment'
- ) {
- throw new Error('Could not find simple block segment');
+ throw new Error(
+ `Should not call this function if it is not AV1: ${codecSegment.codec}`,
+ );
}
const av1BitstreamHeader = getAv1BitstreamHeader(clusterSegment);
+ if (!av1BitstreamHeader) {
+ throw new Error('Could not find av1 bitstream header');
+ }
let str = 'av01.';
diff --git a/packages/media-parser/src/boxes/iso-base-media/make-track.ts b/packages/media-parser/src/boxes/iso-base-media/make-track.ts
new file mode 100644
index 0000000000..3a8d3793d1
--- /dev/null
+++ b/packages/media-parser/src/boxes/iso-base-media/make-track.ts
@@ -0,0 +1,117 @@
+import {getAudioCodecStringFromTrak} from '../../get-audio-codec';
+import {
+ getTimescaleAndDuration,
+ trakBoxContainsAudio,
+ trakBoxContainsVideo,
+} from '../../get-fps';
+import {
+ applyAspectRatios,
+ getDisplayAspectRatio,
+ getSampleAspectRatio,
+ getVideoSample,
+} from '../../get-sample-aspect-ratio';
+import {getSamplePositions} from '../../get-sample-positions';
+import type {AudioTrack, VideoTrack} from '../../get-tracks';
+import {getVideoCodecString} from '../../get-video-codec';
+import {
+ getCttsBox,
+ getStcoBox,
+ getStscBox,
+ getStssBox,
+ getStszBox,
+ getSttsBox,
+ getTkhdBox,
+ getVideoDescriptors,
+} from '../../traversal';
+import type {TrakBox} from './trak/trak';
+
+export const makeBaseMediaTrack = (
+ trakBox: TrakBox,
+): VideoTrack | AudioTrack | null => {
+ const stszBox = getStszBox(trakBox);
+ const stcoBox = getStcoBox(trakBox);
+ const stscBox = getStscBox(trakBox);
+ const stssBox = getStssBox(trakBox);
+ const sttsBox = getSttsBox(trakBox);
+ const tkhdBox = getTkhdBox(trakBox);
+ const cttsBox = getCttsBox(trakBox);
+ const videoDescriptors = getVideoDescriptors(trakBox);
+ const timescaleAndDuration = getTimescaleAndDuration(trakBox);
+
+ if (!tkhdBox) {
+ throw new Error('Expected tkhd box in trak box');
+ }
+
+ if (!stszBox) {
+ throw new Error('Expected stsz box in trak box');
+ }
+
+ if (!stcoBox) {
+ throw new Error('Expected stco box in trak box');
+ }
+
+ if (!stscBox) {
+ throw new Error('Expected stsc box in trak box');
+ }
+
+ if (!sttsBox) {
+ throw new Error('Expected stts box in trak box');
+ }
+
+ if (!timescaleAndDuration) {
+ throw new Error('Expected timescale and duration in trak box');
+ }
+
+ const samplePositions = getSamplePositions({
+ stcoBox,
+ stscBox,
+ stszBox,
+ stssBox,
+ sttsBox,
+ cttsBox,
+ });
+
+ if (trakBoxContainsAudio(trakBox)) {
+ return {
+ type: 'audio',
+ samplePositions,
+ trackId: tkhdBox.trackId,
+ timescale: timescaleAndDuration.timescale,
+ codecString: getAudioCodecStringFromTrak(trakBox),
+ };
+ }
+
+ if (!trakBoxContainsVideo(trakBox)) {
+ return null;
+ }
+
+ const videoSample = getVideoSample(trakBox);
+ if (!videoSample) {
+ throw new Error('No video sample');
+ }
+
+ const sampleAspectRatio = getSampleAspectRatio(trakBox);
+
+ const applied = applyAspectRatios({
+ dimensions: videoSample,
+ sampleAspectRatio,
+ displayAspectRatio: getDisplayAspectRatio({
+ sampleAspectRatio,
+ nativeDimensions: videoSample,
+ }),
+ });
+
+ return {
+ type: 'video',
+ samplePositions,
+ trackId: tkhdBox.trackId,
+ description: videoDescriptors,
+ timescale: timescaleAndDuration.timescale,
+ codecString: getVideoCodecString(trakBox),
+ sampleAspectRatio: getSampleAspectRatio(trakBox),
+ width: applied.width,
+ height: applied.height,
+ untransformedWidth: videoSample.width,
+ untransformedHeight: videoSample.height,
+ } as VideoTrack;
+};
diff --git a/packages/media-parser/src/boxes/iso-base-media/process-box.ts b/packages/media-parser/src/boxes/iso-base-media/process-box.ts
index b3714af96a..941e4694c0 100644
--- a/packages/media-parser/src/boxes/iso-base-media/process-box.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/process-box.ts
@@ -10,6 +10,7 @@ import type {BoxAndNext} from '../../parse-video';
import type {ParserContext} from '../../parser-context';
import {parseEsds} from './esds/esds';
import {parseFtyp} from './ftyp';
+import {makeBaseMediaTrack} from './make-track';
import {parseMdat} from './mdat/mdat';
import {parseMdhd} from './mdhd';
import {parseMoov} from './moov/moov';
@@ -318,6 +319,16 @@ const processBox = ({
offsetAtStart: fileOffset,
options,
});
+ const transformedTrack = makeBaseMediaTrack(box);
+ if (transformedTrack) {
+ if (transformedTrack.type === 'audio') {
+ options.onAudioTrack?.(transformedTrack);
+ }
+
+ if (transformedTrack.type === 'video') {
+ options.onVideoTrack?.(transformedTrack);
+ }
+ }
return {
type: 'complete',
diff --git a/packages/media-parser/src/boxes/webm/get-ready-tracks.ts b/packages/media-parser/src/boxes/webm/get-ready-tracks.ts
new file mode 100644
index 0000000000..90bce71c80
--- /dev/null
+++ b/packages/media-parser/src/boxes/webm/get-ready-tracks.ts
@@ -0,0 +1,151 @@
+import {av1CodecStringToString} from '../../av1-codec-string';
+import type {AudioTrack, VideoTrack} from '../../get-tracks';
+import {
+ getClusterSegment,
+ getHeightSegment,
+ getTimescaleSegment,
+ getTrackId,
+ getTrackTypeSegment,
+ getTracksSegment,
+ getWidthSegment,
+} from '../../traversal';
+import type {MainSegment} from './segments/main';
+import type {ClusterSegment, TrackEntrySegment} from './segments/track-entry';
+
+const getMatroskaVideoCodecString = (
+ track: TrackEntrySegment,
+ cluster: ClusterSegment | null,
+): string => {
+ const codec = track.children.find((b) => b.type === 'codec-segment');
+
+ if (!codec || codec.type !== 'codec-segment') {
+ throw new Error('Expected codec segment');
+ }
+
+ if (codec.codec === 'V_VP8') {
+ return 'vp8';
+ }
+
+ if (codec.codec === 'V_VP9') {
+ return 'vp9';
+ }
+
+ if (codec.codec === 'V_MPEG4/ISO/AVC') {
+ // TODO: different avc1!
+ // Failing test: Should stream MKV video
+ return 'avc1';
+ }
+
+ if (codec.codec === 'V_AV1') {
+ if (!cluster) {
+ throw new Error('No cluster segment');
+ }
+
+ return av1CodecStringToString({track, clusterSegment: cluster});
+ }
+
+ throw new Error(`Unknown codec: ${codec.codec}`);
+};
+
+const getMatroskaAudioCodecString = (track: TrackEntrySegment): string => {
+ const codec = track.children.find((b) => b.type === 'codec-segment');
+
+ if (!codec || codec.type !== 'codec-segment') {
+ throw new Error('Expected codec segment');
+ }
+
+ if (codec.codec === 'A_OPUS') {
+ return 'opus';
+ }
+
+ // TODO: Wrong, see here how to parse it correctly
+ if (codec.codec === 'A_PCM/INT/LIT') {
+ return 'pcm-s16';
+ }
+
+ throw new Error(`Unknown codec: ${codec.codec}`);
+};
+
+export const getTracksFromMatroska = (
+ segment: MainSegment,
+): {videoTracks: VideoTrack[]; audioTracks: AudioTrack[]} => {
+ const tracksSegment = getTracksSegment(segment);
+ if (!tracksSegment) {
+ throw new Error('No tracks segment');
+ }
+
+ const timescale = getTimescaleSegment(segment);
+
+ if (!timescale) {
+ throw new Error('No timescale segment');
+ }
+
+ const clusterSegment = getClusterSegment(segment);
+
+ const videoTracks: VideoTrack[] = [];
+ const audioTracks: AudioTrack[] = [];
+
+ for (const track of tracksSegment.children) {
+ if (track.type === 'crc32-segment') {
+ continue;
+ }
+
+ if (track.type !== 'track-entry-segment') {
+ throw new Error('Expected track entry segment');
+ }
+
+ const trackType = getTrackTypeSegment(track);
+
+ if (!trackType) {
+ throw new Error('Expected track type segment');
+ }
+
+ const trackId = getTrackId(track);
+
+ if (trackType.trackType === 'video') {
+ const width = getWidthSegment(track);
+
+ if (width === null) {
+ throw new Error('Expected width segment');
+ }
+
+ const height = getHeightSegment(track);
+
+ if (height === null) {
+ throw new Error('Expected height segment');
+ }
+
+ videoTracks.push({
+ type: 'video',
+ trackId,
+ codecString: getMatroskaVideoCodecString(track, clusterSegment ?? null),
+ description: null,
+ height: height.height,
+ width: width.width,
+ sampleAspectRatio: {
+ numerator: 1,
+ denominator: 1,
+ },
+ timescale: timescale.timestampScale,
+ samplePositions: [],
+ untransformedHeight: height.height,
+ untransformedWidth: width.width,
+ });
+ }
+
+ if (trackType.trackType === 'audio') {
+ audioTracks.push({
+ type: 'audio',
+ trackId,
+ codecString: getMatroskaAudioCodecString(track),
+ samplePositions: null,
+ timescale: timescale.timestampScale,
+ });
+ }
+ }
+
+ return {
+ videoTracks,
+ audioTracks,
+ };
+};
diff --git a/packages/media-parser/src/boxes/webm/traversal.ts b/packages/media-parser/src/boxes/webm/traversal.ts
index 5f80a7ab59..01a1c08aef 100644
--- a/packages/media-parser/src/boxes/webm/traversal.ts
+++ b/packages/media-parser/src/boxes/webm/traversal.ts
@@ -29,7 +29,7 @@ export const getTrackByNumber = (tracks: TrackEntrySegment[], id: number) => {
export const getAv1BitstreamHeader = (
clusterSegment: ClusterSegment,
-): Av1BitstreamHeaderSegment => {
+): Av1BitstreamHeaderSegment | null => {
const simpleBlockSegment = clusterSegment.children.find((b) => {
if (b.type !== 'simple-block-segment') {
return false;
diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.ts
index 80d73e34b1..fb1251f654 100644
--- a/packages/media-parser/src/get-tracks.ts
+++ b/packages/media-parser/src/get-tracks.ts
@@ -1,40 +1,9 @@
-import {av1CodecStringToString} from './av1-codec-string';
+import {makeBaseMediaTrack} from './boxes/iso-base-media/make-track';
import type {MoovBox} from './boxes/iso-base-media/moov/moov';
-import type {MainSegment} from './boxes/webm/segments/main';
-import type {
- ClusterSegment,
- TrackEntrySegment,
-} from './boxes/webm/segments/track-entry';
-import {getAudioCodecStringFromTrak} from './get-audio-codec';
-import {
- getTimescaleAndDuration,
- trakBoxContainsAudio,
- trakBoxContainsVideo,
-} from './get-fps';
-import {
- applyAspectRatios,
- getDisplayAspectRatio,
- getSampleAspectRatio,
- getVideoSample,
-} from './get-sample-aspect-ratio';
+import {getTracksFromMatroska} from './boxes/webm/get-ready-tracks';
import type {SamplePosition} from './get-sample-positions';
-import {getSamplePositions} from './get-sample-positions';
-import {getVideoCodecString} from './get-video-codec';
import type {AnySegment} from './parse-result';
-import {
- getCttsBox,
- getFtypBox,
- getMoovBox,
- getMvhdBox,
- getStcoBox,
- getStscBox,
- getStssBox,
- getStszBox,
- getSttsBox,
- getTkhdBox,
- getTraks,
- getVideoDescriptors,
-} from './traversal';
+import {getFtypBox, getMoovBox, getMvhdBox, getTraks} from './traversal';
type SampleAspectRatio = {
numerator: number;
@@ -94,176 +63,6 @@ export const hasTracks = (segments: AnySegment[]): boolean => {
return tracks.length === numberOfTracks;
};
-const getMatroskaVideoCodecString = (
- track: TrackEntrySegment,
- cluster: ClusterSegment | null,
-): string => {
- const codec = track.children.find((b) => b.type === 'codec-segment');
-
- if (!codec || codec.type !== 'codec-segment') {
- throw new Error('Expected codec segment');
- }
-
- if (codec.codec === 'V_VP8') {
- return 'vp8';
- }
-
- if (codec.codec === 'V_VP9') {
- return 'vp9';
- }
-
- if (codec.codec === 'V_MPEG4/ISO/AVC') {
- // TODO: different avc1!
- // Failing test: Should stream MKV video
- return 'avc1';
- }
-
- if (codec.codec === 'V_AV1') {
- if (!cluster) {
- throw new Error('No cluster segment');
- }
-
- return av1CodecStringToString({track, clusterSegment: cluster});
- }
-
- throw new Error(`Unknown codec: ${codec.codec}`);
-};
-
-const getMatroskaAudioCodecString = (track: TrackEntrySegment): string => {
- const codec = track.children.find((b) => b.type === 'codec-segment');
-
- if (!codec || codec.type !== 'codec-segment') {
- throw new Error('Expected codec segment');
- }
-
- if (codec.codec === 'A_OPUS') {
- return 'opus';
- }
-
- // TODO: Wrong, see here how to parse it correctly
- if (codec.codec === 'A_PCM/INT/LIT') {
- return 'pcm-s16';
- }
-
- throw new Error(`Unknown codec: ${codec.codec}`);
-};
-
-const getTracksFromMatroska = (
- segment: MainSegment,
-): {videoTracks: VideoTrack[]; audioTracks: AudioTrack[]} => {
- const tracksSegment = segment.children.find(
- (b) => b.type === 'tracks-segment',
- );
- if (!tracksSegment || tracksSegment.type !== 'tracks-segment') {
- throw new Error('No tracks segment');
- }
-
- const infoSegment = segment.children.find((b) => b.type === 'info-segment');
-
- if (!infoSegment || infoSegment.type !== 'info-segment') {
- throw new Error('No info segment');
- }
-
- const timescale = infoSegment.children.find(
- (b) => b.type === 'timestamp-scale-segment',
- );
-
- if (!timescale || timescale.type !== 'timestamp-scale-segment') {
- throw new Error('No timescale segment');
- }
-
- const clusterSegment = segment.children.find(
- (b) => b.type === 'cluster-segment',
- ) as ClusterSegment | undefined;
-
- const videoTracks: VideoTrack[] = [];
- const audioTracks: AudioTrack[] = [];
-
- for (const track of tracksSegment.children) {
- if (track.type === 'crc32-segment') {
- continue;
- }
-
- if (track.type !== 'track-entry-segment') {
- throw new Error('Expected track entry segment');
- }
-
- const trackType = track.children.find(
- (b) => b.type === 'track-type-segment',
- );
-
- if (!trackType || trackType.type !== 'track-type-segment') {
- throw new Error('Expected track type segment');
- }
-
- const trackId = track.children.find(
- (b) => b.type === 'track-number-segment',
- );
-
- if (!trackId || trackId.type !== 'track-number-segment') {
- throw new Error('Expected track id segment');
- }
-
- if (trackType.trackType === 'video') {
- const videoSegment = track.children.find(
- (b) => b.type === 'video-segment',
- );
-
- if (!videoSegment || videoSegment.type !== 'video-segment') {
- throw new Error('Expected video segment');
- }
-
- const width = videoSegment.children.find(
- (b) => b.type === 'width-segment',
- );
-
- if (!width || width.type !== 'width-segment') {
- throw new Error('Expected width segment');
- }
-
- const height = videoSegment.children.find(
- (b) => b.type === 'height-segment',
- );
-
- if (!height || height.type !== 'height-segment') {
- throw new Error('Expected height segment');
- }
-
- videoTracks.push({
- type: 'video',
- trackId: trackId.trackNumber,
- codecString: getMatroskaVideoCodecString(track, clusterSegment ?? null),
- description: null,
- height: height.height,
- width: width.width,
- sampleAspectRatio: {
- numerator: 1,
- denominator: 1,
- },
- timescale: timescale.timestampScale,
- samplePositions: [],
- untransformedHeight: height.height,
- untransformedWidth: width.width,
- });
- }
-
- if (trackType.trackType === 'audio') {
- audioTracks.push({
- type: 'audio',
- trackId: trackId.trackNumber,
- codecString: getMatroskaAudioCodecString(track),
- samplePositions: null,
- timescale: timescale.timestampScale,
- });
- }
- }
-
- return {
- videoTracks,
- audioTracks,
- };
-};
-
export const getTracks = (
segments: AnySegment[],
): {
@@ -287,90 +86,18 @@ export const getTracks = (
const audioTracks: AudioTrack[] = [];
const tracks = getTraks(moovBox);
- for (const track of tracks) {
- const stszBox = getStszBox(track);
- const stcoBox = getStcoBox(track);
- const stscBox = getStscBox(track);
- const stssBox = getStssBox(track);
- const sttsBox = getSttsBox(track);
- const tkhdBox = getTkhdBox(track);
- const cttsBox = getCttsBox(track);
- const videoDescriptors = getVideoDescriptors(track);
- const timescaleAndDuration = getTimescaleAndDuration(track);
-
- if (!tkhdBox) {
- throw new Error('Expected tkhd box in trak box');
- }
-
- if (!stszBox) {
- throw new Error('Expected stsz box in trak box');
- }
-
- if (!stcoBox) {
- throw new Error('Expected stco box in trak box');
- }
-
- if (!stscBox) {
- throw new Error('Expected stsc box in trak box');
- }
-
- if (!sttsBox) {
- throw new Error('Expected stts box in trak box');
- }
-
- if (!timescaleAndDuration) {
- throw new Error('Expected timescale and duration in trak box');
+ for (const trakBox of tracks) {
+ const track = makeBaseMediaTrack(trakBox);
+ if (!track) {
+ continue;
}
- const samplePositions = getSamplePositions({
- stcoBox,
- stscBox,
- stszBox,
- stssBox,
- sttsBox,
- cttsBox,
- });
-
- if (trakBoxContainsAudio(track)) {
- audioTracks.push({
- type: 'audio',
- samplePositions,
- trackId: tkhdBox.trackId,
- timescale: timescaleAndDuration.timescale,
- codecString: getAudioCodecStringFromTrak(track),
- });
+ if (track.type === 'video') {
+ videoTracks.push(track);
}
- if (trakBoxContainsVideo(track)) {
- const videoSample = getVideoSample(track);
- if (!videoSample) {
- throw new Error('No video sample');
- }
-
- const sampleAspectRatio = getSampleAspectRatio(track);
-
- const applied = applyAspectRatios({
- dimensions: videoSample,
- sampleAspectRatio,
- displayAspectRatio: getDisplayAspectRatio({
- sampleAspectRatio,
- nativeDimensions: videoSample,
- }),
- });
-
- videoTracks.push({
- type: 'video',
- samplePositions,
- trackId: tkhdBox.trackId,
- description: videoDescriptors,
- timescale: timescaleAndDuration.timescale,
- codecString: getVideoCodecString(track),
- sampleAspectRatio: getSampleAspectRatio(track),
- width: applied.width,
- height: applied.height,
- untransformedWidth: videoSample.width,
- untransformedHeight: videoSample.height,
- });
+ if (track.type === 'audio') {
+ audioTracks.push(track);
}
}
diff --git a/packages/media-parser/src/options.ts b/packages/media-parser/src/options.ts
index d198a2cf5e..b14a3283e3 100644
--- a/packages/media-parser/src/options.ts
+++ b/packages/media-parser/src/options.ts
@@ -23,6 +23,9 @@ export type KnownAudioCodecs =
| 'pcm'
| 'unknown';
+export type OnAudioTrack = (track: AudioTrack) => void;
+export type OnVideoTrack = (track: VideoTrack) => void;
+
export type Options<
EnableDimensions extends boolean,
EnableDuration extends boolean,
@@ -95,6 +98,8 @@ export type ParseMedia = <
reader?: ReaderInterface;
onAudioSample?: OnAudioSample;
onVideoSample?: OnVideoSample;
+ onAudioTrack?: OnAudioTrack;
+ onVideoTrack?: OnVideoTrack;
}) => Promise<
Metadata<
EnableDimensions,
diff --git a/packages/media-parser/src/parse-media.ts b/packages/media-parser/src/parse-media.ts
index eb9f2a478b..5f90ee1589 100644
--- a/packages/media-parser/src/parse-media.ts
+++ b/packages/media-parser/src/parse-media.ts
@@ -20,6 +20,8 @@ export const parseMedia: ParseMedia = async ({
reader: readerInterface = fetchReader,
onAudioSample,
onVideoSample,
+ onAudioTrack,
+ onVideoTrack,
}) => {
const {reader, contentLength} = await readerInterface.read(src, null);
let currentReader = reader;
@@ -69,9 +71,16 @@ export const parseMedia: ParseMedia = async ({
parseResult = parseVideo({
iterator,
options: {
- canSkipVideoData: !(onAudioSample || onVideoSample),
+ canSkipVideoData: !(
+ onAudioSample ||
+ onVideoSample ||
+ onAudioTrack ||
+ onVideoTrack
+ ),
onAudioSample: onAudioSample ?? null,
onVideoSample: onVideoSample ?? null,
+ onAudioTrack: onAudioTrack ?? null,
+ onVideoTrack: onVideoTrack ?? null,
onTrackEntrySegment,
getTracks: () => trackEntries,
// TODO: Skip frames if onSimpleBlock is null
diff --git a/packages/media-parser/src/parser-context.ts b/packages/media-parser/src/parser-context.ts
index eccef5ea40..07aa51fe36 100644
--- a/packages/media-parser/src/parser-context.ts
+++ b/packages/media-parser/src/parser-context.ts
@@ -4,10 +4,13 @@ import type {
} from './boxes/iso-base-media/mdat/mdat';
import type {OnTrackEntrySegment} from './boxes/webm/segments';
import type {GetTracks} from './boxes/webm/segments/track-entry';
+import type {OnAudioTrack, OnVideoTrack} from './options';
export type ParserContext = {
onAudioSample: OnAudioSample | null;
onVideoSample: OnVideoSample | null;
+ onAudioTrack: OnAudioTrack | null;
+ onVideoTrack: OnVideoTrack | null;
canSkipVideoData: boolean;
onTrackEntrySegment: OnTrackEntrySegment;
getTracks: GetTracks;
diff --git a/packages/media-parser/src/test/matroska.test.ts b/packages/media-parser/src/test/matroska.test.ts
index eb2b28100a..70d982c7ae 100644
--- a/packages/media-parser/src/test/matroska.test.ts
+++ b/packages/media-parser/src/test/matroska.test.ts
@@ -241,6 +241,8 @@ test('Should get duration of AV1 video', async () => {
color_range: false,
subsampling_x: true,
subsampling_y: true,
+ color_description_present_flag: true,
+ mono_chrome: false,
},
film_grain_params_present: false,
enable_interintra_compound: 0,
diff --git a/packages/media-parser/src/test/stream-local.test.ts b/packages/media-parser/src/test/stream-local.test.ts
index b0239f04a5..31e885e1ff 100644
--- a/packages/media-parser/src/test/stream-local.test.ts
+++ b/packages/media-parser/src/test/stream-local.test.ts
@@ -4,6 +4,8 @@ import {nodeReader} from '../from-node';
import {parseMedia} from '../parse-media';
test('Should stream ISO base media', async () => {
+ let videoTracks = 0;
+ let audioTracks = 0;
const result = await parseMedia({
src: RenderInternals.exampleVideos.iphonevideo,
fields: {
@@ -17,6 +19,12 @@ test('Should stream ISO base media', async () => {
unrotatedDimension: true,
},
reader: nodeReader,
+ onVideoTrack: () => {
+ videoTracks++;
+ },
+ onAudioTrack: () => {
+ audioTracks++;
+ },
});
expect(result.dimensions).toEqual({
width: 2160,
@@ -33,6 +41,8 @@ test('Should stream ISO base media', async () => {
height: 2160,
width: 3840,
});
+ expect(videoTracks).toBe(1);
+ expect(audioTracks).toBe(1);
});
test('Should stream WebM with no duration', async () => {
diff --git a/packages/media-parser/src/test/stsd.test.ts b/packages/media-parser/src/test/stsd.test.ts
index 1837425bdb..961e47b625 100644
--- a/packages/media-parser/src/test/stsd.test.ts
+++ b/packages/media-parser/src/test/stsd.test.ts
@@ -23,7 +23,7 @@ test('Should be able to parse a STSD audio box correctly', () => {
0, 1,
// version
0, 1,
- // revisioon level
+ // revision level
0, 0, 0, 0, 0, 0,
// revision level
0, 2, 0, 16, 255, 254, 0, 0, 172, 68, 0, 0, 0, 0, 4, 0, 0, 0, 0, 1, 0, 0, 0,
@@ -45,6 +45,8 @@ test('Should be able to parse a STSD audio box correctly', () => {
canSkipVideoData: true,
onAudioSample: null,
onVideoSample: null,
+ onAudioTrack: null,
+ onVideoTrack: null,
onTrackEntrySegment: () => undefined,
getTracks: () => [],
},
@@ -193,6 +195,8 @@ test('Should be able to parse a STSD video box correctly', () => {
canSkipVideoData: true,
onAudioSample: null,
onVideoSample: null,
+ onAudioTrack: null,
+ onVideoTrack: null,
onTrackEntrySegment: () => undefined,
getTracks: () => [],
},
diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.ts
index 01038cb46b..cb565c7fcc 100644
--- a/packages/media-parser/src/traversal.ts
+++ b/packages/media-parser/src/traversal.ts
@@ -11,6 +11,17 @@ import type {StszBox} from './boxes/iso-base-media/stsd/stsz';
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 {MainSegment} from './boxes/webm/segments/main';
+import type {TimestampScaleSegment} from './boxes/webm/segments/timestamp-scale';
+import type {
+ ClusterSegment,
+ CodecSegment,
+ HeightSegment,
+ TrackEntrySegment,
+ TrackTypeSegment,
+ VideoSegment,
+ WidthSegment,
+} from './boxes/webm/segments/track-entry';
import type {AnySegment, RegularBox} from './parse-result';
export const getFtypBox = (segments: AnySegment[]): FtypBox | null => {
@@ -224,3 +235,120 @@ export const getStssBox = (trakBox: TrakBox): StssBox | null => {
return stssBox;
};
+
+export const getClusterSegment = (
+ segment: MainSegment,
+): ClusterSegment | null => {
+ const clusterSegment = segment.children.find(
+ (b) => b.type === 'cluster-segment',
+ ) as ClusterSegment | undefined;
+
+ return clusterSegment ?? null;
+};
+
+export const getTracksSegment = (segment: MainSegment) => {
+ const tracksSegment = segment.children.find(
+ (b) => b.type === 'tracks-segment',
+ );
+ if (!tracksSegment || tracksSegment.type !== 'tracks-segment') {
+ return null;
+ }
+
+ return tracksSegment;
+};
+
+export const getTimescaleSegment = (
+ segment: MainSegment,
+): TimestampScaleSegment | null => {
+ const infoSegment = segment.children.find((b) => b.type === 'info-segment');
+
+ if (!infoSegment || infoSegment.type !== 'info-segment') {
+ return null;
+ }
+
+ const timescale = infoSegment.children.find(
+ (b) => b.type === 'timestamp-scale-segment',
+ );
+
+ if (!timescale || timescale.type !== 'timestamp-scale-segment') {
+ return null;
+ }
+
+ return timescale;
+};
+
+export const getVideoSegment = (
+ track: TrackEntrySegment,
+): VideoSegment | null => {
+ const videoSegment = track.children.find((b) => b.type === 'video-segment');
+ if (!videoSegment || videoSegment.type !== 'video-segment') {
+ return null;
+ }
+
+ return videoSegment ?? null;
+};
+
+export const getWidthSegment = (
+ track: TrackEntrySegment,
+): WidthSegment | null => {
+ const videoSegment = getVideoSegment(track);
+ if (!videoSegment) {
+ return null;
+ }
+
+ const width = videoSegment.children.find((b) => b.type === 'width-segment');
+
+ if (!width || width.type !== 'width-segment') {
+ return null;
+ }
+
+ return width;
+};
+
+export const getHeightSegment = (
+ track: TrackEntrySegment,
+): HeightSegment | null => {
+ const videoSegment = getVideoSegment(track);
+ if (!videoSegment) {
+ return null;
+ }
+
+ const height = videoSegment.children.find((b) => b.type === 'height-segment');
+
+ if (!height || height.type !== 'height-segment') {
+ return null;
+ }
+
+ return height;
+};
+
+export const getTrackTypeSegment = (
+ track: TrackEntrySegment,
+): TrackTypeSegment | null => {
+ const trackType = track.children.find((b) => b.type === 'track-type-segment');
+ if (!trackType || trackType.type !== 'track-type-segment') {
+ return null;
+ }
+
+ return trackType;
+};
+
+export const getTrackId = (track: TrackEntrySegment): number => {
+ const trackId = track.children.find((b) => b.type === 'track-number-segment');
+ if (!trackId || trackId.type !== 'track-number-segment') {
+ throw new Error('Expected track number segment');
+ }
+
+ return trackId.trackNumber;
+};
+
+export const getCodecSegment = (
+ track: TrackEntrySegment,
+): CodecSegment | null => {
+ const codec = track.children.find((b) => b.type === 'codec-segment');
+ if (!codec || codec.type !== 'codec-segment') {
+ return null;
+ }
+
+ return codec;
+};