youtube: fix audio track selection for dubbed videos & clean up

closes #642
This commit is contained in:
wukko 2024-07-26 19:54:19 +06:00
parent 0895c695e7
commit 7071157580
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2

View file

@ -1,25 +1,27 @@
import { Innertube, Session } from 'youtubei.js'; import { fetch } from "undici";
import { env } from '../../config.js';
import { cleanString } from '../../sub/utils.js'; import { Innertube, Session } from "youtubei.js";
import { fetch } from 'undici'
import { getCookie, updateCookieValues } from '../cookie/manager.js' import { env } from "../../config.js";
import { cleanString } from "../../sub/utils.js";
import { getCookie, updateCookieValues } from "../cookie/manager.js";
const ytBase = Innertube.create().catch(e => e); const ytBase = Innertube.create().catch(e => e);
const codecMatch = { const codecMatch = {
h264: { h264: {
codec: "avc1", videoCodec: "avc1",
aCodec: "mp4a", audioCodec: "mp4a",
container: "mp4" container: "mp4"
}, },
av1: { av1: {
codec: "av01", videoCodec: "av01",
aCodec: "mp4a", audioCodec: "mp4a",
container: "mp4" container: "mp4"
}, },
vp9: { vp9: {
codec: "vp9", videoCodec: "vp9",
aCodec: "opus", audioCodec: "opus",
container: "webm" container: "webm"
} }
} }
@ -94,11 +96,16 @@ const cloneInnertube = async (customFetch) => {
export default async function(o) { export default async function(o) {
const yt = await cloneInnertube( const yt = await cloneInnertube(
(input, init) => fetch(input, { ...init, dispatcher: o.dispatcher }) (input, init) => fetch(input, {
...init,
dispatcher: o.dispatcher
})
); );
let info, isDubbed, format = o.format || "h264"; const quality = o.quality === "max" ? "9000" : o.quality;
let quality = o.quality === "max" ? "9000" : o.quality; // 9000(p) - max quality
let info, isDubbed,
format = o.format || "h264";
function qual(i) { function qual(i) {
if (!i.quality_label) { if (!i.quality_label) {
@ -121,6 +128,7 @@ export default async function(o) {
if (!info) return { error: 'ErrorCantConnectToServiceAPI' }; if (!info) return { error: 'ErrorCantConnectToServiceAPI' };
const playability = info.playability_status; const playability = info.playability_status;
const basicInfo = info.basic_info;
if (playability.status === 'LOGIN_REQUIRED') { if (playability.status === 'LOGIN_REQUIRED') {
if (playability.reason.endsWith('bot')) { if (playability.reason.endsWith('bot')) {
@ -135,11 +143,11 @@ export default async function(o) {
} }
if (playability.status !== 'OK') return { error: 'ErrorYTUnavailable' }; if (playability.status !== 'OK') return { error: 'ErrorYTUnavailable' };
if (info.basic_info.is_live) return { error: 'ErrorLiveVideo' }; if (basicInfo.is_live) return { error: 'ErrorLiveVideo' };
// return a critical error if returned video is "Video Not Available" // return a critical error if returned video is "Video Not Available"
// or a similar stub by youtube // or a similar stub by youtube
if (info.basic_info.id !== o.id) { if (basicInfo.id !== o.id) {
return { return {
error: 'ErrorCantConnectToServiceAPI', error: 'ErrorCantConnectToServiceAPI',
critical: true critical: true
@ -148,11 +156,16 @@ export default async function(o) {
let bestQuality, hasAudio; let bestQuality, hasAudio;
const filterByCodec = (formats) => formats.filter(e => const filterByCodec = (formats) =>
e.mime_type.includes(codecMatch[format].codec) || e.mime_type.includes(codecMatch[format].aCodec) formats
).sort((a, b) => Number(b.bitrate) - Number(a.bitrate)); .filter(e =>
e.mime_type.includes(codecMatch[format].videoCodec)
|| e.mime_type.includes(codecMatch[format].audioCodec)
)
.sort((a, b) => Number(b.bitrate) - Number(a.bitrate));
let adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats); let adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats);
if (adaptive_formats.length === 0 && format === "vp9") { if (adaptive_formats.length === 0 && format === "vp9") {
format = "h264" format = "h264"
adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats) adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats)
@ -162,27 +175,43 @@ export default async function(o) {
hasAudio = adaptive_formats.find(i => i.has_audio && i.content_length); hasAudio = adaptive_formats.find(i => i.has_audio && i.content_length);
if (bestQuality) bestQuality = qual(bestQuality); if (bestQuality) bestQuality = qual(bestQuality);
if (!bestQuality && !o.isAudioOnly || !hasAudio) return { error: 'ErrorYTTryOtherCodec' };
if (info.basic_info.duration > env.durationLimit) return { error: ['ErrorLengthLimit', env.durationLimit / 60] };
let checkBestAudio = (i) => (i.has_audio && !i.has_video), if (!bestQuality && !o.isAudioOnly || !hasAudio)
audio = adaptive_formats.find(i => checkBestAudio(i) && !i.is_dubbed); return { error: 'ErrorYTTryOtherCodec' };
if (basicInfo.duration > env.durationLimit)
return { error: ['ErrorLengthLimit', env.durationLimit / 60] };
const checkBestAudio = (i) => (i.has_audio && !i.has_video);
let audio = adaptive_formats.find(i =>
checkBestAudio(i) && i.is_original
);
if (o.dubLang) { if (o.dubLang) {
let dubbedAudio = adaptive_formats.find(i => let dubbedAudio = adaptive_formats.find(i =>
checkBestAudio(i) && i.language === o.dubLang && i.audio_track && !i.audio_track.audio_is_default checkBestAudio(i)
); && i.language === o.dubLang
&& i.audio_track
)
if (dubbedAudio) { if (dubbedAudio) {
audio = dubbedAudio; audio = dubbedAudio;
isDubbed = true isDubbed = true;
} }
} }
if (!audio) {
audio = adaptive_formats.find(i => checkBestAudio(i));
}
let fileMetadata = { let fileMetadata = {
title: cleanString(info.basic_info.title.trim()), title: cleanString(basicInfo.title.trim()),
artist: cleanString(info.basic_info.author.replace("- Topic", "").trim()), artist: cleanString(basicInfo.author.replace("- Topic", "").trim()),
} }
if (info.basic_info.short_description && info.basic_info.short_description.startsWith("Provided to YouTube by")) {
let descItems = info.basic_info.short_description.split("\n\n"); if (basicInfo?.short_description?.startsWith("Provided to YouTube by")) {
let descItems = basicInfo.short_description.split("\n\n");
fileMetadata.album = descItems[2]; fileMetadata.album = descItems[2];
fileMetadata.copyright = descItems[3]; fileMetadata.copyright = descItems[3];
if (descItems[4].startsWith("Released on:")) { if (descItems[4].startsWith("Released on:")) {
@ -204,13 +233,17 @@ export default async function(o) {
urls: audio.decipher(yt.session.player), urls: audio.decipher(yt.session.player),
filenameAttributes: filenameAttributes, filenameAttributes: filenameAttributes,
fileMetadata: fileMetadata, fileMetadata: fileMetadata,
bestAudio: format === "h264" ? 'm4a' : 'opus' bestAudio: format === "h264" ? "m4a" : "opus"
} }
const matchingQuality = Number(quality) > Number(bestQuality) ? bestQuality : quality, const matchingQuality = Number(quality) > Number(bestQuality) ? bestQuality : quality,
checkSingle = i => qual(i) === matchingQuality && i.mime_type.includes(codecMatch[format].codec), checkSingle = i =>
checkRender = i => qual(i) === matchingQuality && i.has_video && !i.has_audio; qual(i) === matchingQuality && i.mime_type.includes(codecMatch[format].videoCodec),
checkRender = i =>
qual(i) === matchingQuality && i.has_video && !i.has_audio;
let match, type, urls; let match, type, urls;
if (!o.isAudioOnly && !o.isAudioMuted && format === 'h264') { if (!o.isAudioOnly && !o.isAudioMuted && format === 'h264') {
match = info.streaming_data.formats.find(checkSingle); match = info.streaming_data.formats.find(checkSingle);
type = "bridge"; type = "bridge";
@ -218,10 +251,14 @@ export default async function(o) {
} }
const video = adaptive_formats.find(checkRender); const video = adaptive_formats.find(checkRender);
if (!match && video) { if (!match && video) {
match = video; match = video;
type = "render"; type = "render";
urls = [video.decipher(yt.session.player), audio.decipher(yt.session.player)]; urls = [
video.decipher(yt.session.player),
audio.decipher(yt.session.player)
]
} }
if (match) { if (match) {