Skip to content

🎥 Make videos programmatically with React

awesome

Jonny Burger044c01e53c53
diff --git a/packages/media-parser/src/boxes/iso-base-media/meta/ilst.ts b/packages/media-parser/src/boxes/iso-base-media/meta/ilst.ts
new file mode 100644
index 0000000000..df69c904c2
--- /dev/null
+++ b/packages/media-parser/src/boxes/iso-base-media/meta/ilst.ts
@@ -0,0 +1,139 @@
+import type {BufferIterator} from '../../../buffer-iterator';
+import type {BaseBox} from '../base-type';
+
+type IlstEntry = {
+ index: number;
+ wellKnownType: number;
+ type: string;
+ value: Value;
+};
+
+export type Value =
+ | {
+ type: 'text';
+ value: string;
+ }
+ | {
+ type: 'number';
+ value: number;
+ }
+ | {type: 'unknown'; value: unknown};
+
+export interface IlstBox extends BaseBox {
+ type: 'ilst-box';
+ entries: IlstEntry[];
+}
+
+// https://developer.apple.com/documentation/quicktime-file-format/well-known_types
+const parseFromWellKnownType = (
+ wellKnownType: number,
+ iterator: BufferIterator,
+ size: number,
+): Value => {
+ if (wellKnownType === 1) {
+ const value = iterator.getByteString(size);
+ return {type: 'text', value};
+ }
+
+ if (wellKnownType === 21) {
+ if (size === 1) {
+ return {type: 'number', value: iterator.getInt8()};
+ }
+
+ if (size === 2) {
+ return {type: 'number', value: iterator.getInt16()};
+ }
+
+ if (size === 3) {
+ return {type: 'number', value: iterator.getInt24()};
+ }
+
+ if (size === 4) {
+ return {type: 'number', value: iterator.getInt32()};
+ }
+
+ if (size === 8) {
+ return {type: 'number', value: Number(iterator.getInt64())};
+ }
+
+ throw new Error(`Weird size for number ${size}`);
+ }
+
+ if (wellKnownType === 22) {
+ if (size === 1) {
+ return {type: 'number', value: iterator.getUint8()};
+ }
+
+ if (size === 2) {
+ return {type: 'number', value: iterator.getUint16()};
+ }
+
+ if (size === 3) {
+ return {type: 'number', value: iterator.getUint24()};
+ }
+
+ if (size === 4) {
+ return {type: 'number', value: iterator.getUint32()};
+ }
+
+ throw new Error(`Weird size for number ${size}`);
+ }
+
+ if (wellKnownType === 23) {
+ if (size === 4) {
+ return {type: 'number', value: iterator.getFloat32()};
+ }
+
+ if (size === 8) {
+ return {type: 'number', value: iterator.getFloat64()};
+ }
+
+ throw new Error(`Weird size for number ${size}`);
+ }
+
+ iterator.discard(size);
+ return {type: 'unknown', value: null};
+};
+
+export const parseIlstBox = ({
+ iterator,
+ size,
+ offset,
+}: {
+ iterator: BufferIterator;
+ size: number;
+ offset: number;
+}): IlstBox => {
+ const box = iterator.startBox(size - 8);
+
+ const entries: IlstEntry[] = [];
+ while (iterator.counter.getOffset() < size + offset) {
+ // metadata size
+ iterator.discard(4);
+ const index = iterator.getUint32();
+ const innerSize = iterator.getUint32();
+ const type = iterator.getAtom();
+ const typeIndicator = iterator.getUint8();
+ if (typeIndicator !== 0) {
+ throw new Error('Expected type indicator to be 0');
+ }
+
+ const wellKnownType = iterator.getUint24();
+ iterator.discard(4);
+ const value = parseFromWellKnownType(
+ wellKnownType,
+ iterator,
+ innerSize - 16,
+ );
+ entries.push({index, type, wellKnownType, value});
+ }
+
+ box.discardRest();
+
+ return {
+ type: 'ilst-box',
+ boxSize: size,
+ offset,
+ entries,
+ };
+};
diff --git a/packages/media-parser/src/boxes/iso-base-media/process-box.ts b/packages/media-parser/src/boxes/iso-base-media/process-box.ts
index a470195516..53084faff9 100644
--- a/packages/media-parser/src/boxes/iso-base-media/process-box.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/process-box.ts
@@ -17,6 +17,7 @@ import {makeBaseMediaTrack} from './make-track';
import {parseMdat} from './mdat/mdat';
import {parseMdhd} from './mdhd';
import {parseHdlr} from './meta/hdlr';
+import {parseIlstBox} from './meta/ilst';
import {parseMoov} from './moov/moov';
import {parseMvhd} from './mvhd';
import {parseAv1C} from './stsd/av1c';
@@ -24,6 +25,7 @@ import {parseAvcc} from './stsd/avcc';
import {parseColorParameterBox} from './stsd/colr';
import {parseCtts} from './stsd/ctts';
import {parseHvcc} from './stsd/hvcc';
+import {parseKeys} from './stsd/keys';
import {parseMebx} from './stsd/mebx';
import {parsePasp} from './stsd/pasp';
import {parseStco} from './stsd/stco';
@@ -453,6 +455,32 @@ export const processBox = async ({
};
}
+ if (boxType === 'keys') {
+ const box = parseKeys({iterator, size: boxSize, offset: fileOffset});
+
+ return {
+ type: 'complete',
+ box,
+ size: boxSize,
+ skipTo: null,
+ };
+ }
+
+ if (boxType === 'ilst') {
+ const box = parseIlstBox({
+ iterator,
+ offset: fileOffset,
+ size: boxSize,
+ });
+
+ return {
+ type: 'complete',
+ box,
+ size: boxSize,
+ skipTo: null,
+ };
+ }
+
if (boxType === 'moov') {
const box = await parseMoov({
iterator,
diff --git a/packages/media-parser/src/boxes/iso-base-media/stsd/keys.ts b/packages/media-parser/src/boxes/iso-base-media/stsd/keys.ts
index b8dab16f46..28ca133665 100644
--- a/packages/media-parser/src/boxes/iso-base-media/stsd/keys.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/stsd/keys.ts
@@ -1,27 +1,58 @@
-import {getArrayBufferIterator} from '../../../buffer-iterator';
+import type {BufferIterator} from '../../../buffer-iterator';
import type {BaseBox} from '../base-type';
+type KeysEntry = {
+ keySize: number;
+ namespace: string;
+ value: string;
+};
+
export interface KeysBox extends BaseBox {
type: 'keys-box';
+ version: number;
+ entryCount: number;
+ entries: KeysEntry[];
}
-export const parseKeys = (data: Uint8Array, offset: number): KeysBox => {
- const iterator = getArrayBufferIterator(data, data.byteLength);
- const size = iterator.getUint32();
- if (size !== data.byteLength) {
- throw new Error(`Expected keys size of ${data.byteLength}, got ${size}`);
- }
+export const parseKeys = ({
+ iterator,
+ offset,
+ size,
+}: {
+ iterator: BufferIterator;
+ offset: number;
+ size: number;
+}): KeysBox => {
+ const box = iterator.startBox(size - 8);
+ const version = iterator.getUint8();
+ // flags
+ iterator.discard(3);
+ // entry_count
+ const entryCount = iterator.getUint32();
- const atom = iterator.getAtom();
- if (atom !== 'keys') {
- throw new Error(`Expected keys type of keys, got ${atom}`);
+ const entries: KeysEntry[] = [];
+ for (let i = 0; i < entryCount; i++) {
+ // key_size
+ const keySize = iterator.getUint32();
+ const namespace = iterator.getAtom();
+ const value = iterator.getByteString(keySize - 8);
+ // data
+ const entry: KeysEntry = {
+ keySize,
+ namespace,
+ value,
+ };
+ entries.push(entry);
}
- iterator.destroy();
+ box.discardRest();
return {
type: 'keys-box',
- boxSize: data.byteLength,
+ boxSize: size,
offset,
+ version,
+ entryCount,
+ entries,
};
};
diff --git a/packages/media-parser/src/buffer-iterator.ts b/packages/media-parser/src/buffer-iterator.ts
index 7647b7c62a..649c9b67b4 100644
--- a/packages/media-parser/src/buffer-iterator.ts
+++ b/packages/media-parser/src/buffer-iterator.ts
@@ -203,6 +203,12 @@ export const getArrayBufferIterator = (
return val;
};
+ const getInt64 = (littleEndian = false) => {
+ const val = view.getBigInt64(counter.getDiscardedOffset(), littleEndian);
+ counter.increment(8);
+ return val;
+ };
+
const startBox = (size: number) => {
const startOffset = counter.getOffset();
@@ -549,7 +555,13 @@ export const getArrayBufferIterator = (
counter.increment(3);
return (val1 << 16) | (val2 << 8) | val3;
},
-
+ getInt24: () => {
+ const val1 = view.getInt8(counter.getDiscardedOffset());
+ const val2 = view.getUint8(counter.getDiscardedOffset() + 1);
+ const val3 = view.getUint8(counter.getDiscardedOffset() + 2);
+ counter.increment(3);
+ return (val1 << 16) | (val2 << 8) | val3;
+ },
getInt16: () => {
const val = view.getInt16(counter.getDiscardedOffset());
counter.increment(2);
@@ -557,6 +569,7 @@ export const getArrayBufferIterator = (
},
getUint32,
getUint64,
+ getInt64,
// https://developer.apple.com/documentation/quicktime-file-format/sound_sample_description_version_1
// A 32-bit unsigned fixed-point number (16.16) that indicates the rate at which the sound samples were obtained.
getFixedPointUnsigned1616Number: () => {
diff --git a/packages/media-parser/src/get-metadata.ts b/packages/media-parser/src/get-metadata.ts
new file mode 100644
index 0000000000..42764f6b91
--- /dev/null
+++ b/packages/media-parser/src/get-metadata.ts
@@ -0,0 +1,96 @@
+import {
+ getMoovBox,
+ getTkhdBox,
+ getTraks,
+} from './boxes/iso-base-media/traversal';
+import type {
+ IsoBaseMediaStructure,
+ RegularBox,
+ Structure,
+} from './parse-result';
+import {truthy} from './truthy';
+
+type MetadataEntry = {
+ key: string;
+ value: string | number;
+ trackId: number | null;
+};
+
+const parseIsoMetaBox = (
+ meta: RegularBox,
+ trackId: number | null,
+): MetadataEntry[] => {
+ const ilstBox = meta.children.find((b) => b.type === 'ilst-box');
+ const keysBox = meta.children.find((b) => b.type === 'keys-box');
+ if (!ilstBox || !keysBox) {
+ return [];
+ }
+
+ if (ilstBox.entries.length !== keysBox.entries.length) {
+ throw new Error('Number of entries in ilst and keys box do not match');
+ }
+
+ const entries: MetadataEntry[] = [];
+
+ for (let i = 0; i < ilstBox.entries.length; i++) {
+ const ilstEntry = ilstBox.entries[i];
+ const keysEntry = keysBox.entries[i];
+ if (ilstEntry.value.type !== 'unknown') {
+ entries.push({
+ key: keysEntry.value,
+ value: ilstEntry.value.value,
+ trackId,
+ });
+ }
+ }
+
+ return entries;
+};
+
+const getMetadataFromIsoBase = (
+ isoBase: IsoBaseMediaStructure,
+): MetadataEntry[] => {
+ const moov = getMoovBox(isoBase.boxes);
+ if (!moov) {
+ return [];
+ }
+
+ const traks = getTraks(moov);
+ const meta = moov.children.find(
+ (b) => b.type === 'regular-box' && b.boxType === 'meta',
+ ) as RegularBox | null;
+ const metaInTracks = traks
+ .map((t) => {
+ const metaBox = t.children.find(
+ (child) => child.type === 'regular-box' && child.boxType === 'meta',
+ ) as RegularBox | null;
+ if (metaBox) {
+ const tkhd = getTkhdBox(t);
+ if (!tkhd) {
+ throw new Error('No tkhd box found');
+ }
+
+ return parseIsoMetaBox(metaBox, tkhd.trackId);
+ }
+
+ return null;
+ })
+ .filter(truthy);
+ if (!meta) {
+ return metaInTracks.flat(1);
+ }
+
+ return [...parseIsoMetaBox(meta, null), ...metaInTracks.flat(1)];
+};
+
+export const getMetadata = (structure: Structure) => {
+ if (structure.type === 'matroska') {
+ return null;
+ }
+
+ if (structure.type === 'riff') {
+ return null;
+ }
+
+ return getMetadataFromIsoBase(structure);
+};
diff --git a/packages/media-parser/src/parse-result.ts b/packages/media-parser/src/parse-result.ts
index c2a3fbe424..8f48443684 100644
--- a/packages/media-parser/src/parse-result.ts
+++ b/packages/media-parser/src/parse-result.ts
@@ -4,6 +4,7 @@ import type {FtypBox} from './boxes/iso-base-media/ftyp';
import type {MdatBox} from './boxes/iso-base-media/mdat/mdat';
import type {MdhdBox} from './boxes/iso-base-media/mdhd';
import type {HdlrBox} from './boxes/iso-base-media/meta/hdlr';
+import type {IlstBox} from './boxes/iso-base-media/meta/ilst';
import type {MoovBox} from './boxes/iso-base-media/moov/moov';
import type {MvhdBox} from './boxes/iso-base-media/mvhd';
import type {Av1CBox} from './boxes/iso-base-media/stsd/av1c';
@@ -49,6 +50,7 @@ export type IsoBaseMediaBox =
| TrakBox
| SttsBox
| MdhdBox
+ | IlstBox
| EsdsBox
| MdatBox
| StszBox
diff --git a/packages/media-parser/src/test/keys.test.ts b/packages/media-parser/src/test/keys.test.ts
deleted file mode 100644
index ca9d52160d..0000000000
--- a/packages/media-parser/src/test/keys.test.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-import {expect, test} from 'bun:test';
-import {parseKeys} from '../boxes/iso-base-media/stsd/keys';
-
-test('Should parse keyd box', () => {
- const buffer = Uint8Array.from([
- 0, 0, 2, 60, 107, 101, 121, 115, 0, 0, 2, 52, 0, 0, 0, 1, 0, 0, 0, 47, 107,
- 101, 121, 100, 109, 100, 116, 97, 99, 111, 109, 46, 97, 112, 112, 108, 101,
- 46, 113, 117, 105, 99, 107, 116, 105, 109, 101, 46, 108, 105, 118, 101, 45,
- 112, 104, 111, 116, 111, 45, 105, 110, 102, 111, 0, 0, 0, 67, 100, 116, 121,
- 112, 0, 0, 0, 1, 99, 111, 109, 46, 97, 112, 112, 108, 101, 46, 113, 117,
- 105, 99, 107, 116, 105, 109, 101, 46, 99, 111, 109, 46, 97, 112, 112, 108,
- 101, 46, 113, 117, 105, 99, 107, 116, 105, 109, 101, 46, 108, 105, 118, 101,
- 45, 112, 104, 111, 116, 111, 45, 105, 110, 102, 111, 0, 0, 0, 20, 115, 100,
- 112, 100, 0, 0, 0, 12, 115, 100, 112, 105, 0, 0, 0, 0, 0, 0, 1, 142, 115,
- 101, 116, 117, 0, 0, 1, 118, 99, 102, 103, 118, 98, 112, 108, 105, 115, 116,
- 48, 48, 211, 1, 2, 3, 4, 5, 12, 95, 16, 33, 76, 105, 118, 101, 80, 104, 111,
- 116, 111, 77, 101, 116, 97, 100, 97, 116, 97, 83, 101, 116, 117, 112, 68,
- 97, 116, 97, 86, 101, 114, 115, 105, 111, 110, 93, 83, 121, 115, 116, 101,
- 109, 86, 101, 114, 115, 105, 111, 110, 95, 16, 17, 70, 114, 97, 109, 101,
- 119, 111, 114, 107, 86, 101, 114, 115, 105, 111, 110, 115, 16, 1, 211, 6, 7,
- 8, 9, 10, 11, 95, 16, 19, 80, 114, 111, 100, 117, 99, 116, 66, 117, 105,
- 108, 100, 86, 101, 114, 115, 105, 111, 110, 91, 80, 114, 111, 100, 117, 99,
- 116, 78, 97, 109, 101, 94, 80, 114, 111, 100, 117, 99, 116, 86, 101, 114,
- 115, 105, 111, 110, 88, 50, 49, 70, 53, 48, 52, 56, 102, 89, 105, 80, 104,
- 111, 110, 101, 32, 79, 83, 84, 49, 55, 46, 53, 213, 13, 14, 15, 16, 17, 18,
- 19, 20, 21, 22, 94, 72, 49, 51, 73, 83, 80, 83, 101, 114, 118, 105, 99, 101,
- 115, 90, 67, 111, 114, 101, 77, 111, 116, 105, 111, 110, 93, 67, 77, 67, 97,
- 112, 116, 117, 114, 101, 67, 111, 114, 101, 94, 72, 49, 48, 73, 83, 80, 83,
- 101, 114, 118, 105, 99, 101, 115, 89, 67, 111, 114, 101, 77, 101, 100, 105,
- 97, 85, 56, 46, 53, 48, 50, 94, 50, 56, 57, 48, 46, 49, 54, 46, 49, 51, 46,
- 48, 46, 49, 87, 52, 55, 55, 46, 50, 46, 49, 86, 50, 48, 46, 53, 48, 48, 88,
- 51, 49, 49, 48, 46, 52, 46, 49, 0, 8, 0, 15, 0, 51, 0, 65, 0, 85, 0, 87, 0,
- 94, 0, 116, 0, 128, 0, 143, 0, 152, 0, 162, 0, 167, 0, 178, 0, 193, 0, 204,
- 0, 218, 0, 233, 0, 243, 0, 249, 1, 8, 1, 16, 1, 23, 0, 0, 0, 0, 0, 0, 2, 1,
- 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 32, 0,
- 0, 0, 16, 100, 105, 109, 115, 0, 0, 7, 128, 0, 0, 4, 56, 0, 0, 0, 24, 99,
- 116, 112, 115, 0, 0, 0, 16, 100, 116, 121, 112, 0, 0, 0, 0, 0, 0, 0, 0,
- ]);
-
- const data = parseKeys(buffer, 0);
-
- expect(data).toEqual({
- type: 'keys-box',
- boxSize: 572,
- offset: 0,
- });
-});
diff --git a/packages/media-parser/src/test/read-metadata.test.ts b/packages/media-parser/src/test/read-metadata.test.ts
index 2a3f0fdc30..b11f39ec09 100644
--- a/packages/media-parser/src/test/read-metadata.test.ts
+++ b/packages/media-parser/src/test/read-metadata.test.ts
@@ -1,6 +1,6 @@
import {exampleVideos} from '@remotion/example-videos';
-import {test} from 'bun:test';
-import {getMoovBox} from '../boxes/iso-base-media/traversal';
+import {expect, test} from 'bun:test';
+import {getMetadata} from '../get-metadata';
import {parseMedia} from '../parse-media';
import {nodeReader} from '../readers/from-node';
@@ -13,15 +13,76 @@ test('iPhone metadata', async () => {
reader: nodeReader,
});
- const fs = await import('fs');
- fs.writeFileSync('structure.json', JSON.stringify(structure, null, 2));
if (structure.type !== 'iso-base-media') {
throw new Error('Expected video');
}
- const moov = getMoovBox(structure.boxes);
- const meta = moov?.children.find(
- (b) => b.type === 'regular-box' && b.boxType === 'meta',
- );
- console.log(meta);
+ const metadata = getMetadata(structure);
+ expect(metadata).toEqual([
+ {
+ key: 'com.apple.quicktime.location.accuracy.horizontal',
+ trackId: null,
+ value: '5.762147',
+ },
+ {
+ key: 'com.apple.quicktime.live-photo.auto',
+ trackId: null,
+ value: 1,
+ },
+ {
+ key: 'com.apple.quicktime.full-frame-rate-playback-intent',
+ trackId: null,
+ value: 1,
+ },
+ {
+ key: 'com.apple.quicktime.content.identifier',
+ trackId: null,
+ value: '2C1C7C94-E977-45D0-9B3F-9A9CA8EFB47D',
+ },
+ {
+ key: 'com.apple.quicktime.live-photo.vitality-score',
+ trackId: null,
+ value: 1,
+ },
+ {
+ key: 'com.apple.quicktime.live-photo.vitality-scoring-version',
+ trackId: null,
+ value: 4,
+ },
+ {
+ key: 'com.apple.quicktime.location.ISO6709',
+ trackId: null,
+ value: '+47.3915+008.5121+404.680/',
+ },
+ {
+ key: 'com.apple.quicktime.make',
+ trackId: null,
+ value: 'Apple',
+ },
+ {
+ key: 'com.apple.quicktime.model',
+ trackId: null,
+ value: 'iPhone 15 Pro',
+ },
+ {
+ key: 'com.apple.quicktime.software',
+ trackId: null,
+ value: '18.0',
+ },
+ {
+ key: 'com.apple.quicktime.creationdate',
+ trackId: null,
+ value: '2024-10-01T12:46:18+0200',
+ },
+ {
+ key: 'com.apple.quicktime.camera.lens_model',
+ trackId: 1,
+ value: 'iPhone 15 Pro back camera 6.765mm f/1.78',
+ },
+ {
+ key: 'com.apple.quicktime.camera.focal_length.35mm_equivalent',
+ trackId: 1,
+ value: 23,
+ },
+ ]);
});