Skip to content

🎥 Make videos programmatically with React

finish iphone

Jonny Burger130ac9651e0d
diff --git a/packages/media-parser/src/av1-codec-string.ts b/packages/media-parser/src/av1-codec-string.ts
index e22cd09892..929bddcbff 100644
--- a/packages/media-parser/src/av1-codec-string.ts
+++ b/packages/media-parser/src/av1-codec-string.ts
@@ -1,8 +1,18 @@
// https://aomediacodec.github.io/av1-isobmff/#codecsparam
-import type {TrackEntrySegment} from './boxes/webm/segments/track-entry';
+import type {
+ ClusterSegment,
+ TrackEntrySegment,
+} from './boxes/webm/segments/track-entry';
+import {getAv1BitstreamHeader} from './boxes/webm/traversal';
-export const av1CodecStringToString = (track: TrackEntrySegment): string => {
+export const av1CodecStringToString = ({
+ track,
+ clusterSegment,
+}: {
+ track: TrackEntrySegment;
+ clusterSegment: ClusterSegment;
+}): string => {
const codecSegment = track.children.find((b) => b.type === 'codec-segment');
if (!codecSegment || codecSegment.type !== 'codec-segment') {
@@ -13,7 +23,101 @@ export const av1CodecStringToString = (track: TrackEntrySegment): string => {
throw new Error(`Unknown codec: ${codecSegment.codec}`);
}
- const str = 'av01.';
+ const simpleBlockSegment = clusterSegment.children.find((b) => {
+ if (b.type !== 'simple-block-segment') {
+ return false;
+ }
+
+ const child = b.children.find((c) => c.type === 'av1-bitstream-header');
+ return child;
+ });
+
+ if (
+ !simpleBlockSegment ||
+ simpleBlockSegment.type !== 'simple-block-segment'
+ ) {
+ throw new Error('Could not find simple block segment');
+ }
+
+ const av1BitstreamHeader = getAv1BitstreamHeader(clusterSegment);
+
+ let str = 'av01.';
+
+ // Profile
+ str += av1BitstreamHeader.seq_profile;
+ str += '.';
+
+ // Level
+ // The level parameter value SHALL equal the first level value indicated by seq_level_idx in the Sequence Header OBU
+ str += av1BitstreamHeader.seq_level[0].seq_level_idx
+ .toString()
+ .padStart(2, '0');
+
+ // Tier
+ // The tier parameter value SHALL be equal to M when the first seq_tier value in the Sequence Header OBU is equal to 0, and H when it is equal to 1
+ str += av1BitstreamHeader.seq_level[0].seq_tier ? 'H' : 'M';
+ str += '.';
+
+ // bitDepth
+ // The bitDepth parameter value SHALL equal the value of BitDepth variable as defined in [AV1] derived from the Sequence Header OBU
+ str += av1BitstreamHeader.color_config.bitDepth.toString().padStart(2, '0');
+ str += '.';
+
+ // monochrome
+ // The monochrome parameter value, represented by a single digit decimal, SHALL equal the value of mono_chrome in the Sequence Header OBU
+ str += av1BitstreamHeader.color_config.mono_chrome ? '1' : '0';
+ str += '.';
+
+ // The chromaSubsampling parameter value, represented by a three-digit decimal,
+ // SHALL have its first digit equal to subsampling_x
+ str += av1BitstreamHeader.color_config.subsampling_x ? '1' : '0';
+ // and its second digit equal to subsampling_y.
+ str += av1BitstreamHeader.color_config.subsampling_y ? '1' : '0';
+ // If both subsampling_x and subsampling_y are set to 1, then the third digit SHALL be equal to chroma_sample_position, otherwise it SHALL be set to 0
+ str +=
+ av1BitstreamHeader.color_config.subsampling_x &&
+ av1BitstreamHeader.color_config.subsampling_y
+ ? av1BitstreamHeader.color_config.chroma_sample_position === 1
+ ? '1'
+ : '0'
+ : '0';
+ str += '.';
+
+ // The colorPrimaries, transferCharacteristics, matrixCoefficients, and videoFullRangeFlag parameter values are set as follows:
+ // If a colr box with colour_type set to nclx is present, the colorPrimaries, transferCharacteristics, matrixCoefficients, and videoFullRangeFlag parameter values SHALL equal the values of matching fields in the colr box.
+ // This is not implemented in matroska
+
+ // Otherwise, a colr box with colour_type set to nclx is absent.
+ // If the color_description_present_flag is set to 1 in the Sequence Header OBU, the colorPrimaries, transferCharacteristics, and matrixCoefficients parameter values SHALL equal the values of matching fields in the Sequence Header OBU.
+ if (av1BitstreamHeader.color_config.color_description_present_flag) {
+ str += av1BitstreamHeader.color_config.color_primaries
+ .toString()
+ .padStart(2, '0');
+ str += '.';
+ str += av1BitstreamHeader.color_config.transfer_characteristics
+ .toString()
+ .padStart(2, '0');
+ str += '.';
+ str += av1BitstreamHeader.color_config.matrix_coefficients
+ .toString()
+ .padStart(2, '0');
+ str += '.';
+ // The videoFullRangeFlag parameter value SHALL equal the color_range flag in the Sequence Header OBU.
+ str += av1BitstreamHeader.color_config.color_range ? '1' : '0';
+ } else {
+ // Otherwise, the color_description_present_flag is set to 0 in the Sequence Header OBU. The colorPrimaries, transferCharacteristics, and matrixCoefficients parameter values SHOULD be set to the default values below.
+ // colorPrimaries 01 (ITU-R BT.709)
+ str += '01';
+ str += '.';
+ // transferCharacteristics 01 (ITU-R BT.709)
+ str += '01';
+ str += '.';
+ // matrixCoefficients 00 (ITU-R BT.709)
+ str += '01';
+ str += '.';
+ // videoFullRangeFlag 0 (studio swing representation)
+ str += '0';
+ }
return str;
};
diff --git a/packages/media-parser/src/boxes/webm/bitstream/av1/color-config.ts b/packages/media-parser/src/boxes/webm/bitstream/av1/color-config.ts
index a0dd40e24b..8b433fdd7c 100644
--- a/packages/media-parser/src/boxes/webm/bitstream/av1/color-config.ts
+++ b/packages/media-parser/src/boxes/webm/bitstream/av1/color-config.ts
@@ -15,6 +15,8 @@ export type ColorConfig = {
subsampling_x: boolean;
subsampling_y: boolean;
chroma_sample_position: number;
+ mono_chrome: boolean;
+ color_description_present_flag: boolean;
};
export const getColorConfig = (
@@ -64,6 +66,8 @@ export const getColorConfig = (
subsampling_x,
subsampling_y,
chroma_sample_position,
+ mono_chrome,
+ color_description_present_flag,
};
}
@@ -113,5 +117,7 @@ export const getColorConfig = (
color_range,
subsampling_x,
subsampling_y,
+ mono_chrome,
+ color_description_present_flag,
};
};
diff --git a/packages/media-parser/src/boxes/webm/traversal.ts b/packages/media-parser/src/boxes/webm/traversal.ts
index 877b8ac0ce..5f80a7ab59 100644
--- a/packages/media-parser/src/boxes/webm/traversal.ts
+++ b/packages/media-parser/src/boxes/webm/traversal.ts
@@ -1,4 +1,6 @@
+import type {Av1BitstreamHeaderSegment} from './bitstream/av1/header-segment';
import type {
+ ClusterSegment,
CodecSegment,
TrackEntrySegment,
TrackNumberSegment,
@@ -24,3 +26,36 @@ export const getTrackByNumber = (tracks: TrackEntrySegment[], id: number) => {
return trackNumber === id;
});
};
+
+export const getAv1BitstreamHeader = (
+ clusterSegment: ClusterSegment,
+): Av1BitstreamHeaderSegment => {
+ const simpleBlockSegment = clusterSegment.children.find((b) => {
+ if (b.type !== 'simple-block-segment') {
+ return false;
+ }
+
+ const child = b.children.find((c) => c.type === 'av1-bitstream-header');
+ return child;
+ });
+
+ if (
+ !simpleBlockSegment ||
+ simpleBlockSegment.type !== 'simple-block-segment'
+ ) {
+ throw new Error('Could not find simple block segment');
+ }
+
+ const av1BitstreamHeader = simpleBlockSegment.children.find(
+ (c) => c.type === 'av1-bitstream-header',
+ );
+
+ if (
+ !av1BitstreamHeader ||
+ av1BitstreamHeader.type !== 'av1-bitstream-header'
+ ) {
+ throw new Error('Could not find av1 bitstream header');
+ }
+
+ return av1BitstreamHeader ?? null;
+};
diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.ts
index d8b989683b..80d73e34b1 100644
--- a/packages/media-parser/src/get-tracks.ts
+++ b/packages/media-parser/src/get-tracks.ts
@@ -1,7 +1,10 @@
import {av1CodecStringToString} from './av1-codec-string';
import type {MoovBox} from './boxes/iso-base-media/moov/moov';
import type {MainSegment} from './boxes/webm/segments/main';
-import type {TrackEntrySegment} from './boxes/webm/segments/track-entry';
+import type {
+ ClusterSegment,
+ TrackEntrySegment,
+} from './boxes/webm/segments/track-entry';
import {getAudioCodecStringFromTrak} from './get-audio-codec';
import {
getTimescaleAndDuration,
@@ -91,7 +94,10 @@ export const hasTracks = (segments: AnySegment[]): boolean => {
return tracks.length === numberOfTracks;
};
-const getMatroskaVideoCodecString = (track: TrackEntrySegment): string => {
+const getMatroskaVideoCodecString = (
+ track: TrackEntrySegment,
+ cluster: ClusterSegment | null,
+): string => {
const codec = track.children.find((b) => b.type === 'codec-segment');
if (!codec || codec.type !== 'codec-segment') {
@@ -113,7 +119,11 @@ const getMatroskaVideoCodecString = (track: TrackEntrySegment): string => {
}
if (codec.codec === 'V_AV1') {
- return av1CodecStringToString(track);
+ if (!cluster) {
+ throw new Error('No cluster segment');
+ }
+
+ return av1CodecStringToString({track, clusterSegment: cluster});
}
throw new Error(`Unknown codec: ${codec.codec}`);
@@ -162,6 +172,10 @@ const getTracksFromMatroska = (
throw new Error('No timescale segment');
}
+ const clusterSegment = segment.children.find(
+ (b) => b.type === 'cluster-segment',
+ ) as ClusterSegment | undefined;
+
const videoTracks: VideoTrack[] = [];
const audioTracks: AudioTrack[] = [];
@@ -218,7 +232,7 @@ const getTracksFromMatroska = (
videoTracks.push({
type: 'video',
trackId: trackId.trackNumber,
- codecString: getMatroskaVideoCodecString(track),
+ codecString: getMatroskaVideoCodecString(track, clusterSegment ?? null),
description: null,
height: height.height,
width: width.width,
diff --git a/packages/media-parser/src/test/stream-local.test.ts b/packages/media-parser/src/test/stream-local.test.ts
index 2adbfbc14e..b0239f04a5 100644
--- a/packages/media-parser/src/test/stream-local.test.ts
+++ b/packages/media-parser/src/test/stream-local.test.ts
@@ -45,10 +45,11 @@ test('Should stream WebM with no duration', async () => {
videoCodec: true,
audioCodec: true,
rotation: true,
- // TODO: Return WebM tracks
+ tracks: true,
},
reader: nodeReader,
});
+
expect(result.durationInSeconds).toBe(6.57);
expect(result.dimensions).toEqual({
width: 1470,
@@ -58,6 +59,8 @@ test('Should stream WebM with no duration', async () => {
expect(result.videoCodec).toBe('vp8');
expect(result.audioCodec).toBe(null);
expect(result.rotation).toBe(0);
+ expect(result.videoTracks.length).toBe(1);
+ expect(result.videoTracks[0].codecString).toBe('vp8');
});
test('Should stream AV1 with no duration', async () => {
@@ -88,10 +91,7 @@ test('Should stream AV1 with no duration', async () => {
expect(parsed.videoTracks.length).toBe(1);
expect(parsed.videoTracks[0]).toEqual({
type: 'video',
- // TODO: it is the wrong one
- // <sample entry 4CC>.<profile>.<level><tier>.<bitDepth>.<monochrome>.<chromaSubsampling>.
- // <colorPrimaries>.<transferCharacteristics>.<matrixCoefficients>.<videoFullRangeFlag>
- codecString: 'av01.',
+ codecString: 'av01.0.08M.08.0.110.01.01.01.0',
description: null,
sampleAspectRatio: {
denominator: 1,