Skip to content

🎥 Make videos programmatically with React

get it working

Jonny Burgerb559289cd60e
diff --git a/packages/video-parser/src/boxes/iso-base-media/moov/moov.ts b/packages/video-parser/src/boxes/iso-base-media/moov/moov.ts
new file mode 100644
index 0000000000..7bd767f7c2
--- /dev/null
+++ b/packages/video-parser/src/boxes/iso-base-media/moov/moov.ts
@@ -0,0 +1,32 @@
+import type {Box} from '../../../parse-video';
+import type {BaseBox} from '../base-type';
+import {parseBoxes} from '../process-box';
+
+export interface MoovBox extends BaseBox {
+ type: 'moov-box';
+ children: Box[];
+}
+
+export const parseMoov = (data: Buffer, offset: number): MoovBox => {
+ let chunkOffset = 0;
+ const size = data.readUInt32BE(0);
+ chunkOffset += 4;
+ if (size !== data.length) {
+ throw new Error(`Expected moov size of ${data.length}, got ${size}`);
+ }
+
+ const type = data.subarray(chunkOffset, chunkOffset + 4).toString('utf-8');
+ chunkOffset += 4;
+ if (type !== 'moov') {
+ throw new Error(`Expected moov type of moov, got ${type}`);
+ }
+
+ const children = parseBoxes(data.subarray(chunkOffset), offset + chunkOffset);
+
+ return {
+ offset,
+ boxSize: data.length,
+ type: 'moov-box',
+ children,
+ };
+};
diff --git a/packages/video-parser/src/boxes/iso-base-media/process-box.ts b/packages/video-parser/src/boxes/iso-base-media/process-box.ts
index 666bf86f32..6a69fc6df1 100644
--- a/packages/video-parser/src/boxes/iso-base-media/process-box.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/process-box.ts
@@ -1,17 +1,12 @@
-import type {Box} from '../../parse-video';
+import type {Box, BoxAndNext} from '../../parse-video';
import {parseDims} from './dims';
import {fourByteToNumber, parseFtyp} from './ftype';
+import {parseMoov} from './moov/moov';
import {parseMvhd} from './mvhd';
import {parseMebx} from './stsd/mebx';
import {parseStsd} from './stsd/stsd';
import {parseTkhd} from './tkhd';
-type BoxAndNext = {
- box: Box;
- next: Buffer;
- size: number;
-};
-
const processBoxAndSubtract = ({
data,
fileOffset,
@@ -79,6 +74,14 @@ const processBoxAndSubtract = ({
};
}
+ if (boxType === 'moov') {
+ return {
+ box: parseMoov(sub, fileOffset),
+ next,
+ size: boxSize,
+ };
+ }
+
const children =
boxType === 'moov' ||
boxType === 'trak' ||
diff --git a/packages/video-parser/src/get-duration.ts b/packages/video-parser/src/get-duration.ts
index 98b069573b..c37877939d 100644
--- a/packages/video-parser/src/get-duration.ts
+++ b/packages/video-parser/src/get-duration.ts
@@ -1,10 +1,8 @@
import type {Box} from './parse-video';
export const getDuration = (boxes: Box[]): number | null => {
- const moovBox = boxes.find(
- (b) => b.type === 'regular-box' && b.boxType === 'moov',
- );
- if (!moovBox || moovBox.type !== 'regular-box') {
+ const moovBox = boxes.find((b) => b.type === 'moov-box');
+ if (!moovBox || moovBox.type !== 'moov-box') {
return null;
}
diff --git a/packages/video-parser/src/parse-video.ts b/packages/video-parser/src/parse-video.ts
index 5f286cea2f..76de7f73da 100644
--- a/packages/video-parser/src/parse-video.ts
+++ b/packages/video-parser/src/parse-video.ts
@@ -2,8 +2,10 @@ import {createReadStream} from 'fs';
import type {BaseBox} from './boxes/iso-base-media/base-type';
import type {DimensionsBox} from './boxes/iso-base-media/dims';
import type {FtypBox} from './boxes/iso-base-media/ftype';
+import type {MoovBox} from './boxes/iso-base-media/moov/moov';
import type {MvhdBox} from './boxes/iso-base-media/mvhd';
import {parseBoxes} from './boxes/iso-base-media/process-box';
+import type {KeysBox} from './boxes/iso-base-media/stsd/keys';
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';
@@ -23,7 +25,15 @@ export type Box =
| TkhdBox
| DimensionsBox
| StsdBox
- | MebxBox;
+ | MebxBox
+ | KeysBox
+ | MoovBox;
+
+export type BoxAndNext = {
+ box: Box;
+ next: Buffer;
+ size: number;
+};
const isoBaseMediaMp4Pattern = Buffer.from('ftyp');
diff --git a/packages/video-parser/src/test/duration.test.ts b/packages/video-parser/src/test/duration.test.ts
index 0678b6b6fd..b612b8f40a 100644
--- a/packages/video-parser/src/test/duration.test.ts
+++ b/packages/video-parser/src/test/duration.test.ts
@@ -21,11 +21,13 @@ test('Should get duration of HEVC video', async () => {
throw new Error('No parsed data');
}
- const t = parsed[3];
- if (t.type !== 'regular-box') {
+ const moovBox = parsed[3];
+ if (moovBox.type !== 'moov-box') {
throw new Error('Expected regular box');
}
+ console.log(moovBox);
+
const duration = getDuration(parsed);
expect(duration).toBe(3.4);
});