@@ -1,88 +1,92 @@
|
||||
import ytdl from "better-ytdl-core";
|
||||
import { maxVideoDuration, quality as mq } from "../../config.js";
|
||||
import selectQuality from "../../stream/selectQuality.js";
|
||||
import { Innertube } from 'youtubei.js';
|
||||
import { maxVideoDuration } from '../../config.js';
|
||||
|
||||
export default async function(obj) {
|
||||
let isAudioOnly = !!obj.isAudioOnly,
|
||||
infoInitial = await ytdl.getInfo(obj.id);
|
||||
if (!infoInitial) return { error: 'ErrorCantConnectToServiceAPI' };
|
||||
const yt = await Innertube.create();
|
||||
|
||||
let info = infoInitial.formats;
|
||||
if (info[0]["isLive"]) return { error: 'ErrorLiveVideo' };
|
||||
|
||||
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 (audio.length === 0) return { error: 'ErrorBadFetch' };
|
||||
if (audio[0]["approxDurationMs"] > maxVideoDuration) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] };
|
||||
|
||||
if (!isAudioOnly) {
|
||||
video = info.filter((a) => {
|
||||
if (!a["isHLS"] && !a["isDashMPD"] && a["hasVideo"] && a["container"] === obj.format) {
|
||||
if (obj.quality !== "max") {
|
||||
if (a["hasAudio"] && String(mq[obj.quality]) === String(a["height"])) {
|
||||
fullVideoMatch.push(a)
|
||||
} else if (!a["hasAudio"] && String(mq[obj.quality]) === String(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]["qualityLabel"].slice(0, 5).replace('p', '').trim());
|
||||
videoMatch = video.filter((a) => {
|
||||
if (a["qualityLabel"].slice(0, 5).replace('p', '').trim() === String(ss)) return true
|
||||
})
|
||||
} else if (fullVideoMatch.length > 0) {
|
||||
videoMatch = [fullVideoMatch[0]]
|
||||
}
|
||||
} else videoMatch = [video[0]];
|
||||
if (obj.quality === "los") videoMatch = [video[video.length - 1]];
|
||||
const c = {
|
||||
h264: {
|
||||
codec: "avc1",
|
||||
aCodec: "mp4a",
|
||||
container: "mp4"
|
||||
},
|
||||
av1: {
|
||||
codec: "av01",
|
||||
aCodec: "mp4a",
|
||||
container: "mp4"
|
||||
},
|
||||
vp9: {
|
||||
codec: "vp9",
|
||||
aCodec: "opus",
|
||||
container: "webm"
|
||||
}
|
||||
if (video.length === 0) isAudioOnly = true;
|
||||
}
|
||||
|
||||
if (isAudioOnly) {
|
||||
export default async function(o) {
|
||||
let info, isDubbed, quality = o.quality === "max" ? "9000" : o.quality; //set quality 9000(p) to be interpreted as max
|
||||
try {
|
||||
info = await yt.getBasicInfo(o.id, 'ANDROID');
|
||||
} catch (e) {
|
||||
return { error: 'ErrorCantConnectToServiceAPI' };
|
||||
}
|
||||
|
||||
if (!info) return { error: 'ErrorCantConnectToServiceAPI' };
|
||||
if (info.playability_status.status !== 'OK') return { error: 'ErrorYTUnavailable' };
|
||||
if (info.basic_info.is_live) return { error: 'ErrorLiveVideo' };
|
||||
|
||||
let adaptive_formats = info.streaming_data.adaptive_formats.filter((e) => {
|
||||
if (e["mime_type"].includes(c[o.format].codec) || e["mime_type"].includes(c[o.format].aCodec)) return true
|
||||
}).sort((a, b) => Number(b.bitrate) - Number(a.bitrate));
|
||||
let bestQuality = adaptive_formats[0]['quality_label'].split('p')[0];
|
||||
|
||||
let checkSingle = (i) => ((i['quality_label'].split('p')[0] === quality || i['quality_label'].split('p')[0] === bestQuality) && i["mime_type"].includes(c[o.format].codec));
|
||||
let checkBestAudio = (i) => (i["has_audio"] && !i["has_video"]);
|
||||
let checkBestVideo = (i) => (i['quality_label'].split('p')[0] === bestQuality && !i["has_audio"] && i["has_video"]);
|
||||
let checkRightVideo = (i) => (i['quality_label'].split('p')[0] === quality && !i["has_audio"] && i["has_video"]);
|
||||
|
||||
if (!o.isAudioOnly && !o.isAudioMuted) {
|
||||
let single = info.streaming_data.formats.find(i => checkSingle(i));
|
||||
if (single) return {
|
||||
type: "bridge",
|
||||
urls: single.url,
|
||||
filename: `youtube_${o.id}_${single.width}x${single.height}_${o.format}.${c[o.format].container}`
|
||||
}
|
||||
};
|
||||
|
||||
if (info.basic_info.duration > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] };
|
||||
let audio = adaptive_formats.find(i => checkBestAudio(i) && i["is_original"]);
|
||||
if (o.dubLang) {
|
||||
let dubbedAudio = adaptive_formats.find(i => checkBestAudio(i) && i["language"] === o.dubLang);
|
||||
if (dubbedAudio) {
|
||||
audio = dubbedAudio;
|
||||
isDubbed = true
|
||||
}
|
||||
}
|
||||
if (o.isAudioOnly) {
|
||||
let r = {
|
||||
type: "render",
|
||||
isAudioOnly: true,
|
||||
urls: audio[0]["url"],
|
||||
audioFilename: `youtube_${obj.id}_audio`,
|
||||
urls: audio.url,
|
||||
audioFilename: `youtube_${o.id}_audio${isDubbed ? `_${o.dubLang}`:''}`,
|
||||
fileMetadata: {
|
||||
title: infoInitial.videoDetails.title,
|
||||
artist: infoInitial.videoDetails.ownerChannelName.replace("- Topic", "").trim(),
|
||||
title: info.basic_info.title,
|
||||
artist: info.basic_info.author.replace("- Topic", "").trim(),
|
||||
}
|
||||
}
|
||||
if (infoInitial.videoDetails.description) {
|
||||
let isAutoGenAudio = infoInitial.videoDetails.description.startsWith("Provided to YouTube by");
|
||||
if (isAutoGenAudio) {
|
||||
let descItems = infoInitial.videoDetails.description.split("\n\n")
|
||||
r.fileMetadata.album = descItems[2]
|
||||
r.fileMetadata.copyright = descItems[3]
|
||||
if (descItems[4].startsWith("Released on:")) r.fileMetadata.date = descItems[4].replace("Released on: ", '').trim();
|
||||
}
|
||||
}
|
||||
};
|
||||
if (info.basic_info.short_description && info.basic_info.short_description.startsWith("Provided to YouTube by")) {
|
||||
let descItems = info.basic_info.short_description.split("\n\n")
|
||||
r.fileMetadata.album = descItems[2]
|
||||
r.fileMetadata.copyright = descItems[3]
|
||||
if (descItems[4].startsWith("Released on:")) r.fileMetadata.date = descItems[4].replace("Released on: ", '').trim();
|
||||
};
|
||||
return r
|
||||
}
|
||||
let singleTest;
|
||||
if (videoMatch.length > 0) {
|
||||
singleTest = videoMatch[0]["hasVideo"] && videoMatch[0]["hasAudio"];
|
||||
return {
|
||||
type: singleTest ? "bridge" : "render",
|
||||
urls: singleTest ? videoMatch[0]["url"] : [videoMatch[0]["url"], audio[0]["url"]],
|
||||
time: videoMatch[0]["approxDurationMs"],
|
||||
filename: `youtube_${obj.id}_${videoMatch[0]["width"]}x${videoMatch[0]["height"]}.${obj.format}`
|
||||
}
|
||||
}
|
||||
singleTest = video[0]["hasVideo"] && video[0]["hasAudio"];
|
||||
return {
|
||||
type: singleTest ? "bridge" : "render",
|
||||
urls: singleTest ? video[0]["url"] : [video[0]["url"], audio[0]["url"]],
|
||||
time: video[0]["approxDurationMs"],
|
||||
filename: `youtube_${obj.id}_${video[0]["width"]}x${video[0]["height"]}.${obj.format}`
|
||||
}
|
||||
};
|
||||
|
||||
let video = adaptive_formats.find(i => ((Number(quality) > Number(bestQuality)) ? checkBestVideo(i) : checkRightVideo(i)));
|
||||
if (video && audio) return {
|
||||
type: "render",
|
||||
urls: [video.url, audio.url],
|
||||
filename: `youtube_${o.id}_${video.width}x${video.height}_${o.format}${isDubbed ? `_${o.dubLang}`:''}.${c[o.format].container}`
|
||||
};
|
||||
|
||||
return { error: 'ErrorYTTryOtherCodec' }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user