Skip to content

🎥 Make videos programmatically with React

skip ahead in reader

Jonny Burgerf05efa2cc46c
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 67ecc0a90a..bf1423092b 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
@@ -67,23 +67,31 @@ const processBox = ({
throw new Error(`Expected box size of not 0, got ${boxSize}`);
}
- let skipTo: number | null = null;
-
if (bytesRemaining < boxSize) {
if (bytesRemaining >= 4) {
const type = iterator.getByteString(4);
+ iterator.counter.decrement(4);
+
if (type === 'mdat') {
- skipTo = fileOffset + boxSize;
+ return {
+ type: 'complete',
+ box: {
+ type: 'regular-box',
+ boxType: 'mdat',
+ children: [],
+ boxSize,
+ offset: fileOffset,
+ },
+ size: boxSize,
+ skipTo: fileOffset + boxSize,
+ };
}
-
- iterator.counter.decrement(4);
}
iterator.counter.decrement(iterator.counter.getOffset() - fileOffset);
if (allowIncompleteBoxes) {
return {
type: 'incomplete',
- skipTo,
};
}
@@ -100,6 +108,7 @@ const processBox = ({
type: 'complete',
box,
size: boxSize,
+ skipTo: null,
};
}
@@ -110,6 +119,7 @@ const processBox = ({
type: 'complete',
box,
size: boxSize,
+ skipTo: null,
};
}
@@ -120,6 +130,7 @@ const processBox = ({
type: 'complete',
box,
size: boxSize,
+ skipTo: null,
};
}
@@ -130,6 +141,7 @@ const processBox = ({
type: 'complete',
box,
size: boxSize,
+ skipTo: null,
};
}
@@ -140,6 +152,7 @@ const processBox = ({
type: 'complete',
box,
size: boxSize,
+ skipTo: null,
};
}
@@ -150,6 +163,7 @@ const processBox = ({
type: 'complete',
box,
size: boxSize,
+ skipTo: null,
};
}
@@ -164,6 +178,7 @@ const processBox = ({
type: 'complete',
box,
size: boxSize,
+ skipTo: null,
};
}
@@ -178,6 +193,7 @@ const processBox = ({
type: 'complete',
box,
size: boxSize,
+ skipTo: null,
};
}
@@ -192,6 +208,7 @@ const processBox = ({
type: 'complete',
box,
size: boxSize,
+ skipTo: null,
};
}
@@ -206,6 +223,7 @@ const processBox = ({
type: 'complete',
box,
size: boxSize,
+ skipTo: null,
};
}
@@ -228,6 +246,7 @@ const processBox = ({
offset: fileOffset,
},
size: boxSize,
+ skipTo: null,
};
};
@@ -269,11 +288,28 @@ export const parseBoxes = ({
initialBoxes: boxes,
});
},
- skipTo: result.skipTo,
+ skipTo: null,
};
}
boxes.push(result.box);
+
+ if (result.skipTo !== null) {
+ return {
+ status: 'incomplete',
+ segments: boxes,
+ continueParsing: () => {
+ return parseBoxes({
+ iterator,
+ maxBytes,
+ allowIncompleteBoxes,
+ initialBoxes: boxes,
+ });
+ },
+ skipTo: result.skipTo,
+ };
+ }
+
iterator.discardFirstBytes();
}
diff --git a/packages/media-parser/src/parse-video.ts b/packages/media-parser/src/parse-video.ts
index 0245d84215..b95ee86e99 100644
--- a/packages/media-parser/src/parse-video.ts
+++ b/packages/media-parser/src/parse-video.ts
@@ -8,10 +8,10 @@ export type BoxAndNext =
type: 'complete';
box: IsoBaseMediaBox;
size: number;
+ skipTo: number | null;
}
| {
type: 'incomplete';
- skipTo: number | null;
};
export const parseVideo = (iterator: BufferIterator): ParseResult => {
diff --git a/packages/media-parser/src/test/stream-local.test.ts b/packages/media-parser/src/test/stream-local.test.ts
index 5980b8b442..b489e19ffd 100644
--- a/packages/media-parser/src/test/stream-local.test.ts
+++ b/packages/media-parser/src/test/stream-local.test.ts
@@ -11,11 +11,9 @@ test('Should stream ISO base media', async () => {
fps: true,
videoCodec: true,
audioCodec: true,
- boxes: true,
},
nodeReader,
);
- await Bun.write('boxes.json', JSON.stringify(result.boxes));
expect(result.durationInSeconds).toBe(12.568333333333333);
expect(result.fps).toBe(29.99602174777881);
expect(result.videoCodec).toBe('h265');