2023-08-21 11:46:48 -05:00
|
|
|
const ytdl = require('ytdl-core'),
|
|
|
|
fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
express = require('express'),
|
2023-08-23 11:35:10 -05:00
|
|
|
ffmpeg = require('fluent-ffmpeg'),
|
|
|
|
bodyParser = require('body-parser'),
|
|
|
|
multer = require('multer')
|
2023-05-05 11:15:22 -05:00
|
|
|
|
|
|
|
const PORT = process.env.PORT || 8080
|
|
|
|
const app = express()
|
|
|
|
|
2023-08-23 11:35:10 -05:00
|
|
|
const storage = multer.diskStorage({
|
|
|
|
destination: function (req, file, cb) {
|
|
|
|
cb(null, 'uploads/')
|
|
|
|
},
|
|
|
|
filename: function (req, file, cb) {
|
|
|
|
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
|
|
|
|
cb(null, file.fieldname + '-' + uniqueSuffix + '-' + file.originalname)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }))
|
|
|
|
|
|
|
|
var jsonParser = bodyParser.json()
|
|
|
|
|
2023-05-05 11:15:22 -05:00
|
|
|
app.get("/download", (req, res) => {
|
2023-05-08 12:52:41 -05:00
|
|
|
const filename = req.query.filename
|
|
|
|
const url = req.query.url
|
2023-05-10 12:36:26 -05:00
|
|
|
const format = req.query.format
|
2023-05-05 11:15:22 -05:00
|
|
|
const quality = req.query.quality
|
2023-05-10 12:36:26 -05:00
|
|
|
|
2023-08-21 11:46:48 -05:00
|
|
|
if (format != "mp4" && format != "webm") {
|
2023-08-23 11:35:10 -05:00
|
|
|
res.header('Content-Disposition', `attachment; filename="${filename}.${format};"`);
|
|
|
|
|
|
|
|
var proc = new ffmpeg({ source: ytdl(url, { 'quality': quality }) })
|
|
|
|
.format(format)
|
|
|
|
.pipe(res)
|
2023-08-21 11:46:48 -05:00
|
|
|
|
|
|
|
} else {
|
2023-08-23 11:35:10 -05:00
|
|
|
res.header('Content-Disposition', `attachment; filename="${filename}.${format}"`);
|
|
|
|
ytdl(url, { 'format': format, 'quality': quality }).pipe(res);
|
2023-08-21 11:46:48 -05:00
|
|
|
}
|
2023-05-05 11:15:22 -05:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2023-08-23 11:35:10 -05:00
|
|
|
const upload = multer({ limits: { fieldSize: 100_000_000 }, storage: storage })
|
|
|
|
|
|
|
|
app.post("/convert", upload.single('video'), (req, res) => {
|
|
|
|
console.log(req.file.path)
|
|
|
|
const filename = req.body.filename
|
|
|
|
const vid = req.file.path
|
|
|
|
const format = req.body.format
|
|
|
|
|
|
|
|
var proc = new ffmpeg({ source: vid })
|
|
|
|
.format(format)
|
|
|
|
.pipe(res)
|
|
|
|
.on("end", () => {
|
|
|
|
console.log("AAAA")
|
|
|
|
fs.unlinkSync(req.file.path)
|
|
|
|
})
|
|
|
|
|
|
|
|
res.header('Content-Disposition', `attachment; filename="${filename}.${format}"`);
|
|
|
|
});
|
|
|
|
|
2023-05-05 11:15:22 -05:00
|
|
|
|
|
|
|
app.use(express.static(path.join(__dirname, 'static')))
|
|
|
|
|
2023-05-10 12:36:26 -05:00
|
|
|
app.listen(PORT, function () {
|
2023-05-05 11:15:22 -05:00
|
|
|
console.log("Hosted on port " + PORT)
|
|
|
|
})
|