Skip to content

🎥 Make videos programmatically with React

Update mdat.ts

Jonny Burgerabce72494bfc
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 a4dde61fad..da6d9e84a1 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,4 +1,5 @@
import type {BufferIterator} from '../../../buffer-iterator';
+import {getTracks} from '../../../get-tracks';
import type {AnySegment} from '../../../parse-result';
export interface MdatBox {
@@ -10,6 +11,7 @@ export const parseMdat = ({
data,
size,
fileOffset,
+ existingBoxes,
}: {
data: BufferIterator;
size: number;
@@ -17,8 +19,34 @@ export const parseMdat = ({
existingBoxes: AnySegment[];
}): MdatBox => {
// TODO: Do something cool with it
- // const tracks = getTracks(existingBoxes);
- data.discard(size - (data.counter.getOffset() - fileOffset));
+ const tracks = getTracks(existingBoxes);
+ const flatSamples = tracks
+ .map((track) => {
+ return track.samplePositions.map((samplePosition) => {
+ return {
+ type: track.type,
+ trackId: track.trackId,
+ samplePosition,
+ };
+ });
+ })
+ .flat(1);
+
+ while (true) {
+ const sampleWithIndex = flatSamples.find((sample) => {
+ return sample.samplePosition.offset === data.counter.getOffset();
+ });
+ if (!sampleWithIndex) {
+ throw new Error('Could not find sample');
+ }
+
+ const bytes = data.getSlice(sampleWithIndex.samplePosition.size);
+ console.log('found bytes', bytes.length, sampleWithIndex);
+ const remaining = size - (data.counter.getOffset() - fileOffset);
+ if (remaining === 0) {
+ break;
+ }
+ }
return {
type: 'mdat-box',