Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(toDash): Add color information #430

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/parser/classes/misc/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export default class Format {
is_descriptive?: boolean;
is_original?: boolean;

color_info?: {
primaries?: string;
transfer_characteristics?: string;
matrix_coefficients?: string;
};

constructor(data: RawNode) {
this.itag = data.itag;
this.mime_type = data.mimeType;
Expand Down Expand Up @@ -81,6 +87,12 @@ export default class Format {
this.has_audio = !!data.audioBitrate || !!data.audioQuality;
this.has_video = !!data.qualityLabel;

this.color_info = data.colorInfo ? {
primaries: data.colorInfo.primaries?.replace('COLOR_PRIMARIES_', ''),
transfer_characteristics: data.colorInfo.transferCharacteristics?.replace('COLOR_TRANSFER_CHARACTERISTICS_', ''),
matrix_coefficients: data.colorInfo.matrixCoefficients?.replace('COLOR_MATRIX_COEFFICIENTS_', '')
} : undefined;

if (this.has_audio) {
const args = new URLSearchParams(this.cipher || this.signature_cipher);
const url_components = new URLSearchParams(args.get('url') || this.url);
Expand Down
89 changes: 89 additions & 0 deletions src/utils/FormatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,95 @@ class FormatUtils {
subsegmentAlignment: 'true'
});

const color_info = mime_objects[i][0].color_info;
if (typeof color_info !== 'undefined') {
// Section 5.5 Video source metadata signalling https://dashif.org/docs/IOP-Guidelines/DASH-IF-IOP-Part7-v5.0.0.pdf
// Section 8 Video code points https://www.itu.int/rec/T-REC-H.273-202107-I/en
// The player.js file was also helpful

if (color_info.primaries) {
let primaries = '';

switch (color_info.primaries) {
case 'BT709':
primaries = '1';
break;
case 'BT2020':
primaries = '9';
break;
}

if (primaries !== '') {
set.appendChild(this.#el(document, 'EssentialProperty', {
schemeIdUri: 'urn:mpeg:mpegB:cicp:ColourPrimaries',
value: primaries
}));
}
}

if (color_info.transfer_characteristics) {
let transfer_characteristics = '';

switch (color_info.transfer_characteristics) {
case 'BT709':
transfer_characteristics = '1';
break;
case 'BT2020_10':
transfer_characteristics = '14';
break;
case 'SMPTEST2084':
transfer_characteristics = '16';
break;
case 'ARIB_STD_B67':
transfer_characteristics = '18';
break;
}

if (transfer_characteristics !== '') {
set.appendChild(this.#el(document, 'EssentialProperty', {
schemeIdUri: 'urn:mpeg:mpegB:cicp:TransferCharacteristics',
value: transfer_characteristics
}));
}
}


if (color_info.matrix_coefficients) {
let matrix_coefficients = '';

// This list is incomplete, as the player.js doesn't currently have any code for matrix coefficients,
// So it doesn't have a list like with the other two, so this is just based on what we've seen in responses
switch (color_info.matrix_coefficients) {
case 'BT709':
matrix_coefficients = '1';
break;
case 'BT2020_NCL':
matrix_coefficients = '14';
break;
default: {
const format = mime_objects[i][0];
const url = new URL(format.url as string);

const anonymisedFormat = JSON.parse(JSON.stringify(format));
anonymisedFormat.url = 'REDACTED';
anonymisedFormat.signature_cipher = 'REDACTED';
anonymisedFormat.cipher = 'REDACTED';

console.warn(`YouTube.js toDash(): Unknown matrix coefficients "${color_info.matrix_coefficients}", the DASH manifest is still usuable without this.\n`
+ `Please report it at ${Platform.shim.info.bugs_url} so we can add support for it.\n`
+ `Innertube client: ${url.searchParams.get('c')}\nformat:`, anonymisedFormat);
}
}

if (matrix_coefficients !== '') {
set.appendChild(this.#el(document, 'EssentialProperty', {
schemeIdUri: 'urn:mpeg:mpegB:cicp:MatrixCoefficients',
value: matrix_coefficients
}));
}
}
}

period.appendChild(set);

for (const format of mime_objects[i]) {
Expand Down