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(FormatUtils): Support multiple audio tracks in the DASH manifest #308

Merged
merged 1 commit into from
Feb 11, 2023
Merged
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
89 changes: 74 additions & 15 deletions src/utils/FormatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class FormatUtils {
if (!video_format.index_range || !video_format.init_range) {
return;
}
const mime_type = video_format.mime_type;
const mime_type = video_format.mime_type.split(';')[0];
const mime_type_index = mime_types.indexOf(mime_type);
if (mime_type_index > -1) {
mime_objects[mime_type_index].push(video_format);
Expand All @@ -326,23 +326,81 @@ class FormatUtils {
}
});

let set_id = 0;
for (let i = 0; i < mime_types.length; i++) {
const set = this.#el(document, 'AdaptationSet', {
id: `${i}`,
mimeType: mime_types[i].split(';')[0],
startWithSAP: '1',
subsegmentAlignment: 'true'
});

period.appendChild(set);
// 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[][] = [ [] ];

mime_objects[i].forEach((format) => {
const language_index = languages.indexOf(format.language as string);
if (language_index > -1) {
language_objects[language_index].push(format);
} else {
languages.push(format.language as string);
language_objects.push([]);
language_objects[languages.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];

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
})
);
}

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

mime_objects[i].forEach((format) => {
if (format.has_video) {
this.#generateRepresentationVideo(document, set, format, url_transformer, cpn, player);
} else {
this.#generateRepresentationAudio(document, set, format, url_transformer, cpn, player);
period.appendChild(set);

language_objects[j].forEach((format) => {
this.#generateRepresentationAudio(document, set, format, url_transformer, cpn, player);
});
}
});
} else {
const set = this.#el(document, 'AdaptationSet', {
id: `${set_id++}`,
mimeType: mime_types[i],
startWithSAP: '1',
subsegmentAlignment: 'true'
});

period.appendChild(set);

mime_objects[i].forEach((format) => {
if (format.has_video) {
this.#generateRepresentationVideo(document, set, format, url_transformer, cpn, player);
} else {
this.#generateRepresentationAudio(document, set, format, url_transformer, cpn, player);
}
});
}
}
}

Expand Down Expand Up @@ -388,7 +446,8 @@ class FormatUtils {
set.appendChild(this.#el(document, 'Representation', {
id: format.itag?.toString(),
codecs,
bandwidth: format.bitrate?.toString()
bandwidth: format.bitrate?.toString(),
audioSamplingRate: format.audio_sample_rate?.toString()
}, [
this.#el(document, 'AudioChannelConfiguration', {
schemeIdUri: 'urn:mpeg:dash:23003:3:audio_channel_configuration:2011',
Expand Down