Skip to content

🎥 Make videos programmatically with React

parse info segment

Jonny Burger9b0a8b4a27d2
diff --git a/packages/video-parser/src/boxes/webm/segments.ts b/packages/video-parser/src/boxes/webm/segments.ts
index 73fa32b9dd..ee743201b4 100644
--- a/packages/video-parser/src/boxes/webm/segments.ts
+++ b/packages/video-parser/src/boxes/webm/segments.ts
@@ -1,4 +1,6 @@
import type {BufferIterator} from '../../read-and-increment-offset';
+import type {InfoSegment} from './segments/info';
+import {parseInfoSegment} from './segments/info';
import type {MainSegment} from './segments/main';
import {parseMainSegment} from './segments/main';
import {parseSeekSegment, type SeekSegment} from './segments/seek';
@@ -8,6 +10,8 @@ import {
parseSeekPositionSegment,
type SeekPositionSegment,
} from './segments/seek-position';
+import type {TimestampScaleSegment} from './segments/timestamp-scale';
+import {parseTimestampScaleSegment} from './segments/timestamp-scale';
import type {UnknownSegment} from './segments/unknown';
import {parseUnknownSegment} from './segments/unknown';
import type {VoidSegment} from './segments/void';
@@ -19,7 +23,9 @@ export type MatroskaSegment =
| SeekHeadSegment
| SeekSegment
| SeekPositionSegment
- | VoidSegment;
+ | VoidSegment
+ | InfoSegment
+ | TimestampScaleSegment;
export const expectSegment = (iterator: BufferIterator) => {
const segmentId = iterator.getMatroskaSegmentId();
@@ -44,6 +50,15 @@ export const expectSegment = (iterator: BufferIterator) => {
return parseVoidSegment(iterator);
}
+ if (segmentId === '0x1549a966') {
+ return parseInfoSegment(iterator);
+ }
+
+ console.log(segmentId);
+ if (segmentId === '0x2ad7b183') {
+ return parseTimestampScaleSegment(iterator);
+ }
+
const length = iterator.getVint(8);
const child = parseUnknownSegment(iterator, segmentId, length);
return child;
diff --git a/packages/video-parser/src/boxes/webm/segments/info.ts b/packages/video-parser/src/boxes/webm/segments/info.ts
new file mode 100644
index 0000000000..ed50cafed6
--- /dev/null
+++ b/packages/video-parser/src/boxes/webm/segments/info.ts
@@ -0,0 +1,20 @@
+import type {BufferIterator} from '../../../read-and-increment-offset';
+import type {MatroskaSegment} from '../segments';
+import {expectChildren} from './parse-children';
+
+export type InfoSegment = {
+ type: 'info-segment';
+ length: number;
+ children: MatroskaSegment[];
+};
+
+export const parseInfoSegment = (iterator: BufferIterator): InfoSegment => {
+ const length = iterator.getVint(1);
+ const children = expectChildren(iterator, length);
+
+ return {
+ type: 'info-segment',
+ length,
+ children,
+ };
+};
diff --git a/packages/video-parser/src/boxes/webm/segments/timestamp-scale.ts b/packages/video-parser/src/boxes/webm/segments/timestamp-scale.ts
new file mode 100644
index 0000000000..a6998c7832
--- /dev/null
+++ b/packages/video-parser/src/boxes/webm/segments/timestamp-scale.ts
@@ -0,0 +1,17 @@
+import type {BufferIterator} from '../../../read-and-increment-offset';
+
+export type TimestampScaleSegment = {
+ type: 'timestamp-scale-segment';
+ timestampScale: number;
+};
+
+export const parseTimestampScaleSegment = (
+ iterator: BufferIterator,
+): TimestampScaleSegment => {
+ const timestampScale = iterator.getDecimalBytes(3);
+
+ return {
+ type: 'timestamp-scale-segment',
+ timestampScale,
+ };
+};
diff --git a/packages/video-parser/src/test/duration.test.ts b/packages/video-parser/src/test/duration.test.ts
index d421a9a4d6..065e857fdb 100644
--- a/packages/video-parser/src/test/duration.test.ts
+++ b/packages/video-parser/src/test/duration.test.ts
@@ -55,9 +55,23 @@ test('Should get duration of AV1 video', async () => {
length: 88,
type: 'void-segment',
},
+ {
+ length: 48,
+ type: 'info-segment',
+ children: [
+ {
+ timestampScale: 1000000,
+ type: 'timestamp-scale-segment',
+ },
+ {
+ id: '0x4d808c4c',
+ type: 'unknown-segment',
+ },
+ ],
+ },
{
type: 'unknown-segment',
- id: '0x1549a966',
+ id: '0xdbb4c607',
},
],
},