Skip to content

🎥 Make videos programmatically with React

MP4 muxer

Jonny Burger5c79455b4df9
diff --git a/packages/media-parser/src/create/iso-base-media/create-ftyp.ts b/packages/media-parser/src/create/iso-base-media/create-ftyp.ts
index f8dbc162bb..22e59a1a29 100644
--- a/packages/media-parser/src/create/iso-base-media/create-ftyp.ts
+++ b/packages/media-parser/src/create/iso-base-media/create-ftyp.ts
@@ -1,21 +1,9 @@
import {combineUint8Arrays} from '../../boxes/webm/make-header';
-
-const stringsToUint8Array = (str: string) => {
- return new TextEncoder().encode(str);
-};
-
-const numberToUint8Array = (num: number) => {
- return new Uint8Array([
- (num >> 24) & 0xff,
- (num >> 16) & 0xff,
- (num >> 8) & 0xff,
- num & 0xff,
- ]);
-};
-
-export const addSize = (arr: Uint8Array) => {
- return combineUint8Arrays([numberToUint8Array(arr.length + 4), arr]);
-};
+import {
+ addSize,
+ numberTo32BitUIntOrInt,
+ stringsToUint8Array,
+} from './primitives';
export const createFtyp = ({
majorBrand,
@@ -28,7 +16,7 @@ export const createFtyp = ({
}) => {
const type = stringsToUint8Array('ftyp');
const majorBrandArr = stringsToUint8Array(majorBrand);
- const minorBrandArr = numberToUint8Array(minorBrand);
+ const minorBrandArr = numberTo32BitUIntOrInt(minorBrand);
const compatibleBrandsArr = combineUint8Arrays(
compatibleBrands.map((b) => stringsToUint8Array(b)),
);
diff --git a/packages/media-parser/src/create/iso-base-media/create-mvhd.ts b/packages/media-parser/src/create/iso-base-media/create-mvhd.ts
new file mode 100644
index 0000000000..b0dd80d79d
--- /dev/null
+++ b/packages/media-parser/src/create/iso-base-media/create-mvhd.ts
@@ -0,0 +1,77 @@
+import {combineUint8Arrays} from '../../boxes/webm/make-header';
+import {
+ addSize,
+ floatTo16Point1632Bit,
+ floatTo16Point16_16Bit,
+ numberTo32BitUIntOrInt,
+ setFixedPointSigned1616Number,
+ setFixedPointSigned230Number,
+ stringsToUint8Array,
+} from './primitives';
+
+const serializeMatrix = (matrix: number[]) => {
+ const result: Uint8Array[] = [];
+ for (let i = 0; i < matrix.length; i++) {
+ if (i % 3 === 2) {
+ result.push(setFixedPointSigned230Number(matrix[i]));
+ } else {
+ result.push(setFixedPointSigned1616Number(matrix[i]));
+ }
+ }
+
+ return combineUint8Arrays(result);
+};
+
+export const createMvhd = ({
+ timescale,
+ durationInUnits,
+ rate,
+ volume,
+ nextTrackId,
+ matrix,
+}: {
+ timescale: number;
+ durationInUnits: number;
+ rate: number;
+ volume: number;
+ nextTrackId: number;
+ matrix: number[];
+}) => {
+ if (matrix.length !== 9) {
+ throw new Error('Matrix must be 9 elements long');
+ }
+
+ const content = combineUint8Arrays([
+ // type
+ stringsToUint8Array('mvhd'),
+ // version
+ new Uint8Array([0]),
+ // flags
+ new Uint8Array([0, 0, 0]),
+ // creation time, 32bit for version 0
+ numberTo32BitUIntOrInt(0),
+ // modification time, 32bit for version 0
+ numberTo32BitUIntOrInt(0),
+ // timescale
+ numberTo32BitUIntOrInt(timescale),
+ // duration in units, 32bit for version 0
+ numberTo32BitUIntOrInt(durationInUnits),
+ // rate
+ floatTo16Point1632Bit(rate),
+ // volume
+ floatTo16Point16_16Bit(volume),
+ // reserved
+ new Uint8Array([0, 0]),
+ // reserved
+ new Uint8Array([0, 0, 0, 0]),
+ // reserved
+ new Uint8Array([0, 0, 0, 0]),
+ serializeMatrix(matrix),
+ // predefined
+ combineUint8Arrays(new Array(6).fill(new Uint8Array([0, 0, 0, 0]))),
+ // next track id
+ numberTo32BitUIntOrInt(nextTrackId),
+ ]);
+
+ return addSize(content);
+};
diff --git a/packages/media-parser/src/create/iso-base-media/primitives.ts b/packages/media-parser/src/create/iso-base-media/primitives.ts
new file mode 100644
index 0000000000..35003ffd57
--- /dev/null
+++ b/packages/media-parser/src/create/iso-base-media/primitives.ts
@@ -0,0 +1,68 @@
+import {combineUint8Arrays} from '../../boxes/webm/make-header';
+
+export const stringsToUint8Array = (str: string) => {
+ return new TextEncoder().encode(str);
+};
+
+export const numberTo32BitUIntOrInt = (num: number) => {
+ return new Uint8Array([
+ (num >> 24) & 0xff,
+ (num >> 16) & 0xff,
+ (num >> 8) & 0xff,
+ num & 0xff,
+ ]);
+};
+
+export const setFixedPointSigned1616Number = (num: number) => {
+ const val = Math.round(num * 2 ** 16);
+ return numberTo32BitUIntOrInt(val);
+};
+
+export const setFixedPointSigned230Number = (num: number) => {
+ const val = Math.round(num * 2 ** 30);
+ return numberTo32BitUIntOrInt(val);
+};
+
+export const addSize = (arr: Uint8Array) => {
+ return combineUint8Arrays([numberTo32BitUIntOrInt(arr.length + 4), arr]);
+};
+
+export const floatTo16Point1632Bit = (number: number) => {
+ // Ensure the number has exactly 2 decimal places
+ const fixedNumber = Number(number.toFixed(2));
+
+ // Create a new Uint8Array of 4 bytes
+ const result = new Uint8Array(4);
+
+ // Extract digits
+ const tens = Math.floor(fixedNumber / 10);
+ const ones = Math.floor(fixedNumber % 10);
+ const tenths = Math.floor((fixedNumber * 10) % 10);
+ const hundredths = Math.floor((fixedNumber * 100) % 10);
+
+ // Assign to array
+ result[0] = tens;
+ result[1] = ones;
+ result[2] = tenths;
+ result[3] = hundredths;
+
+ return result;
+};
+
+export const floatTo16Point16_16Bit = (number: number) => {
+ // Ensure the number has exactly 2 decimal places
+ const fixedNumber = Number(number.toFixed(2));
+
+ // Create a new Uint8Array of 4 bytes
+ const result = new Uint8Array(2);
+
+ // Extract digits
+ const ones = Math.floor(fixedNumber % 10);
+ const tenths = Math.floor((fixedNumber * 10) % 10);
+
+ // Assign to array
+ result[0] = ones;
+ result[1] = tenths;
+
+ return result;
+};
diff --git a/packages/media-parser/src/test/create-mvhd.test.ts b/packages/media-parser/src/test/create-mvhd.test.ts
new file mode 100644
index 0000000000..ad932f7942
--- /dev/null
+++ b/packages/media-parser/src/test/create-mvhd.test.ts
@@ -0,0 +1,110 @@
+import {expect, test} from 'bun:test';
+import {parseMvhd} from '../boxes/iso-base-media/mvhd';
+import {getArrayBufferIterator} from '../buffer-iterator';
+import {createMvhd} from '../create/iso-base-media/create-mvhd';
+
+const input = new Uint8Array([
+ // size
+ 0x00, 0x00, 0x00, 0x6c,
+ // mvhd
+ 0x6d, 0x76, 0x68, 0x64,
+ // version
+ 0x00,
+ // flags
+ 0x00, 0x00, 0x00,
+ // creation time
+ 0x00, 0x00, 0x00, 0x00,
+ // modification time
+ 0x00, 0x00, 0x00, 0x00,
+ // timescale
+ 0x00, 0x00, 0x03, 0xe8,
+ // duration in units
+ 0x00, 0x00, 0x00, 0x00,
+ // rate
+ 0x00, 0x01, 0x00, 0x00,
+ // volume
+ 0x01, 0x00,
+ // reserved
+ 0x00, 0x00,
+ // reserved
+ 0x00, 0x00, 0x00, 0x00,
+ // reserved
+ 0x00, 0x00, 0x00, 0x00,
+ // matrix[0]
+ 0x00, 0x01, 0x00, 0x00,
+ // matrix[1]
+ 0x00, 0x00, 0x00, 0x00,
+ // matrix[2]
+ 0x00, 0x00, 0x00, 0x00,
+ // matrix[3]
+ 0x00, 0x00, 0x00, 0x00,
+ // matrix[4]
+ 0x00, 0x01, 0x00, 0x00,
+ // matrix[5]
+ 0x00, 0x00, 0x00, 0x00,
+ // matrix[6]
+ 0x00, 0x00, 0x00, 0x00,
+ // matrix[7]
+ 0x00, 0x00, 0x00, 0x00,
+ // matrix[8]
+ 0x40, 0x00, 0x00, 0x00,
+ // predefined[0]
+ 0x00, 0x00, 0x00, 0x00,
+ // predefined[1]
+ 0x00, 0x00, 0x00, 0x00,
+ // predefined[2]
+ 0x00, 0x00, 0x00, 0x00,
+ // predefined[3]
+ 0x00, 0x00, 0x00, 0x00,
+ // predefined[4]
+ 0x00, 0x00, 0x00, 0x00,
+ // predefined[5]
+ 0x00, 0x00, 0x00, 0x00,
+ // next track id
+ 0x00, 0x00, 0x00, 0x02,
+]);
+
+test('Create mvhd box', () => {
+ const iterator = getArrayBufferIterator(input, null);
+ const size = iterator.getFourByteNumber();
+ iterator.discard(4);
+
+ const parsed = parseMvhd({
+ iterator,
+ offset: 0,
+ size,
+ });
+
+ expect(parsed).toEqual({
+ creationTime: null,
+ modificationTime: null,
+ timeScale: 1000,
+ durationInUnits: 0,
+ durationInSeconds: 0,
+ rate: 1,
+ volume: 1,
+ matrix: [1, 0, 0, 0, 1, 0, 0, 0, 1],
+ nextTrackId: 2,
+ type: 'mvhd-box',
+ boxSize: 108,
+ offset: 0,
+ });
+
+ const content = createMvhd({
+ timescale: 1000,
+ durationInUnits: 0,
+ rate: 1,
+ volume: 1,
+ nextTrackId: 2,
+ matrix: [
+ //
+ 1, 0, 0,
+ //
+ 0, 1, 0,
+ //
+ 0, 0, 1,
+ ],
+ });
+
+ expect(content).toEqual(input);
+});