const ytdl = require('ytdl-core'), fs = require('fs'), path = require('path'), express = require('express'), ffmpeg = require('fluent-ffmpeg'), bodyParser = require('body-parser') const PORT = process.env.PORT || 8080 const app = express() 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", (req, res) => { const filename = req.query.filename const url = req.query.url const format = req.query.format const quality = req.query.quality if (!ytdl.validateURL(url)) { res.send("Invalid URL!") return } if (format != "mp4" && format != "webm") { res.header('Content-Disposition', `attachment; filename="${filename}.${format};"`); var proc = new ffmpeg({ source: ytdl(url, { 'quality': quality }) }) .format(format) .pipe(res) } else { res.header('Content-Disposition', `attachment; filename="${filename}.${format}"`); ytdl(url, { 'format': format, 'quality': quality }).pipe(res); } }); app.use(express.static(path.join(__dirname, 'static'))) app.listen(PORT, function () { console.log("Hosted on port " + PORT) })