Skip to content

🎥 Make videos programmatically with React

tkhd

Jonny Burger2b93d6b5ede9
diff --git a/packages/media-parser/src/create/iso-base-media/primitives.ts b/packages/media-parser/src/create/iso-base-media/primitives.ts
index 76d9816077..9347e502e5 100644
--- a/packages/media-parser/src/create/iso-base-media/primitives.ts
+++ b/packages/media-parser/src/create/iso-base-media/primitives.ts
@@ -132,7 +132,11 @@ export const padIsoBaseMediaBytes = (data: Uint8Array, totalLength: number) => {
return data;
}
- return combineUint8Arrays([data, new Uint8Array(totalLength - data.length)]);
+ return combineUint8Arrays([
+ stringsToUint8Array('free'),
+ data,
+ new Uint8Array(totalLength - data.length - 8),
+ ]);
};
export const IDENTITY_MATRIX: ThreeDMatrix = [1, 0, 0, 0, 1, 0, 0, 0, 1];
diff --git a/packages/media-parser/src/create/iso-base-media/serialize-track.ts b/packages/media-parser/src/create/iso-base-media/serialize-track.ts
index 69e3dd1d87..c0cfa3d5f2 100644
--- a/packages/media-parser/src/create/iso-base-media/serialize-track.ts
+++ b/packages/media-parser/src/create/iso-base-media/serialize-track.ts
@@ -3,8 +3,12 @@ import type {MakeTrackAudio, MakeTrackVideo} from '../make-track-info';
import {createMdia} from './create-mdia';
import {createTrak} from './create-trak';
import {createMdhd} from './mdia/create-mdhd';
-import {IDENTITY_MATRIX} from './primitives';
-import {createTkhd, TKHD_FLAGS} from './trak/create-tkhd';
+import {IDENTITY_MATRIX, stringsToUint8Array} from './primitives';
+import {
+ createTkhdForAudio,
+ createTkhdForVideo,
+ TKHD_FLAGS,
+} from './trak/create-tkhd';
import {createElstItem} from './trak/edts/create-elst';
import {createMinf} from './trak/mdia/create-minf';
import {createStbl} from './trak/mdia/minf/create-stbl';
@@ -27,9 +31,8 @@ export const serializeTrack = ({
durationInUnits,
samplePositions,
}: IsoBaseMediaTrackData) => {
- console.log(track);
- if (track.codec !== 'h264') {
- throw new Error('Currently only H.264 is supported');
+ if (track.codec !== 'h264' && track.codec !== 'aac') {
+ throw new Error('Currently only H.264 and AAC is supported');
}
if (!track.codecPrivate) {
@@ -37,17 +40,29 @@ export const serializeTrack = ({
}
return createTrak({
- tkhd: createTkhd({
- creationTime: null,
- modificationTime: null,
- duration: durationInUnits,
- flags: TKHD_FLAGS.TRACK_ENABLED | TKHD_FLAGS.TRACK_IN_MOVIE,
- height: track.height,
- width: track.width,
- matrix: IDENTITY_MATRIX,
- trackId: track.trackNumber,
- volume: 1,
- }),
+ tkhd:
+ track.codec === 'aac'
+ ? createTkhdForAudio({
+ creationTime: null,
+ flags: TKHD_FLAGS.TRACK_ENABLED | TKHD_FLAGS.TRACK_IN_MOVIE,
+ modificationTime: null,
+ duration: durationInUnits,
+ trackId: track.trackNumber,
+ volume: 1,
+ })
+ : track.type === 'video'
+ ? createTkhdForVideo({
+ creationTime: null,
+ modificationTime: null,
+ duration: durationInUnits,
+ flags: TKHD_FLAGS.TRACK_ENABLED | TKHD_FLAGS.TRACK_IN_MOVIE,
+ height: track.height,
+ width: track.width,
+ matrix: IDENTITY_MATRIX,
+ trackId: track.trackNumber,
+ volume: 1,
+ })
+ : new Uint8Array(stringsToUint8Array('wrong')),
edts: createElstItem({
segmentDuration: durationInUnits,
mediaTime: 0,
@@ -63,19 +78,33 @@ export const serializeTrack = ({
minf: createMinf({
stblAtom: createStbl({
samplePositions,
- codecSpecificData: {
- avccBox: createAvccBox(track.codecPrivate),
- // TODO: Investigate which values to put in
- btrt: createBtrt({maxBitrate: 437875, avgBitrate: 437875}),
- compressorName: '',
- depth: 24,
- horizontalResolution: 72,
- verticalResolution: 72,
- height: track.height,
- width: track.width,
- pasp: createPasp(1, 1),
- type: 'avc1-data',
- },
+ codecSpecificData:
+ track.type === 'audio'
+ ? {
+ type: 'mp4a-data',
+ // TODO: Investigate which values to put in
+ avgBitrate: 317370,
+ maxBitrate: 319999,
+ btrt: createBtrt({
+ avgBitrate: 317370,
+ maxBitrate: 319999,
+ }),
+ channelCount: track.numberOfChannels,
+ sampleRate: track.sampleRate,
+ }
+ : {
+ avccBox: createAvccBox(track.codecPrivate),
+ // TODO: Investigate which values to put in
+ btrt: createBtrt({maxBitrate: 437875, avgBitrate: 437875}),
+ compressorName: '',
+ depth: 24,
+ horizontalResolution: 72,
+ verticalResolution: 72,
+ height: track.height,
+ width: track.width,
+ pasp: createPasp(1, 1),
+ type: 'avc1-data',
+ },
}),
vmhdAtom: createVmhd(),
}),
diff --git a/packages/media-parser/src/create/iso-base-media/trak/create-tkhd.ts b/packages/media-parser/src/create/iso-base-media/trak/create-tkhd.ts
index a29680a34a..5cff400964 100644
--- a/packages/media-parser/src/create/iso-base-media/trak/create-tkhd.ts
+++ b/packages/media-parser/src/create/iso-base-media/trak/create-tkhd.ts
@@ -3,6 +3,7 @@ import {combineUint8Arrays} from '../../../boxes/webm/make-header';
import {
addSize,
floatTo16Point16_16Bit,
+ IDENTITY_MATRIX,
numberTo32BitUIntOrInt,
serializeMatrix,
setFixedPointSignedOrUnsigned1616Number,
@@ -16,7 +17,65 @@ export const TKHD_FLAGS = {
TRACK_IN_POSTER: 0x000008,
};
-export const createTkhd = ({
+export const createTkhdForAudio = ({
+ creationTime,
+ modificationTime,
+ flags,
+ trackId,
+ duration,
+ volume,
+}: {
+ creationTime: number | null;
+ modificationTime: number | null;
+ flags: number;
+ trackId: number;
+ duration: number;
+ volume: number;
+}) => {
+ return addSize(
+ combineUint8Arrays([
+ // name
+ stringsToUint8Array('tkhd'),
+ // version
+ new Uint8Array([0]),
+ // flags
+ new Uint8Array([0, 0, flags]),
+ // creation time
+ creationTime === null
+ ? numberTo32BitUIntOrInt(0)
+ : numberTo32BitUIntOrInt(fromUnixTimestamp(creationTime)),
+ // modification time
+ modificationTime === null
+ ? numberTo32BitUIntOrInt(0)
+ : numberTo32BitUIntOrInt(fromUnixTimestamp(modificationTime)),
+ // trackId
+ numberTo32BitUIntOrInt(trackId),
+ // reserved
+ new Uint8Array([0, 0, 0, 0]),
+ // duration
+ numberTo32BitUIntOrInt(duration),
+ // reserved
+ new Uint8Array([0, 0, 0, 0]),
+ new Uint8Array([0, 0, 0, 0]),
+ // layer
+ new Uint8Array([0, 0]),
+ // alternate group, 1 = 'sound'
+ new Uint8Array([0, 1]),
+ // volume
+ floatTo16Point16_16Bit(volume),
+ // reserved
+ new Uint8Array([0, 0]),
+ // matrix
+ serializeMatrix(IDENTITY_MATRIX),
+ // width
+ setFixedPointSignedOrUnsigned1616Number(0),
+ // height
+ setFixedPointSignedOrUnsigned1616Number(0),
+ ]),
+ );
+};
+
+export const createTkhdForVideo = ({
creationTime,
modificationTime,
duration,
@@ -63,7 +122,7 @@ export const createTkhd = ({
new Uint8Array([0, 0, 0, 0]),
// layer
new Uint8Array([0, 0]),
- // alternate group
+ // alternate group, 0 = 'video'
new Uint8Array([0, 0]),
// volume
floatTo16Point16_16Bit(volume),
diff --git a/packages/media-parser/src/test/tkhd.test.ts b/packages/media-parser/src/test/tkhd.test.ts
index 5934e9b5ce..6cb939046b 100644
--- a/packages/media-parser/src/test/tkhd.test.ts
+++ b/packages/media-parser/src/test/tkhd.test.ts
@@ -2,7 +2,8 @@ import {expect, test} from 'bun:test';
import {parseTkhd} from '../boxes/iso-base-media/tkhd';
import {getArrayBufferIterator} from '../buffer-iterator';
import {
- createTkhd,
+ createTkhdForAudio,
+ createTkhdForVideo,
TKHD_FLAGS,
} from '../create/iso-base-media/trak/create-tkhd';
@@ -90,10 +91,9 @@ test('Should be able to parse a TKHD box', () => {
});
});
-test('Should be able to create', () => {
- const end = 200;
+test('Should be able to create a tkhd', () => {
expect(
- createTkhd({
+ createTkhdForVideo({
creationTime: 1714993714000,
modificationTime: 1714993718000,
trackId: 5,
@@ -107,8 +107,8 @@ test('Should be able to create', () => {
TKHD_FLAGS.TRACK_IN_MOVIE |
TKHD_FLAGS.TRACK_IN_PREVIEW |
TKHD_FLAGS.TRACK_IN_POSTER,
- }).slice(4, end),
- ).toEqual(buffer.slice(4, end));
+ }),
+ ).toEqual(buffer);
});
const buffer2 = new Uint8Array([
@@ -151,7 +151,7 @@ test('Should be able to parse a TKHD box 2', () => {
});
expect(
- createTkhd({
+ createTkhdForVideo({
creationTime: null,
modificationTime: null,
duration: 0,
@@ -164,3 +164,27 @@ test('Should be able to parse a TKHD box 2', () => {
}),
).toEqual(buffer2);
});
+
+const audioTkhd = new Uint8Array([
+ 0x00, 0x00, 0x00, 0x5c, 0x74, 0x6b, 0x68, 0x64, 0x00, 0x00, 0x00, 0x03, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x0f, 0xd6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x0,
+]);
+
+test('tkhd', () => {
+ expect(
+ createTkhdForAudio({
+ creationTime: null,
+ flags: 3,
+ modificationTime: null,
+ trackId: 2,
+ duration: 4054,
+ volume: 1,
+ }),
+ ).toEqual(audioTkhd);
+});