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

View file

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