Skip to content

🎥 Make videos programmatically with React

`@remotion/media-parser`: Handle colr prof atom

Jonny Burgerddba6fe9264b
diff --git a/package.json b/package.json
index a8fbd07717..7330f35002 100644
--- a/package.json
+++ b/package.json
@@ -35,7 +35,7 @@
"prettier-plugin-organize-imports": "3.2.4"
},
"dependencies": {
- "turbo": "2.2.3",
+ "turbo": "2.3.0",
"typescript": "5.5.4"
},
"packageManager": "[email protected]"
diff --git a/packages/example-videos/src/index.ts b/packages/example-videos/src/index.ts
index eb1dd7dc7b..3a290291e4 100644
--- a/packages/example-videos/src/index.ts
+++ b/packages/example-videos/src/index.ts
@@ -49,4 +49,5 @@ export const exampleVideos = {
opusWebm: path.join(examplePackage, 'public', 'opus.webm'),
avi: path.join(examplePackage, 'public', 'example.avi'),
opus51Webm: path.join(examplePackage, 'public', 'vp8-opus-5-1-channels.webm'),
+ profColrTyp: path.join(__dirname, '..', 'videos', 'prof-color-type.mp4'),
};
diff --git a/packages/example-videos/videos/prof-color-type.mp4 b/packages/example-videos/videos/prof-color-type.mp4
new file mode 100644
index 0000000000..7abecae2f2
Binary files /dev/null and b/packages/example-videos/videos/prof-color-type.mp4 differ
diff --git a/packages/media-parser/src/boxes/iso-base-media/parse-icc-profile.ts b/packages/media-parser/src/boxes/iso-base-media/parse-icc-profile.ts
new file mode 100644
index 0000000000..24f51e73ef
--- /dev/null
+++ b/packages/media-parser/src/boxes/iso-base-media/parse-icc-profile.ts
@@ -0,0 +1,161 @@
+import {getArrayBufferIterator} from '../../buffer-iterator';
+
+type Point = {
+ x: number;
+ y: number;
+ z: number;
+};
+
+export type IccProfile = {
+ size: number;
+ preferredCMMType: string;
+ profileVersion: string;
+ profileDeviceClass: string;
+ colorSpace: string;
+ pcs: string;
+ dateTime: Uint8Array;
+ signature: string;
+ primaryPlatform: string;
+ profileFlags: number;
+ deviceManufacturer: string;
+ deviceModel: string;
+ deviceAttributes: bigint;
+ renderingIntent: number;
+ pcsIlluminant: [number, number, number];
+ profileCreator: string;
+ profileId: string;
+ entries: Entry[];
+ rXYZ: Point | null;
+ gXYZ: Point | null;
+ bXYZ: Point | null;
+ whitePoint: Point | null;
+};
+
+type Entry = {
+ tag: string;
+ size: number;
+ offset: number;
+};
+
+export const parseIccProfile = (data: Uint8Array): IccProfile => {
+ const iterator = getArrayBufferIterator(data, Infinity);
+ const size = iterator.getUint32();
+ if (size !== data.length) {
+ throw new Error('Invalid ICC profile size');
+ }
+
+ const preferredCMMType = iterator.getByteString(4);
+ const profileVersion = iterator.getByteString(4);
+ const profileDeviceClass = iterator.getByteString(4);
+ const colorSpace = iterator.getByteString(4);
+ const pcs = iterator.getByteString(4);
+ const dateTime = iterator.getSlice(12);
+ const signature = iterator.getByteString(4);
+ if (signature !== 'acsp') {
+ throw new Error('Invalid ICC profile signature');
+ }
+
+ const primaryPlatform = iterator.getByteString(4);
+ const profileFlags = iterator.getUint32();
+ const deviceManufacturer = iterator.getByteString(4);
+ const deviceModel = iterator.getByteString(4);
+ const deviceAttributes = iterator.getUint64();
+ const renderingIntent = iterator.getUint32();
+ const pcsIlluminant1 = iterator.getUint32();
+ const pcsIlluminant2 = iterator.getUint32();
+ const pcsIlluminant3 = iterator.getUint32();
+ const profileCreator = iterator.getByteString(4);
+ const profileId = iterator.getByteString(16);
+
+ // reserved
+ iterator.discard(28);
+
+ const tagCount = iterator.getUint32();
+ const entries: Entry[] = [];
+ for (let i = 0; i < tagCount; i++) {
+ const entry: Entry = {
+ tag: iterator.getByteString(4),
+ offset: iterator.getUint32(),
+ size: iterator.getUint32(),
+ };
+ entries.push(entry);
+ }
+
+ let lastOffset = -1;
+
+ let rXYZ: Point | null = null;
+ let gXYZ: Point | null = null;
+ let bXYZ: Point | null = null;
+ let whitePoint: Point | null = null;
+
+ for (const entry of entries) {
+ const found = data.slice(entry.offset, entry.offset + entry.size);
+ if (
+ entry.tag === 'rXYZ' ||
+ entry.tag === 'gXYZ' ||
+ entry.tag === 'bXYZ' ||
+ entry.tag === 'wtpt'
+ ) {
+ const it = getArrayBufferIterator(found, Infinity);
+ it.discard(4);
+
+ const x = it.getInt32() / 65536;
+ const y = it.getInt32() / 65536;
+ const z = it.getInt32() / 65536;
+ it.destroy();
+ const point = {x, y, z};
+ if (entry.tag === 'rXYZ') {
+ rXYZ = point;
+ } else if (entry.tag === 'gXYZ') {
+ gXYZ = point;
+ } else if (entry.tag === 'bXYZ') {
+ bXYZ = point;
+ } else if (entry.tag === 'wtpt') {
+ whitePoint = point;
+ }
+ }
+
+ if (lastOffset !== -1) {
+ const bytesToAdvance = entry.offset - lastOffset;
+ const bytesToGoBackwards = entry.size - bytesToAdvance;
+ if (bytesToGoBackwards > 0) {
+ iterator.counter.decrement(bytesToGoBackwards);
+ }
+ }
+
+ lastOffset = entry.offset;
+ }
+
+ const profile: IccProfile = {
+ size,
+ preferredCMMType,
+ profileVersion,
+ profileDeviceClass,
+ colorSpace,
+ pcs,
+ dateTime,
+ signature,
+ primaryPlatform,
+ profileFlags,
+ deviceManufacturer,
+ deviceModel,
+ deviceAttributes,
+ renderingIntent,
+ pcsIlluminant: [
+ pcsIlluminant1 / 65536,
+ pcsIlluminant2 / 65536,
+ pcsIlluminant3 / 65536,
+ ],
+ profileCreator,
+ profileId,
+ entries,
+ bXYZ,
+ gXYZ,
+ rXYZ,
+ whitePoint,
+ };
+
+ iterator.destroy();
+
+ return profile;
+};
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 a1482035ed..9be84f319a 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
@@ -246,6 +246,7 @@ export const processBox = async ({
if (boxType === 'colr') {
const box = parseColorParameterBox({
iterator,
+ size: boxSize,
});
return {
type: 'complete',
diff --git a/packages/media-parser/src/boxes/iso-base-media/stsd/colr.ts b/packages/media-parser/src/boxes/iso-base-media/stsd/colr.ts
index cabfc463ee..1a42918b4c 100644
--- a/packages/media-parser/src/boxes/iso-base-media/stsd/colr.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/stsd/colr.ts
@@ -1,19 +1,34 @@
import type {BufferIterator} from '../../../buffer-iterator';
+import type {IccProfile} from '../parse-icc-profile';
+import {parseIccProfile} from '../parse-icc-profile';
-export interface ColorParameterBox {
+type ExtraData =
+ | {
+ colorType: 'transfer-characteristics';
+ primaries: number;
+ transfer: number;
+ matrixIndex: number;
+ fullRangeFlag: boolean;
+ }
+ | {
+ colorType: 'icc-profile';
+ profile: Uint8Array;
+ parsed: IccProfile;
+ };
+
+export type ColorParameterBox = {
type: 'colr-box';
- primaries: number;
- transfer: number;
- matrixIndex: number;
- fullRangeFlag: boolean;
-}
+} & ExtraData;
export const parseColorParameterBox = ({
iterator,
+ size,
}: {
iterator: BufferIterator;
+ size: number;
}): ColorParameterBox => {
const byteString = iterator.getByteString(4);
+
if (byteString === 'nclx') {
const primaries = iterator.getUint16();
const transfer = iterator.getUint16();
@@ -24,6 +39,7 @@ export const parseColorParameterBox = ({
return {
type: 'colr-box',
+ colorType: 'transfer-characteristics',
fullRangeFlag,
matrixIndex,
primaries,
@@ -38,6 +54,7 @@ export const parseColorParameterBox = ({
return {
type: 'colr-box',
+ colorType: 'transfer-characteristics',
fullRangeFlag: false,
matrixIndex,
primaries,
@@ -45,5 +62,16 @@ export const parseColorParameterBox = ({
};
}
+ if (byteString === 'prof') {
+ const profile = iterator.getSlice(size - 12);
+
+ return {
+ type: 'colr-box',
+ colorType: 'icc-profile',
+ profile,
+ parsed: parseIccProfile(profile),
+ };
+ }
+
throw new Error('Unexpected box type ' + byteString);
};
diff --git a/packages/media-parser/src/boxes/webm/av1-codec-private.ts b/packages/media-parser/src/boxes/webm/av1-codec-private.ts
index c0a841f4a5..6672d22245 100644
--- a/packages/media-parser/src/boxes/webm/av1-codec-private.ts
+++ b/packages/media-parser/src/boxes/webm/av1-codec-private.ts
@@ -77,7 +77,7 @@ export const parseAv1PrivateData = (
: '0';
str += '.';
- if (colrAtom) {
+ if (colrAtom && colrAtom.colorType === 'transfer-characteristics') {
str += colrAtom.primaries.toString().padStart(2, '0');
str += '.';
str += colrAtom.transfer.toString().padStart(2, '0');
diff --git a/packages/media-parser/src/get-video-codec.ts b/packages/media-parser/src/get-video-codec.ts
index f1ce3e9604..e6b2694c67 100644
--- a/packages/media-parser/src/get-video-codec.ts
+++ b/packages/media-parser/src/get-video-codec.ts
@@ -175,6 +175,11 @@ export const getIsoBmColrConfig = (
return null;
}
+ // TODO: Not doing anything with a in ICC color profile yet
+ if (colrAtom.colorType !== 'transfer-characteristics') {
+ return null;
+ }
+
// https://github.com/bbc/qtff-parameter-editor
return {
fullRange: colrAtom.fullRangeFlag,
diff --git a/packages/media-parser/src/test/parse-colr.test.ts b/packages/media-parser/src/test/parse-colr.test.ts
new file mode 100644
index 0000000000..03d17ed2a5
--- /dev/null
+++ b/packages/media-parser/src/test/parse-colr.test.ts
@@ -0,0 +1,142 @@
+import {exampleVideos} from '@remotion/example-videos';
+import {expect, test} from 'bun:test';
+import {getStsdBox} from '../boxes/iso-base-media/traversal';
+import {parseMedia} from '../parse-media';
+import {nodeReader} from '../readers/from-node';
+
+test('parse colr', async () => {
+ const {boxes} = await parseMedia({
+ src: exampleVideos.profColrTyp,
+ fields: {
+ boxes: true,
+ },
+ reader: nodeReader,
+ });
+
+ const trak = boxes
+ .find((b) => b.type === 'moov-box')
+ ?.children?.find((t) => t.type === 'trak-box');
+
+ const mdia = getStsdBox(trak!);
+ const sample = mdia?.samples[0]!;
+ if (sample.type !== 'video') {
+ throw new Error('Expected video sample');
+ }
+
+ const colrBox = sample.descriptors.find((d) => d.type === 'colr-box')!;
+ expect(colrBox).toEqual({
+ type: 'colr-box',
+ colorType: 'icc-profile',
+ profile: new Uint8Array([
+ 0, 0, 1, 200, 0, 0, 0, 0, 4, 48, 0, 0, 109, 110, 116, 114, 82, 71, 66, 32,
+ 88, 89, 90, 32, 7, 224, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 97, 99, 115, 112, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 1, 0, 0, 246, 214, 0, 1, 0, 0, 0, 0, 211, 45, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 100, 101, 115,
+ 99, 0, 0, 0, 240, 0, 0, 0, 36, 114, 88, 89, 90, 0, 0, 1, 20, 0, 0, 0, 20,
+ 103, 88, 89, 90, 0, 0, 1, 40, 0, 0, 0, 20, 98, 88, 89, 90, 0, 0, 1, 60, 0,
+ 0, 0, 20, 119, 116, 112, 116, 0, 0, 1, 80, 0, 0, 0, 20, 114, 84, 82, 67,
+ 0, 0, 1, 100, 0, 0, 0, 40, 103, 84, 82, 67, 0, 0, 1, 100, 0, 0, 0, 40, 98,
+ 84, 82, 67, 0, 0, 1, 100, 0, 0, 0, 40, 99, 112, 114, 116, 0, 0, 1, 140, 0,
+ 0, 0, 60, 109, 108, 117, 99, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 12, 101,
+ 110, 85, 83, 0, 0, 0, 8, 0, 0, 0, 28, 0, 115, 0, 82, 0, 71, 0, 66, 88, 89,
+ 90, 32, 0, 0, 0, 0, 0, 0, 111, 162, 0, 0, 56, 245, 0, 0, 3, 144, 88, 89,
+ 90, 32, 0, 0, 0, 0, 0, 0, 98, 153, 0, 0, 183, 133, 0, 0, 24, 218, 88, 89,
+ 90, 32, 0, 0, 0, 0, 0, 0, 36, 160, 0, 0, 15, 132, 0, 0, 182, 207, 88, 89,
+ 90, 32, 0, 0, 0, 0, 0, 0, 246, 214, 0, 1, 0, 0, 0, 0, 211, 45, 112, 97,
+ 114, 97, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 102, 102, 0, 0, 242, 167, 0, 0, 13,
+ 89, 0, 0, 19, 208, 0, 0, 10, 91, 0, 0, 0, 0, 0, 0, 0, 0, 109, 108, 117,
+ 99, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 12, 101, 110, 85, 83, 0, 0, 0, 32, 0,
+ 0, 0, 28, 0, 71, 0, 111, 0, 111, 0, 103, 0, 108, 0, 101, 0, 32, 0, 73, 0,
+ 110, 0, 99, 0, 46, 0, 32, 0, 50, 0, 48, 0, 49, 0, 54,
+ ]),
+ parsed: {
+ bXYZ: {
+ x: 0,
+ y: 0.14306640625,
+ z: 0.06060791015625,
+ },
+ colorSpace: 'RGB',
+ dateTime: new Uint8Array([7, 224, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0]),
+ deviceAttributes: BigInt(0),
+ deviceManufacturer: '\u0000\u0000\u0000\u0000',
+ deviceModel: '\u0000\u0000\u0000\u0000',
+ entries: [
+ {
+ offset: 240,
+ size: 36,
+ tag: 'desc',
+ },
+ {
+ offset: 276,
+ size: 20,
+ tag: 'rXYZ',
+ },
+ {
+ offset: 296,
+ size: 20,
+ tag: 'gXYZ',
+ },
+ {
+ offset: 316,
+ size: 20,
+ tag: 'bXYZ',
+ },
+ {
+ offset: 336,
+ size: 20,
+ tag: 'wtpt',
+ },
+ {
+ offset: 356,
+ size: 40,
+ tag: 'rTRC',
+ },
+ {
+ offset: 356,
+ size: 40,
+ tag: 'gTRC',
+ },
+ {
+ offset: 356,
+ size: 40,
+ tag: 'bTRC',
+ },
+ {
+ offset: 396,
+ size: 60,
+ tag: 'cprt',
+ },
+ ],
+ gXYZ: {
+ x: 0,
+ y: 0.3851470947265625,
+ z: 0.7168731689453125,
+ },
+ pcs: 'XYZ',
+ pcsIlluminant: [0.964202880859375, 1, 0.8249053955078125],
+ preferredCMMType: '\u0000\u0000\u0000\u0000',
+ primaryPlatform: '\u0000\u0000\u0000\u0000',
+ profileCreator: '\u0000\u0000\u0000\u0000',
+ profileDeviceClass: 'mntr',
+ profileFlags: 0,
+ profileId:
+ '\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000',
+ profileVersion: '\u00040\u0000\u0000',
+ rXYZ: {
+ x: 0,
+ y: 0.436065673828125,
+ z: 0.2224884033203125,
+ },
+ renderingIntent: 1,
+ signature: 'acsp',
+ size: 456,
+ whitePoint: {
+ x: 0,
+ y: 0.964202880859375,
+ z: 1,
+ },
+ },
+ });
+});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index ff17306f4d..887619a04f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -12,8 +12,8 @@ importers:
.:
dependencies:
turbo:
- specifier: 2.2.3
- version: 2.2.3
+ specifier: 2.3.0
+ version: 2.3.0
typescript:
specifier: 5.5.4
version: 5.5.4
@@ -28261,32 +28261,32 @@ packages:
safe-buffer: 5.2.1
dev: true
- resolution: {integrity: sha512-Rcm10CuMKQGcdIBS3R/9PMeuYnv6beYIHqfZFeKWVYEWH69sauj4INs83zKMTUiZJ3/hWGZ4jet9AOwhsssLyg==}
+ resolution: {integrity: sha512-pji+D49PhFItyQjf2QVoLZw2d3oRGo8gJgKyOiRzvip78Rzie74quA8XNwSg/DuzM7xx6gJ3p2/LylTTlgZXxQ==}
cpu: [x64]
os: [darwin]
requiresBuild: true
dev: false
optional: true
- resolution: {integrity: sha512-+EIMHkuLFqUdJYsA3roj66t9+9IciCajgj+DVek+QezEdOJKcRxlvDOS2BUaeN8kEzVSsNiAGnoysFWYw4K0HA==}
+ resolution: {integrity: sha512-AJrGIL9BO41mwDF/IBHsNGwvtdyB911vp8f5mbNo1wG66gWTvOBg7WCtYQBvCo11XTenTfXPRSsAb7w3WAZb6w==}
cpu: [arm64]
os: [darwin]
requiresBuild: true
dev: false
optional: true
- resolution: {integrity: sha512-UBhJCYnqtaeOBQLmLo8BAisWbc9v9daL9G8upLR+XGj6vuN/Nz6qUAhverN4Pyej1g4Nt1BhROnj6GLOPYyqxQ==}
+ resolution: {integrity: sha512-jZqW6vc2sPJT3M/3ZmV1Cg4ecQVPqsbHncG/RnogHpBu783KCSXIndgxvUQNm9qfgBYbZDBnP1md63O4UTElhw==}
cpu: [x64]
os: [linux]
requiresBuild: true
dev: false
optional: true
- resolution: {integrity: sha512-hJYT9dN06XCQ3jBka/EWvvAETnHRs3xuO/rb5bESmDfG+d9yQjeTMlhRXKrr4eyIMt6cLDt1LBfyi+6CQ+VAwQ==}
+ resolution: {integrity: sha512-HUbDLJlvd/hxuyCNO0BmEWYQj0TugRMvSQeG8vHJH+Lq8qOgDAe7J0K73bFNbZejZQxW3C3XEiZFB3pnpO78+A==}
cpu: [arm64]
os: [linux]
requiresBuild: true
@@ -28299,32 +28299,32 @@ packages:
resolution: {integrity: sha512-FHncC10WpBd2eOmGwpmQsWLDoK4cqsA/UT/GqNoaKOQnT8uzhtCbg3EoUDMvqpOSAI0S26mr0rkjzbOO6S3v1g==}
- resolution: {integrity: sha512-NPrjacrZypMBF31b4HE4ROg4P3nhMBPHKS5WTpMwf7wydZ8uvdEHpESVNMOtqhlp857zbnKYgP+yJF30H3N2dQ==}
+ resolution: {integrity: sha512-c5rxrGNTYDWX9QeMzWLFE9frOXnKjHGEvQMp1SfldDlbZYsloX9UKs31TzUThzfTgTiz8NYuShaXJ2UvTMnV/g==}
cpu: [x64]
os: [win32]
requiresBuild: true
dev: false
optional: true
- resolution: {integrity: sha512-fnNrYBCqn6zgKPKLHu4sOkihBI/+0oYFr075duRxqUZ+1aLWTAGfHZLgjVeLh3zR37CVzuerGIPWAEkNhkWEIw==}
+ resolution: {integrity: sha512-7qfUuYhfIVb1AZgs89DxhXK+zZez6O2ocmixEQ4hXZK7ytnBt5vaz2zGNJJKFNYIL5HX1C3tuHolnpNgDNCUIg==}
cpu: [arm64]
os: [win32]
requiresBuild: true
dev: false
optional: true
- resolution: {integrity: sha512-5lDvSqIxCYJ/BAd6rQGK/AzFRhBkbu4JHVMLmGh/hCb7U3CqSnr5Tjwfy9vc+/5wG2DJ6wttgAaA7MoCgvBKZQ==}
+ resolution: {integrity: sha512-/uOq5o2jwRPyaUDnwBpOR5k9mQq4c3wziBgWNWttiYQPmbhDtrKYPRBxTvA2WpgQwRIbt8UM612RMN8n/TvmHA==}
hasBin: true
optionalDependencies:
- turbo-darwin-64: 2.2.3
- turbo-darwin-arm64: 2.2.3
- turbo-linux-64: 2.2.3
- turbo-linux-arm64: 2.2.3
- turbo-windows-64: 2.2.3
- turbo-windows-arm64: 2.2.3
+ turbo-darwin-64: 2.3.0
+ turbo-darwin-arm64: 2.3.0
+ turbo-linux-64: 2.3.0
+ turbo-linux-arm64: 2.3.0
+ turbo-windows-64: 2.3.0
+ turbo-windows-arm64: 2.3.0
dev: false