youtube: fall back to m4a audio if opus isn't available

This commit is contained in:
wukko 2024-04-30 11:24:12 +06:00
parent 709d14ee9e
commit 276caa011a
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
2 changed files with 21 additions and 13 deletions

View file

@ -137,10 +137,11 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di
audioFormat = "best" audioFormat = "best"
} }
const serviceBestAudio = r.bestAudio || services[host]["bestAudio"];
const isBestAudio = audioFormat === "best"; const isBestAudio = audioFormat === "best";
const isBestOrMp3 = audioFormat === "mp3" || isBestAudio; const isBestOrMp3 = audioFormat === "mp3" || isBestAudio;
const isBestAudioDefined = isBestAudio && services[host]["bestAudio"]; const isBestAudioDefined = isBestAudio && serviceBestAudio;
const isBestHostAudio = services[host]["bestAudio"] && (audioFormat === services[host]["bestAudio"]); const isBestHostAudio = serviceBestAudio && (audioFormat === serviceBestAudio);
const isTikTok = host === "tiktok" || host === "douyin"; const isTikTok = host === "tiktok" || host === "douyin";
const isTumblrAudio = host === "tumblr" && !r.filename; const isTumblrAudio = host === "tumblr" && !r.filename;
@ -169,7 +170,7 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di
} }
if (isBestAudioDefined || isBestHostAudio) { if (isBestAudioDefined || isBestHostAudio) {
audioFormat = services[host]["bestAudio"]; audioFormat = serviceBestAudio;
processType = "bridge"; processType = "bridge";
} else if (isBestAudio && !isSoundCloud) { } else if (isBestAudio && !isSoundCloud) {
audioFormat = "m4a"; audioFormat = "m4a";

View file

@ -4,7 +4,7 @@ import { cleanString } from '../../sub/utils.js';
const yt = await Innertube.create(); const yt = await Innertube.create();
const c = { const codecMatch = {
h264: { h264: {
codec: "avc1", codec: "avc1",
aCodec: "mp4a", aCodec: "mp4a",
@ -23,8 +23,8 @@ const c = {
} }
export default async function(o) { export default async function(o) {
let info, isDubbed, let info, isDubbed, format = o.format || "h264";
quality = o.quality === "max" ? "9000" : o.quality; // 9000(p) - max quality let quality = o.quality === "max" ? "9000" : o.quality; // 9000(p) - max quality
function qual(i) { function qual(i) {
if (!i.quality_label) { if (!i.quality_label) {
@ -56,10 +56,16 @@ export default async function(o) {
let bestQuality, hasAudio; let bestQuality, hasAudio;
let adaptive_formats = info.streaming_data.adaptive_formats.filter(e => const filterByCodec = (formats) => formats.filter(e =>
e.mime_type.includes(c[o.format].codec) || e.mime_type.includes(c[o.format].aCodec) e.mime_type.includes(codecMatch[format].codec) || e.mime_type.includes(codecMatch[format].aCodec)
).sort((a, b) => Number(b.bitrate) - Number(a.bitrate)); ).sort((a, b) => Number(b.bitrate) - Number(a.bitrate));
let adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats);
if (adaptive_formats.length === 0 && format === "vp9") {
format = "h264"
adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats)
}
bestQuality = adaptive_formats.find(i => i.has_video); bestQuality = adaptive_formats.find(i => i.has_video);
hasAudio = adaptive_formats.find(i => i.has_audio); hasAudio = adaptive_formats.find(i => i.has_audio);
@ -105,14 +111,15 @@ export default async function(o) {
isAudioOnly: true, isAudioOnly: true,
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'
} }
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(c[o.format].codec), checkSingle = i => qual(i) === matchingQuality && i.mime_type.includes(codecMatch[format].codec),
checkRender = i => qual(i) === matchingQuality && i.has_video && !i.has_audio; checkRender = i => qual(i) === matchingQuality && i.has_video && !i.has_audio;
let match, type, urls; let match, type, urls;
if (!o.isAudioOnly && !o.isAudioMuted && o.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";
urls = match?.decipher(yt.session.player); urls = match?.decipher(yt.session.player);
@ -128,8 +135,8 @@ export default async function(o) {
if (match) { if (match) {
filenameAttributes.qualityLabel = match.quality_label; filenameAttributes.qualityLabel = match.quality_label;
filenameAttributes.resolution = `${match.width}x${match.height}`; filenameAttributes.resolution = `${match.width}x${match.height}`;
filenameAttributes.extension = c[o.format].container; filenameAttributes.extension = codecMatch[format].container;
filenameAttributes.youtubeFormat = o.format; filenameAttributes.youtubeFormat = format;
return { return {
type, type,
urls, urls,