best audio picking improvements for youtube, tiktok, and soundcloud (#476)

This commit is contained in:
jj 2024-04-30 09:39:32 +02:00 committed by GitHub
commit 080fc043ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 49 additions and 60 deletions

View file

@ -137,40 +137,22 @@ 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 = isBestAudio || audioFormat === "mp3";
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 isTumblrAudio = host === "tumblr" && !r.filename; const isTumblrAudio = host === "tumblr" && !r.filename;
const isSoundCloud = host === "soundcloud"; const isSoundCloud = host === "soundcloud";
if (isTikTok && services.tiktok.audioFormats.includes(audioFormat)) {
if (r.isMp3 && isBestOrMp3) {
audioFormat = "mp3";
processType = "bridge"
} else if (isBestAudio) {
audioFormat = "m4a";
processType = "bridge"
}
}
if (isSoundCloud && services.soundcloud.audioFormats.includes(audioFormat)) {
if (r.isMp3 && isBestOrMp3) {
audioFormat = "mp3";
processType = "render"
copy = true
} else if (isBestAudio || audioFormat === "opus") {
audioFormat = "opus";
processType = "render"
copy = true
}
}
if (isBestAudioDefined || isBestHostAudio) { if (isBestAudioDefined || isBestHostAudio) {
audioFormat = services[host]["bestAudio"]; audioFormat = serviceBestAudio;
processType = "bridge"; processType = "bridge";
if (isSoundCloud) {
processType = "render"
copy = true
}
} else if (isBestAudio && !isSoundCloud) { } else if (isBestAudio && !isSoundCloud) {
audioFormat = "m4a"; audioFormat = "m4a";
copy = true copy = true

View file

@ -4,7 +4,7 @@ import { cleanString } from "../../sub/utils.js";
const cachedID = { const cachedID = {
version: '', version: '',
id: '' id: ''
}; }
async function findClientID() { async function findClientID() {
try { try {
@ -32,9 +32,7 @@ async function findClientID() {
cachedID.id = clientid; cachedID.id = clientid;
return clientid; return clientid;
} catch (e) { } catch {}
return false;
}
} }
export default async function(obj) { export default async function(obj) {
@ -58,27 +56,30 @@ export default async function(obj) {
let json = await fetch(`https://api-v2.soundcloud.com/resolve?url=${link}&client_id=${clientId}`).then((r) => { let json = await fetch(`https://api-v2.soundcloud.com/resolve?url=${link}&client_id=${clientId}`).then((r) => {
return r.status === 200 ? r.json() : false return r.status === 200 ? r.json() : false
}).catch(() => { return false }); }).catch(() => {});
if (!json) return { error: 'ErrorCouldntFetch' }; if (!json) return { error: 'ErrorCouldntFetch' };
if (!json["media"]["transcodings"]) return { error: 'ErrorEmptyDownload' }; if (!json["media"]["transcodings"]) return { error: 'ErrorEmptyDownload' };
let isMp3, let bestAudio = "opus",
selectedStream = json.media.transcodings.filter(v => v.preset === "opus_0_0") selectedStream = json.media.transcodings.find(v => v.preset === "opus_0_0");
// fall back to mp3 if no opus is available // fall back to mp3 if no opus is available
if (selectedStream.length === 0) { if (selectedStream.length === 0) {
selectedStream = json.media.transcodings.filter(v => v.preset === "mp3_0_0") selectedStream = json.media.transcodings.find(v => v.preset === "mp3_0_0");
isMp3 = true bestAudio = "mp3"
} }
let fileUrlBase = selectedStream[0]["url"];
let fileUrlBase = selectedStream.url;
let fileUrl = `${fileUrlBase}${fileUrlBase.includes("?") ? "&" : "?"}client_id=${clientId}&track_authorization=${json.track_authorization}`; let fileUrl = `${fileUrlBase}${fileUrlBase.includes("?") ? "&" : "?"}client_id=${clientId}&track_authorization=${json.track_authorization}`;
if (fileUrl.substring(0, 54) !== "https://api-v2.soundcloud.com/media/soundcloud:tracks:") return { error: 'ErrorEmptyDownload' }; if (fileUrl.substring(0, 54) !== "https://api-v2.soundcloud.com/media/soundcloud:tracks:") return { error: 'ErrorEmptyDownload' };
if (json.duration > maxVideoDuration) return { error: ['ErrorLengthAudioConvert', maxVideoDuration / 60000] }; if (json.duration > maxVideoDuration)
return { error: ['ErrorLengthAudioConvert', maxVideoDuration / 60000] };
let file = await fetch(fileUrl).then(async (r) => { return (await r.json()).url }).catch(() => { return false }); let file = await fetch(fileUrl).then(async (r) => { return (await r.json()).url }).catch(() => {});
if (!file) return { error: 'ErrorCouldntFetch' }; if (!file) return { error: 'ErrorCouldntFetch' };
let fileMetadata = { let fileMetadata = {
@ -94,7 +95,7 @@ export default async function(obj) {
title: fileMetadata.title, title: fileMetadata.title,
author: fileMetadata.artist author: fileMetadata.artist
}, },
isMp3, bestAudio,
fileMetadata fileMetadata
} }
} }

View file

@ -41,8 +41,9 @@ export default async function(obj) {
detail = detail?.aweme_list?.find(v => v.aweme_id === postId); detail = detail?.aweme_list?.find(v => v.aweme_id === postId);
if (!detail) return { error: 'ErrorCouldntFetch' }; if (!detail) return { error: 'ErrorCouldntFetch' };
let video, videoFilename, audioFilename, isMp3, audio, images, let video, videoFilename, audioFilename, audio, images,
filenameBase = `tiktok_${detail.author.unique_id}_${postId}`; filenameBase = `tiktok_${detail.author.unique_id}_${postId}`,
bestAudio = 'm4a';
images = detail.image_post_info?.images; images = detail.image_post_info?.images;
@ -56,12 +57,12 @@ export default async function(obj) {
} else { } else {
let fallback = playAddr.url_list[0]; let fallback = playAddr.url_list[0];
audio = fallback; audio = fallback;
audioFilename = `${filenameBase}_audio_fv`; // fv - from video audioFilename = `${filenameBase}_audio`;
if (obj.fullAudio || fallback.includes("music")) { if (obj.fullAudio || fallback.includes("music")) {
audio = detail.music.play_url.url_list[0] audio = detail.music.play_url.url_list[0]
audioFilename = `${filenameBase}_audio` audioFilename = `${filenameBase}_audio_original`
} }
if (audio.slice(-4) === ".mp3") isMp3 = true; if (audio.slice(-4) === ".mp3") bestAudio = 'mp3';
} }
if (video) return { if (video) return {
@ -72,7 +73,7 @@ export default async function(obj) {
urls: audio, urls: audio,
audioFilename: audioFilename, audioFilename: audioFilename,
isAudioOnly: true, isAudioOnly: true,
isMp3: isMp3 bestAudio
} }
if (images) { if (images) {
let imageLinks = []; let imageLinks = [];
@ -86,13 +87,13 @@ export default async function(obj) {
urls: audio, urls: audio,
audioFilename: audioFilename, audioFilename: audioFilename,
isAudioOnly: true, isAudioOnly: true,
isMp3: isMp3 bestAudio
} }
} }
if (audio) return { if (audio) return {
urls: audio, urls: audio,
audioFilename: audioFilename, audioFilename: audioFilename,
isAudioOnly: true, isAudioOnly: true,
isMp3: isMp3 bestAudio
} }
} }

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,

View file

@ -57,7 +57,6 @@
"alias": "tiktok videos, photos & audio", "alias": "tiktok videos, photos & audio",
"patterns": [":user/video/:postId", ":id", "t/:id", ":user/photo/:postId"], "patterns": [":user/video/:postId", ":id", "t/:id", ":user/photo/:postId"],
"subdomains": ["vt", "vm"], "subdomains": ["vt", "vm"],
"audioFormats": ["best", "m4a", "mp3"],
"enabled": true "enabled": true
}, },
"douyin": { "douyin": {
@ -74,7 +73,6 @@
"soundcloud": { "soundcloud": {
"patterns": [":author/:song/s-:accessKey", ":author/:song", ":shortLink"], "patterns": [":author/:song/s-:accessKey", ":author/:song", ":shortLink"],
"subdomains": ["on", "m"], "subdomains": ["on", "m"],
"audioFormats": ["best", "opus", "mp3"],
"enabled": true "enabled": true
}, },
"instagram": { "instagram": {