OMG ITS FIXED :DDD !!!!
This commit is contained in:
parent
56835a9221
commit
cf43b56572
5 changed files with 51 additions and 57 deletions
42
index.js
42
index.js
|
@ -1,8 +1,9 @@
|
|||
const { randomInt } = require('crypto');
|
||||
const ytdl = require('ytdl-core'),
|
||||
fs = require('fs'),
|
||||
path = require('path'),
|
||||
express = require('express'),
|
||||
ffmpeg = require('fluent-ffmpeg'),
|
||||
ffmpeg = require('ffmpeg'),
|
||||
bodyParser = require('body-parser')
|
||||
|
||||
const PORT = process.env.PORT || 8080
|
||||
|
@ -18,7 +19,7 @@ process.on('uncaughtException', (err, origin) => {
|
|||
|
||||
app.use(bodyParser.urlencoded({ extended: false }))
|
||||
|
||||
app.get("/download", (req, res) => {
|
||||
app.get("/download", async (req, res) => {
|
||||
const filename = req.query.filename
|
||||
const url = req.query.url
|
||||
const format = req.query.format
|
||||
|
@ -29,17 +30,36 @@ app.get("/download", (req, res) => {
|
|||
return
|
||||
}
|
||||
|
||||
if (format != "mp4" && format != "webm") {
|
||||
res.header('Content-Disposition', `attachment; filename="${filename}.${format};"`);
|
||||
res.header('Content-Disposition', `attachment; filename="${filename}.${format}"`);
|
||||
|
||||
var proc = new ffmpeg({ source: ytdl(url, { 'quality': quality }) })
|
||||
.format(format)
|
||||
.pipe(res)
|
||||
var ytpath = path.join(__dirname, 'cached/' + ytdl.getVideoID(url) + String(Math.floor(Math.random() * 100000)) + '.mp4')
|
||||
|
||||
var vidinfo = await ytdl.getBasicInfo(url)
|
||||
|
||||
var ytvid = ytdl(url, { 'quality': quality })
|
||||
.pipe(fs.createWriteStream(ytpath))
|
||||
.on("close", ()=> {
|
||||
console.log("Downloaded!")
|
||||
var proc = new ffmpeg(ytpath)
|
||||
proc.then(function(video) {
|
||||
video
|
||||
.setVideoDuration(vidinfo.videoDetails.lengthSeconds)
|
||||
.setVideoFormat(format)
|
||||
.save('cached/' + ytdl.getVideoID(url) + String(Math.floor(Math.random() * 100000)) + "Converted." + format,function (err, file) {
|
||||
console.log("Converted!")
|
||||
fs.createReadStream(file).pipe(res)
|
||||
.on("close", () => {
|
||||
console.log("Freed!")
|
||||
fs.unlinkSync(file)
|
||||
if (fs.existsSync(ytpath)) {
|
||||
fs.unlinkSync(ytpath)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
} else {
|
||||
res.header('Content-Disposition', `attachment; filename="${filename}.${format}"`);
|
||||
ytdl(url, { 'format': format, 'quality': quality }).pipe(res);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue