Redirect options!
This commit is contained in:
parent
8dd4dce08b
commit
d54639549c
2 changed files with 56 additions and 23 deletions
65
index.js
65
index.js
|
@ -8,7 +8,7 @@ const ytdl = require('ytdl-core'),
|
|||
const PORT = process.env.PORT || 8080
|
||||
const app = express()
|
||||
if (fs.existsSync(path.join(__dirname, 'cached'))) {
|
||||
fs.rmSync(path.join(__dirname, 'cached'), { recursive: true, force: true})
|
||||
fs.rmSync(path.join(__dirname, 'cached'), { recursive: true, force: true })
|
||||
}
|
||||
|
||||
function formatBytes(bytes, decimals = 2) {
|
||||
|
@ -42,6 +42,7 @@ app.get("/getLink", async (req, res) => {
|
|||
const url = req.query.url
|
||||
const format = req.query.format
|
||||
const quality = req.query.quality
|
||||
const redir = req.query.redirect
|
||||
|
||||
if (!ytdl.validateURL(url)) {
|
||||
res.header("Content-Type", "text/html")
|
||||
|
@ -50,20 +51,26 @@ app.get("/getLink", async (req, res) => {
|
|||
return
|
||||
}
|
||||
|
||||
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')
|
||||
|
||||
var vidinfo = await ytdl.getBasicInfo(url)
|
||||
|
||||
res.write(`<p>Starting download...</p>`)
|
||||
|
||||
if (redir != "redirect") {
|
||||
res.write(`<p>Starting download...</p>`)
|
||||
}
|
||||
var lastp = 0
|
||||
var ytvid = ytdl(url, { 'quality': quality, 'format': 'mp4' })
|
||||
|
||||
.on("progress", (data, ctotal, etotal) => {
|
||||
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) {
|
||||
|
@ -78,28 +85,46 @@ app.get("/getLink", async (req, res) => {
|
|||
lastp = percent
|
||||
}
|
||||
})
|
||||
.pipe(fs.createWriteStream(ytpath))
|
||||
}
|
||||
ytvid.pipe(fs.createWriteStream(ytpath))
|
||||
.on("close", () => {
|
||||
res.write("<p>Downloaded!</p>")
|
||||
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)
|
||||
res.write(`<p>Starting Video Conversion... Conversion may be slow depending on instance.</p>`)
|
||||
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)=>{
|
||||
res.write(`<p>Converted! (<a href="https://github.com/damianociarla/node-ffmpeg/issues/33">Why doesn't conversion support %s?</a>)</p>`)
|
||||
res.write(`
|
||||
.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>`)
|
||||
setTimeout(()=>{
|
||||
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}`)
|
||||
}
|
||||
setTimeout(() => {
|
||||
if (fs.existsSync(ytpath)) {
|
||||
fs.unlinkSync(ytpath)
|
||||
}
|
||||
|
@ -121,19 +146,19 @@ app.get("/getLink", async (req, res) => {
|
|||
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(`
|
||||
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) {
|
||||
if (percent < 100 && redir != "redirect") {
|
||||
setTimeout(() => {
|
||||
update()
|
||||
}, 2000);
|
||||
|
@ -154,7 +179,7 @@ app.get('/download', (req, res) => {
|
|||
} else {
|
||||
res.header("Content-Type", "text/html")
|
||||
res.write(`<link rel="stylesheet" href="/style.css">`)
|
||||
res.write(`<p>Uh oh! It seems that video couldn't be found... Maybe the URL expired? Or, the link was invalid. <br>If you believe this was a mistake, please put a issue on <a href="https://github.com/Violets-puragtory/NoJS-YTConverter/">Github</a></p>`, ()=>{
|
||||
res.write(`<p>Uh oh! It seems that video couldn't be found... Maybe the URL expired? Or, the link was invalid. <br>If you believe this was a mistake, please put a issue on <a href="https://github.com/Violets-puragtory/NoJS-YTConverter/">Github</a></p>`, () => {
|
||||
res.end()
|
||||
})
|
||||
}
|
||||
|
|
|
@ -40,11 +40,19 @@
|
|||
<option value="ogg">.ogg</option>
|
||||
<option value="wav">.wav</option>
|
||||
</select>
|
||||
<p>Redirect:</p>
|
||||
<select required id="redirect" name="redirect">
|
||||
<option value="false">Don't redirect</option>
|
||||
<option value="redirectjs">Redirect (JS) *1</option>
|
||||
<option value="redirect">Redirect (disable progress) *2</option>
|
||||
</select>
|
||||
<br>
|
||||
<input type="submit" placeholder="test">
|
||||
</form>
|
||||
<hr>
|
||||
<p>Videos over a certain size may fail to download!</p>
|
||||
<p>Currently video quality may not be too high, due to formats above 720p having seperated audio tracks. This is being worked on!</p>
|
||||
<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>*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>
|
||||
<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>
|
||||
|
||||
|
|
Loading…
Reference in a new issue