Skip to content

🎥 Make videos programmatically with React

create mdhdd

Jonny Burger5e5767683a05
diff --git a/packages/media-parser/src/create/iso-base-media/create-mdhd.ts b/packages/media-parser/src/create/iso-base-media/create-mdhd.ts
new file mode 100644
index 0000000000..f75d781fcc
--- /dev/null
+++ b/packages/media-parser/src/create/iso-base-media/create-mdhd.ts
@@ -0,0 +1,46 @@
+import {fromUnixTimestamp} from '../../boxes/iso-base-media/to-date';
+import {combineUint8Arrays} from '../../boxes/webm/make-header';
+import {
+ addSize,
+ numberTo32BitUIntOrInt,
+ stringsToUint8Array,
+} from './primitives';
+
+export const createMdhd = ({
+ creationTime,
+ modificationTime,
+ timescale,
+ duration,
+}: {
+ creationTime: number | null;
+ modificationTime: number | null;
+ timescale: number;
+ duration: number;
+}) => {
+ return addSize(
+ combineUint8Arrays([
+ // type
+ stringsToUint8Array('mdhd'),
+ // version
+ new Uint8Array([0]),
+ // flags
+ new Uint8Array([0, 0, 0]),
+ // creation time
+ creationTime === null
+ ? numberTo32BitUIntOrInt(0)
+ : numberTo32BitUIntOrInt(fromUnixTimestamp(creationTime)),
+ // modification time
+ modificationTime === null
+ ? numberTo32BitUIntOrInt(0)
+ : numberTo32BitUIntOrInt(fromUnixTimestamp(modificationTime)),
+ // timescale
+ numberTo32BitUIntOrInt(timescale),
+ // duration
+ numberTo32BitUIntOrInt(duration),
+ // language: unspecified
+ new Uint8Array([0x55, 0xc4]),
+ // quality
+ new Uint8Array([0, 0]),
+ ]),
+ );
+};
diff --git a/packages/media-parser/src/test/mdhd.test.ts b/packages/media-parser/src/test/mdhd.test.ts
new file mode 100644
index 0000000000..e64d13635f
--- /dev/null
+++ b/packages/media-parser/src/test/mdhd.test.ts
@@ -0,0 +1,20 @@
+import {expect, test} from 'bun:test';
+import {createMdhd} from '../create/iso-base-media/create-mdhd';
+
+const reference = new Uint8Array([
+ 0x00, 0x00, 0x00, 0x20, 0x6d, 0x64, 0x68, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x5f, 0x90, 0x00, 0x05,
+ 0x7e, 0x40, 0x55, 0xc4, 0x00, 0x00,
+]);
+
+test('mdhd', () => {
+ expect(reference.length).toBe(32);
+ expect(
+ createMdhd({
+ creationTime: null,
+ modificationTime: null,
+ timescale: 90000,
+ duration: 360000,
+ }),
+ ).toEqual(reference);
+});