Download length header added, fast download back!

This commit is contained in:
Violet 2023-10-25 17:32:46 +00:00
parent 66ac5c3a29
commit 23f9cc6e3a
3 changed files with 163 additions and 124 deletions

186
index.js
View file

@ -50,122 +50,135 @@ app.get("/getLink", async (req, res) => {
res.write(`<p>Invalid URL! Check the url for any errors! <br> ${url}</p>`) res.write(`<p>Invalid URL! Check the url for any errors! <br> ${url}</p>`)
return 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.write(`<link rel="stylesheet" href="/style.css">`)
res.setHeader("Content-Type", "text/html"); }
res.write(`<link rel="stylesheet" href="/style.css">`)
}
// String(Math.floor(Math.random() * 100000)) + // String(Math.floor(Math.random() * 100000)) +
var ytpath = path.join(__dirname, 'cached/' + ytdl.getVideoID(url) + '.mp4') 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") { if (redir != "redirect") {
res.write(`<p>Starting download...</p>`) res.write(`<p>Starting download...</p>`)
} }
var lastp = 0 var lastp = 0
var ytvid = ytdl(url, { 'quality': quality, 'format': 'mp4' }) var ytvid = ytdl(url, { 'quality': quality, 'format': 'mp4' })
if (redir != "redirect") { if (redir != "redirect") {
ytvid.on("progress", (data, ctotal, etotal) => { ytvid.on("progress", (data, ctotal, etotal) => {
var integer = 1 var integer = 1
var percent = (Math.ceil(ctotal / etotal * 100)) var percent = (Math.ceil(ctotal / etotal * 100))
if (percent % integer == 0 && lastp != percent) { if (percent % integer == 0 && lastp != percent) {
res.write(`<p class="percent" id="percent${percent}">Downloading... ${percent}% (${formatBytes(ctotal)})</p>`) res.write(`<p class="percent" id="percent${percent}">Downloading... ${percent}% (${formatBytes(ctotal)})</p>`)
res.write(` res.write(`
<style> <style>
#percent${lastp} { #percent${lastp} {
display: none; display: none;
} }
</style> </style>
`) `)
lastp = percent lastp = percent
} }
}) })
} }
ytvid.pipe(fs.createWriteStream(ytpath)) ytvid.pipe(fs.createWriteStream(ytpath))
.on("close", () => { .on("close", () => {
if (redir != "redirect") { if (redir != "redirect") {
res.write("<p>Downloaded!</p>") res.write("<p>Downloaded!</p>")
} }
var proc = new ffmpeg(ytpath) var proc = new ffmpeg(ytpath)
// res.write(`<p>Converting... (May take a while, <a href="https://github.com/Violets-puragtory/NoJS-YTConverter"> doesn't support progress updates yet</a>)</p>`) // res.write(`<p>Converting... (May take a while, <a href="https://github.com/Violets-puragtory/NoJS-YTConverter"> doesn't support progress updates yet</a>)</p>`)
var endpath = ('cached/' + ytdl.getVideoID(url) + String(Math.floor(Math.random() * 100000)) + "Converted." + format) var endpath = ('cached/' + ytdl.getVideoID(url) + String(Math.floor(Math.random() * 100000)) + "Converted." + format)
if (redir != "redirect") { if (redir != "redirect") {
res.write(`<p>Starting Video Conversion... Conversion may be slow depending on instance.</p>`) res.write(`<p>Starting Video Conversion... Conversion may be slow depending on instance.</p>`)
} else { } else {
console.log(redir) console.log(redir)
} }
proc.then(function (video) { proc.then(function (video) {
video video
.setVideoDuration(vidinfo.videoDetails.lengthSeconds) .setVideoDuration(vidinfo.videoDetails.lengthSeconds)
.setVideoFormat(format) .setVideoFormat(format)
.save(endpath, (error, file) => { .save(endpath, (error, file) => {
if (redir != "redirect") { if (redir != "redirect") {
res.write(`<p>Converted! (<a href="https://github.com/damianociarla/node-ffmpeg/issues/33">Why doesn't conversion support %s?</a>)</p>`) res.write(`<p>Converted! (<a href="https://github.com/damianociarla/node-ffmpeg/issues/33">Why doesn't conversion support %s?</a>)</p>`)
res.write(` res.write(`
<style> <style>
.cv { .cv {
display: none; display: none;
} }
</style> </style>
`) `)
res.write(`<p><a href="/download?path=${path.basename(file)}&filename=${filename}.${format}" target="_blank">Link to video download!</a> (Link is deleted after ${cacheDuration} minutes!)</p>`) res.write(`<p><a href="/download?path=${path.basename(file)}&filename=${filename}.${format}" target="_blank">Link to video download!</a> (Link is deleted after ${cacheDuration} minutes!)</p>`)
if (redir == "redirectjs") { if (redir == "redirectjs") {
res.write(` res.write(`
<script> <script>
window.location = "/download?path=${path.basename(file)}&filename=${filename}.${format}" window.location = "/download?path=${path.basename(file)}&filename=${filename}.${format}"
</script> </script>
`) `)
}
} else {
res.redirect(`/download?path=${path.basename(file)}&filename=${filename}.${format}`)
} }
} else { setTimeout(() => {
res.redirect(`/download?path=${path.basename(file)}&filename=${filename}.${format}`) if (fs.existsSync(ytpath)) {
} fs.unlinkSync(ytpath)
setTimeout(() => { }
if (fs.existsSync(ytpath)) { if (fs.existsSync(file)) {
fs.unlinkSync(ytpath) fs.unlinkSync(file)
} }
if (fs.existsSync(file)) { }, 60 * 1000)
fs.unlinkSync(file) })
} })
}, 60 * 1000)
})
})
var count = 0 var count = 0
var integer = 1 var integer = 1
var lastc = 0 var lastc = 0
var percent = 0 var percent = 0
async function update() { async function update() {
if (fs.existsSync(endpath)) { if (fs.existsSync(endpath)) {
var ffile = await fs.readFileSync(endpath) var ffile = await fs.readFileSync(endpath)
var yt = await fs.readFileSync(ytpath) var yt = await fs.readFileSync(ytpath)
percent = (Math.round(ffile.length / yt.length * (100 / integer))) * integer percent = (Math.round(ffile.length / yt.length * (100 / integer))) * integer
// console.log(ffile.length) // console.log(ffile.length)
// if (ffile.length != lastc) { // if (ffile.length != lastc) {
res.write(`<p class="cv" id="convert${count}">Converting... (${formatBytes(ffile.length)})</p>`) res.write(`<p class="cv" id="convert${count}">Converting... (${formatBytes(ffile.length)})</p>`)
res.write(` res.write(`
<style> <style>
#convert${count - 1} { #convert${count - 1} {
display: none; display: none;
} }
</style> </style>
`) `)
lastc = percent lastc = percent
count += 1 count += 1
// } // }
}
if (percent < 100 && redir != "redirect") {
setTimeout(() => {
update()
}, 2000);
}
} }
if (percent < 100 && redir != "redirect") { update()
setTimeout(() => { })
update() }
}, 2000);
}
}
update()
})
}); });
app.get('/download', (req, res) => { app.get('/download', (req, res) => {
@ -174,7 +187,8 @@ app.get('/download', (req, res) => {
var dest = path.join(__dirname, 'cached/', file) var dest = path.join(__dirname, 'cached/', file)
if (fs.existsSync(dest)) { 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) fs.createReadStream(dest).pipe(res)
} else { } else {
res.header("Content-Type", "text/html") res.header("Content-Type", "text/html")

View file

@ -12,48 +12,58 @@
</head> </head>
<body> <body>
<h1>Youtube Downloader <p>v2.1</p> <h1>Youtube Downloader <p>v2.2</p>
<hr> <hr>
</h1> </h1>
<form action="/getLink" method="get" target="_blank"> <form action="/getLink" method="get" target="_blank">
<p style="margin-bottom: 1px;">Video URL:</p> <div class="setting">
<input required id="url" placeholder="Enter url" name="url"> <p style="margin-bottom: 1px;">Video URL:</p>
<p>Quality:</p> <input required id="url" placeholder="Enter url" name="url">
<select required id="quality" name="quality"> </div>
<option value="highest">Highest</option>
<option value="lowest">Lowest</option> <div class="setting">
</select> <p>Quality:</p>
<p>Filename:</p> <select required id="quality" name="quality">
<input required id="filename" placeholder="Enter filename" name="filename"><br> <option value="highest">Highest</option>
<p>Format:</p> <option value="lowest">Lowest</option>
<select required id="format" name="format"> </select>
<option disabled>Video Options</option> </div>
<option value="mp4">.mp4</option> <div class="setting">
<option value="webm">.webm</option> <p>Filename:</p>
<option value="flv">.flv</option> <input required id="filename" placeholder="Enter filename" name="filename">
<option value="avi">.avi</option> </div>
<option value="mpeg">.mpeg</option> <div class="setting">
<option value="mov">.mov</option> <p>Format:</p>
<option disabled>Audio Options</option> <select required id="format" name="format">
<option value="mp3">.mp3</option> <option disabled>Video Options</option>
<option value="ogg">.ogg</option> <option value="mp4" class="tooltip1">.mp4 (fastest)</option>
<option value="wav">.wav</option> <option value="webm" class="tooltip1">.webm (fastest)</option>
</select> <option value="flv">.flv</option>
<p>Redirect:</p> <option value="avi">.avi</option>
<select required id="redirect" name="redirect"> <option value="mpeg">.mpeg</option>
<option value="false">Don't redirect</option> <option value="mov">.mov</option>
<option value="redirectjs">Redirect (JS) *1</option> <option disabled>Audio Options</option>
<option value="redirect">Redirect (disable progress) *2</option> <option value="mp3">.mp3</option>
</select> <option value="ogg">.ogg</option>
<option value="wav">.wav</option>
</select>
</div>
<!-- <p class="note">* When using mp4 and webm, progress updates and redirect options are disabled, as it is directly downloaded to the browser.</p> -->
<div class="setting">
<p>Redirect:</p>
<select required id="redirect" name="redirect">
<option value="redirectjs">Redirect (JS, reccomended)</option>
<option value="false">Don't auto redirect</option>
<option value="redirect">Redirect (disables progress, not reccomended)</option>
</select>
</div>
<br> <br>
<input type="submit" placeholder="test"> <input type="submit">
</form> </form>
<hr> <br>
<p>*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!</p> <p><a href="https://ko-fi.com/bingus_violet">Please consider donating</a> or host the website yourself! Anything
<p>*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!</p> helps! (Check <a href="https://github.com/Violets-puragtory/NoJS-YTConverter">github</a> for more info)</p>
<hr>
<p><a href="https://ko-fi.com/bingus_violet">Please consider donating</a> or host the website yourself! Anything helps! (Check <a href="https://github.com/Violets-puragtory/NoJS-YTConverter">github</a> for more info)</p><br>
</body> </body>
</html> </html>

View file

@ -24,6 +24,7 @@ input {
p { p {
margin: 5px; margin: 5px;
display: inline-block
} }
body, body,
@ -31,6 +32,10 @@ html {
background-color: var(--background-color); background-color: var(--background-color);
} }
/* body {
padding: 20px;
} */
.grid-container { .grid-container {
width: 100%; width: 100%;
margin: auto; margin: auto;
@ -38,7 +43,7 @@ html {
select, select,
input { input {
display: inline-grid; display: inline-block;
background-color: var(--second-bg-color); background-color: var(--second-bg-color);
color: var(--text-color); color: var(--text-color);
margin: 5px auto; margin: 5px auto;
@ -63,4 +68,14 @@ label {
hr { hr {
opacity: 0.3; opacity: 0.3;
} }
.note {
font-size: 1.1rem;
color: white;
}
.setting {
display: block;
margin: 5px;
}