From 23f9cc6e3aa7ed3aae932772e98ecbcfd7570918 Mon Sep 17 00:00:00 2001 From: Violet <110205356+Violets-puragtory@users.noreply.github.com> Date: Wed, 25 Oct 2023 17:32:46 +0000 Subject: [PATCH] Download length header added, fast download back! --- index.js | 186 +++++++++++++++++++++++++--------------------- static/index.html | 82 +++++++++++--------- static/style.css | 19 ++++- 3 files changed, 163 insertions(+), 124 deletions(-) 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}

`) return } + if (["mp4", "webm"].includes(format)) { + var ytvid = ytdl(url, { 'quality': quality, 'format': format }) + var debounce = false + ytvid.on("progress", (chunk, cd, td) => { + if (!debounce) { + res.setHeader('Content-Disposition', `attachment; filename="${filename}.${format}";`); + res.setHeader("Content-Length", td) + ytvid.pipe(res) + // console.log(td) + debounce = true + } + }) + } else { + if (redir != "redirect") { + res.setHeader("Content-Type", "text/html"); - if (redir != "redirect") { - res.setHeader("Content-Type", "text/html"); - - res.write(``) - } + res.write(``) + } - // String(Math.floor(Math.random() * 100000)) + - var ytpath = path.join(__dirname, 'cached/' + ytdl.getVideoID(url) + '.mp4') + // String(Math.floor(Math.random() * 100000)) + + var ytpath = path.join(__dirname, 'cached/' + ytdl.getVideoID(url) + '.mp4') - var vidinfo = await ytdl.getBasicInfo(url) + var vidinfo = await ytdl.getBasicInfo(url) - if (redir != "redirect") { - res.write(`

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 @@ -

Youtube Downloader

v2.1

+

Youtube Downloader

v2.2


-

Video URL:

- -

Quality:

- -

Filename:

-
-

Format:

- -

Redirect:

- +
+

Video URL:

+ +
+ +
+

Quality:

+ +
+
+

Filename:

+ +
+
+

Format:

+ +
+ +
+

Redirect:

+ +

- +
-
-

*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)

\ No newline at end of file diff --git a/static/style.css b/static/style.css index a1c6245..c1accc5 100644 --- a/static/style.css +++ b/static/style.css @@ -24,6 +24,7 @@ input { p { margin: 5px; + display: inline-block } body, @@ -31,6 +32,10 @@ html { background-color: var(--background-color); } +/* body { + padding: 20px; +} */ + .grid-container { width: 100%; margin: auto; @@ -38,7 +43,7 @@ html { select, input { - display: inline-grid; + display: inline-block; background-color: var(--second-bg-color); color: var(--text-color); margin: 5px auto; @@ -63,4 +68,14 @@ label { hr { opacity: 0.3; -} \ No newline at end of file +} + +.note { + font-size: 1.1rem; + color: white; +} + +.setting { + display: block; + margin: 5px; +}