Skip to content

🎥 Make videos programmatically with React

alright

Jonny Burgerdb59bb8bd185
diff --git a/packages/video-parser/src/boxes/iso-base-media/mdia.ts b/packages/video-parser/src/boxes/iso-base-media/mdia.ts
new file mode 100644
index 0000000000..db47930a2b
--- /dev/null
+++ b/packages/video-parser/src/boxes/iso-base-media/mdia.ts
@@ -0,0 +1,35 @@
+import type {Box} from '../../parse-video';
+import type {BaseBox} from './base-type';
+import {parseBoxes} from './process-box';
+
+export interface MdiaBox extends BaseBox {
+ type: 'mdia-box';
+ children: Box[];
+}
+
+export const parseMdia = (data: Buffer, offset: number): MdiaBox => {
+ let chunkOffset = 0;
+
+ const size = data.readUInt32BE(chunkOffset);
+ chunkOffset += 4;
+
+ if (size !== data.length) {
+ throw new Error(`Data size of version 0 is ${size}, got ${data.length}`);
+ }
+
+ const atom = data.subarray(chunkOffset, chunkOffset + 4);
+ if (atom.toString() !== 'mdia') {
+ throw new Error(`Expected mdia atom, got ${atom.toString('utf-8')}`);
+ }
+
+ chunkOffset += 4;
+
+ const children = parseBoxes(data.subarray(chunkOffset), offset + chunkOffset);
+
+ return {
+ type: 'mdia-box',
+ boxSize: data.length,
+ offset,
+ 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 6a69fc6df1..95c81a637e 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,11 +1,13 @@
import type {Box, BoxAndNext} from '../../parse-video';
import {parseDims} from './dims';
import {fourByteToNumber, parseFtyp} from './ftype';
+import {parseMdia} from './mdia';
import {parseMoov} from './moov/moov';
import {parseMvhd} from './mvhd';
import {parseMebx} from './stsd/mebx';
import {parseStsd} from './stsd/stsd';
import {parseTkhd} from './tkhd';
+import {parseTrak} from './trak/trak';
const processBoxAndSubtract = ({
data,
@@ -82,9 +84,23 @@ const processBoxAndSubtract = ({
};
}
+ if (boxType === 'trak') {
+ return {
+ box: parseTrak(sub, fileOffset),
+ next,
+ size: boxSize,
+ };
+ }
+
+ if (boxType === 'mdia') {
+ return {
+ box: parseMdia(sub, fileOffset),
+ next,
+ size: boxSize,
+ };
+ }
+
const children =
- boxType === 'moov' ||
- boxType === 'trak' ||
boxType === 'mdia' ||
boxType === 'minf' ||
boxType === 'stbl' ||
diff --git a/packages/video-parser/src/boxes/iso-base-media/trak/trak.ts b/packages/video-parser/src/boxes/iso-base-media/trak/trak.ts
new file mode 100644
index 0000000000..5f2d8fc07c
--- /dev/null
+++ b/packages/video-parser/src/boxes/iso-base-media/trak/trak.ts
@@ -0,0 +1,35 @@
+import type {Box} from '../../../parse-video';
+import type {BaseBox} from '../base-type';
+import {parseBoxes} from '../process-box';
+
+export interface TrakBox extends BaseBox {
+ type: 'trak-box';
+ children: Box[];
+}
+
+export const parseTrak = (data: Buffer, offset: number): TrakBox => {
+ let chunkOffset = 0;
+
+ const size = data.readUInt32BE(chunkOffset);
+ chunkOffset += 4;
+
+ if (size !== data.length) {
+ throw new Error(`Data size of version 0 is ${size}, got ${data.length}`);
+ }
+
+ const atom = data.subarray(chunkOffset, chunkOffset + 4);
+ if (atom.toString() !== 'trak') {
+ throw new Error(`Expected trak atom, got ${atom.toString('utf-8')}`);
+ }
+
+ chunkOffset += 4;
+
+ const children = parseBoxes(data.subarray(chunkOffset), chunkOffset);
+
+ return {
+ offset,
+ boxSize: data.length,
+ type: 'trak-box',
+ children,
+ };
+};
diff --git a/packages/video-parser/src/parse-video.ts b/packages/video-parser/src/parse-video.ts
index 76de7f73da..626d4d1921 100644
--- a/packages/video-parser/src/parse-video.ts
+++ b/packages/video-parser/src/parse-video.ts
@@ -2,6 +2,7 @@ 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 {MdiaBox} from './boxes/iso-base-media/mdia';
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';
@@ -9,6 +10,7 @@ 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';
+import type {TrakBox} from './boxes/iso-base-media/trak/trak';
interface RegularBox extends BaseBox {
boxType: string;
@@ -27,7 +29,9 @@ export type Box =
| StsdBox
| MebxBox
| KeysBox
- | MoovBox;
+ | MoovBox
+ | TrakBox
+ | MdiaBox;
export type BoxAndNext = {
box: Box;
diff --git a/packages/video-parser/src/test/duration.test.ts b/packages/video-parser/src/test/duration.test.ts
index b612b8f40a..0019d1d1e0 100644
--- a/packages/video-parser/src/test/duration.test.ts
+++ b/packages/video-parser/src/test/duration.test.ts
@@ -26,7 +26,12 @@ test('Should get duration of HEVC video', async () => {
throw new Error('Expected regular box');
}
- console.log(moovBox);
+ const trak = moovBox.children[1];
+ if (trak.type !== 'trak-box') {
+ throw new Error('Expected trak box');
+ }
+
+ console.log(trak.children[3]);
const duration = getDuration(parsed);
expect(duration).toBe(3.4);