mirror of
https://github.com/wukko/cobalt.git
synced 2025-01-12 20:25:06 +01:00
soundcloud: fall back to mp3 when no opus found
also made match action decider readable
This commit is contained in:
parent
4b9d61b13f
commit
197198ad79
5 changed files with 104 additions and 67 deletions
|
@ -108,8 +108,7 @@ export default async function(host, patternMatch, url, lang, obj) {
|
||||||
author: patternMatch["author"],
|
author: patternMatch["author"],
|
||||||
song: patternMatch["song"],
|
song: patternMatch["song"],
|
||||||
shortLink: patternMatch["shortLink"] || false,
|
shortLink: patternMatch["shortLink"] || false,
|
||||||
accessKey: patternMatch["accessKey"] || false,
|
accessKey: patternMatch["accessKey"] || false
|
||||||
format: obj.aFormat
|
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "instagram":
|
case "instagram":
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { apiJSON } from "../sub/utils.js";
|
||||||
import loc from "../../localization/manager.js";
|
import loc from "../../localization/manager.js";
|
||||||
import createFilename from "./createFilename.js";
|
import createFilename from "./createFilename.js";
|
||||||
|
|
||||||
export default function(r, host, audioFormat, isAudioOnly, lang, isAudioMuted, disableMetadata, filenamePattern) {
|
export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, disableMetadata, filenamePattern) {
|
||||||
let action,
|
let action,
|
||||||
responseType = 2,
|
responseType = 2,
|
||||||
defaultParams = {
|
defaultParams = {
|
||||||
|
@ -13,7 +13,8 @@ export default function(r, host, audioFormat, isAudioOnly, lang, isAudioMuted, d
|
||||||
createFilename(r.filenameAttributes, filenamePattern, isAudioOnly, isAudioMuted) : r.filename,
|
createFilename(r.filenameAttributes, filenamePattern, isAudioOnly, isAudioMuted) : r.filename,
|
||||||
fileMetadata: !disableMetadata ? r.fileMetadata : false
|
fileMetadata: !disableMetadata ? r.fileMetadata : false
|
||||||
},
|
},
|
||||||
params = {}
|
params = {},
|
||||||
|
audioFormat = String(userFormat)
|
||||||
|
|
||||||
if (r.isPhoto) action = "photo";
|
if (r.isPhoto) action = "photo";
|
||||||
else if (r.picker) action = "picker"
|
else if (r.picker) action = "picker"
|
||||||
|
@ -32,9 +33,49 @@ export default function(r, host, audioFormat, isAudioOnly, lang, isAudioMuted, d
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
|
default:
|
||||||
|
return apiJSON(0, { t: loc(lang, 'ErrorEmptyDownload') });
|
||||||
|
|
||||||
case "photo":
|
case "photo":
|
||||||
responseType = 1;
|
responseType = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "singleM3U8":
|
||||||
|
params = { type: "remux" }
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "muteVideo":
|
||||||
|
params = {
|
||||||
|
type: Array.isArray(r.urls) ? "bridge" : "mute",
|
||||||
|
u: Array.isArray(r.urls) ? r.urls[0] : r.urls,
|
||||||
|
mute: true
|
||||||
|
}
|
||||||
|
if (host === "reddit" && r.typeId === 1) responseType = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "picker":
|
||||||
|
responseType = 5;
|
||||||
|
switch (host) {
|
||||||
|
case "instagram":
|
||||||
|
case "twitter":
|
||||||
|
params = { picker: r.picker };
|
||||||
|
break;
|
||||||
|
case "douyin":
|
||||||
|
case "tiktok":
|
||||||
|
let pickerType = "render";
|
||||||
|
if (audioFormat === "mp3" || audioFormat === "best") {
|
||||||
|
audioFormat = "mp3";
|
||||||
|
pickerType = "bridge"
|
||||||
|
}
|
||||||
|
params = {
|
||||||
|
type: pickerType,
|
||||||
|
picker: r.picker,
|
||||||
|
u: Array.isArray(r.urls) ? r.urls[1] : r.urls,
|
||||||
|
copy: audioFormat === "best" ? true : false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case "video":
|
case "video":
|
||||||
switch (host) {
|
switch (host) {
|
||||||
case "bilibili":
|
case "bilibili":
|
||||||
|
@ -78,81 +119,63 @@ export default function(r, host, audioFormat, isAudioOnly, lang, isAudioMuted, d
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "singleM3U8":
|
|
||||||
params = { type: "remux" }
|
|
||||||
break;
|
|
||||||
case "muteVideo":
|
|
||||||
params = {
|
|
||||||
type: Array.isArray(r.urls) ? "bridge" : "mute",
|
|
||||||
u: Array.isArray(r.urls) ? r.urls[0] : r.urls,
|
|
||||||
mute: true
|
|
||||||
}
|
|
||||||
if (host === "reddit" && r.typeId === 1) responseType = 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "picker":
|
|
||||||
responseType = 5;
|
|
||||||
switch (host) {
|
|
||||||
case "instagram":
|
|
||||||
case "twitter":
|
|
||||||
params = { picker: r.picker };
|
|
||||||
break;
|
|
||||||
case "douyin":
|
|
||||||
case "tiktok":
|
|
||||||
let pickerType = "render";
|
|
||||||
if (audioFormat === "mp3" || audioFormat === "best") {
|
|
||||||
audioFormat = "mp3";
|
|
||||||
pickerType = "bridge"
|
|
||||||
}
|
|
||||||
params = {
|
|
||||||
type: pickerType,
|
|
||||||
picker: r.picker,
|
|
||||||
u: Array.isArray(r.urls) ? r.urls[1] : r.urls,
|
|
||||||
copy: audioFormat === "best" ? true : false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "audio":
|
case "audio":
|
||||||
if ((host === "reddit" && r.typeId === 1) || audioIgnore.includes(host)) {
|
if ((host === "reddit" && r.typeId === 1) || audioIgnore.includes(host)) {
|
||||||
return apiJSON(0, { t: loc(lang, 'ErrorEmptyDownload') })
|
return apiJSON(0, { t: loc(lang, 'ErrorEmptyDownload') })
|
||||||
}
|
}
|
||||||
|
|
||||||
let processType = "render";
|
let processType = "render",
|
||||||
let copy = false;
|
copy = false;
|
||||||
|
|
||||||
if (!supportedAudio.includes(audioFormat)) audioFormat = "best";
|
if (!supportedAudio.includes(audioFormat)) {
|
||||||
|
audioFormat = "best"
|
||||||
|
}
|
||||||
|
|
||||||
if ((host === "tiktok" || host === "douyin")
|
const isBestAudio = audioFormat === "best";
|
||||||
&& services.tiktok.audioFormats.includes(audioFormat)) {
|
const isBestOrMp3 = audioFormat === "mp3" || isBestAudio;
|
||||||
if (r.isMp3) {
|
const isBestAudioDefined = isBestAudio && services[host]["bestAudio"];
|
||||||
if (audioFormat === "mp3" || audioFormat === "best") {
|
const isBestHostAudio = services[host]["bestAudio"] && (audioFormat === services[host]["bestAudio"]);
|
||||||
audioFormat = "mp3";
|
|
||||||
processType = "bridge"
|
const isTikTok = host === "tiktok" || host === "douyin";
|
||||||
}
|
const isTumblr = host === "tumblr" && !r.filename;
|
||||||
} else if (audioFormat === "best") {
|
const isSoundCloud = host === "soundcloud";
|
||||||
|
|
||||||
|
if (isTikTok && services.tiktok.audioFormats.includes(audioFormat)) {
|
||||||
|
if (r.isMp3 && isBestOrMp3) {
|
||||||
|
audioFormat = "mp3";
|
||||||
|
processType = "bridge"
|
||||||
|
} else if (isBestAudio) {
|
||||||
audioFormat = "m4a";
|
audioFormat = "m4a";
|
||||||
processType = "bridge"
|
processType = "bridge"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (host === "tumblr" && !r.filename
|
|
||||||
&& (audioFormat === "best" || audioFormat === "mp3")) {
|
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 (isTumblr && isBestOrMp3) {
|
||||||
audioFormat = "mp3";
|
audioFormat = "mp3";
|
||||||
processType = "bridge"
|
processType = "bridge"
|
||||||
}
|
}
|
||||||
if ((audioFormat === "best" && services[host]["bestAudio"])
|
|
||||||
|| (services[host]["bestAudio"] && (audioFormat === services[host]["bestAudio"]))) {
|
if (isBestAudioDefined || isBestHostAudio) {
|
||||||
audioFormat = services[host]["bestAudio"];
|
audioFormat = services[host]["bestAudio"];
|
||||||
if (host === "soundcloud") {
|
processType = "bridge";
|
||||||
processType = "render"
|
} else if (isBestAudio && !isSoundCloud) {
|
||||||
copy = true
|
|
||||||
} else {
|
|
||||||
processType = "bridge"
|
|
||||||
}
|
|
||||||
} else if (audioFormat === "best") {
|
|
||||||
audioFormat = "m4a";
|
audioFormat = "m4a";
|
||||||
copy = true;
|
copy = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if (r.isM3U8 || host === "vimeo") {
|
if (r.isM3U8 || host === "vimeo") {
|
||||||
copy = false;
|
copy = false;
|
||||||
processType = "render"
|
processType = "render"
|
||||||
|
@ -165,8 +188,6 @@ export default function(r, host, audioFormat, isAudioOnly, lang, isAudioMuted, d
|
||||||
copy: copy
|
copy: copy
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
return apiJSON(0, { t: loc(lang, 'ErrorEmptyDownload') });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return apiJSON(responseType, {...defaultParams, ...params})
|
return apiJSON(responseType, {...defaultParams, ...params})
|
||||||
|
|
|
@ -60,8 +60,16 @@ export default async function(obj) {
|
||||||
|
|
||||||
if (!json["media"]["transcodings"]) return { error: 'ErrorEmptyDownload' };
|
if (!json["media"]["transcodings"]) return { error: 'ErrorEmptyDownload' };
|
||||||
|
|
||||||
let fileUrlBase = json.media.transcodings.filter(v => v.preset === "opus_0_0")[0]["url"],
|
let isMp3,
|
||||||
fileUrl = `${fileUrlBase}${fileUrlBase.includes("?") ? "&" : "?"}client_id=${clientId}&track_authorization=${json.track_authorization}`;
|
selectedStream = json.media.transcodings.filter(v => v.preset === "opus_0_0")
|
||||||
|
|
||||||
|
// fall back to mp3 if no opus is available
|
||||||
|
if (selectedStream.length === 0) {
|
||||||
|
selectedStream = json.media.transcodings.filter(v => v.preset === "mp3_0_0")
|
||||||
|
isMp3 = true
|
||||||
|
}
|
||||||
|
let fileUrlBase = selectedStream[0]["url"];
|
||||||
|
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' };
|
||||||
|
|
||||||
|
@ -83,6 +91,7 @@ export default async function(obj) {
|
||||||
title: fileMetadata.title,
|
title: fileMetadata.title,
|
||||||
author: fileMetadata.artist
|
author: fileMetadata.artist
|
||||||
},
|
},
|
||||||
fileMetadata: fileMetadata
|
isMp3,
|
||||||
|
fileMetadata
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@
|
||||||
"soundcloud": {
|
"soundcloud": {
|
||||||
"patterns": [":author/:song/s-:accessKey", ":author/:song", ":shortLink"],
|
"patterns": [":author/:song/s-:accessKey", ":author/:song", ":shortLink"],
|
||||||
"subdomains": ["on"],
|
"subdomains": ["on"],
|
||||||
"bestAudio": "opus",
|
"audioFormats": ["best", "opus", "mp3"],
|
||||||
"enabled": true
|
"enabled": true
|
||||||
},
|
},
|
||||||
"instagram": {
|
"instagram": {
|
||||||
|
|
|
@ -304,6 +304,14 @@
|
||||||
"code": 200,
|
"code": 200,
|
||||||
"status": "stream"
|
"status": "stream"
|
||||||
}
|
}
|
||||||
|
}, {
|
||||||
|
"name": "no opus audio, fallback to mp3",
|
||||||
|
"url": "https://soundcloud.com/frums/credits",
|
||||||
|
"params": {},
|
||||||
|
"expected": {
|
||||||
|
"code": 200,
|
||||||
|
"status": "stream"
|
||||||
|
}
|
||||||
}],
|
}],
|
||||||
"youtube": [{
|
"youtube": [{
|
||||||
"name": "4k video (h264, 1440)",
|
"name": "4k video (h264, 1440)",
|
||||||
|
|
Loading…
Reference in a new issue