Skip to content

🎥 Make videos programmatically with React

matroska duration

Jonny Burger9b8d63215555
diff --git a/packages/video-parser/src/get-duration.ts b/packages/video-parser/src/get-duration.ts
index 80560fa5e1..4ebac3b349 100644
--- a/packages/video-parser/src/get-duration.ts
+++ b/packages/video-parser/src/get-duration.ts
@@ -1,6 +1,44 @@
import type {AnySegment} from './parse-video';
+const getDurationFromMatroska = (segments: AnySegment[]): number | null => {
+ const mainSegment = segments.find((s) => s.type === 'main-segment');
+ if (!mainSegment || mainSegment.type !== 'main-segment') {
+ return null;
+ }
+
+ const {children} = mainSegment;
+ if (!children) {
+ return null;
+ }
+
+ const infoSegment = children.find((s) => s.type === 'info-segment');
+ if (!infoSegment || infoSegment.type !== 'info-segment') {
+ return null;
+ }
+
+ const timestampScale = infoSegment.children.find(
+ (s) => s.type === 'timestamp-scale-segment',
+ );
+ if (!timestampScale || timestampScale.type !== 'timestamp-scale-segment') {
+ return null;
+ }
+
+ const duration = infoSegment.children.find(
+ (s) => s.type === 'duration-segment',
+ );
+ if (!duration || duration.type !== 'duration-segment') {
+ return null;
+ }
+
+ return (duration.duration / timestampScale.timestampScale) * 1000;
+};
+
export const getDuration = (boxes: AnySegment[]): number | null => {
+ const matroskaBox = boxes.find((b) => b.type === 'main-segment');
+ if (matroskaBox) {
+ return getDurationFromMatroska(boxes);
+ }
+
const moovBox = boxes.find((b) => b.type === 'moov-box');
if (!moovBox || moovBox.type !== 'moov-box') {
return null;
diff --git a/packages/video-parser/src/test/duration.test.ts b/packages/video-parser/src/test/duration.test.ts
index 723502f762..2ddb0b4741 100644
--- a/packages/video-parser/src/test/duration.test.ts
+++ b/packages/video-parser/src/test/duration.test.ts
@@ -17,71 +17,3 @@ test('Should get duration of HEVC video', async () => {
expect(getDuration(parsed)).toBe(3.4);
expect(getDimensions(parsed)).toEqual([1920, 1080]);
});
-
-test('Should get duration of AV1 video', async () => {
- const parsed = await parseVideo(exampleVideos.av1, Infinity);
- // TODO: AV1 duration is not yet supported
- expect(parsed).toEqual([
- {
- type: 'main-segment',
- children: [
- {
- type: 'seek-head-segment',
- length: 59,
- children: [
- {
- type: 'seek-segment',
- seekId: '0x1549a966',
- child: {type: 'seek-position-segment', seekPosition: 161},
- },
- {
- 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},
- },
- ],
- },
- {
- length: 88,
- type: 'void-segment',
- },
- {
- length: 48,
- type: 'info-segment',
- children: [
- {
- timestampScale: 1000000,
- type: 'timestamp-scale-segment',
- },
- {
- type: 'muxing-app-segment',
- value: 'Lavf60.3.100',
- },
- {
- type: 'writing-app-segment',
- value: 'Lavf60.3.100',
- },
- {
- duration: 1000,
- type: 'duration-segment',
- },
- ],
- },
- {
- type: 'unknown-segment',
- id: '0x1654ae6b',
- },
- ],
- },
- ]);
-});
diff --git a/packages/video-parser/src/test/matroska.test.ts b/packages/video-parser/src/test/matroska.test.ts
new file mode 100644
index 0000000000..c0d61cef25
--- /dev/null
+++ b/packages/video-parser/src/test/matroska.test.ts
@@ -0,0 +1,73 @@
+import {expect, test} from 'bun:test';
+import {getDuration} from '../get-duration';
+import {parseVideo} from '../parse-video';
+import {exampleVideos} from './example-videos';
+
+test('Should get duration of AV1 video', async () => {
+ const parsed = await parseVideo(exampleVideos.av1, Infinity);
+ expect(parsed).toEqual([
+ {
+ type: 'main-segment',
+ children: [
+ {
+ type: 'seek-head-segment',
+ length: 59,
+ children: [
+ {
+ type: 'seek-segment',
+ seekId: '0x1549a966',
+ child: {type: 'seek-position-segment', seekPosition: 161},
+ },
+ {
+ 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},
+ },
+ ],
+ },
+ {
+ length: 88,
+ type: 'void-segment',
+ },
+ {
+ length: 48,
+ type: 'info-segment',
+ children: [
+ {
+ timestampScale: 1000000,
+ type: 'timestamp-scale-segment',
+ },
+ {
+ type: 'muxing-app-segment',
+ value: 'Lavf60.3.100',
+ },
+ {
+ type: 'writing-app-segment',
+ value: 'Lavf60.3.100',
+ },
+ {
+ duration: 1000,
+ type: 'duration-segment',
+ },
+ ],
+ },
+ {
+ type: 'unknown-segment',
+ id: '0x1654ae6b',
+ },
+ ],
+ },
+ ]);
+
+ expect(getDuration(parsed)).toBe(1);
+});