const fs = require('fs'), path = require('path'), express = require('express'), cp = require("child_process"), ffmpeg = require("ffmpeg-static") const PORT = process.env.PORT || 8080 const app = express() const MAX_FILESIZE = process.env.MAX_FILESIZE || 500 var formats = { "mkv": "matroska" } app.get("/convert", async (req, res) => { var file = req.query.file || "" var format = req.query.format var url = req.query.url var filePath = path.join(__dirname, 'downloads', file) if (fs.existsSync(filePath)) { var ytdlpProcess = cp.spawnSync('yt-dlp', ['--get-filename', url]) var name = ytdlpProcess.stdout.toString() name = name.substring(0, name.lastIndexOf("[") - 1) res.setHeader('Content-Disposition', `attachment; filename="${encodeURIComponent(name)}.${format}"`); const ffmpegProcess = cp.spawn(ffmpeg, [ '-i', filePath, '-f', formats[format] || format, '-movflags', 'frag_keyframe+empty_moov', '-vcodec', 'copy', '-' ], { stdio: [ 'pipe', 'pipe', 'pipe', 'pipe', 'pipe', ], }) // ffmpegProcess.stderr.setEncoding('utf-8') // ffmpegProcess.stderr.on('data', (data) => { // console.log(data) // }) // These are debugging lines to watch FFMPEG output :3 ffmpegProcess.stdio[1].pipe(res) .on('close', () => { fs.rmSync(filePath) }) } }) app.get("/download", async (req, res) => { const url = req.query.url const format = req.query.format || 'mp4' const quality = req.query.quality || 'highest' res.setHeader("X-Accel-Buffering", "no") res.setHeader("Content-Type", "text/html") var downloadHTML = fs.readFileSync(path.join(__dirname, 'resources/downloading.html')).toString() res.write(downloadHTML.substring(0, downloadHTML.indexOf("{CONTENT}"))) var fileExtension = cp.spawnSync('yt-dlp', ['--get-filename', url]).stdout.toString() fileExtension = fileExtension.substring(fileExtension.lastIndexOf('.')) if (!["mp3"].includes(format)) { var fileName = Math.round(Math.random() * 100_000_000_000_000).toString() + fileExtension var filePath = path.join(__dirname, 'downloads', fileName).trim() var ytdlpProcess = cp.spawn('yt-dlp', [ url, '-o', filePath.substring(0, filePath.lastIndexOf('.')), '--max-filesize', MAX_FILESIZE + 'm', '-S', 'res:' + quality, '--no-playlist', ]) } else { var fileName = Math.round(Math.random() * 100_000_000_000_000).toString() + fileExtension var filePath = path.join(__dirname, 'downloads', fileName).trim() var ytdlpProcess = cp.spawn('yt-dlp', [ url, '-x', '-o', filePath.substring(0, filePath.lastIndexOf('.')), '--max-filesize', MAX_FILESIZE + 'm', '--no-playlist', ]) } var lastDownload = 0 ytdlpProcess.stderr.setEncoding('utf-8') ytdlpProcess.stderr.on('data', (data) => { console.log(data) res.write(`

` + data + `

`) }) var debounce = false ytdlpProcess.stdout.setEncoding('utf-8') ytdlpProcess.stdout.on('data', (data) => { if (!debounce) { if (data.includes("max-filesize")) { debounce = true res.write(`

Uh oh! The video you're trying to download is too large for this server's current settings ${MAX_FILESIZE}. Please try another server? (Visit main page and go to the codeberg for a list of instances!)

`) } else if (data.includes("[download]")) { res.write(``) lastDownload += 1 res.write(`

` + data.substring(12) + `

`) } } }) var exited = false ytdlpProcess.on('close', () => { if (exited) { if (fs.existsSync(filePath)) { fs.rmSync(filePath) } } else if (fs.existsSync(filePath)) { res.write(`"`) res.write(downloadHTML.substring(downloadHTML.indexOf("{CONTENT}") + 9) + ``, () => { res.end() }) } else { res.write("

An error has occured!!! We're not exactly sure what the error is, but we cant seem to find the download file. Double check the URL, and if the URL is fine, then file an issue on codeberg.

") } }) res.on("error", () => { if (fs.existsSync(filePath)) { fs.rmSync(filePath) } exited = true }) }) app.use(express.static(path.join(__dirname, 'static'))) app.listen(PORT, function () { console.log("Hosted on port " + PORT) })