Skip to content

🎥 Make videos programmatically with React

parse metadata

Jonny Burgerae9171664af9
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
index 95f95f871c..16e27622c5 100644
--- a/packages/media-parser/src/boxes/iso-base-media/make-track.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/make-track.ts
@@ -11,7 +11,7 @@ import {
getVideoSample,
} from '../../get-sample-aspect-ratio';
import {getSamplePositions} from '../../get-sample-positions';
-import type {AudioTrack, VideoTrack} from '../../get-tracks';
+import type {AudioTrack, OtherTrack, VideoTrack} from '../../get-tracks';
import {getVideoCodecString} from '../../get-video-codec';
import {
getCttsBox,
@@ -27,7 +27,7 @@ import type {TrakBox} from './trak/trak';
export const makeBaseMediaTrack = (
trakBox: TrakBox,
-): VideoTrack | AudioTrack | null => {
+): VideoTrack | AudioTrack | OtherTrack | null => {
const stszBox = getStszBox(trakBox);
const stcoBox = getStcoBox(trakBox);
const stscBox = getStscBox(trakBox);
@@ -82,8 +82,12 @@ export const makeBaseMediaTrack = (
}
if (!trakBoxContainsVideo(trakBox)) {
- console.log(trakBox);
- return null;
+ return {
+ type: 'other',
+ samplePositions,
+ trackId: tkhdBox.trackId,
+ timescale: timescaleAndDuration.timescale,
+ };
}
const videoSample = getVideoSample(trakBox);
diff --git a/packages/media-parser/src/boxes/iso-base-media/mdat/mdat.ts b/packages/media-parser/src/boxes/iso-base-media/mdat/mdat.ts
index bd03b2208b..77b4c009a8 100644
--- a/packages/media-parser/src/boxes/iso-base-media/mdat/mdat.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/mdat/mdat.ts
@@ -1,3 +1,4 @@
+/* eslint-disable max-depth */
import type {BufferIterator} from '../../../buffer-iterator';
import {getTracks, hasTracks} from '../../../get-tracks';
import type {AnySegment} from '../../../parse-result';
@@ -36,7 +37,11 @@ export const parseMdat = ({
}
const tracks = getTracks(existingBoxes);
- const allTracks = [...tracks.videoTracks, ...tracks.audioTracks];
+ const allTracks = [
+ ...tracks.videoTracks,
+ ...tracks.audioTracks,
+ ...tracks.otherTracks,
+ ];
const flatSamples = allTracks
.map((track) => {
@@ -59,7 +64,36 @@ export const parseMdat = ({
return sample.samplePosition.offset === data.counter.getOffset();
});
if (!sampleWithIndex) {
- throw new Error('Could not find sample');
+ if (data.bytesRemaining() >= 8) {
+ const possibleAtomLength = data.getFourByteNumber();
+ const possibleAtom = data.getByteString(4);
+ data.counter.decrement(8);
+ // if a weird hoov atom appears, like in iphonevideo.hevc
+ // then we skip to the next sample
+ if (possibleAtom === 'hoov' || possibleAtom === 'moof') {
+ const nextSample = flatSamples
+ .filter((s) => s.samplePosition.offset > data.counter.getOffset())
+ .sort(
+ (a, b) => a.samplePosition.offset - b.samplePosition.offset,
+ )[0];
+ if (nextSample) {
+ data.discard(
+ nextSample.samplePosition.offset - data.counter.getOffset(),
+ );
+ continue;
+ } else {
+ data.discard(possibleAtomLength);
+ break;
+ }
+ }
+
+ console.log('byte string', possibleAtomLength, possibleAtom);
+ data.peekB(8);
+ }
+
+ throw new Error(
+ 'Could not find sample with offset ' + data.counter.getOffset(),
+ );
}
const bytes = data.getSlice(sampleWithIndex.samplePosition.size);
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 b388aea501..2eee19d966 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
@@ -75,7 +75,7 @@ const getChildren = ({
return [];
};
-const processBox = ({
+export const processBox = ({
iterator,
allowIncompleteBoxes,
parsedBoxes,
diff --git a/packages/media-parser/src/boxes/webm/get-ready-tracks.ts b/packages/media-parser/src/boxes/webm/get-ready-tracks.ts
index 6eb157bb20..0a807e5143 100644
--- a/packages/media-parser/src/boxes/webm/get-ready-tracks.ts
+++ b/packages/media-parser/src/boxes/webm/get-ready-tracks.ts
@@ -1,4 +1,4 @@
-import type {AudioTrack, VideoTrack} from '../../get-tracks';
+import type {AudioTrack, OtherTrack, VideoTrack} from '../../get-tracks';
import {
getClusterSegment,
getTimescaleSegment,
@@ -9,7 +9,11 @@ import type {MainSegment} from './segments/main';
export const getTracksFromMatroska = (
segment: MainSegment,
-): {videoTracks: VideoTrack[]; audioTracks: AudioTrack[]} => {
+): {
+ videoTracks: VideoTrack[];
+ audioTracks: AudioTrack[];
+ otherTracks: OtherTrack[];
+} => {
const tracksSegment = getTracksSegment(segment);
if (!tracksSegment) {
throw new Error('No tracks segment');
@@ -25,6 +29,7 @@ export const getTracksFromMatroska = (
const videoTracks: VideoTrack[] = [];
const audioTracks: AudioTrack[] = [];
+ const otherTracks: OtherTrack[] = [];
for (const trackEntrySegment of tracksSegment.children) {
if (trackEntrySegment.type === 'crc32-segment') {
@@ -53,5 +58,6 @@ export const getTracksFromMatroska = (
return {
videoTracks,
audioTracks,
+ otherTracks,
};
};
diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.ts
index 620bbf4af2..fe948ac8fc 100644
--- a/packages/media-parser/src/get-tracks.ts
+++ b/packages/media-parser/src/get-tracks.ts
@@ -33,6 +33,13 @@ export type AudioTrack = {
codecString: string | null;
};
+export type OtherTrack = {
+ type: 'other';
+ samplePositions: SamplePosition[] | null;
+ trackId: number;
+ timescale: number;
+};
+
export type Track = VideoTrack | AudioTrack;
// TODO: Use this to determine if all tracks are present
@@ -70,6 +77,7 @@ export const getTracks = (
): {
videoTracks: VideoTrack[];
audioTracks: AudioTrack[];
+ otherTracks: OtherTrack[];
} => {
const mainSegment = segments.find((s) => s.type === 'main-segment');
if (mainSegment && mainSegment.type === 'main-segment') {
@@ -81,11 +89,13 @@ export const getTracks = (
return {
videoTracks: [],
audioTracks: [],
+ otherTracks: [],
};
}
const videoTracks: VideoTrack[] = [];
const audioTracks: AudioTrack[] = [];
+ const otherTracks: OtherTrack[] = [];
const tracks = getTraks(moovBox);
for (const trakBox of tracks) {
@@ -98,13 +108,14 @@ export const getTracks = (
videoTracks.push(track);
} else if (track.type === 'audio') {
audioTracks.push(track);
- } else {
- console.log('Unknown track type', track);
+ } else if (track.type === 'other') {
+ otherTracks.push(track);
}
}
return {
videoTracks,
audioTracks,
+ otherTracks,
};
};
diff --git a/packages/media-parser/src/test/stream-local.test.ts b/packages/media-parser/src/test/stream-local.test.ts
index a63ed3c3b3..30573e060c 100644
--- a/packages/media-parser/src/test/stream-local.test.ts
+++ b/packages/media-parser/src/test/stream-local.test.ts
@@ -51,9 +51,8 @@ test('Should stream ISO base media', async () => {
});
expect(videoTracks).toBe(1);
expect(audioTracks).toBe(1);
- // TODO: Should emit a video sample
- expect(videoSamples).toBe(0);
- expect(audioSamples).toBe(0);
+ expect(videoSamples).toBe(377);
+ expect(audioSamples).toBe(544);
});
test('Should stream WebM with no duration', async () => {
@@ -363,7 +362,7 @@ test('Should get duration of HEVC video', async () => {
height: 1080,
});
expect(parsed.videoCodec).toBe('h265');
- expect(videoSamples).toBe(1);
+ expect(videoSamples).toBe(102);
});
test('Custom DAR', async () => {