Univerter/index.js
Bingus_Violet 72defd0e69 oops
2023-11-20 14:41:17 -06:00

125 lines
3.2 KiB
JavaScript

const ytdl = require('ytdl-core'),
fs = require('fs'),
path = require('path'),
express = require('express'),
bodyParser = require('body-parser'),
cp = require("child_process"),
ffmpeg = require('ffmpeg-static')
const PORT = process.env.PORT || 8080
const app = express()
var formats = {
"matroska": "mkv"
}
const characters = "abcdefghijklmnopqrstuvwxyz!@$%^*()[]_-=+ "
process.on('uncaughtException', (err, origin) => {
fs.writeSync(
process.stderr.fd,
`Caught exception: ${err}\n` +
`Exception origin: ${origin}`,
);
});
app.use(bodyParser.urlencoded({ extended: false }))
app.get("/download", async (req, res) => {
const url = req.query.url
const format = req.query.format
const quality = req.query.quality
const defin = req.query.definition
res.setHeader("X-Accel-Buffering", "no")
if (!ytdl.validateURL(url)) {
res.header("Content-Type", "text/html")
res.write(`<link rel="stylesheet" href="/style.css">`)
res.write(`<p>Invalid URL! Check the url for any errors! <br>URL: ${url}</p>`)
return
}
var vidinfo = await ytdl.getBasicInfo(url)
const ogname = vidinfo.videoDetails.title
var filename = ""
for (let index = 0; index < ogname.length; index++) {
const letter = ogname[index];
if ((characters + characters.toUpperCase()).includes(letter)) {
filename += letter
}
}
if ("mp3 ogg wav".includes(format)) {
var audio = ytdl(url, { filter: 'audioonly', quality: quality })
res.setHeader('Content-Disposition', `attachment; filename="${filename}.${formats[format] || format}"`);
audio.pipe(res)
} else if (defin == "hd") {
var video = ytdl(url, { filter: 'videoonly', quality: quality })
var audio = ytdl(url, { filter: 'audioonly', highWaterMark: 1<<25 })
const ffmpegProcess = cp.spawn(ffmpeg, [
'-i', `pipe:3`,
'-i', `pipe:4`,
'-map','0:v',
'-map','1:a',
'-c:v', 'copy',
'-c:a', 'libmp3lame',
'-crf','27',
'-preset','veryfast',
'-movflags','frag_keyframe+empty_moov',
'-f', format,
'-loglevel','error',
'-'
], {
stdio: [
'pipe', 'pipe', 'pipe', 'pipe', 'pipe',
],
})
video.pipe(ffmpegProcess.stdio[3])
audio.pipe(ffmpegProcess.stdio[4])
res.setHeader('Content-Disposition', `attachment; filename="${filename}.${formats[format] || format}"`);
ffmpegProcess.stdio[1].pipe(res)
} else {
var video = ytdl(url, { filter: 'videoandaudio', quality: quality })
const ffmpegProcess = cp.spawn(ffmpeg, [
'-i', `pipe:3`,
'-c:v', 'copy',
'-c:a', 'libmp3lame',
'-crf','27',
'-preset','veryfast',
'-movflags','frag_keyframe+empty_moov',
'-f', format,
'-loglevel','error',
'-'
], {
stdio: [
'pipe', 'pipe', 'pipe', 'pipe', 'pipe',
],
})
video.pipe(ffmpegProcess.stdio[3])
res.setHeader('Content-Disposition', `attachment; filename="${filename}.${formats[format] || format}"`);
// res.setHeader('Content-Length', fs.readFileSync(dest).length)
ffmpegProcess.stdio[1].pipe(res)
}
})
app.use(express.static(path.join(__dirname, 'static')))
app.listen(PORT, function () {
console.log("Hosted on port " + PORT)
})