2024-07-26 19:54:19 +06:00
|
|
|
import { fetch } from "undici";
|
|
|
|
|
|
|
|
import { Innertube, Session } from "youtubei.js";
|
|
|
|
|
|
|
|
import { env } from "../../config.js";
|
|
|
|
import { cleanString } from "../../sub/utils.js";
|
|
|
|
import { getCookie, updateCookieValues } from "../cookie/manager.js";
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2024-05-29 10:26:17 +02:00
|
|
|
const ytBase = Innertube.create().catch(e => e);
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2024-04-30 11:24:12 +06:00
|
|
|
const codecMatch = {
|
2023-02-26 22:49:25 +06:00
|
|
|
h264: {
|
2024-07-26 19:54:19 +06:00
|
|
|
videoCodec: "avc1",
|
|
|
|
audioCodec: "mp4a",
|
2023-02-26 22:49:25 +06:00
|
|
|
container: "mp4"
|
|
|
|
},
|
|
|
|
av1: {
|
2024-07-26 19:54:19 +06:00
|
|
|
videoCodec: "av01",
|
|
|
|
audioCodec: "mp4a",
|
2023-02-26 22:49:25 +06:00
|
|
|
container: "mp4"
|
|
|
|
},
|
|
|
|
vp9: {
|
2024-07-26 19:54:19 +06:00
|
|
|
videoCodec: "vp9",
|
|
|
|
audioCodec: "opus",
|
2023-02-26 22:49:25 +06:00
|
|
|
container: "webm"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-08 09:18:29 +00:00
|
|
|
const transformSessionData = (cookie) => {
|
|
|
|
if (!cookie)
|
|
|
|
return;
|
|
|
|
|
2024-07-10 14:13:56 +00:00
|
|
|
const values = { ...cookie.values() };
|
2024-07-11 07:56:13 +00:00
|
|
|
const REQUIRED_VALUES = [ 'access_token', 'refresh_token' ];
|
2024-06-08 09:18:29 +00:00
|
|
|
|
|
|
|
if (REQUIRED_VALUES.some(x => typeof values[x] !== 'string')) {
|
|
|
|
return;
|
|
|
|
}
|
2024-07-10 14:13:56 +00:00
|
|
|
|
|
|
|
if (values.expires) {
|
|
|
|
values.expiry_date = values.expires;
|
|
|
|
delete values.expires;
|
|
|
|
} else if (!values.expiry_date) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
2024-06-08 09:18:29 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 10:26:17 +02:00
|
|
|
const cloneInnertube = async (customFetch) => {
|
|
|
|
const innertube = await ytBase;
|
|
|
|
if (innertube instanceof Error) {
|
2024-08-01 07:43:51 +06:00
|
|
|
if (innertube?.message?.endsWith("decipher algorithm")) {
|
|
|
|
return { error: "ErrorYoutubeDecipher" }
|
|
|
|
} else throw innertube;
|
2024-05-29 10:26:17 +02:00
|
|
|
}
|
|
|
|
|
2024-05-12 16:13:01 +00:00
|
|
|
const session = new Session(
|
2024-05-29 10:26:17 +02:00
|
|
|
innertube.session.context,
|
|
|
|
innertube.session.key,
|
|
|
|
innertube.session.api_version,
|
|
|
|
innertube.session.account_index,
|
|
|
|
innertube.session.player,
|
2024-06-08 09:26:58 +00:00
|
|
|
undefined,
|
2024-05-29 10:26:17 +02:00
|
|
|
customFetch ?? innertube.session.http.fetch,
|
|
|
|
innertube.session.cache
|
2024-05-12 16:13:01 +00:00
|
|
|
);
|
|
|
|
|
2024-06-08 09:30:12 +00:00
|
|
|
const cookie = getCookie('youtube_oauth');
|
|
|
|
const oauthData = transformSessionData(cookie);
|
2024-06-08 09:18:29 +00:00
|
|
|
|
|
|
|
if (!session.logged_in && oauthData) {
|
|
|
|
await session.oauth.init(oauthData);
|
|
|
|
session.logged_in = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session.logged_in) {
|
2024-07-10 14:13:56 +00:00
|
|
|
if (session.oauth.shouldRefreshToken()) {
|
|
|
|
await session.oauth.refreshAccessToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
const cookieValues = cookie.values();
|
|
|
|
const oldExpiry = new Date(cookieValues.expiry_date);
|
|
|
|
const newExpiry = new Date(session.oauth.oauth2_tokens.expiry_date);
|
2024-06-08 09:30:12 +00:00
|
|
|
|
|
|
|
if (oldExpiry.getTime() !== newExpiry.getTime()) {
|
|
|
|
updateCookieValues(cookie, {
|
2024-07-10 14:13:56 +00:00
|
|
|
...session.oauth.client_id,
|
|
|
|
...session.oauth.oauth2_tokens,
|
|
|
|
expiry_date: newExpiry.toISOString()
|
2024-06-08 09:30:12 +00:00
|
|
|
});
|
|
|
|
}
|
2024-06-08 09:18:29 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 16:13:01 +00:00
|
|
|
const yt = new Innertube(session);
|
|
|
|
return yt;
|
|
|
|
}
|
|
|
|
|
2023-02-26 22:49:25 +06:00
|
|
|
export default async function(o) {
|
2024-05-29 10:26:17 +02:00
|
|
|
const yt = await cloneInnertube(
|
2024-07-26 19:54:19 +06:00
|
|
|
(input, init) => fetch(input, {
|
|
|
|
...init,
|
|
|
|
dispatcher: o.dispatcher
|
|
|
|
})
|
2024-05-12 16:13:01 +00:00
|
|
|
);
|
2024-08-01 07:43:51 +06:00
|
|
|
if (yt.error) return yt;
|
2024-05-12 16:13:01 +00:00
|
|
|
|
2024-07-26 19:54:19 +06:00
|
|
|
const quality = o.quality === "max" ? "9000" : o.quality;
|
|
|
|
|
|
|
|
let info, isDubbed,
|
|
|
|
format = o.format || "h264";
|
2024-04-27 18:05:43 +06:00
|
|
|
|
2023-06-05 12:43:04 +06:00
|
|
|
function qual(i) {
|
2024-01-31 11:49:15 +00:00
|
|
|
if (!i.quality_label) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.quality_label.split('p')[0].split('s')[0]
|
2023-06-05 12:43:04 +06:00
|
|
|
}
|
|
|
|
|
2023-02-26 22:49:25 +06:00
|
|
|
try {
|
2024-08-02 18:44:09 +06:00
|
|
|
info = await yt.getBasicInfo(o.id, yt.session.logged_in ? 'ANDROID' : 'IOS');
|
2024-05-29 08:28:17 +00:00
|
|
|
} catch(e) {
|
|
|
|
if (e?.message === 'This video is unavailable') {
|
|
|
|
return { error: 'ErrorCouldntFetch' };
|
|
|
|
} else {
|
|
|
|
return { error: 'ErrorCantConnectToServiceAPI' };
|
|
|
|
}
|
2023-02-26 22:49:25 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!info) return { error: 'ErrorCantConnectToServiceAPI' };
|
2023-06-05 12:43:04 +06:00
|
|
|
|
2024-06-07 21:46:45 +06:00
|
|
|
const playability = info.playability_status;
|
2024-07-26 19:54:19 +06:00
|
|
|
const basicInfo = info.basic_info;
|
2024-06-07 21:46:45 +06:00
|
|
|
|
|
|
|
if (playability.status === 'LOGIN_REQUIRED') {
|
|
|
|
if (playability.reason.endsWith('bot')) {
|
|
|
|
return { error: 'ErrorYTLogin' }
|
|
|
|
}
|
|
|
|
if (playability.reason.endsWith('age')) {
|
|
|
|
return { error: 'ErrorYTAgeRestrict' }
|
|
|
|
}
|
|
|
|
}
|
2024-06-08 22:59:30 +06:00
|
|
|
if (playability.status === "UNPLAYABLE" && playability.reason.endsWith('request limit.')) {
|
|
|
|
return { error: 'ErrorYTRateLimit' }
|
|
|
|
}
|
2024-06-07 21:46:45 +06:00
|
|
|
|
|
|
|
if (playability.status !== 'OK') return { error: 'ErrorYTUnavailable' };
|
2024-07-26 19:54:19 +06:00
|
|
|
if (basicInfo.is_live) return { error: 'ErrorLiveVideo' };
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2024-04-27 18:05:43 +06:00
|
|
|
// return a critical error if returned video is "Video Not Available"
|
|
|
|
// or a similar stub by youtube
|
2024-07-26 19:54:19 +06:00
|
|
|
if (basicInfo.id !== o.id) {
|
2024-04-27 18:05:43 +06:00
|
|
|
return {
|
|
|
|
error: 'ErrorCantConnectToServiceAPI',
|
|
|
|
critical: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-26 12:25:22 +06:00
|
|
|
let bestQuality, hasAudio;
|
|
|
|
|
2024-07-26 19:54:19 +06:00
|
|
|
const filterByCodec = (formats) =>
|
|
|
|
formats
|
|
|
|
.filter(e =>
|
|
|
|
e.mime_type.includes(codecMatch[format].videoCodec)
|
|
|
|
|| e.mime_type.includes(codecMatch[format].audioCodec)
|
|
|
|
)
|
|
|
|
.sort((a, b) => Number(b.bitrate) - Number(a.bitrate));
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2024-04-30 11:24:12 +06:00
|
|
|
let adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats);
|
2024-07-26 19:54:19 +06:00
|
|
|
|
2024-04-30 11:24:12 +06:00
|
|
|
if (adaptive_formats.length === 0 && format === "vp9") {
|
|
|
|
format = "h264"
|
|
|
|
adaptive_formats = filterByCodec(info.streaming_data.adaptive_formats)
|
|
|
|
}
|
|
|
|
|
2024-05-13 16:54:00 +00:00
|
|
|
bestQuality = adaptive_formats.find(i => i.has_video && i.content_length);
|
|
|
|
hasAudio = adaptive_formats.find(i => i.has_audio && i.content_length);
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2023-06-05 12:43:04 +06:00
|
|
|
if (bestQuality) bestQuality = qual(bestQuality);
|
2023-03-01 08:37:26 +06:00
|
|
|
|
2024-07-26 20:18:59 +06:00
|
|
|
if ((!bestQuality && !o.isAudioOnly) || !hasAudio)
|
2024-07-26 19:54:19 +06:00
|
|
|
return { error: 'ErrorYTTryOtherCodec' };
|
|
|
|
|
|
|
|
if (basicInfo.duration > env.durationLimit)
|
|
|
|
return { error: ['ErrorLengthLimit', env.durationLimit / 60] };
|
|
|
|
|
|
|
|
const checkBestAudio = (i) => (i.has_audio && !i.has_video);
|
|
|
|
|
|
|
|
let audio = adaptive_formats.find(i =>
|
|
|
|
checkBestAudio(i) && i.is_original
|
|
|
|
);
|
2023-03-01 08:37:26 +06:00
|
|
|
|
2023-02-26 22:49:25 +06:00
|
|
|
if (o.dubLang) {
|
2023-10-15 22:13:01 +06:00
|
|
|
let dubbedAudio = adaptive_formats.find(i =>
|
2024-07-26 19:54:19 +06:00
|
|
|
checkBestAudio(i)
|
|
|
|
&& i.language === o.dubLang
|
|
|
|
&& i.audio_track
|
|
|
|
)
|
|
|
|
|
2023-02-26 22:49:25 +06:00
|
|
|
if (dubbedAudio) {
|
|
|
|
audio = dubbedAudio;
|
2024-07-26 19:54:19 +06:00
|
|
|
isDubbed = true;
|
2023-02-26 22:49:25 +06:00
|
|
|
}
|
2023-02-12 13:40:49 +06:00
|
|
|
}
|
2024-07-26 19:54:19 +06:00
|
|
|
|
|
|
|
if (!audio) {
|
|
|
|
audio = adaptive_formats.find(i => checkBestAudio(i));
|
|
|
|
}
|
|
|
|
|
2023-08-20 18:14:15 +06:00
|
|
|
let fileMetadata = {
|
2024-07-26 19:54:19 +06:00
|
|
|
title: cleanString(basicInfo.title.trim()),
|
|
|
|
artist: cleanString(basicInfo.author.replace("- Topic", "").trim()),
|
2023-08-20 18:14:15 +06:00
|
|
|
}
|
2024-07-26 19:54:19 +06:00
|
|
|
|
|
|
|
if (basicInfo?.short_description?.startsWith("Provided to YouTube by")) {
|
|
|
|
let descItems = basicInfo.short_description.split("\n\n");
|
2023-08-20 18:16:00 +06:00
|
|
|
fileMetadata.album = descItems[2];
|
|
|
|
fileMetadata.copyright = descItems[3];
|
2023-08-20 18:14:15 +06:00
|
|
|
if (descItems[4].startsWith("Released on:")) {
|
2023-08-20 18:16:00 +06:00
|
|
|
fileMetadata.date = descItems[4].replace("Released on: ", '').trim()
|
2023-08-20 18:14:15 +06:00
|
|
|
}
|
2023-10-12 23:14:54 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
let filenameAttributes = {
|
|
|
|
service: "youtube",
|
|
|
|
id: o.id,
|
|
|
|
title: fileMetadata.title,
|
|
|
|
author: fileMetadata.artist,
|
|
|
|
youtubeDubName: isDubbed ? o.dubLang : false
|
|
|
|
}
|
2023-08-20 18:14:15 +06:00
|
|
|
|
2024-07-26 20:18:59 +06:00
|
|
|
if (audio && o.isAudioOnly) return {
|
2023-08-20 18:14:15 +06:00
|
|
|
type: "render",
|
|
|
|
isAudioOnly: true,
|
2024-04-26 12:25:22 +06:00
|
|
|
urls: audio.decipher(yt.session.player),
|
2023-10-12 23:14:54 +06:00
|
|
|
filenameAttributes: filenameAttributes,
|
2024-04-30 11:24:12 +06:00
|
|
|
fileMetadata: fileMetadata,
|
2024-07-26 19:54:19 +06:00
|
|
|
bestAudio: format === "h264" ? "m4a" : "opus"
|
2023-03-01 08:37:26 +06:00
|
|
|
}
|
2024-07-26 19:54:19 +06:00
|
|
|
|
2023-10-17 16:48:51 +00:00
|
|
|
const matchingQuality = Number(quality) > Number(bestQuality) ? bestQuality : quality,
|
2024-07-26 19:54:19 +06:00
|
|
|
checkSingle = i =>
|
|
|
|
qual(i) === matchingQuality && i.mime_type.includes(codecMatch[format].videoCodec),
|
|
|
|
checkRender = i =>
|
|
|
|
qual(i) === matchingQuality && i.has_video && !i.has_audio;
|
2023-03-01 08:37:26 +06:00
|
|
|
|
2023-10-17 16:54:46 +00:00
|
|
|
let match, type, urls;
|
2024-07-26 19:54:19 +06:00
|
|
|
|
2024-04-30 11:24:12 +06:00
|
|
|
if (!o.isAudioOnly && !o.isAudioMuted && format === 'h264') {
|
2023-10-17 16:54:46 +00:00
|
|
|
match = info.streaming_data.formats.find(checkSingle);
|
|
|
|
type = "bridge";
|
2024-04-26 12:25:22 +06:00
|
|
|
urls = match?.decipher(yt.session.player);
|
2023-10-17 16:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const video = adaptive_formats.find(checkRender);
|
2024-07-26 19:54:19 +06:00
|
|
|
|
2024-07-26 20:18:59 +06:00
|
|
|
if (!match && video && audio) {
|
2023-10-17 16:54:46 +00:00
|
|
|
match = video;
|
|
|
|
type = "render";
|
2024-07-26 19:54:19 +06:00
|
|
|
urls = [
|
|
|
|
video.decipher(yt.session.player),
|
|
|
|
audio.decipher(yt.session.player)
|
|
|
|
]
|
2023-10-12 23:14:54 +06:00
|
|
|
}
|
2023-02-26 22:49:25 +06:00
|
|
|
|
2023-10-17 16:54:46 +00:00
|
|
|
if (match) {
|
|
|
|
filenameAttributes.qualityLabel = match.quality_label;
|
|
|
|
filenameAttributes.resolution = `${match.width}x${match.height}`;
|
2024-04-30 11:24:12 +06:00
|
|
|
filenameAttributes.extension = codecMatch[format].container;
|
|
|
|
filenameAttributes.youtubeFormat = format;
|
2023-10-12 23:14:54 +06:00
|
|
|
return {
|
2024-01-31 17:10:02 +06:00
|
|
|
type,
|
|
|
|
urls,
|
2024-07-26 19:54:19 +06:00
|
|
|
filenameAttributes,
|
2024-01-31 17:10:02 +06:00
|
|
|
fileMetadata
|
2023-10-12 23:14:54 +06:00
|
|
|
}
|
|
|
|
}
|
2023-02-26 22:49:25 +06:00
|
|
|
|
|
|
|
return { error: 'ErrorYTTryOtherCodec' }
|
2023-02-12 13:40:49 +06:00
|
|
|
}
|