FUNCTIONING
This commit is contained in:
parent
3939aa416f
commit
c05f172aec
7 changed files with 440 additions and 14 deletions
86
index.js
86
index.js
|
@ -1,15 +1,43 @@
|
|||
const fs = require('fs'),
|
||||
path = require('path'),
|
||||
express = require('express'),
|
||||
cp = require("child_process")
|
||||
|
||||
cp = require("child_process"),
|
||||
ffmpeg = require("ffmpeg-static")
|
||||
|
||||
const PORT = process.env.PORT || 8080
|
||||
const app = express()
|
||||
|
||||
var formats = {
|
||||
"matroska": "mkv"
|
||||
}
|
||||
const MAX_FILESIZE = process.env.MAX_FILESIZE || 500
|
||||
|
||||
app.get("/convert", async (req, res) => {
|
||||
var file = req.query.file || ""
|
||||
var format = req.query.format
|
||||
var filePath = path.join(__dirname, 'downloads', file)
|
||||
if (fs.existsSync(filePath)) {
|
||||
res.setHeader('Content-Disposition', `attachment; filename="test.${format}"`);
|
||||
const ffmpegProcess = cp.spawn(ffmpeg, [
|
||||
'-i', filePath,
|
||||
'-f', format,
|
||||
'-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
|
||||
|
@ -19,8 +47,56 @@ app.get("/download", async (req, res) => {
|
|||
const preset = 'medium'
|
||||
|
||||
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 fileName = Math.round(Math.random() * 100_000_000_000_000).toString() + '.' + 'webm'
|
||||
var filePath = path.join(__dirname, 'downloads', fileName)
|
||||
|
||||
var ytdlpProcess = cp.spawn('yt-dlp', [
|
||||
url,
|
||||
'-o', filePath,
|
||||
'--max-filesize', MAX_FILESIZE + 'm'
|
||||
])
|
||||
|
||||
var lastDownload = 0
|
||||
|
||||
ytdlpProcess.stderr.setEncoding('utf-8')
|
||||
ytdlpProcess.stderr.on('data', (data) => {
|
||||
console.log(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(`<p>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!)</p>`)
|
||||
}
|
||||
else if (data.includes("[download]")) {
|
||||
res.write(`<style>#downloading${lastDownload}{ display: none; }</style>`)
|
||||
lastDownload += 1
|
||||
res.write(`<p id="downloading${lastDownload}">` + data.substring(12) + `</p>`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
|
||||
ytdlpProcess.on('close', () => {
|
||||
if (fs.existsSync(filePath)) {
|
||||
res.write(`<iframe src="/convert?file=${fileName}&format=${format}"></iframe>"`)
|
||||
res.write(downloadHTML.substring(downloadHTML.indexOf("{CONTENT}") + 9), () => {res.end()})
|
||||
} else {
|
||||
res.write("<p>An error has occured!!! We're not exactly sure what the error is, but we cant seem to find the download file.</p>")
|
||||
console.log(filePath)
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue