asd
This commit is contained in:
parent
38a1cedba5
commit
4f94c31dc4
1 changed files with 137 additions and 64 deletions
201
index.js
201
index.js
|
@ -3,6 +3,7 @@ const fs = require('fs'),
|
||||||
express = require('express'),
|
express = require('express'),
|
||||||
cp = require("child_process"),
|
cp = require("child_process"),
|
||||||
ffmpeg = require("ffmpeg-static")
|
ffmpeg = require("ffmpeg-static")
|
||||||
|
const { stringify } = require('querystring')
|
||||||
|
|
||||||
const PORT = process.env.PORT || 8080
|
const PORT = process.env.PORT || 8080
|
||||||
const app = express()
|
const app = express()
|
||||||
|
@ -54,91 +55,163 @@ app.get("/convert", async (req, res) => {
|
||||||
app.get("/download", async (req, res) => {
|
app.get("/download", async (req, res) => {
|
||||||
const url = req.query.url
|
const url = req.query.url
|
||||||
const format = req.query.format || 'mp4'
|
const format = req.query.format || 'mp4'
|
||||||
const quality = req.query.quality || 'highest'
|
const quality = req.query.quality || '720'
|
||||||
|
|
||||||
res.setHeader("X-Accel-Buffering", "no")
|
res.setHeader("X-Accel-Buffering", "no")
|
||||||
res.setHeader("Content-Type", "text/html")
|
|
||||||
|
|
||||||
var downloadHTML = fs.readFileSync(path.join(__dirname, 'resources/downloading.html')).toString()
|
var fileName = Math.ceil(Math.random() * 100_000_000_000).toString()
|
||||||
|
console.log(fileName)
|
||||||
|
var videoName = cp.spawnSync('yt-dlp', ['-S', 'res:' + quality, '--get-filename', url]).stdout.toString()
|
||||||
|
|
||||||
res.write(downloadHTML.substring(0, downloadHTML.indexOf("{CONTENT}")))
|
videoName = videoName.substring(0, videoName.lastIndexOf('.'))
|
||||||
|
videoName = videoName.substring(0, videoName.lastIndexOf('['))
|
||||||
|
|
||||||
var fileName = cp.spawnSync('yt-dlp', ['-S', 'res:' + quality,'--get-filename', url]).stdout.toString()
|
if (!["mp3", "wav", "ogx"].includes(format) && Number(quality) > 720) {
|
||||||
|
res.setHeader("Content-Type", "text/html")
|
||||||
|
|
||||||
if (!["mp3", "wav", "ogx"].includes(format)) {
|
var downloadHTML = fs.readFileSync(path.join(__dirname, 'resources/downloading.html')).toString()
|
||||||
var filePath = path.join(__dirname, 'downloads', fileName).trim()
|
|
||||||
|
res.write(downloadHTML.substring(0, downloadHTML.indexOf("{CONTENT}")))
|
||||||
|
|
||||||
|
var filePath = path.join(__dirname, 'downloads', fileName)
|
||||||
var ytdlpProcess = cp.spawn('yt-dlp', [
|
var ytdlpProcess = cp.spawn('yt-dlp', [
|
||||||
url,
|
url,
|
||||||
'-o', filePath.substring(0, filePath.lastIndexOf('.')),
|
'-o', filePath,
|
||||||
'--max-filesize', MAX_FILESIZE + 'm',
|
'--max-filesize', MAX_FILESIZE + 'm',
|
||||||
'-S', 'res:' + quality,
|
'-S', 'res:' + quality,
|
||||||
'--no-playlist',
|
'--no-playlist',
|
||||||
])
|
])
|
||||||
} else {
|
|
||||||
fileName = fileName.substring(0, fileName.lastIndexOf(".")) + '.mp3'
|
var lastDownload = 0
|
||||||
var filePath = path.join(__dirname, 'downloads', fileName).trim()
|
|
||||||
|
ytdlpProcess.stderr.setEncoding('utf-8')
|
||||||
|
ytdlpProcess.stderr.on('data', (data) => {
|
||||||
|
console.log(data)
|
||||||
|
res.write(`<div class="error"><p>` + data + `</p></div>`)
|
||||||
|
})
|
||||||
|
|
||||||
|
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>`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
var exited = false
|
||||||
|
|
||||||
|
ytdlpProcess.on('close', () => {
|
||||||
|
|
||||||
|
var files = fs.readdirSync(path.join(__dirname, 'downloads'))
|
||||||
|
for (let index = 0; index < files.length; index++) {
|
||||||
|
const file = files[index];
|
||||||
|
console.log(file)
|
||||||
|
if (file.includes(fileName)) {
|
||||||
|
fileName = file
|
||||||
|
filePath = path.join(__dirname, 'downloads', fileName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exited) {
|
||||||
|
if (fs.existsSync(filePath)) {
|
||||||
|
fs.rmSync(filePath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (fs.existsSync(filePath)) {
|
||||||
|
res.write(`<iframe src="/convert?file=${fileName}&format=${format}&url=${url}"></iframe>"`)
|
||||||
|
res.write(downloadHTML.substring(downloadHTML.indexOf("{CONTENT}") + 9) + `<meta http-equiv="Refresh" content="0; url='/'" />`, () => { 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. Double check the URL, and if the URL is fine, then file an issue on codeberg. </p>")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
res.on("error", () => {
|
||||||
|
if (fs.existsSync(filePath)) {
|
||||||
|
fs.rmSync(filePath)
|
||||||
|
}
|
||||||
|
exited = true
|
||||||
|
})
|
||||||
|
} else if (["mp3", "wav", "ogx"].includes(format)) {
|
||||||
|
res.setHeader('Content-Disposition', `attachment; filename="${encodeURIComponent(videoName)}.${format}"`);
|
||||||
|
|
||||||
var ytdlpProcess = cp.spawn('yt-dlp', [
|
var ytdlpProcess = cp.spawn('yt-dlp', [
|
||||||
url,
|
url,
|
||||||
'-x',
|
'-x',
|
||||||
'-o', filePath.substring(0, filePath.lastIndexOf('.')),
|
'-o', '-',
|
||||||
|
'--max-filesize', MAX_FILESIZE + 'm',
|
||||||
|
'--audio-format', 'mp3',
|
||||||
|
'--no-playlist',
|
||||||
|
])
|
||||||
|
|
||||||
|
const ffmpegProcess = cp.spawn(ffmpeg, [
|
||||||
|
'-i', 'pipe:3',
|
||||||
|
'-f', formats[format] || format,
|
||||||
|
'-movflags', 'frag_keyframe+empty_moov',
|
||||||
|
'-c:a', 'libmp3lame',
|
||||||
|
'-preset','ultrafast',
|
||||||
|
'-crf', '23',
|
||||||
|
'-loglevel', 'error',
|
||||||
|
'-'
|
||||||
|
], {
|
||||||
|
stdio: [
|
||||||
|
'pipe', 'pipe', 'pipe', 'pipe', 'pipe',
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
ytdlpProcess.stdout.pipe(ffmpegProcess.stdio[3])
|
||||||
|
|
||||||
|
ytdlpProcess.stderr.setEncoding('utf-8')
|
||||||
|
ytdlpProcess.stderr.on('data', (data) => {
|
||||||
|
console.log(data)
|
||||||
|
})
|
||||||
|
|
||||||
|
ffmpegProcess.stdio[1].pipe(res)
|
||||||
|
} else {
|
||||||
|
res.setHeader('Content-Disposition', `attachment; filename="${encodeURIComponent(videoName)}.${format}"`);
|
||||||
|
|
||||||
|
var ytdlpProcess = cp.spawn('yt-dlp', [
|
||||||
|
url,
|
||||||
|
'-o', '-',
|
||||||
'--max-filesize', MAX_FILESIZE + 'm',
|
'--max-filesize', MAX_FILESIZE + 'm',
|
||||||
'--no-playlist',
|
'--no-playlist',
|
||||||
'--audio-format', 'mp3',
|
|
||||||
])
|
])
|
||||||
|
|
||||||
|
const ffmpegProcess = cp.spawn(ffmpeg, [
|
||||||
|
'-i', 'pipe:3',
|
||||||
|
'-f', formats[format] || format,
|
||||||
|
'-movflags', 'frag_keyframe+empty_moov',
|
||||||
|
'-c:a', 'libmp3lame',
|
||||||
|
'-preset','ultrafast',
|
||||||
|
'-crf', '23',
|
||||||
|
'-loglevel', 'error',
|
||||||
|
'-'
|
||||||
|
], {
|
||||||
|
stdio: [
|
||||||
|
'pipe', 'pipe', 'pipe', 'pipe', 'pipe',
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
ytdlpProcess.stdout.pipe(ffmpegProcess.stdio[3])
|
||||||
|
|
||||||
|
ytdlpProcess.stderr.setEncoding('utf-8')
|
||||||
|
ytdlpProcess.stderr.on('data', (data) => {
|
||||||
|
console.log(data)
|
||||||
|
})
|
||||||
|
|
||||||
|
ffmpegProcess.stdio[1].pipe(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
var lastDownload = 0
|
|
||||||
|
|
||||||
ytdlpProcess.stderr.setEncoding('utf-8')
|
|
||||||
ytdlpProcess.stderr.on('data', (data) => {
|
|
||||||
console.log(data)
|
|
||||||
res.write(`<div class="error"><p>` + data + `</p></div>`)
|
|
||||||
})
|
|
||||||
|
|
||||||
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>`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
var exited = false
|
|
||||||
|
|
||||||
ytdlpProcess.on('close', () => {
|
|
||||||
console.log(filePath)
|
|
||||||
if (exited) {
|
|
||||||
if (fs.existsSync(filePath)) {
|
|
||||||
fs.rmSync(filePath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (fs.existsSync(filePath)) {
|
|
||||||
res.write(`<iframe src="/convert?file=${fileName}&format=${format}&url=${url}"></iframe>"`)
|
|
||||||
res.write(downloadHTML.substring(downloadHTML.indexOf("{CONTENT}") + 9) + `<meta http-equiv="Refresh" content="0; url='/'" />`, () => { 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. Double check the URL, and if the URL is fine, then file an issue on codeberg. </p>")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
res.on("error", () => {
|
|
||||||
if (fs.existsSync(filePath)) {
|
|
||||||
fs.rmSync(filePath)
|
|
||||||
}
|
|
||||||
exited = true
|
|
||||||
})
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
app.use(express.static(path.join(__dirname, 'static')))
|
app.use(express.static(path.join(__dirname, 'static')))
|
||||||
|
|
Loading…
Reference in a new issue