1
0
Fork 0
mirror of https://github.com/wukko/cobalt.git synced 2025-04-02 16:21:43 +02:00
cobalt/src/modules/processing/services/youtube.js

143 lines
4.6 KiB
JavaScript
Raw Normal View History

2023-02-26 22:49:25 +06:00
import { Innertube } from 'youtubei.js';
import { maxVideoDuration } from '../../config.js';
import { cleanString } from '../../sub/utils.js';
2023-02-26 22:49:25 +06:00
const yt = await Innertube.create();
2023-02-26 22:49:25 +06:00
const c = {
h264: {
codec: "avc1",
aCodec: "mp4a",
container: "mp4"
},
av1: {
codec: "av01",
aCodec: "mp4a",
container: "mp4"
},
vp9: {
codec: "vp9",
aCodec: "opus",
container: "webm"
}
}
export default async function(o) {
2024-04-27 18:05:43 +06:00
let info, isDubbed,
quality = o.quality === "max" ? "9000" : o.quality; // 9000(p) - max quality
function qual(i) {
if (!i.quality_label) {
return;
}
return i.quality_label.split('p')[0].split('s')[0]
}
2023-02-26 22:49:25 +06:00
try {
info = await yt.getBasicInfo(o.id, 'WEB');
2023-02-26 22:49:25 +06:00
} catch (e) {
return { error: 'ErrorCantConnectToServiceAPI' };
}
if (!info) return { error: 'ErrorCantConnectToServiceAPI' };
2023-02-26 22:49:25 +06:00
if (info.playability_status.status !== 'OK') return { error: 'ErrorYTUnavailable' };
if (info.basic_info.is_live) return { error: 'ErrorLiveVideo' };
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
}
}
let bestQuality, hasAudio;
let adaptive_formats = info.streaming_data.adaptive_formats.filter(e =>
2024-01-31 17:10:02 +06:00
e.mime_type.includes(c[o.format].codec) || e.mime_type.includes(c[o.format].aCodec)
2023-08-23 01:03:31 +06:00
).sort((a, b) => Number(b.bitrate) - Number(a.bitrate));
2024-01-31 17:10:02 +06:00
bestQuality = adaptive_formats.find(i => i.has_video);
hasAudio = adaptive_formats.find(i => i.has_audio);
if (bestQuality) bestQuality = qual(bestQuality);
if (!bestQuality && !o.isAudioOnly || !hasAudio) return { error: 'ErrorYTTryOtherCodec' };
2023-02-26 22:49:25 +06:00
if (info.basic_info.duration > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] };
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-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
}
}
let fileMetadata = {
title: cleanString(info.basic_info.title.trim()),
artist: cleanString(info.basic_info.author.replace("- Topic", "").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");
2023-08-20 18:16:00 +06:00
fileMetadata.album = descItems[2];
fileMetadata.copyright = descItems[3];
if (descItems[4].startsWith("Released on:")) {
2023-08-20 18:16:00 +06:00
fileMetadata.date = descItems[4].replace("Released on: ", '').trim()
}
}
let filenameAttributes = {
service: "youtube",
id: o.id,
title: fileMetadata.title,
author: fileMetadata.artist,
youtubeDubName: isDubbed ? o.dubLang : false
}
if (hasAudio && o.isAudioOnly) return {
type: "render",
isAudioOnly: true,
urls: audio.decipher(yt.session.player),
filenameAttributes: filenameAttributes,
fileMetadata: fileMetadata
}
2023-10-17 16:48:51 +00:00
const matchingQuality = Number(quality) > Number(bestQuality) ? bestQuality : quality,
checkSingle = i => qual(i) === matchingQuality && i.mime_type.includes(c[o.format].codec),
checkRender = i => qual(i) === matchingQuality && i.has_video && !i.has_audio;
let match, type, urls;
if (!o.isAudioOnly && !o.isAudioMuted && o.format === 'h264') {
match = info.streaming_data.formats.find(checkSingle);
type = "bridge";
urls = match?.decipher(yt.session.player);
}
const video = adaptive_formats.find(checkRender);
if (!match && video) {
match = video;
type = "render";
urls = [video.decipher(yt.session.player), audio.decipher(yt.session.player)];
}
2023-02-26 22:49:25 +06:00
if (match) {
filenameAttributes.qualityLabel = match.quality_label;
filenameAttributes.resolution = `${match.width}x${match.height}`;
filenameAttributes.extension = c[o.format].container;
filenameAttributes.youtubeFormat = o.format;
return {
2024-01-31 17:10:02 +06:00
type,
urls,
filenameAttributes,
fileMetadata
}
}
2023-02-26 22:49:25 +06:00
return { error: 'ErrorYTTryOtherCodec' }
}