From 924eea1621e357d1bd30c4a583a79c0aacf60e0e Mon Sep 17 00:00:00 2001 From: Violet <110205356+Violets-puragtory@users.noreply.github.com> Date: Fri, 20 Oct 2023 23:03:54 +0000 Subject: [PATCH] Progress bars!!! --- index.js | 127 ++++++++++++++++++++++++++++++++++++---------- package-lock.json | 2 +- package.json | 2 +- static/index.html | 5 +- 4 files changed, 105 insertions(+), 31 deletions(-) diff --git a/index.js b/index.js index e68d690..e1eca6d 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -const { randomInt } = require('crypto'); const ytdl = require('ytdl-core'), fs = require('fs'), path = require('path'), @@ -8,6 +7,13 @@ const ytdl = require('ytdl-core'), const PORT = process.env.PORT || 8080 const app = express() +if (fs.existsSync(path.join(__dirname, 'cached'))) { + fs.unlinkSync(path.join(__dirname, 'cached')) +} + +fs.mkdirSync(path.join(__dirname, 'cached')) + +const cacheDuration = 15 || process.env.CACHEDUR process.on('uncaughtException', (err, origin) => { fs.writeSync( @@ -19,50 +25,117 @@ process.on('uncaughtException', (err, origin) => { app.use(bodyParser.urlencoded({ extended: false })) -app.get("/download", async (req, res) => { +app.get("/getLink", async (req, res) => { const filename = req.query.filename const url = req.query.url const format = req.query.format const quality = req.query.quality if (!ytdl.validateURL(url)) { - res.send("Invalid URL!") + res.header("Content-Type", "text/html") + res.write(``) + res.write(`
Invalid URL! Check the url for any errors!
${url}
Downloading... ${percent}%
`) + res.write(` + + `) + lastp = percent + } }) - }) - - + .pipe(fs.createWriteStream(ytpath)) + .on("close", () => { + res.write("Downloaded!
") + var proc = new ffmpeg(ytpath) + // res.write(`Converting... (May take a while, doesn't support progress updates yet)
`) + var endpath = ('cached/' + ytdl.getVideoID(url) + String(Math.floor(Math.random() * 100000)) + "Converted." + format) + res.write(`Converting... 0% (Potentially Innacurate, Experimental!)
`) + proc.then(function (video) { + video + .setVideoDuration(vidinfo.videoDetails.lengthSeconds) + .setVideoFormat(format) + .save(endpath, (error, file)=>{ + res.write("Converted!
") + res.write(`Link to video download! (Link is deleted after 15 minutes!)
`) + setTimeout(()=>{ + if (fs.existsSync(ytpath)) { + fs.unlinkSync(ytpath) + } + if (fs.existsSync(file)) { + fs.unlinkSync(file) + } + }, 60 * 1000) + }) + }) + var integer = 1 + var lastc = 0 + var percent = 0 + async function update() { + if (fs.existsSync(endpath)) { + var ffile = await fs.readFileSync(endpath) + var yt = await fs.readFileSync(ytpath) + percent = (Math.round(ffile.length / yt.length * (100 / integer))) * integer + if (percent % integer == 0 && percent != lastc) { + res.write(`Converting... ${Math.min(percent, 100)}% (Potentially Innacurate, Experimental!)
`) + res.write(` + + `) + lastc = percent + } + } + if (percent < 100) { + setTimeout(() => { + update() + }, 500); + } + } + update() + }) }); +app.get('/download', (req, res) => { + var file = req.query.path + var filename = req.query.filename + var dest = path.join(__dirname, 'cached/', file) + + if (fs.existsSync(dest)) { + res.header('Content-Disposition', `attachment; filename="${filename}"`); + fs.createReadStream(dest).pipe(res) + } else { + res.header("Content-Type", "text/html") + res.write(``) + res.write(`Uh oh! It seems that video couldn't be found... Maybe the URL expired? Or, the link was invalid.
If you believe this was a mistake, please put a issue on Github
v0.4
+v2.0
Warning!: Although the tab might seem frozen, it is not! When you download a video, it first downloads, then converts, then sends it! If you would know any ways to indicated without javascript the progress of the conversion, or how to pipe the ffmpeg conversion automatically, please help!
Please consider donating or host the website yourself! Anything helps! (Check github for more info)