2024-05-12 16:13:01 +00:00
|
|
|
import { Innertube, Session } from 'youtubei.js';
|
2024-05-16 20:57:48 +06:00
|
|
|
import { env } from '../../config.js';
|
2023-08-20 18:14:15 +06:00
|
|
|
import { cleanString } from '../../sub/utils.js';
|
2024-05-12 16:13:01 +00:00
|
|
|
import { fetch } from 'undici'
|
2024-06-08 09:30:12 +00:00
|
|
|
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: {
|
|
|
|
codec: "avc1",
|
|
|
|
aCodec: "mp4a",
|
|
|
|
container: "mp4"
|
|
|
|
},
|
|
|
|
av1: {
|
|
|
|
codec: "av01",
|
|
|
|
aCodec: "mp4a",
|
|
|
|
container: "mp4"
|
|
|
|
},
|
|
|
|
vp9: {
|
|
|
|
codec: "vp9",
|
|
|
|
aCodec: "opus",
|
|
|
|
container: "webm"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-08 09:18:29 +00:00
|
|
|
const transformSessionData = (cookie) => {
|
|
|
|
if (!cookie)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const values = cookie.values();
|
|
|
|
const REQUIRED_VALUES = [
|
|
|
|
'access_token', 'refresh_token',
|
|
|
|
'client_id', 'client_secret',
|
|
|
|
'expires'
|
|
|
|
];
|
|
|
|
|
|
|
|
if (REQUIRED_VALUES.some(x => typeof values[x] !== 'string')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...values,
|
|
|
|
expires: new Date(values.expires),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-05-29 10:26:17 +02:00
|
|
|
const cloneInnertube = async (customFetch) => {
|
|
|
|
const innertube = await ytBase;
|
|
|
|
if (innertube instanceof Error) {
|
|
|
|
throw innertube;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
await session.oauth.refreshIfRequired();
|
2024-06-08 09:30:12 +00:00
|
|
|
const oldExpiry = new Date(cookie.values().expires);
|
|
|
|
const newExpiry = session.oauth.credentials.expires;
|
|
|
|
|
|
|
|
if (oldExpiry.getTime() !== newExpiry.getTime()) {
|
|
|
|
updateCookieValues(cookie, {
|
|
|
|
...session.oauth.credentials,
|
|
|
|
expires: session.oauth.credentials.expires.toISOString()
|
|
|
|
});
|
|
|
|
}
|
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-05-12 16:13:01 +00:00
|
|
|
(input, init) => fetch(input, { ...init, dispatcher: o.dispatcher })
|
|
|
|
);
|
|
|
|
|
2024-04-30 11:24:12 +06:00
|
|
|
let info, isDubbed, format = o.format || "h264";
|
|
|
|
let quality = o.quality === "max" ? "9000" : o.quality; // 9000(p) - max quality
|
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-06-08 09:18:29 +00: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;
|
|
|
|
|
|
|
|
if (playability.status === 'LOGIN_REQUIRED') {
|
|
|
|
if (playability.reason.endsWith('bot')) {
|
|
|
|
return { error: 'ErrorYTLogin' }
|
|
|
|
}
|
|
|
|
if (playability.reason.endsWith('age')) {
|
|
|
|
return { error: 'ErrorYTAgeRestrict' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (playability.status !== 'OK') return { error: 'ErrorYTUnavailable' };
|
2023-02-26 22:49:25 +06:00
|
|
|
if (info.basic_info.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
|
|
|
|
if (info.basic_info.id !== o.id) {
|
|
|
|
return {
|
|
|
|
error: 'ErrorCantConnectToServiceAPI',
|
|
|
|
critical: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-26 12:25:22 +06:00
|
|
|
let bestQuality, hasAudio;
|
|
|
|
|
2024-04-30 11:24:12 +06:00
|
|
|
const filterByCodec = (formats) => formats.filter(e =>
|
|
|
|
e.mime_type.includes(codecMatch[format].codec) || e.mime_type.includes(codecMatch[format].aCodec)
|
2023-08-23 01:03:31 +06:00
|
|
|
).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);
|
|
|
|
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
|
|
|
if (!bestQuality && !o.isAudioOnly || !hasAudio) return { error: 'ErrorYTTryOtherCodec' };
|
2024-05-16 20:57:48 +06:00
|
|
|
if (info.basic_info.duration > env.durationLimit) return { error: ['ErrorLengthLimit', env.durationLimit / 60] };
|
2023-03-01 08:37:26 +06:00
|
|
|
|
2024-01-31 17:10:02 +06:00
|
|
|
let checkBestAudio = (i) => (i.has_audio && !i.has_video),
|
|
|
|
audio = adaptive_formats.find(i => checkBestAudio(i) && !i.is_dubbed);
|
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-01-31 17:10:02 +06:00
|
|
|
checkBestAudio(i) && i.language === o.dubLang && i.audio_track && !i.audio_track.audio_is_default
|
2023-10-15 22:13:01 +06:00
|
|
|
);
|
2023-02-26 22:49:25 +06:00
|
|
|
if (dubbedAudio) {
|
|
|
|
audio = dubbedAudio;
|
|
|
|
isDubbed = true
|
|
|
|
}
|
2023-02-12 13:40:49 +06:00
|
|
|
}
|
2023-08-20 18:14:15 +06:00
|
|
|
let fileMetadata = {
|
2023-10-15 14:39:17 +06:00
|
|
|
title: cleanString(info.basic_info.title.trim()),
|
|
|
|
artist: cleanString(info.basic_info.author.replace("- Topic", "").trim()),
|
2023-08-20 18:14:15 +06:00
|
|
|
}
|
|
|
|
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");
|
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
|
|
|
|
|
|
|
if (hasAudio && o.isAudioOnly) return {
|
|
|
|
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,
|
|
|
|
bestAudio: format === "h264" ? 'm4a' : 'opus'
|
2023-03-01 08:37:26 +06:00
|
|
|
}
|
2023-10-17 16:48:51 +00:00
|
|
|
const matchingQuality = Number(quality) > Number(bestQuality) ? bestQuality : quality,
|
2024-04-30 11:24:12 +06:00
|
|
|
checkSingle = i => qual(i) === matchingQuality && i.mime_type.includes(codecMatch[format].codec),
|
2023-10-17 16:54:46 +00:00
|
|
|
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-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);
|
|
|
|
if (!match && video) {
|
|
|
|
match = video;
|
|
|
|
type = "render";
|
2024-04-26 12:25:22 +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,
|
|
|
|
filenameAttributes,
|
|
|
|
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
|
|
|
}
|