wukko 20798685c2 added support for tumblr
- support for tumblr is back! (i tried all types of links in clear sight and everything seems to work, lmk if something doesn't)
- increased stream lifespan up to 1 hour
- made youtube audios download without additional conversion, speed should be a bit better but not by much cause youtube limits the audio download speed :(
2022-08-01 23:53:44 +06:00

74 lines
4.2 KiB
JavaScript

import ytdl from "ytdl-core";
import loc from "../../localization/manager.js";
import { maxVideoDuration, quality as mq } from "../config.js";
import selectQuality from "../stream/selectQuality.js";
export default async function (obj) {
try {
let info = await ytdl.getInfo(obj.id);
if (info) {
info = info.formats;
if (!info[0]["isLive"]) {
let videoMatch = [], fullVideoMatch = [], video = [], audio = info.filter((a) => {
if (!a["isHLS"] && !a["isDashMPD"] && a["hasAudio"] && !a["hasVideo"] && a["container"] == obj.format) return true;
}).sort((a, b) => Number(b.bitrate) - Number(a.bitrate));
if (!obj.isAudioOnly) {
video = info.filter((a) => {
if (!a["isHLS"] && !a["isDashMPD"] && a["hasVideo"] && a["container"] == obj.format && a["height"] != 4320) {
if (obj.quality != "max") {
if (a["hasAudio"] && mq[obj.quality] == a["height"]) {
fullVideoMatch.push(a)
} else if (!a["hasAudio"] && mq[obj.quality] == a["height"]) {
videoMatch.push(a);
}
}
return true
}
}).sort((a, b) => Number(b.bitrate) - Number(a.bitrate));
if (obj.quality != "max") {
if (videoMatch.length == 0) {
let ss = selectQuality("youtube", obj.quality, video[0]["height"])
videoMatch = video.filter((a) => {
if (a["height"] == ss) return true;
})
} else if (fullVideoMatch.length > 0) {
videoMatch = [fullVideoMatch[0]]
}
} else videoMatch = [video[0]];
if (obj.quality == "los") videoMatch = [video[video.length - 1]];
}
if (audio[0]["approxDurationMs"] <= maxVideoDuration) {
if (!obj.isAudioOnly && videoMatch.length > 0) {
if (video.length > 0 && audio.length > 0) {
if (videoMatch[0]["hasVideo"] && videoMatch[0]["hasAudio"]) {
return { type: "bridge", urls: videoMatch[0]["url"], time: videoMatch[0]["approxDurationMs"],
filename: `youtube_${obj.id}_${videoMatch[0]["width"]}x${videoMatch[0]["height"]}.${obj.format}` };
} else {
return { type: "render", urls: [videoMatch[0]["url"], audio[0]["url"]], time: videoMatch[0]["approxDurationMs"],
filename: `youtube_${obj.id}_${videoMatch[0]["width"]}x${videoMatch[0]["height"]}.${obj.format}` };
}
} else {
return { error: loc(obj.lang, 'ErrorBadFetch') };
}
} else if (!obj.isAudioOnly) {
return { type: "render", urls: [video[0]["url"], audio[0]["url"]], time: video[0]["approxDurationMs"],
filename: `youtube_${obj.id}_${video[0]["width"]}x${video[0]["height"]}.${video[0]["container"]}` };
} else if (audio.length > 0) {
return { type: "bridge", isAudioOnly: true, urls: audio[0]["url"], filename: `youtube_${obj.id}_${audio[0]["audioBitrate"]}kbps.${audio[0]["container"] == "webm" ? "opus" : "m4a"}` };
} else {
return { error: loc(obj.lang, 'ErrorBadFetch') };
}
} else {
return { error: loc(obj.lang, 'ErrorLengthLimit', maxVideoDuration / 60000) };
}
} else {
return { error: loc(obj.lang, 'ErrorLiveVideo') };
}
} else {
return { error: loc(obj.lang, 'ErrorCantConnectToServiceAPI') };
}
} catch (e) {
return { error: loc(obj.lang, 'ErrorBadFetch') };
}
}