Skip to content

🎥 Make videos programmatically with React

mvhd

Jonny Burger34b7a2b69aa5
diff --git a/packages/video-parser/src/boxes/iso-base-media/mvhd.ts b/packages/video-parser/src/boxes/iso-base-media/mvhd.ts
index 8e59092706..a3409f5618 100644
--- a/packages/video-parser/src/boxes/iso-base-media/mvhd.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/mvhd.ts
@@ -1,104 +1,104 @@
type ThreeDMatrix = [
- number,
- number,
- number,
- number,
- number,
- number,
- number,
- number,
- number,
+ number,
+ number,
+ number,
+ number,
+ number,
+ number,
+ number,
+ number,
+ number,
];
export type MvhdBox = {
- durationInUnits: number;
- durationInSeconds: number;
- creationTime: number | null;
- modificationTime: number | null;
- timeScale: number;
- rate: number;
- volume: number;
- matrix: ThreeDMatrix;
- nextTrackId: number;
+ durationInUnits: number;
+ durationInSeconds: number;
+ creationTime: number | null;
+ modificationTime: number | null;
+ timeScale: number;
+ rate: number;
+ volume: number;
+ matrix: ThreeDMatrix;
+ nextTrackId: number;
};
export const parseMvhd = (data: Buffer): MvhdBox => {
- let offset = 0;
-
- const size = data.readUInt32BE(offset);
- offset += 4;
- const atom = data.subarray(offset, offset + 4);
- if (atom.toString() !== "mvhd") {
- throw new Error(`Expected mvhd atom, got ${atom.toString("utf-8")}`);
- }
-
- offset += 4;
- const version = data.readUInt8(offset);
- if (version !== 0) {
- throw new Error(`Unsupported MVHD version ${version}`);
- }
-
- offset += 1;
-
- if (size !== 108) {
- throw new Error(`Expected mvhd size of version 0 to be 108, got ${size}`);
- }
-
- // Flags, we discard them
- data.subarray(offset, offset + 3);
- offset += 3;
-
- const creationTime = data.readUInt32BE(offset);
- offset += 4;
-
- const modificationTime = data.readUInt32BE(offset);
- offset += 4;
-
- const timeScale = data.readUInt32BE(offset);
- offset += 4;
-
- const durationInUnits = data.readUInt32BE(offset);
- const durationInSeconds = durationInUnits / timeScale;
- offset += 4;
-
- const rateArray = data.subarray(offset, offset + 4);
- const rate =
- rateArray[0] * 10 + rateArray[1] + rateArray[2] * 0.1 + rateArray[3] * 0.01;
- offset += 4;
-
- const volumeArray = data.subarray(offset, offset + 2);
- offset += 2;
- const volume = volumeArray[0] + volumeArray[1] * 0.1;
-
- // reserved 16bit
- offset += 2;
-
- // reserved 32bit x2
- offset += 4 * 2;
-
- // matrix
- const matrix: number[] = [];
- for (let i = 0; i < 9; i++) {
- matrix.push(data.readUInt32BE(offset));
- offset += 4;
- }
-
- // pre-defined
- offset += 4 * 6;
-
- // next track id
- const nextTrackId = data.readUInt32BE(offset);
- offset += 4;
-
- return {
- creationTime: creationTime === 0 ? null : 0,
- modificationTime: modificationTime === 0 ? null : 0,
- timeScale,
- durationInUnits,
- durationInSeconds,
- rate,
- volume,
- matrix: matrix as ThreeDMatrix,
- nextTrackId,
- };
+ let offset = 0;
+
+ const size = data.readUInt32BE(offset);
+ offset += 4;
+ const atom = data.subarray(offset, offset + 4);
+ if (atom.toString() !== 'mvhd') {
+ throw new Error(`Expected mvhd atom, got ${atom.toString('utf-8')}`);
+ }
+
+ offset += 4;
+ const version = data.readUInt8(offset);
+ if (version !== 0) {
+ throw new Error(`Unsupported MVHD version ${version}`);
+ }
+
+ offset += 1;
+
+ if (size !== 108) {
+ throw new Error(`Expected mvhd size of version 0 to be 108, got ${size}`);
+ }
+
+ // Flags, we discard them
+ data.subarray(offset, offset + 3);
+ offset += 3;
+
+ const creationTime = data.readUInt32BE(offset);
+ offset += 4;
+
+ const modificationTime = data.readUInt32BE(offset);
+ offset += 4;
+
+ const timeScale = data.readUInt32BE(offset);
+ offset += 4;
+
+ const durationInUnits = data.readUInt32BE(offset);
+ const durationInSeconds = durationInUnits / timeScale;
+ offset += 4;
+
+ const rateArray = data.subarray(offset, offset + 4);
+ const rate =
+ rateArray[0] * 10 + rateArray[1] + rateArray[2] * 0.1 + rateArray[3] * 0.01;
+ offset += 4;
+
+ const volumeArray = data.subarray(offset, offset + 2);
+ offset += 2;
+ const volume = volumeArray[0] + volumeArray[1] * 0.1;
+
+ // reserved 16bit
+ offset += 2;
+
+ // reserved 32bit x2
+ offset += 4 * 2;
+
+ // matrix
+ const matrix: number[] = [];
+ for (let i = 0; i < 9; i++) {
+ matrix.push(data.readUInt32BE(offset));
+ offset += 4;
+ }
+
+ // pre-defined
+ offset += 4 * 6;
+
+ // next track id
+ const nextTrackId = data.readUInt32BE(offset);
+ offset += 4;
+
+ return {
+ creationTime: creationTime === 0 ? null : 0,
+ modificationTime: modificationTime === 0 ? null : 0,
+ timeScale,
+ durationInUnits,
+ durationInSeconds,
+ rate,
+ volume,
+ matrix: matrix as ThreeDMatrix,
+ nextTrackId,
+ };
};
diff --git a/packages/video-parser/src/get-duration.ts b/packages/video-parser/src/get-duration.ts
index e187c38aab..9cd671d6fb 100644
--- a/packages/video-parser/src/get-duration.ts
+++ b/packages/video-parser/src/get-duration.ts
@@ -1,27 +1,27 @@
-import { Box } from "./parse-video";
+import {Box} from './parse-video';
export const getDuration = (boxes: Box[]): number | null => {
- const moovBox = boxes.find((b) => b.boxType === "moov");
- if (!moovBox) {
- return null;
- }
- const extraData = moovBox.extraData;
- if (!extraData) {
- return null;
- }
+ const moovBox = boxes.find((b) => b.boxType === 'moov');
+ if (!moovBox) {
+ return null;
+ }
+ const extraData = moovBox.extraData;
+ if (!extraData) {
+ return null;
+ }
- if (extraData.type !== "boxes") {
- return null;
- }
+ if (extraData.type !== 'boxes') {
+ return null;
+ }
- const mvhdBox = extraData.boxes.find((b) => b.boxType === "mvhd");
- if (!mvhdBox || !mvhdBox.extraData) {
- return null;
- }
+ const mvhdBox = extraData.boxes.find((b) => b.boxType === 'mvhd');
+ if (!mvhdBox || !mvhdBox.extraData) {
+ return null;
+ }
- if (mvhdBox.extraData.type !== "mvhd-box") {
- throw new Error("Expected mvhd-box");
- }
+ if (mvhdBox.extraData.type !== 'mvhd-box') {
+ throw new Error('Expected mvhd-box');
+ }
- return mvhdBox.extraData.box.durationInSeconds;
+ return mvhdBox.extraData.box.durationInSeconds;
};
diff --git a/packages/video-parser/src/index.ts b/packages/video-parser/src/index.ts
index 79163c6963..d83a1715fe 100644
--- a/packages/video-parser/src/index.ts
+++ b/packages/video-parser/src/index.ts
@@ -1,2 +1,2 @@
-export { parseVideo } from "./parse-video";
-export { getDuration } from "./get-duration";
+export {getDuration} from './get-duration';
+export {parseVideo} from './parse-video';
diff --git a/packages/video-parser/src/test/mvhd.test.ts b/packages/video-parser/src/test/mvhd.test.ts
index 5e2c38f101..d147408a72 100644
--- a/packages/video-parser/src/test/mvhd.test.ts
+++ b/packages/video-parser/src/test/mvhd.test.ts
@@ -1,78 +1,78 @@
-import { test, expect } from "bun:test";
-import { parseMvhd } from "../boxes/iso-base-media/mvhd";
+import {expect, test} from 'bun:test';
+import {parseMvhd} from '../boxes/iso-base-media/mvhd';
-test("Should be able to parse a MVHD box correctly", () => {
- const buffer = Buffer.from([
- // size, 32 bit
- 0, 0, 0, 108,
- // mvhd atom, 32 bit
- 109, 118, 104, 100,
- // version, 8 unsigned bit
- 0,
- // flags
- 0, 0, 0,
- // creation time
- 0, 0, 0, 0,
- // modification time
- 0, 0, 0, 0,
- // time scale
- 0, 0, 3, 232,
- // duration
- 0, 0, 16, 71,
- // rate
- 0, 1, 0, 0,
- // volume
- 1, 0,
- // reserved 16 bit
- 0, 0,
- // reserved 32 bit [0]
- 0, 0, 0, 0,
- // reserved 32 bit [1]
- 0, 0, 0, 0,
- // matrix [0]
- 0, 1, 0, 0,
- // matrix [1]
- 0, 0, 0, 0,
- // matrix [2]
- 0, 0, 0, 0,
- // matrix [3]
- 0, 0, 0, 0,
- // matrix [4]
- 0, 1, 0, 0,
- // matrix [5]
- 0, 0, 0, 0,
- // matrix [6]
- 0, 0, 0, 0,
- // matrix [7]
- 0, 0, 0, 0,
- // matrix [8]
- 64, 0, 0, 0,
- // predefined [0]
- 0, 0, 0, 0,
- // predefined [1]
- 0, 0, 0, 0,
- // predefined [2]
- 0, 0, 0, 0,
- // predefined [3]
- 0, 0, 0, 0,
- // predefined [4]
- 0, 0, 0, 0,
- // predefined [5]
- 0, 0, 0, 0,
- // next track id
- 0, 0, 0, 2,
- ]);
+test('Should be able to parse a MVHD box correctly', () => {
+ const buffer = Buffer.from([
+ // size, 32 bit
+ 0, 0, 0, 108,
+ // mvhd atom, 32 bit
+ 109, 118, 104, 100,
+ // version, 8 unsigned bit
+ 0,
+ // flags
+ 0, 0, 0,
+ // creation time
+ 0, 0, 0, 0,
+ // modification time
+ 0, 0, 0, 0,
+ // time scale
+ 0, 0, 3, 232,
+ // duration
+ 0, 0, 16, 71,
+ // rate
+ 0, 1, 0, 0,
+ // volume
+ 1, 0,
+ // reserved 16 bit
+ 0, 0,
+ // reserved 32 bit [0]
+ 0, 0, 0, 0,
+ // reserved 32 bit [1]
+ 0, 0, 0, 0,
+ // matrix [0]
+ 0, 1, 0, 0,
+ // matrix [1]
+ 0, 0, 0, 0,
+ // matrix [2]
+ 0, 0, 0, 0,
+ // matrix [3]
+ 0, 0, 0, 0,
+ // matrix [4]
+ 0, 1, 0, 0,
+ // matrix [5]
+ 0, 0, 0, 0,
+ // matrix [6]
+ 0, 0, 0, 0,
+ // matrix [7]
+ 0, 0, 0, 0,
+ // matrix [8]
+ 64, 0, 0, 0,
+ // predefined [0]
+ 0, 0, 0, 0,
+ // predefined [1]
+ 0, 0, 0, 0,
+ // predefined [2]
+ 0, 0, 0, 0,
+ // predefined [3]
+ 0, 0, 0, 0,
+ // predefined [4]
+ 0, 0, 0, 0,
+ // predefined [5]
+ 0, 0, 0, 0,
+ // next track id
+ 0, 0, 0, 2,
+ ]);
- const mvhd = parseMvhd(buffer);
- expect(mvhd).toEqual({
- creationTime: null,
- modificationTime: null,
- timeScale: 1000,
- durationInUnits: 4167,
- durationInSeconds: 4.167,
- rate: 1,
- volume: 1,
- matrix: [65536, 0, 0, 0, 65536, 0, 0, 0, 1073741824],
- nextTrackId: 2,
- });
+ const mvhd = parseMvhd(buffer);
+ expect(mvhd).toEqual({
+ creationTime: null,
+ modificationTime: null,
+ timeScale: 1000,
+ durationInUnits: 4167,
+ durationInSeconds: 4.167,
+ rate: 1,
+ volume: 1,
+ matrix: [65536, 0, 0, 0, 65536, 0, 0, 0, 1073741824],
+ nextTrackId: 2,
+ });
});