Skip to content

🎥 Make videos programmatically with React

pass track and color

Jonny Burgerf852bd494004
diff --git a/packages/media-parser/src/boxes/iso-base-media/make-track.ts b/packages/media-parser/src/boxes/iso-base-media/make-track.ts
index fab356231b..da1e68aeb7 100644
--- a/packages/media-parser/src/boxes/iso-base-media/make-track.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/make-track.ts
@@ -13,10 +13,14 @@ import {
applyTkhdBox,
getDisplayAspectRatio,
getSampleAspectRatio,
- getVideoSample,
+ getStsdVideoConfig,
} from '../../get-sample-aspect-ratio';
import type {AudioTrack, OtherTrack, VideoTrack} from '../../get-tracks';
-import {getVideoCodecString} from '../../get-video-codec';
+import {
+ getIsoBmColrConfig,
+ getVideoCodecString,
+ getVideoPrivateData,
+} from '../../get-video-codec';
import type {TrakBox} from './trak/trak';
import {getTkhdBox, getVideoDescriptors} from './traversal';
@@ -71,7 +75,7 @@ export const makeBaseMediaTrack = (
};
}
- const videoSample = getVideoSample(trakBox);
+ const videoSample = getStsdVideoConfig(trakBox);
if (!videoSample) {
throw new Error('No video sample');
}
@@ -96,6 +100,8 @@ export const makeBaseMediaTrack = (
throw new Error('Could not find video codec');
}
+ const privateData = getVideoPrivateData(trakBox);
+
const track: VideoTrack = {
type: 'video',
trackId: tkhdBox.trackId,
@@ -112,17 +118,12 @@ export const makeBaseMediaTrack = (
displayAspectHeight,
rotation,
trakBox,
- // TODO: Copy over private data
- codecPrivate: null,
- color: {
- // TODO: Set transferCharacteristics
- transferCharacteristics: null,
- // TODO matrixCoefficients
+ codecPrivate: privateData,
+ color: getIsoBmColrConfig(trakBox) ?? {
+ fullRange: null,
matrixCoefficients: null,
- // TODO primaries
primaries: null,
- // TODO fullRange
- fullRange: null,
+ transferCharacteristics: null,
},
};
return track;
diff --git a/packages/media-parser/src/boxes/iso-base-media/stsd/avcc.ts b/packages/media-parser/src/boxes/iso-base-media/stsd/avcc.ts
index 17a8ae5a49..304befc875 100644
--- a/packages/media-parser/src/boxes/iso-base-media/stsd/avcc.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/stsd/avcc.ts
@@ -2,7 +2,7 @@ import type {BufferIterator} from '../../../buffer-iterator';
export interface AvccBox {
type: 'avcc-box';
- description: Uint8Array;
+ privateData: Uint8Array;
configurationString: string;
}
@@ -26,11 +26,11 @@ export const parseAvcc = ({
data.counter.decrement(4);
- const description = data.getSlice(size - 8);
+ const privateData = data.getSlice(size - 8);
return {
type: 'avcc-box',
- description,
+ privateData,
configurationString: str,
};
};
diff --git a/packages/media-parser/src/boxes/iso-base-media/stsd/hvcc.ts b/packages/media-parser/src/boxes/iso-base-media/stsd/hvcc.ts
index 3a55a0971d..181a46152f 100644
--- a/packages/media-parser/src/boxes/iso-base-media/stsd/hvcc.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/stsd/hvcc.ts
@@ -3,7 +3,7 @@ import {getHvc1CodecString} from '../../../make-hvc1-codec-strings';
export interface HvccBox {
type: 'hvcc-box';
- data: Uint8Array;
+ privateData: Uint8Array;
configurationString: string;
}
@@ -16,7 +16,7 @@ export const parseHvcc = ({
size: number;
offset: number;
}): HvccBox => {
- const raw = data.getSlice(size - 8);
+ const privateData = data.getSlice(size - 8);
data.counter.decrement(size - 8);
const constraintString = getHvc1CodecString(data);
@@ -26,7 +26,7 @@ export const parseHvcc = ({
return {
type: 'hvcc-box',
- data: raw,
+ privateData,
configurationString: constraintString,
};
};
diff --git a/packages/media-parser/src/boxes/iso-base-media/traversal.ts b/packages/media-parser/src/boxes/iso-base-media/traversal.ts
index d99946d6c6..b73ca8d178 100644
--- a/packages/media-parser/src/boxes/iso-base-media/traversal.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/traversal.ts
@@ -145,9 +145,9 @@ export const getVideoDescriptors = (trakBox: TrakBox): Uint8Array | null => {
return s.type === 'video'
? s.descriptors.map((d) => {
return d.type === 'avcc-box'
- ? d.description
+ ? d.privateData
: d.type === 'hvcc-box'
- ? d.data
+ ? d.privateData
: null;
})
: [];
diff --git a/packages/media-parser/src/get-sample-aspect-ratio.ts b/packages/media-parser/src/get-sample-aspect-ratio.ts
index bc6a653bfc..9e3d77da21 100644
--- a/packages/media-parser/src/get-sample-aspect-ratio.ts
+++ b/packages/media-parser/src/get-sample-aspect-ratio.ts
@@ -14,28 +14,28 @@ type AspectRatio = {
denominator: number;
};
-export const getVideoSample = (trakBox: TrakBox): VideoSample | null => {
+export const getStsdVideoConfig = (trakBox: TrakBox): VideoSample | null => {
const stsdBox = getStsdBox(trakBox);
if (!stsdBox) {
return null;
}
- const videoSample = stsdBox.samples.find((s) => s.type === 'video');
- if (!videoSample || videoSample.type !== 'video') {
+ const videoConfig = stsdBox.samples.find((s) => s.type === 'video');
+ if (!videoConfig || videoConfig.type !== 'video') {
return null;
}
- return videoSample;
+ return videoConfig;
};
export const getAvccBox = (trakBox: TrakBox): AvccBox | null => {
- const videoSample = getVideoSample(trakBox);
- if (!videoSample) {
+ const videoConfig = getStsdVideoConfig(trakBox);
+ if (!videoConfig) {
return null;
}
- const avccBox = videoSample.descriptors.find((c) => c.type === 'avcc-box');
+ const avccBox = videoConfig.descriptors.find((c) => c.type === 'avcc-box');
if (!avccBox || avccBox.type !== 'avcc-box') {
return null;
@@ -45,12 +45,12 @@ export const getAvccBox = (trakBox: TrakBox): AvccBox | null => {
};
export const getAv1CBox = (trakBox: TrakBox): Av1CBox | null => {
- const videoSample = getVideoSample(trakBox);
- if (!videoSample) {
+ const videoConfig = getStsdVideoConfig(trakBox);
+ if (!videoConfig) {
return null;
}
- const av1cBox = videoSample.descriptors.find((c) => c.type === 'av1C-box');
+ const av1cBox = videoConfig.descriptors.find((c) => c.type === 'av1C-box');
if (!av1cBox || av1cBox.type !== 'av1C-box') {
return null;
@@ -60,12 +60,12 @@ export const getAv1CBox = (trakBox: TrakBox): Av1CBox | null => {
};
export const getPaspBox = (trakBox: TrakBox): PaspBox | null => {
- const videoSample = getVideoSample(trakBox);
- if (!videoSample) {
+ const videoConfig = getStsdVideoConfig(trakBox);
+ if (!videoConfig) {
return null;
}
- const paspBox = videoSample.descriptors.find((c) => c.type === 'pasp-box');
+ const paspBox = videoConfig.descriptors.find((c) => c.type === 'pasp-box');
if (!paspBox || paspBox.type !== 'pasp-box') {
return null;
@@ -75,12 +75,12 @@ export const getPaspBox = (trakBox: TrakBox): PaspBox | null => {
};
export const getHvccBox = (trakBox: TrakBox): HvccBox | null => {
- const videoSample = getVideoSample(trakBox);
- if (!videoSample) {
+ const videoConfig = getStsdVideoConfig(trakBox);
+ if (!videoConfig) {
return null;
}
- const hvccBox = videoSample.descriptors.find((c) => c.type === 'hvcc-box');
+ const hvccBox = videoConfig.descriptors.find((c) => c.type === 'hvcc-box');
if (!hvccBox || hvccBox.type !== 'hvcc-box') {
return null;
diff --git a/packages/media-parser/src/get-video-codec.ts b/packages/media-parser/src/get-video-codec.ts
index 51f95361e9..d2b3af9185 100644
--- a/packages/media-parser/src/get-video-codec.ts
+++ b/packages/media-parser/src/get-video-codec.ts
@@ -12,8 +12,9 @@ import {
getAvccBox,
getColrBox,
getHvccBox,
- getVideoSample,
+ getStsdVideoConfig,
} from './get-sample-aspect-ratio';
+import type {VideoTrackColorParams} from './get-tracks';
import type {KnownVideoCodecs} from './options';
import type {AnySegment} from './parse-result';
@@ -25,8 +26,76 @@ export const hasVideoCodec = (boxes: AnySegment[]): boolean => {
}
};
+export const getVideoPrivateData = (trakBox: TrakBox): Uint8Array | null => {
+ const videoSample = getStsdVideoConfig(trakBox);
+ const avccBox = getAvccBox(trakBox);
+ const hvccBox = getHvccBox(trakBox);
+ const av1cBox = getAv1CBox(trakBox);
+
+ if (!videoSample) {
+ return null;
+ }
+
+ if (avccBox) {
+ return avccBox.privateData;
+ }
+
+ if (hvccBox) {
+ return hvccBox.privateData;
+ }
+
+ if (av1cBox) {
+ return av1cBox.privateData;
+ }
+
+ return null;
+};
+
+export const getIsoBmColrConfig = (
+ trakBox: TrakBox,
+): VideoTrackColorParams | null => {
+ const videoSample = getStsdVideoConfig(trakBox);
+ if (!videoSample) {
+ return null;
+ }
+
+ const colrAtom = getColrBox(videoSample);
+ if (!colrAtom) {
+ return null;
+ }
+
+ // https://github.com/bbc/qtff-parameter-editor
+ return {
+ fullRange: colrAtom.fullRangeFlag,
+ matrixCoefficients:
+ colrAtom.matrixIndex === 1
+ ? 'bt709'
+ : colrAtom.matrixIndex === 5
+ ? 'bt470bg'
+ : colrAtom.matrixIndex === 6
+ ? 'smpte170m'
+ : null,
+ primaries:
+ colrAtom.primaries === 1
+ ? 'bt709'
+ : colrAtom.primaries === 5
+ ? 'bt470bg'
+ : colrAtom.primaries === 6
+ ? 'smpte170m'
+ : null,
+ transferCharacteristics:
+ colrAtom.transfer === 1
+ ? 'bt709'
+ : colrAtom.transfer === 6
+ ? 'smpte170m'
+ : colrAtom.transfer === 13
+ ? 'iec61966-2-1'
+ : null,
+ };
+};
+
export const getVideoCodecString = (trakBox: TrakBox): string | null => {
- const videoSample = getVideoSample(trakBox);
+ const videoSample = getStsdVideoConfig(trakBox);
const avccBox = getAvccBox(trakBox);
const hvccBox = getHvccBox(trakBox);
const av1cBox = getAv1CBox(trakBox);
diff --git a/packages/media-parser/src/test/stream-local.test.ts b/packages/media-parser/src/test/stream-local.test.ts
index 9ddbd538bd..f63549e07b 100644
--- a/packages/media-parser/src/test/stream-local.test.ts
+++ b/packages/media-parser/src/test/stream-local.test.ts
@@ -152,7 +152,7 @@ test('Should stream AV1', async () => {
129, 8, 12, 0, 10, 14, 0, 0, 0, 66, 171, 191, 195, 118, 0, 8, 8, 8, 8, 32,
]),
color: {
- fullRange: true,
+ fullRange: null,
transferCharacteristics: 'bt709',
matrixCoefficients: 'bt709',
primaries: 'bt709',
@@ -314,10 +314,10 @@ test('Should stream variable fps video', async () => {
trakBox: null,
codecPrivate: null,
color: {
- fullRange: true,
- transferCharacteristics: 'bt709',
- matrixCoefficients: 'bt709',
- primaries: 'bt709',
+ fullRange: null,
+ transferCharacteristics: null,
+ matrixCoefficients: null,
+ primaries: null,
},
});
expect(parsed.audioTracks.length).toBe(1);
@@ -613,10 +613,10 @@ test('Stretched VP8', async () => {
rotation: 0,
codecPrivate: null,
color: {
- fullRange: true,
- transferCharacteristics: 'bt709',
- matrixCoefficients: 'bt709',
- primaries: 'bt709',
+ fullRange: null,
+ transferCharacteristics: null,
+ matrixCoefficients: null,
+ primaries: null,
},
});
});
diff --git a/packages/media-parser/src/test/stream-samples.test.ts b/packages/media-parser/src/test/stream-samples.test.ts
index b55def78ac..f033c88edc 100644
--- a/packages/media-parser/src/test/stream-samples.test.ts
+++ b/packages/media-parser/src/test/stream-samples.test.ts
@@ -40,12 +40,16 @@ test('Stream samples', async () => {
displayAspectHeight: 1080,
displayAspectWidth: 1080,
rotation: 0,
- codecPrivate: null,
+ codecPrivate: new Uint8Array([
+ 1, 100, 0, 32, 255, 225, 0, 32, 103, 100, 0, 32, 172, 217, 64, 68, 2, 39,
+ 150, 92, 5, 168, 16, 16, 45, 40, 0, 0, 3, 0, 8, 0, 0, 3, 1, 224, 120, 193,
+ 140, 176, 1, 0, 6, 104, 235, 224, 140, 178, 44, 253, 248, 248, 0,
+ ]),
color: {
- fullRange: true,
- transferCharacteristics: 'bt709',
- matrixCoefficients: 'bt709',
- primaries: 'bt709',
+ fullRange: null,
+ transferCharacteristics: null,
+ matrixCoefficients: null,
+ primaries: null,
},
});
diff --git a/packages/media-parser/src/test/stsd.test.ts b/packages/media-parser/src/test/stsd.test.ts
index 21307a315e..9f21a1459f 100644
--- a/packages/media-parser/src/test/stsd.test.ts
+++ b/packages/media-parser/src/test/stsd.test.ts
@@ -247,7 +247,7 @@ test('Should be able to parse a STSD video box correctly', async () => {
colorTableId: -1,
descriptors: [
{
- description: new Uint8Array([
+ privateData: new Uint8Array([
1, 100, 0, 32, 255, 225, 0, 27, 103, 100, 0, 32, 172, 217, 64, 68, 2,
39, 150, 92, 4, 64, 0, 0, 3, 0, 64, 0, 0, 12, 3, 198, 12, 101, 128, 1,
0, 6, 104, 235, 224, 140, 178, 44, 253, 248, 248, 0,
diff --git a/packages/webcodecs/src/on-video-track.ts b/packages/webcodecs/src/on-video-track.ts
index 609940a74a..691ed956b2 100644
--- a/packages/webcodecs/src/on-video-track.ts
+++ b/packages/webcodecs/src/on-video-track.ts
@@ -50,12 +50,7 @@ export const makeVideoTrackHandler =
const videoTrack = await state.addTrack({
type: 'video',
// TODO: Copy over colors, CodecPrivate...
- color: {
- transferCharacteristics: 'bt709',
- matrixCoefficients: 'bt709',
- primaries: 'bt709',
- fullRange: true,
- },
+ color: track.color,
width: track.codedWidth,
height: track.codedHeight,
codecId: track.codec,