Redirect options!

This commit is contained in:
Violet 2023-10-23 23:31:23 -05:00
parent 8dd4dce08b
commit d54639549c
2 changed files with 56 additions and 23 deletions

View file

@ -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
}
if (redir != "redirect") {
res.setHeader("Content-Type", "text/html");
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)
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,18 +85,26 @@ app.get("/getLink", async (req, res) => {
lastp = percent
}
})
.pipe(fs.createWriteStream(ytpath))
}
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>
@ -99,6 +114,16 @@ app.get("/getLink", async (req, res) => {
</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(`
<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)
@ -133,7 +158,7 @@ app.get("/getLink", async (req, res) => {
count += 1
// }
}
if (percent < 100) {
if (percent < 100 && redir != "redirect") {
setTimeout(() => {
update()
}, 2000);

View file

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