Download length header added, fast download back!
This commit is contained in:
parent
66ac5c3a29
commit
23f9cc6e3a
3 changed files with 163 additions and 124 deletions
186
index.js
186
index.js
|
@ -50,122 +50,135 @@ app.get("/getLink", async (req, res) => {
|
|||
res.write(`<p>Invalid URL! Check the url for any errors! <br> ${url}</p>`)
|
||||
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(`<link rel="stylesheet" href="/style.css">`)
|
||||
}
|
||||
res.write(`<link rel="stylesheet" href="/style.css">`)
|
||||
}
|
||||
|
||||
|
||||
// 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(`<p>Starting download...</p>`)
|
||||
}
|
||||
var lastp = 0
|
||||
var ytvid = ytdl(url, { 'quality': quality, 'format': 'mp4' })
|
||||
if (redir != "redirect") {
|
||||
res.write(`<p>Starting download...</p>`)
|
||||
}
|
||||
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(`<p class="percent" id="percent${percent}">Downloading... ${percent}% (${formatBytes(ctotal)})</p>`)
|
||||
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(`<p class="percent" id="percent${percent}">Downloading... ${percent}% (${formatBytes(ctotal)})</p>`)
|
||||
res.write(`
|
||||
<style>
|
||||
#percent${lastp} {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
`)
|
||||
lastp = percent
|
||||
}
|
||||
})
|
||||
}
|
||||
ytvid.pipe(fs.createWriteStream(ytpath))
|
||||
.on("close", () => {
|
||||
if (redir != "redirect") {
|
||||
res.write("<p>Downloaded!</p>")
|
||||
}
|
||||
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>`)
|
||||
var endpath = ('cached/' + ytdl.getVideoID(url) + String(Math.floor(Math.random() * 100000)) + "Converted." + format)
|
||||
if (redir != "redirect") {
|
||||
res.write(`<p>Starting Video Conversion... Conversion may be slow depending on instance.</p>`)
|
||||
} else {
|
||||
console.log(redir)
|
||||
}
|
||||
proc.then(function (video) {
|
||||
video
|
||||
.setVideoDuration(vidinfo.videoDetails.lengthSeconds)
|
||||
.setVideoFormat(format)
|
||||
.save(endpath, (error, file) => {
|
||||
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(`
|
||||
lastp = percent
|
||||
}
|
||||
})
|
||||
}
|
||||
ytvid.pipe(fs.createWriteStream(ytpath))
|
||||
.on("close", () => {
|
||||
if (redir != "redirect") {
|
||||
res.write("<p>Downloaded!</p>")
|
||||
}
|
||||
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>`)
|
||||
var endpath = ('cached/' + ytdl.getVideoID(url) + String(Math.floor(Math.random() * 100000)) + "Converted." + format)
|
||||
if (redir != "redirect") {
|
||||
res.write(`<p>Starting Video Conversion... Conversion may be slow depending on instance.</p>`)
|
||||
} else {
|
||||
console.log(redir)
|
||||
}
|
||||
proc.then(function (video) {
|
||||
video
|
||||
.setVideoDuration(vidinfo.videoDetails.lengthSeconds)
|
||||
.setVideoFormat(format)
|
||||
.save(endpath, (error, file) => {
|
||||
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(`
|
||||
<style>
|
||||
.cv {
|
||||
display: none;
|
||||
}
|
||||
</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>`)
|
||||
if (redir == "redirectjs") {
|
||||
res.write(`
|
||||
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") {
|
||||
res.write(`
|
||||
<script>
|
||||
window.location = "/download?path=${path.basename(file)}&filename=${filename}.${format}"
|
||||
</script>
|
||||
`)
|
||||
}
|
||||
} 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(`<p class="cv" id="convert${count}">Converting... (${formatBytes(ffile.length)})</p>`)
|
||||
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(`<p class="cv" id="convert${count}">Converting... (${formatBytes(ffile.length)})</p>`)
|
||||
res.write(`
|
||||
<style>
|
||||
#convert${count - 1} {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
`)
|
||||
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")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue