🎥 Make videos programmatically with React
make duration work for more formats
Jonny Burger6ce76a4cbfdc
diff --git a/packages/video-parser/src/boxes/webm/segments.ts b/packages/video-parser/src/boxes/webm/segments.tsindex b63e60162c..78242434db 100644--- a/packages/video-parser/src/boxes/webm/segments.ts+++ b/packages/video-parser/src/boxes/webm/segments.ts@@ -116,15 +116,15 @@ export const expectSegment = (iterator: BufferIterator): MatroskaSegment => {return parseTimestampScaleSegment(iterator);}- if (segmentId.startsWith('0x4d80')) {+ if (segmentId === '0x4d80') {return parseMuxingSegment(iterator);}- if (segmentId.startsWith('0x5741')) {+ if (segmentId === '0x5741') {return parseWritingSegment(iterator);}- if (segmentId.startsWith('0x4489')) {+ if (segmentId === '0x4489') {return parseDurationSegment(iterator);}diff --git a/packages/video-parser/src/boxes/webm/segments/duration.ts b/packages/video-parser/src/boxes/webm/segments/duration.tsindex 8520e93eed..5aee412ded 100644--- a/packages/video-parser/src/boxes/webm/segments/duration.ts+++ b/packages/video-parser/src/boxes/webm/segments/duration.ts@@ -8,6 +8,11 @@ export type DurationSegment = {export const parseDurationSegment = (iterator: BufferIterator,): DurationSegment => {+ const length = iterator.getVint(1);+ if (length !== 8) {+ throw new Error('Expected duration segment to be 8 bytes');+ }+const duration = iterator.getFloat64();return {diff --git a/packages/video-parser/src/boxes/webm/segments/muxing.ts b/packages/video-parser/src/boxes/webm/segments/muxing.tsindex 8255804bd4..2ceec81e23 100644--- a/packages/video-parser/src/boxes/webm/segments/muxing.ts+++ b/packages/video-parser/src/boxes/webm/segments/muxing.ts@@ -8,7 +8,8 @@ export type MuxingAppSegment = {export const parseMuxingSegment = (iterator: BufferIterator,): MuxingAppSegment => {- const value = iterator.getByteString(12);+ const length = iterator.getVint(1);+ const value = iterator.getByteString(length);return {type: 'muxing-app-segment',diff --git a/packages/video-parser/src/boxes/webm/segments/writing.ts b/packages/video-parser/src/boxes/webm/segments/writing.tsindex 4135dee6bf..8bf34935aa 100644--- a/packages/video-parser/src/boxes/webm/segments/writing.ts+++ b/packages/video-parser/src/boxes/webm/segments/writing.ts@@ -8,7 +8,8 @@ export type WritingAppSegment = {export const parseWritingSegment = (iterator: BufferIterator,): WritingAppSegment => {- const value = iterator.getByteString(12);+ const length = iterator.getVint(1);+ const value = iterator.getByteString(length);return {type: 'writing-app-segment',diff --git a/packages/video-parser/src/buffer-iterator.ts b/packages/video-parser/src/buffer-iterator.tsindex ede6f0beac..67a1b44a44 100644--- a/packages/video-parser/src/buffer-iterator.ts+++ b/packages/video-parser/src/buffer-iterator.ts@@ -167,6 +167,9 @@ export const getArrayBufferIterator = (initialData: Uint8Array) => {'0xec01','0x73c5','0x53c0',+ '0x4d80',+ '0x5741',+ '0x4489',];const firstTwoString = `${firstOneString}${Array.from(diff --git a/packages/video-parser/src/get-duration.ts b/packages/video-parser/src/get-duration.tsindex b726174a5a..34e805e128 100644--- a/packages/video-parser/src/get-duration.ts+++ b/packages/video-parser/src/get-duration.ts@@ -12,20 +12,22 @@ const getDurationFromMatroska = (segments: AnySegment[]): number | null => {}const infoSegment = children.find((s) => s.type === 'info-segment');- if (!infoSegment || infoSegment.type !== 'info-segment') {- return null;- }- const timestampScale = infoSegment.children.find(+ const relevantBoxes = [+ ...mainSegment.children,+ ...(infoSegment && infoSegment.type === 'info-segment'+ ? infoSegment.children+ : []),+ ];++ const timestampScale = relevantBoxes.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',- );+ const duration = relevantBoxes.find((s) => s.type === 'duration-segment');if (!duration || duration.type !== 'duration-segment') {return null;}diff --git a/packages/video-parser/src/test/stream-local.test.ts b/packages/video-parser/src/test/stream-local.test.tsindex 9e7ef05b3a..895a24f2ea 100644--- a/packages/video-parser/src/test/stream-local.test.ts+++ b/packages/video-parser/src/test/stream-local.test.ts@@ -3,12 +3,11 @@ import {expect, test} from 'bun:test';import {nodeReader} from '../from-node';import {parseMedia} from '../get-video-metadata';-test('Should stream', async () => {+test('Should stream ISO base media', async () => {const result = await parseMedia(RenderInternals.exampleVideos.iphonevideo,{durationInSeconds: true,- boxes: true,fps: true,},nodeReader,@@ -16,3 +15,17 @@ test('Should stream', async () => {expect(result.durationInSeconds).toBe(12.568333333333333);expect(result.fps).toBe(29.99602174777881);});++test('Should stream WebM with no duration', async () => {+ const result = await parseMedia(+ RenderInternals.exampleVideos.nofps,+ {+ durationInSeconds: true,+ fps: true,+ boxes: true,+ },+ nodeReader,+ );+ expect(result.durationInSeconds).toBe(6.57);+ expect(result.fps).toBe(null);+});