Skip to content

🎥 Make videos programmatically with React

parse main segment

Jonny Burgeraf03ad7728a8
diff --git a/packages/video-parser/src/boxes/webm/parse-webm-header.ts b/packages/video-parser/src/boxes/webm/parse-webm-header.ts
index 0e098eb3e9..2b1616b6fe 100644
--- a/packages/video-parser/src/boxes/webm/parse-webm-header.ts
+++ b/packages/video-parser/src/boxes/webm/parse-webm-header.ts
@@ -1,7 +1,8 @@
import {getArrayBufferIterator} from '../../read-and-increment-offset';
+import {expectSegment} from './segments';
// Parsing according to https://darkcoding.net/software/reading-mediarecorders-webm-opus-output/
-export const parseWebmHeader = (data: ArrayBuffer) => {
+export const parseWebm = (data: ArrayBuffer) => {
const counter = getArrayBufferIterator(data, 4);
const length = counter.getEBML();
@@ -12,12 +13,5 @@ export const parseWebmHeader = (data: ArrayBuffer) => {
// Discard header for now
counter.discard(31);
- const magic = counter.getSlice(4);
- // "We find 18 53 80 67."
- // We are done with the generic EBML fields and now come to the Matroska fields.
- if (new Uint8Array(magic).join() !== [0x18, 0x53, 0x80, 0x67].join()) {
- throw new Error(
- 'Expected "EBML" magic fields, got ' + new Uint8Array(magic).join(),
- );
- }
+ return expectSegment(counter);
};
diff --git a/packages/video-parser/src/boxes/webm/segments.ts b/packages/video-parser/src/boxes/webm/segments.ts
new file mode 100644
index 0000000000..0585708f6b
--- /dev/null
+++ b/packages/video-parser/src/boxes/webm/segments.ts
@@ -0,0 +1,15 @@
+import type {BufferIterator} from '../../read-and-increment-offset';
+import {parseMainSegment} from './segments/main';
+
+export const expectSegment = (iterator: BufferIterator) => {
+ const segmentId = iterator.getMatroskaSegmentId();
+
+ // TODO: Could be "unknown size"
+ const length = iterator.getVint();
+
+ if (segmentId === '0x18538067') {
+ return [parseMainSegment(iterator, length)];
+ }
+
+ return [];
+};
diff --git a/packages/video-parser/src/boxes/webm/segments/main.ts b/packages/video-parser/src/boxes/webm/segments/main.ts
new file mode 100644
index 0000000000..e6a245743e
--- /dev/null
+++ b/packages/video-parser/src/boxes/webm/segments/main.ts
@@ -0,0 +1,16 @@
+import type {BufferIterator} from '../../../read-and-increment-offset';
+
+export type MainSegment = {
+ type: 'main-segment';
+};
+
+export const parseMainSegment = (
+ iterator: BufferIterator,
+ length: number,
+): MainSegment => {
+ iterator.discard(length);
+
+ return {
+ type: 'main-segment',
+ };
+};
diff --git a/packages/video-parser/src/parse-video.ts b/packages/video-parser/src/parse-video.ts
index fcac5a1b35..3cbf13d3a4 100644
--- a/packages/video-parser/src/parse-video.ts
+++ b/packages/video-parser/src/parse-video.ts
@@ -9,7 +9,8 @@ import type {MebxBox} from './boxes/iso-base-media/stsd/mebx';
import type {StsdBox} from './boxes/iso-base-media/stsd/stsd';
import type {TkhdBox} from './boxes/iso-base-media/tkhd';
import type {TrakBox} from './boxes/iso-base-media/trak/trak';
-import {parseWebmHeader} from './boxes/webm/parse-webm-header';
+import {parseWebm} from './boxes/webm/parse-webm-header';
+import type {MainSegment} from './boxes/webm/segments/main';
interface RegularBox extends BaseBox {
boxType: string;
@@ -28,7 +29,8 @@ export type Box =
| MebxBox
| KeysBox
| MoovBox
- | TrakBox;
+ | TrakBox
+ | MainSegment;
export type BoxAndNext = {
box: Box;
@@ -74,7 +76,7 @@ export const parseVideo = async (
}
if (matchesPattern(webmPattern)(data.subarray(0, 4))) {
- parseWebmHeader(new Uint8Array(data).buffer as unknown as ArrayBuffer);
+ return parseWebm(new Uint8Array(data).buffer as unknown as ArrayBuffer);
}
return [];
diff --git a/packages/video-parser/src/read-and-increment-offset.ts b/packages/video-parser/src/read-and-increment-offset.ts
index 1a779c03b5..e60e9d541b 100644
--- a/packages/video-parser/src/read-and-increment-offset.ts
+++ b/packages/video-parser/src/read-and-increment-offset.ts
@@ -54,6 +54,38 @@ export const getArrayBufferIterator = (
const atom = getSlice(4);
return new TextDecoder().decode(atom);
},
+ getMatroskaSegmentId: () => {
+ const segmentId = getSlice(4);
+ return `0x${new Uint8Array(segmentId)
+ // @ts-expect-error Convert Uint8Array to string
+ .map((b) => {
+ return b.toString(16);
+ })
+ .join('')}`;
+ },
+ getVint: () => {
+ const d = [...Array.from(new Uint8Array(getSlice(8)))];
+ const totalLength = d[0];
+
+ // Calculate the actual length of the data based on the first set bit
+ let actualLength = 0;
+ while (((totalLength >> (7 - actualLength)) & 0x01) === 0) {
+ actualLength++;
+ }
+
+ actualLength += 1; // Include the first byte set as 1
+
+ // Combine the numbers to form the integer value
+ let value = 0;
+
+ // Mask the first byte properly then start combining
+ value = totalLength & (0xff >> actualLength);
+ for (let i = 1; i < actualLength; i++) {
+ value = (value << 8) | d[i];
+ }
+
+ return value;
+ },
getUint8,
getEBML: () => {
const val = getUint8();
@@ -93,4 +125,4 @@ export const getArrayBufferIterator = (
};
};
-export type Iterator = ReturnType<typeof getArrayBufferIterator>;
+export type BufferIterator = ReturnType<typeof getArrayBufferIterator>;
diff --git a/packages/video-parser/src/test/duration.test.ts b/packages/video-parser/src/test/duration.test.ts
index ff65957f7c..9b5aa8d718 100644
--- a/packages/video-parser/src/test/duration.test.ts
+++ b/packages/video-parser/src/test/duration.test.ts
@@ -21,5 +21,5 @@ test('Should get duration of HEVC video', async () => {
test('Should get duration of AV1 video', async () => {
const parsed = await parseVideo(exampleVideos.av1, Infinity);
// TODO: AV1 duration is not yet supported
- expect(getDuration(parsed)).toBe(null);
+ expect(parsed).toBe([]);
});