diff --git a/index.js b/index.js index 985cf45..a065d67 100644 --- a/index.js +++ b/index.js @@ -50,122 +50,135 @@ app.get("/getLink", async (req, res) => { res.write(`
Invalid URL! Check the url for any errors!
${url}
Starting download...
`) - } - var lastp = 0 - var ytvid = ytdl(url, { 'quality': quality, 'format': 'mp4' }) + if (redir != "redirect") { + res.write(`Starting download...
`) + } + var lastp = 0 + var ytvid = ytdl(url, { 'quality': quality, 'format': 'mp4' }) - if (redir != "redirect") { - ytvid.on("progress", (data, ctotal, etotal) => { - var integer = 1 - var percent = (Math.ceil(ctotal / etotal * 100)) - if (percent % integer == 0 && lastp != percent) { - res.write(`Downloading... ${percent}% (${formatBytes(ctotal)})
`) - res.write(` + if (redir != "redirect") { + ytvid.on("progress", (data, ctotal, etotal) => { + var integer = 1 + var percent = (Math.ceil(ctotal / etotal * 100)) + if (percent % integer == 0 && lastp != percent) { + res.write(`Downloading... ${percent}% (${formatBytes(ctotal)})
`) + res.write(` `) - lastp = percent - } - }) - } - ytvid.pipe(fs.createWriteStream(ytpath)) - .on("close", () => { - if (redir != "redirect") { - 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) - if (redir != "redirect") { - res.write(`Starting Video Conversion... Conversion may be slow depending on instance.
`) - } else { - console.log(redir) - } - proc.then(function (video) { - video - .setVideoDuration(vidinfo.videoDetails.lengthSeconds) - .setVideoFormat(format) - .save(endpath, (error, file) => { - if (redir != "redirect") { - res.write(`Converted! (Why doesn't conversion support %s?)
`) - res.write(` + lastp = percent + } + }) + } + ytvid.pipe(fs.createWriteStream(ytpath)) + .on("close", () => { + if (redir != "redirect") { + 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) + if (redir != "redirect") { + res.write(`Starting Video Conversion... Conversion may be slow depending on instance.
`) + } else { + console.log(redir) + } + proc.then(function (video) { + video + .setVideoDuration(vidinfo.videoDetails.lengthSeconds) + .setVideoFormat(format) + .save(endpath, (error, file) => { + if (redir != "redirect") { + res.write(`Converted! (Why doesn't conversion support %s?)
`) + res.write(` `) - res.write(`Link to video download! (Link is deleted after ${cacheDuration} minutes!)
`) - if (redir == "redirectjs") { - res.write(` + res.write(`Link to video download! (Link is deleted after ${cacheDuration} minutes!)
`) + if (redir == "redirectjs") { + res.write(` `) + } + } else { + res.redirect(`/download?path=${path.basename(file)}&filename=${filename}.${format}`) } - } else { - res.redirect(`/download?path=${path.basename(file)}&filename=${filename}.${format}`) - } - setTimeout(() => { - if (fs.existsSync(ytpath)) { - fs.unlinkSync(ytpath) - } - if (fs.existsSync(file)) { - fs.unlinkSync(file) - } - }, 60 * 1000) - }) - }) + setTimeout(() => { + if (fs.existsSync(ytpath)) { + fs.unlinkSync(ytpath) + } + if (fs.existsSync(file)) { + fs.unlinkSync(file) + } + }, 60 * 1000) + }) + }) - var count = 0 - 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 - // console.log(ffile.length) - // if (ffile.length != lastc) { - res.write(`Converting... (${formatBytes(ffile.length)})
`) - res.write(` + var count = 0 + 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 + // console.log(ffile.length) + // if (ffile.length != lastc) { + res.write(`Converting... (${formatBytes(ffile.length)})
`) + res.write(` `) - lastc = percent - count += 1 - // } + lastc = percent + count += 1 + // } + } + if (percent < 100 && redir != "redirect") { + setTimeout(() => { + update() + }, 2000); + } } - if (percent < 100 && redir != "redirect") { - setTimeout(() => { - update() - }, 2000); - } - } - update() - }) + update() + }) + } }); app.get('/download', (req, res) => { @@ -174,7 +187,8 @@ app.get('/download', (req, res) => { var dest = path.join(__dirname, 'cached/', file) if (fs.existsSync(dest)) { - res.header('Content-Disposition', `attachment; filename="${filename}"`); + res.setHeader('Content-Disposition', `attachment; filename="${filename}"`); + res.setHeader('Content-Length', fs.readFileSync(dest).length) fs.createReadStream(dest).pipe(res) } else { res.header("Content-Type", "text/html") diff --git a/static/index.html b/static/index.html index 37d7bb8..fa98a5d 100644 --- a/static/index.html +++ b/static/index.html @@ -12,48 +12,58 @@ -v2.1
+v2.2
*1: The "Redirect JS" Option is the same as the "Don't redirect" option, except it sends javascript to the browser letting you redirect. BOTH OPTIONS WORK FINE WITHOUT JAVASCRIPT!
-*2: The "Redirect (disable progress)" option doesn't use any javascript and automatically redirects to the download, but you are not given download progress updates!
-Please consider donating or host the website yourself! Anything helps! (Check github for more info)
Please consider donating or host the website yourself! Anything + helps! (Check github for more info)