Skip to content

🎥 Make videos programmatically with React

some progress

Jonny Burgerc37066f49058
diff --git a/packages/media-parser/av1.json b/packages/media-parser/av1.json
index b41ebfe7aa..8d37a662a1 100644
--- a/packages/media-parser/av1.json
+++ b/packages/media-parser/av1.json
@@ -139,13 +139,363 @@
"type": "cluster-segment",
"children": [
{
- "id": "0x8100a324",
- "type": "unknown-segment"
+ "type": "timestamp-segment",
+ "timestamp": 0
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 279307,
+ "trackNumber": 1,
+ "timecode": 0,
+ "headerFlags": 128,
+ "keyframe": true,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 96,
+ "trackNumber": 1,
+ "timecode": 40,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 556,
+ "trackNumber": 1,
+ "timecode": 80,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 948,
+ "trackNumber": 1,
+ "timecode": 120,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 577,
+ "trackNumber": 1,
+ "timecode": 160,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 779,
+ "trackNumber": 1,
+ "timecode": 200,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 793,
+ "trackNumber": 1,
+ "timecode": 240,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 740,
+ "trackNumber": 1,
+ "timecode": 280,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1095,
+ "trackNumber": 1,
+ "timecode": 320,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1097,
+ "trackNumber": 1,
+ "timecode": 360,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1155,
+ "trackNumber": 1,
+ "timecode": 400,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1526,
+ "trackNumber": 1,
+ "timecode": 440,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1487,
+ "trackNumber": 1,
+ "timecode": 480,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 2046,
+ "trackNumber": 1,
+ "timecode": 520,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1372,
+ "trackNumber": 1,
+ "timecode": 560,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1441,
+ "trackNumber": 1,
+ "timecode": 600,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 2947,
+ "trackNumber": 1,
+ "timecode": 640,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 2652,
+ "trackNumber": 1,
+ "timecode": 680,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 4199,
+ "trackNumber": 1,
+ "timecode": 720,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 3998,
+ "trackNumber": 1,
+ "timecode": 760,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 6373,
+ "trackNumber": 1,
+ "timecode": 800,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 5955,
+ "trackNumber": 1,
+ "timecode": 840,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 7943,
+ "trackNumber": 1,
+ "timecode": 880,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 8241,
+ "trackNumber": 1,
+ "timecode": 920,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 9506,
+ "trackNumber": 1,
+ "timecode": 960,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
}
]
},
{
- "id": "0x01f5d31f",
+ "id": "0x1c53bb6b",
"type": "unknown-segment"
}
]
diff --git a/packages/media-parser/parsed.json b/packages/media-parser/parsed.json
new file mode 100644
index 0000000000..7ee2ed3b92
--- /dev/null
+++ b/packages/media-parser/parsed.json
@@ -0,0 +1,511 @@
+{
+ "dimensions": {
+ "width": 1920,
+ "height": 1080
+ },
+ "durationInSeconds": 1,
+ "fps": null,
+ "boxes": [
+ {
+ "type": "main-segment",
+ "children": [
+ {
+ "type": "seek-head-segment",
+ "length": 59,
+ "children": [
+ {
+ "type": "seek-segment",
+ "seekId": "0x53ab8415",
+ "child": {
+ "id": "0x49a96653",
+ "type": "unknown-segment"
+ }
+ },
+ {
+ "id": "0x054cc1ec",
+ "type": "unknown-segment"
+ }
+ ]
+ },
+ {
+ "type": "info-segment",
+ "length": 48,
+ "children": [
+ {
+ "type": "timestamp-scale-segment",
+ "timestampScale": 1000000
+ },
+ {
+ "type": "muxing-app-segment",
+ "value": "Lavf60.3.100"
+ },
+ {
+ "type": "writing-app-segment",
+ "value": "Lavf60.3.100"
+ },
+ {
+ "type": "duration-segment",
+ "duration": 1000
+ }
+ ]
+ },
+ {
+ "type": "tracks-segment",
+ "children": [
+ {
+ "type": "track-entry-segment",
+ "children": [
+ {
+ "type": "track-number-segment",
+ "trackNumber": 1
+ },
+ {
+ "type": "track-uid-segment",
+ "trackUid": "ab2171012bb9020a"
+ },
+ {
+ "type": "flag-lacing-segment",
+ "lacing": false
+ },
+ {
+ "type": "language-segment",
+ "language": "und"
+ },
+ {
+ "type": "codec-segment",
+ "codec": "V_AV1"
+ },
+ {
+ "type": "track-type-segment",
+ "trackType": "video"
+ },
+ {
+ "type": "default-duration-segment",
+ "defaultDuration": 40000000
+ },
+ {
+ "type": "video-segment",
+ "children": [
+ {
+ "type": "width-segment",
+ "width": 1920
+ },
+ {
+ "type": "height-segment",
+ "height": 1080
+ },
+ {
+ "type": "color-segment",
+ "length": 16
+ }
+ ]
+ },
+ {
+ "type": "codec-private-segment",
+ "codecPrivateData": [
+ 129,
+ 8,
+ 12,
+ 0,
+ 10,
+ 14,
+ 0,
+ 0,
+ 0,
+ 66,
+ 171,
+ 191,
+ 195,
+ 118,
+ 0,
+ 8,
+ 8,
+ 8,
+ 8,
+ 32
+ ]
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "type": "tags-segment",
+ "children": [
+ {
+ "type": "tag-segment",
+ "length": 31
+ },
+ {
+ "type": "tag-segment",
+ "length": 51
+ }
+ ]
+ },
+ {
+ "type": "cluster-segment",
+ "children": [
+ {
+ "type": "timestamp-segment",
+ "timestamp": 0
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 279307,
+ "trackNumber": 1,
+ "timecode": 0,
+ "headerFlags": 128,
+ "keyframe": true,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 96,
+ "trackNumber": 1,
+ "timecode": 40,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 556,
+ "trackNumber": 1,
+ "timecode": 80,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 948,
+ "trackNumber": 1,
+ "timecode": 120,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 577,
+ "trackNumber": 1,
+ "timecode": 160,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 779,
+ "trackNumber": 1,
+ "timecode": 200,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 793,
+ "trackNumber": 1,
+ "timecode": 240,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 740,
+ "trackNumber": 1,
+ "timecode": 280,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1095,
+ "trackNumber": 1,
+ "timecode": 320,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1097,
+ "trackNumber": 1,
+ "timecode": 360,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1155,
+ "trackNumber": 1,
+ "timecode": 400,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1526,
+ "trackNumber": 1,
+ "timecode": 440,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1487,
+ "trackNumber": 1,
+ "timecode": 480,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 2046,
+ "trackNumber": 1,
+ "timecode": 520,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1372,
+ "trackNumber": 1,
+ "timecode": 560,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 1441,
+ "trackNumber": 1,
+ "timecode": 600,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 2947,
+ "trackNumber": 1,
+ "timecode": 640,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 2652,
+ "trackNumber": 1,
+ "timecode": 680,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 4199,
+ "trackNumber": 1,
+ "timecode": 720,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 3998,
+ "trackNumber": 1,
+ "timecode": 760,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 6373,
+ "trackNumber": 1,
+ "timecode": 800,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 5955,
+ "trackNumber": 1,
+ "timecode": 840,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 7943,
+ "trackNumber": 1,
+ "timecode": 880,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 8241,
+ "trackNumber": 1,
+ "timecode": 920,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ },
+ {
+ "type": "simple-block-segment",
+ "length": 9506,
+ "trackNumber": 1,
+ "timecode": 960,
+ "headerFlags": 0,
+ "keyframe": false,
+ "lacing": [
+ 0,
+ 0
+ ],
+ "invisible": false,
+ "children": null
+ }
+ ]
+ },
+ {
+ "id": "0x1c53bb6b",
+ "type": "unknown-segment"
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/packages/media-parser/src/boxes/webm/allowed-partial-segments.ts b/packages/media-parser/src/boxes/webm/allowed-partial-segments.ts
new file mode 100644
index 0000000000..69d6926e6b
--- /dev/null
+++ b/packages/media-parser/src/boxes/webm/allowed-partial-segments.ts
@@ -0,0 +1 @@
+export const allowedPartialSegments = ['0x1f43b675', '0x18538067'];
diff --git a/packages/media-parser/src/boxes/webm/parse-webm-header.ts b/packages/media-parser/src/boxes/webm/parse-webm-header.ts
index d935e0f833..53d1119f24 100644
--- a/packages/media-parser/src/boxes/webm/parse-webm-header.ts
+++ b/packages/media-parser/src/boxes/webm/parse-webm-header.ts
@@ -14,7 +14,5 @@ export const parseWebm = (counter: BufferIterator): ParseResult => {
// Discard header for now
counter.discard(length);
- const segments = expectSegment(counter);
-
- return {status: 'done', segments: [segments]};
+ return expectSegment(counter);
};
diff --git a/packages/media-parser/src/boxes/webm/segments.ts b/packages/media-parser/src/boxes/webm/segments.ts
index 013b55097e..93ffea36c8 100644
--- a/packages/media-parser/src/boxes/webm/segments.ts
+++ b/packages/media-parser/src/boxes/webm/segments.ts
@@ -1,11 +1,12 @@
import type {BufferIterator} from '../../buffer-iterator';
+import type {ParseResult} from '../../parse-result';
import type {DurationSegment} from './segments/duration';
import {parseDurationSegment} from './segments/duration';
import type {InfoSegment} from './segments/info';
import {parseInfoSegment} from './segments/info';
-import type {MainSegment} from './segments/main';
-import {parseMainSegment} from './segments/main';
+import {type MainSegment} from './segments/main';
import {parseMuxingSegment, type MuxingAppSegment} from './segments/muxing';
+import {expectChildren} from './segments/parse-children';
import {parseSeekSegment, type SeekSegment} from './segments/seek';
import type {SeekHeadSegment} from './segments/seek-head';
import {parseSeekHeadSegment} from './segments/seek-head';
@@ -44,7 +45,6 @@ import type {
} from './segments/track-entry';
import {
parseAlphaModeSegment,
- parseClusterSegment,
parseCodecPrivateSegment,
parseCodecSegment,
parseColorSegment,
@@ -117,20 +117,13 @@ export type MatroskaSegment =
| TimestampSegment
| SimpleBlockSegment;
-export const expectSegment = (iterator: BufferIterator): MatroskaSegment => {
- const bytesRemaining_ = iterator.byteLength() - iterator.counter.getOffset();
- if (bytesRemaining_ === 0) {
- throw new Error('No bytes remaining');
- }
-
- const segmentId = iterator.getMatroskaSegmentId();
- const length = iterator.getVint();
- const bytesRemainingNow =
- iterator.byteLength() - iterator.counter.getOffset();
- if (bytesRemainingNow < length) {
- if (segmentId !== '0x18538067') {
- throw new Error('Dont have the data yet for this segment ' + segmentId);
- }
+const parseSegment = (
+ segmentId: string,
+ iterator: BufferIterator,
+ length: number,
+): MatroskaSegment => {
+ if (length === 0) {
+ throw new Error('Expected length to be greater than 0');
}
if (segmentId === '0x') {
@@ -140,10 +133,6 @@ export const expectSegment = (iterator: BufferIterator): MatroskaSegment => {
};
}
- if (segmentId === '0x18538067') {
- return parseMainSegment(iterator, length);
- }
-
if (segmentId === '0x114d9b74') {
return parseSeekHeadSegment(iterator, length);
}
@@ -272,10 +261,6 @@ export const expectSegment = (iterator: BufferIterator): MatroskaSegment => {
return parseTagSegment(iterator, length);
}
- if (segmentId === '0x1f43b675') {
- return parseClusterSegment(iterator, length);
- }
-
if (segmentId === '0xe7') {
return parseTimestampSegment(iterator, length);
}
@@ -293,3 +278,72 @@ export const expectSegment = (iterator: BufferIterator): MatroskaSegment => {
const child = parseUnknownSegment(iterator, segmentId, toDiscard);
return child;
};
+
+export const expectSegment = (iterator: BufferIterator): ParseResult => {
+ const bytesRemaining_ = iterator.bytesRemaining();
+ if (bytesRemaining_ === 0) {
+ return {
+ status: 'incomplete',
+ segments: [],
+ continueParsing: () => {
+ return expectSegment(iterator);
+ },
+ skipTo: null,
+ };
+ }
+
+ const offset = iterator.counter.getOffset();
+ const segmentId = iterator.getMatroskaSegmentId();
+ const length = iterator.getVint();
+ const bytesRemainingNow =
+ iterator.byteLength() - iterator.counter.getOffset();
+
+ if (segmentId === '0x18538067' || segmentId === '0x1f43b675') {
+ const main = expectChildren(
+ iterator,
+ length,
+ [],
+ segmentId === '0x18538067'
+ ? (s) => ({
+ type: 'main-segment',
+ children: s,
+ })
+ : (s) => ({
+ type: 'cluster-segment',
+ children: s,
+ }),
+ );
+ if (main.status === 'incomplete') {
+ return {
+ status: 'incomplete',
+ segments: main.segments,
+ skipTo: null,
+ continueParsing: main.continueParsing,
+ };
+ }
+
+ return {
+ status: 'done',
+ segments: main.segments,
+ };
+ }
+
+ if (bytesRemainingNow < length) {
+ const bytesRead = iterator.counter.getOffset() - offset;
+ iterator.counter.decrement(bytesRead);
+ return {
+ status: 'incomplete',
+ segments: [],
+ continueParsing: () => {
+ return expectSegment(iterator);
+ },
+ skipTo: null,
+ };
+ }
+
+ const segment = parseSegment(segmentId, iterator, length);
+ return {
+ status: 'done',
+ segments: [segment],
+ };
+};
diff --git a/packages/media-parser/src/boxes/webm/segments/info.ts b/packages/media-parser/src/boxes/webm/segments/info.ts
index ee87f4be57..0372c9d95b 100644
--- a/packages/media-parser/src/boxes/webm/segments/info.ts
+++ b/packages/media-parser/src/boxes/webm/segments/info.ts
@@ -12,11 +12,15 @@ export const parseInfoSegment = (
iterator: BufferIterator,
length: number,
): InfoSegment => {
- const children = expectChildren(iterator, length);
+ const children = expectChildren(iterator, length, [], null);
+
+ if (children.status === 'incomplete') {
+ throw new Error('Incomplete children');
+ }
return {
type: 'info-segment',
length,
- children,
+ children: children.segments as MatroskaSegment[],
};
};
diff --git a/packages/media-parser/src/boxes/webm/segments/main.ts b/packages/media-parser/src/boxes/webm/segments/main.ts
index c84860ec40..27f1d02c32 100644
--- a/packages/media-parser/src/boxes/webm/segments/main.ts
+++ b/packages/media-parser/src/boxes/webm/segments/main.ts
@@ -1,20 +1,6 @@
-import type {BufferIterator} from '../../../buffer-iterator';
import {type MatroskaSegment} from '../segments';
-import {expectChildren} from './parse-children';
export type MainSegment = {
type: 'main-segment';
children: MatroskaSegment[];
};
-
-export const parseMainSegment = (
- iterator: BufferIterator,
- length: number,
-): MainSegment => {
- const children = expectChildren(iterator, length);
-
- return {
- type: 'main-segment',
- children,
- };
-};
diff --git a/packages/media-parser/src/boxes/webm/segments/parse-children.ts b/packages/media-parser/src/boxes/webm/segments/parse-children.ts
index 6961a57b5d..1d2e16bc46 100644
--- a/packages/media-parser/src/boxes/webm/segments/parse-children.ts
+++ b/packages/media-parser/src/boxes/webm/segments/parse-children.ts
@@ -1,25 +1,56 @@
import type {BufferIterator} from '../../../buffer-iterator';
+import type {ParseResult} from '../../../parse-result';
import type {MatroskaSegment} from '../segments';
import {expectSegment} from '../segments';
+type WrapChildren = (segments: MatroskaSegment[]) => MatroskaSegment;
+
export const expectChildren = (
iterator: BufferIterator,
length: number,
-): MatroskaSegment[] => {
- const children: MatroskaSegment[] = [];
+ initialChildren: MatroskaSegment[],
+ wrap: WrapChildren | null,
+): ParseResult => {
+ const children: MatroskaSegment[] = [...initialChildren];
const startOffset = iterator.counter.getOffset();
while (iterator.counter.getOffset() < startOffset + length) {
+ const blockOffset = iterator.counter.getOffset();
if (iterator.bytesRemaining() === 0) {
break;
}
const child = expectSegment(iterator);
- children.push(child);
- if (child.type === 'unknown-segment') {
- break;
+
+ if (child.status === 'incomplete') {
+ const endOffset = iterator.counter.getOffset();
+ const bytesRead = endOffset - blockOffset;
+ iterator.counter.decrement(bytesRead);
+ return {
+ status: 'incomplete',
+ segments: wrap ? [wrap(children)] : children,
+ continueParsing: () => {
+ return expectChildren(
+ iterator,
+ length - (blockOffset - startOffset),
+ children,
+ wrap,
+ );
+ },
+ skipTo: null,
+ };
+ }
+
+ for (const segment of child.segments) {
+ children.push(segment as MatroskaSegment);
+ if (segment.type === 'unknown-segment') {
+ break;
+ }
}
}
- return children;
+ return {
+ status: 'done',
+ segments: wrap ? [wrap(children)] : children,
+ };
};
diff --git a/packages/media-parser/src/boxes/webm/segments/seek-head.ts b/packages/media-parser/src/boxes/webm/segments/seek-head.ts
index 3c38186fea..dd492bccc4 100644
--- a/packages/media-parser/src/boxes/webm/segments/seek-head.ts
+++ b/packages/media-parser/src/boxes/webm/segments/seek-head.ts
@@ -12,9 +12,15 @@ export const parseSeekHeadSegment = (
iterator: BufferIterator,
length: number,
): SeekHeadSegment => {
+ const children = expectChildren(iterator, length, [], null);
+
+ if (children.status === 'incomplete') {
+ throw new Error('Incomplete children');
+ }
+
return {
type: 'seek-head-segment',
length,
- children: expectChildren(iterator, length),
+ children: children.segments as MatroskaSegment[],
};
};
diff --git a/packages/media-parser/src/boxes/webm/segments/seek.ts b/packages/media-parser/src/boxes/webm/segments/seek.ts
index 694a747421..3084f556f2 100644
--- a/packages/media-parser/src/boxes/webm/segments/seek.ts
+++ b/packages/media-parser/src/boxes/webm/segments/seek.ts
@@ -12,9 +12,13 @@ export const parseSeekSegment = (iterator: BufferIterator): SeekSegment => {
const seekId = iterator.getMatroskaSegmentId();
const child = expectSegment(iterator);
+ if (child.status === 'incomplete') {
+ throw new Error('Incomplete child');
+ }
+
return {
type: 'seek-segment',
seekId,
- child,
+ child: child.segments[0] as MatroskaSegment,
};
};
diff --git a/packages/media-parser/src/boxes/webm/segments/track-entry.ts b/packages/media-parser/src/boxes/webm/segments/track-entry.ts
index 4a3105d104..de0b4202c7 100644
--- a/packages/media-parser/src/boxes/webm/segments/track-entry.ts
+++ b/packages/media-parser/src/boxes/webm/segments/track-entry.ts
@@ -12,14 +12,14 @@ export const parseTrackEntry = (
iterator: BufferIterator,
length: number,
): TrackEntrySegment => {
- const offset = iterator.counter.getOffset();
+ const children = expectChildren(iterator, length, [], null);
+ if (children.status === 'incomplete') {
+ throw new Error('Incomplete children');
+ }
return {
type: 'track-entry-segment',
- children: expectChildren(
- iterator,
- length - (iterator.counter.getOffset() - offset),
- ),
+ children: children.segments as MatroskaSegment[],
};
};
@@ -28,22 +28,6 @@ export type TrackNumberSegment = {
trackNumber: number;
};
-export const parseTrackNumber = (
- iterator: BufferIterator,
- length: number,
-): TrackNumberSegment => {
- if (length !== 1) {
- throw new Error('Expected track number to be 1 byte');
- }
-
- const trackNumber = iterator.getUint8();
-
- return {
- type: 'track-number-segment',
- trackNumber,
- };
-};
-
export type TrackUIDSegment = {
type: 'track-uid-segment';
trackUid: string;
@@ -219,9 +203,15 @@ export const parseVideoSegment = (
iterator: BufferIterator,
length: number,
): VideoSegment => {
+ const children = expectChildren(iterator, length, [], null);
+
+ if (children.status === 'incomplete') {
+ throw new Error('Incomplete children');
+ }
+
return {
type: 'video-segment',
- children: expectChildren(iterator, length),
+ children: children.segments as MatroskaSegment[],
};
};
@@ -353,7 +343,9 @@ export const parseInterlacedSegment = (
length: number,
): InterlacedSegment => {
if (length !== 1) {
- throw new Error('Expected interlaced segment to be 1 byte');
+ throw new Error(
+ 'Expected interlaced segment to be 1 byte, but is ' + length,
+ );
}
const interlaced = iterator.getUint8();
@@ -437,9 +429,15 @@ export const parseTagsSegment = (
iterator: BufferIterator,
length: number,
): TagsSegment => {
+ const children = expectChildren(iterator, length, [], null);
+
+ if (children.status === 'incomplete') {
+ throw new Error('Incomplete children');
+ }
+
return {
type: 'tags-segment',
- children: expectChildren(iterator, length),
+ children: children.segments as MatroskaSegment[],
};
};
@@ -465,16 +463,6 @@ export type ClusterSegment = {
children: MatroskaSegment[];
};
-export const parseClusterSegment = (
- iterator: BufferIterator,
- length: number,
-): ClusterSegment => {
- return {
- type: 'cluster-segment',
- children: expectChildren(iterator, length),
- };
-};
-
export type TimestampSegment = {
type: 'timestamp-segment';
timestamp: number;
@@ -484,11 +472,11 @@ export const parseTimestampSegment = (
iterator: BufferIterator,
length: number,
): TimestampSegment => {
- if (length !== 1) {
- throw new Error('Expected timestamp segment to be 1 byte');
+ if (length > 2) {
+ throw new Error('Expected timestamp segment to be 1 byte or 2 bytes');
}
- const value = iterator.getUint8();
+ const value = length === 2 ? iterator.getUint16() : iterator.getUint8();
return {
type: 'timestamp-segment',
@@ -535,3 +523,19 @@ export const parseSimpleBlockSegment = (
children: null,
};
};
+
+export const parseTrackNumber = (
+ iterator: BufferIterator,
+ length: number,
+): TrackNumberSegment => {
+ if (length !== 1) {
+ throw new Error('Expected track number to be 1 byte');
+ }
+
+ const trackNumber = iterator.getUint8();
+
+ return {
+ type: 'track-number-segment',
+ trackNumber,
+ };
+};
diff --git a/packages/media-parser/src/boxes/webm/segments/tracks.ts b/packages/media-parser/src/boxes/webm/segments/tracks.ts
index 23912358da..59a645e3fe 100644
--- a/packages/media-parser/src/boxes/webm/segments/tracks.ts
+++ b/packages/media-parser/src/boxes/webm/segments/tracks.ts
@@ -11,13 +11,14 @@ export const parseTracksSegment = (
iterator: BufferIterator,
length: number,
): TracksSegment => {
- const offset = iterator.counter.getOffset();
+ const children = expectChildren(iterator, length, [], null);
+
+ if (children.status === 'incomplete') {
+ throw new Error('Incomplete children');
+ }
return {
type: 'tracks-segment',
- children: expectChildren(
- iterator,
- length - (iterator.counter.getOffset() - offset),
- ),
+ children: children.segments as MatroskaSegment[],
};
};
diff --git a/packages/media-parser/src/buffer-iterator.ts b/packages/media-parser/src/buffer-iterator.ts
index acbcc648d7..e3b65d1eb7 100644
--- a/packages/media-parser/src/buffer-iterator.ts
+++ b/packages/media-parser/src/buffer-iterator.ts
@@ -211,6 +211,7 @@ export const getArrayBufferIterator = (
'0x88',
'0xe7',
'0xa3',
+ '0xa0',
];
if (knownIdsWithOneLength.includes(firstOneString)) {
return firstOneString;
diff --git a/packages/media-parser/src/test/matroska.test.ts b/packages/media-parser/src/test/matroska.test.ts
index d39f3a6d7c..b34fe25b7f 100644
--- a/packages/media-parser/src/test/matroska.test.ts
+++ b/packages/media-parser/src/test/matroska.test.ts
@@ -21,6 +21,7 @@ test('Should get duration of AV1 video', async () => {
width: 1920,
height: 1080,
});
+ await Bun.write('parsed.json', JSON.stringify(parsed, null, 2));
expect(parsed.boxes).toEqual([
{
type: 'main-segment',
@@ -31,42 +32,18 @@ test('Should get duration of AV1 video', async () => {
children: [
{
type: 'seek-segment',
- seekId: '0x1549a966',
+ seekId: '0x53ab8415',
child: {
- type: 'seek-position-segment',
- seekPosition: 161,
+ id: '0x49a96653',
+ type: 'unknown-segment',
},
},
{
- type: 'seek-segment',
- seekId: '0x1654ae6b',
- child: {
- type: 'seek-position-segment',
- seekPosition: 214,
- },
- },
- {
- type: 'seek-segment',
- seekId: '0x1254c367',
- child: {
- type: 'seek-position-segment',
- seekPosition: 322,
- },
- },
- {
- type: 'seek-segment',
- seekId: '0x1c53bb6b',
- child: {
- type: 'seek-position-segment',
- seekPosition: 347329,
- },
+ id: '0x054cc1ec',
+ type: 'unknown-segment',
},
],
},
- {
- type: 'void-segment',
- length: 88,
- },
{
type: 'info-segment',
length: 48,
@@ -131,8 +108,8 @@ test('Should get duration of AV1 video', async () => {
width: 1920,
},
{
- height: 1080,
type: 'height-segment',
+ height: 1080,
},
{
type: 'color-segment',
@@ -141,11 +118,11 @@ test('Should get duration of AV1 video', async () => {
],
},
{
+ type: 'codec-private-segment',
codecPrivateData: [
129, 8, 12, 0, 10, 14, 0, 0, 0, 66, 171, 191, 195, 118, 0,
8, 8, 8, 8, 32,
],
- type: 'codec-private-segment',
},
],
},
@@ -155,39 +132,302 @@ test('Should get duration of AV1 video', async () => {
type: 'tags-segment',
children: [
{
- length: 31,
type: 'tag-segment',
+ length: 31,
},
{
- length: 51,
type: 'tag-segment',
+ length: 51,
},
],
},
{
+ type: 'cluster-segment',
children: [
{
type: 'timestamp-segment',
timestamp: 0,
},
{
- length: 279307,
type: 'simple-block-segment',
+ length: 279307,
trackNumber: 1,
+ timecode: 0,
headerFlags: 128,
- invisible: false,
keyframe: true,
lacing: [0, 0],
- timecode: 0,
- children: [
- {
- type: 'unknown-segment',
- id: '0x0a0e0000',
- },
- ],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 96,
+ trackNumber: 1,
+ timecode: 40,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 556,
+ trackNumber: 1,
+ timecode: 80,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 948,
+ trackNumber: 1,
+ timecode: 120,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 577,
+ trackNumber: 1,
+ timecode: 160,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 779,
+ trackNumber: 1,
+ timecode: 200,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 793,
+ trackNumber: 1,
+ timecode: 240,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 740,
+ trackNumber: 1,
+ timecode: 280,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 1095,
+ trackNumber: 1,
+ timecode: 320,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 1097,
+ trackNumber: 1,
+ timecode: 360,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 1155,
+ trackNumber: 1,
+ timecode: 400,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 1526,
+ trackNumber: 1,
+ timecode: 440,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 1487,
+ trackNumber: 1,
+ timecode: 480,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 2046,
+ trackNumber: 1,
+ timecode: 520,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 1372,
+ trackNumber: 1,
+ timecode: 560,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 1441,
+ trackNumber: 1,
+ timecode: 600,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 2947,
+ trackNumber: 1,
+ timecode: 640,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 2652,
+ trackNumber: 1,
+ timecode: 680,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 4199,
+ trackNumber: 1,
+ timecode: 720,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 3998,
+ trackNumber: 1,
+ timecode: 760,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 6373,
+ trackNumber: 1,
+ timecode: 800,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 5955,
+ trackNumber: 1,
+ timecode: 840,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 7943,
+ trackNumber: 1,
+ timecode: 880,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 8241,
+ trackNumber: 1,
+ timecode: 920,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
+ },
+ {
+ type: 'simple-block-segment',
+ length: 9506,
+ trackNumber: 1,
+ timecode: 960,
+ headerFlags: 0,
+ keyframe: false,
+ lacing: [0, 0],
+ invisible: false,
+ children: null,
},
],
- type: 'cluster-segment',
+ },
+ {
+ id: '0x1c53bb6b',
+ type: 'unknown-segment',
},
],
},