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: Add support for descriptive audio tracks #338

Merged
merged 1 commit into from
Mar 8, 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
2 changes: 2 additions & 0 deletions src/parser/classes/misc/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Format {
has_video: boolean;
language?: string | null;
is_dubbed?: boolean;
is_descriptive?: boolean;
is_original?: boolean;

constructor(data: any) {
Expand Down Expand Up @@ -83,6 +84,7 @@ class Format {

this.language = url_components.get('xtags')?.split(':').find((x: string) => x.startsWith('lang='))?.split('=').at(1) || null;
this.is_dubbed = url_components.get('xtags')?.split(':').find((x: string) => x.startsWith('acont='))?.split('=').at(1) === 'dubbed';
this.is_descriptive = url_components.get('xtags')?.split(':').find((x: string) => x.startsWith('acont='))?.split('=').at(1) === 'descriptive';
this.is_original = url_components.get('xtags')?.split(':').find((x: string) => x.startsWith('acont='))?.split('=').at(1) === 'original' || !this.is_dubbed;

if (data.audioTrack) {
Expand Down
64 changes: 33 additions & 31 deletions src/utils/FormatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ class FormatUtils {

const length = adaptive_formats[0].approx_duration_ms / 1000;

// DASH spec: https://standards.iso.org/ittf/PubliclyAvailableStandards/c083314_ISO_IEC%2023009-1_2022(en).zip

const document = new Platform.shim.DOMParser().parseFromString('<?xml version="1.0" encoding="utf-8"?><MPD />', 'application/xml');
const mpd = document.querySelector('MPD') as HTMLElement;
const period = document.createElement('Period');
Expand Down Expand Up @@ -324,57 +326,57 @@ class FormatUtils {

let set_id = 0;
for (let i = 0; i < mime_types.length; i++) {
// When the video has multiple different audio tracks/langues we want to include the extra information in the manifest
if (mime_objects[i][0].has_audio && mime_objects[i][0].language) {
const languages: string[] = [];
const language_objects: Format[][] = [ [] ];
// When the video has multiple different audio tracks we want to include the extra information in the manifest
if (mime_objects[i][0].has_audio && mime_objects[i][0].audio_track) {
const track_ids: string[] = [];
const track_objects: Format[][] = [ [] ];

mime_objects[i].forEach((format) => {
const language_index = languages.indexOf(format.language as string);
if (language_index > -1) {
language_objects[language_index].push(format);
const id_index = track_ids.indexOf(format.audio_track?.id as string);
if (id_index > -1) {
track_objects[id_index].push(format);
} else {
languages.push(format.language as string);
language_objects.push([]);
language_objects[languages.length - 1].push(format);
track_ids.push(format.audio_track?.id as string);
track_objects.push([]);
track_objects[track_ids.length - 1].push(format);
}
});

// The lang attribute has to go on the AdaptationSet element, so we need a separate adaptation set for each language
for (let j = 0; j < languages.length; j++) {
const first_format = language_objects[j][0];
// The lang attribute has to go on the AdaptationSet element and the Role element goes inside the AdaptationSet too, so we need a separate adaptation set for each language and role
for (let j = 0; j < track_ids.length; j++) {
const first_format = track_objects[j][0];

const children = [];

if (first_format.audio_track) {
let role;
if (first_format.audio_track.audio_is_default) {
role = 'main';
} else if (first_format.is_dubbed) {
role = 'dub';
} else {
role = 'alternate';
}

children.push(
this.#el(document, 'Role', {
schemeIdUri: 'urn:mpeg:dash:role:2011',
value: role
})
);
let role;
if (first_format.audio_track?.audio_is_default) {
role = 'main';
} else if (first_format.is_dubbed) {
role = 'dub';
} else if (first_format.is_descriptive) {
role = 'description';
} else {
role = 'alternate';
}

children.push(
this.#el(document, 'Role', {
schemeIdUri: 'urn:mpeg:dash:role:2011',
value: role
})
);

const set = this.#el(document, 'AdaptationSet', {
id: `${set_id++}`,
mimeType: mime_types[i].split(';')[0],
startWithSAP: '1',
subsegmentAlignment: 'true',
lang: languages[j]
lang: first_format.language as string
}, children);

period.appendChild(set);

language_objects[j].forEach((format) => {
track_objects[j].forEach((format) => {
this.#generateRepresentationAudio(document, set, format, url_transformer, cpn, player);
});
}
Expand Down