This commit is contained in:
Bingus_Violet 2024-01-25 19:38:25 -06:00
parent 38a1cedba5
commit 4f94c31dc4

113
index.js
View file

@ -3,6 +3,7 @@ const fs = require('fs'),
express = require('express'),
cp = require("child_process"),
ffmpeg = require("ffmpeg-static")
const { stringify } = require('querystring')
const PORT = process.env.PORT || 8080
const app = express()
@ -54,39 +55,32 @@ app.get("/convert", async (req, res) => {
app.get("/download", async (req, res) => {
const url = req.query.url
const format = req.query.format || 'mp4'
const quality = req.query.quality || 'highest'
const quality = req.query.quality || '720'
res.setHeader("X-Accel-Buffering", "no")
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()
videoName = videoName.substring(0, videoName.lastIndexOf('.'))
videoName = videoName.substring(0, videoName.lastIndexOf('['))
if (!["mp3", "wav", "ogx"].includes(format) && Number(quality) > 720) {
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 = cp.spawnSync('yt-dlp', ['-S', 'res:' + quality,'--get-filename', url]).stdout.toString()
if (!["mp3", "wav", "ogx"].includes(format)) {
var filePath = path.join(__dirname, 'downloads', fileName).trim()
var filePath = path.join(__dirname, 'downloads', fileName)
var ytdlpProcess = cp.spawn('yt-dlp', [
url,
'-o', filePath.substring(0, filePath.lastIndexOf('.')),
'-o', filePath,
'--max-filesize', MAX_FILESIZE + 'm',
'-S', 'res:' + quality,
'--no-playlist',
])
} else {
fileName = fileName.substring(0, fileName.lastIndexOf(".")) + '.mp3'
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',
'--audio-format', 'mp3',
])
}
var lastDownload = 0
@ -118,7 +112,17 @@ app.get("/download", async (req, res) => {
var exited = false
ytdlpProcess.on('close', () => {
console.log(filePath)
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)
@ -138,6 +142,75 @@ app.get("/download", async (req, res) => {
}
exited = true
})
} else if (["mp3", "wav", "ogx"].includes(format)) {
res.setHeader('Content-Disposition', `attachment; filename="${encodeURIComponent(videoName)}.${format}"`);
var ytdlpProcess = cp.spawn('yt-dlp', [
url,
'-x',
'-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',
'--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)
}
})