Now using Cobalt

This commit is contained in:
bingus_violet 2025-06-13 03:12:41 -05:00
parent f1bbd3c80b
commit fdb7b59ea8
4 changed files with 226 additions and 193 deletions

View file

@ -1,11 +1,12 @@
const expressManager = require("./express.js"),
path = require("path"),
fs = require("fs"),
ytdl = require("@distube/ytdl-core"),
ffmpeg = require("ffmpeg-static"),
cp = require("child_process")
cp = require("child_process"),
axios = require("axios"),
{ Readable } = require("stream")
var qualityLabels = [
const qualityLabels = [
"144",
"240",
"360",
@ -13,7 +14,8 @@ var qualityLabels = [
"720",
"1080",
"1440",
"2160"
"2160",
"4320"
]
var audioFormats = [
@ -38,46 +40,47 @@ expressManager.app.get("/download", async (req, res) => {
format = req.query.format,
trimAudio = req.query.trimAudio && audioFormats.includes(format) || false
// url = url.substring(url.indexOf("v=") + 2)
// if (url.includes("&")) url = url.substring(0, url.indexOf("&"))
if (req.query.playInBrowser && !audioFormats.includes(format)) {
var metadata = req.url.substring(req.url.indexOf("?"))
res.redirect("/watch" + metadata)
} else if (ytdl.validateURL(url) && qualityLabels.includes(quality)) {
} else if (url && qualityLabels.includes(quality)) {
var needsVideo = !audioFormats.includes(format)
var info = await ytdl.getInfo(url)
// var info = await fetch("https://cobalt.univerter.dev/", {
// method: 'POST',
// headers: {
// Accept: "application/json",
// "Content-Type": "application/json"
// },
// body: JSON.stringify({
// url
// }),
// })
var downloadMode = "audio"
if (needsVideo) downloadMode = "auto"
var info = await axios.post("https://cobalt.univerter.dev/", {
url,
downloadMode,
videoQuality: quality,
localProcessing: true,
}, {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
console.log(info)
var downloadType = "attachment"
if (req.query.playInBrowser) downloadType = "inline";
res.setHeader("Content-Disposition", `${downloadType}; filename="${info.videoDetails.title.replace(/[^a-z0-9 ]/gi, '')}.${format}"`)
var audioFormat = ytdl.chooseFormat(info.formats, { filter: (format) => {
return format.hasAudio && !format.hasVideo
} })
var videoFormat = undefined
var qualityLabel = quality
while (videoFormat == undefined) {
for (let i = 0; i < info.formats.length; i++) {
const format = info.formats[i];
if (format.hasVideo && !format.hasAudio && format.height && format.height.toString() == qualityLabel.toString() && (format.videoCodec.includes("avc1") || format.codecs.includes("avc1"))) {
// console.log(format.videoCodec)
videoFormat = format
continue
}
}
if (qualityLabels.indexOf(qualityLabel) - 1 < 0) {
break
}
qualityLabel = qualityLabels[qualityLabels.indexOf(qualityLabel) - 1]
}
if (!videoFormat) {
videoFormat = ytdl.chooseFormat(info.formats, { filter: "videoonly" })
}
res.setHeader("Content-Disposition", `${downloadType}; filename="${info.data.output.filename.replace(/[^a-z0-9 ]/gi, '')}.${format}"`)
var baseArgs = [
// Remove ffmpeg's console spamming
'-loglevel', 'error',
@ -115,7 +118,7 @@ expressManager.app.get("/download", async (req, res) => {
}
var args = inputArgs.concat(mapArgs).concat(bonusArgs).concat(baseArgs)
const ffmpegProcess = cp.spawn(ffmpeg, args, {
windowsHide: true,
stdio: [
@ -127,13 +130,15 @@ expressManager.app.get("/download", async (req, res) => {
});
ffmpegProcess.stdio[1].pipe(res)
var audio = ytdl(url, { format: audioFormat || "audioonly" })
audio.pipe(ffmpegProcess.stdio[4])
var audio = await fetch(info.data.tunnel[1] || info.data.tunnel[0])
Readable.fromWeb(audio.body).pipe(ffmpegProcess.stdio[4])
if (needsVideo) {
var video = ytdl(url, { format: videoFormat || "videoonly" })
video.pipe(ffmpegProcess.stdio[5])
var video = await fetch(info.data.tunnel[0])
Readable.fromWeb(video.body).pipe(ffmpegProcess.stdio[5])
}
} else {