From b6de202102ee5bda5084157ab53a3d349a023084 Mon Sep 17 00:00:00 2001 From: Bingus_Violet Date: Sun, 19 Nov 2023 23:52:48 -0600 Subject: [PATCH] blharg --- index.js | 71 ++++++++++++++++++++++++++++++++++++++++--- resources/player.html | 1 + 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 3441934..15d6829 100644 --- a/index.js +++ b/index.js @@ -46,6 +46,37 @@ app.listen(PORT, () => { console.log("Simpletube is now listening on port: " + PORT) }) +function msth(ms) { + var x = ms / 1000 + var seconds = Math.floor(x % 60) + x /= 60 + var minutes = Math.floor(x % 60) + x /= 60 + var hours = Math.floor(x) + + var fs = "" + + if (hours >= 1) { + fs += hours + if (hours == 1) { + fs += " hour " + } else { + fs += " hours " + } + } + if (minutes >= 1) { + fs += minutes + if (minutes == 1) { + fs += " minute " + } else { + fs += " minutes " + } + } + fs += ` ${seconds} seconds.` + + return fs.trim() +} + async function cacher(id, quality, ready) { vidpath = path.join(cachePath, `${id + quality}.mp4`) var debounce = true @@ -73,7 +104,6 @@ async function cacher(id, quality, ready) { var percent = Math.round(ct / et * 100) if (!debounce && percent > dp && id in videoCache && "path" in videoCache[id]) { dp = percent - videoCache[id]["download%"] = dp } }) .on("finish", () => { @@ -98,6 +128,7 @@ async function cacher(id, quality, ready) { '-movflags','frag_keyframe+empty_moov', '-f','mp4', '-loglevel','error', + '-t', vidInfo.videoDetails.lengthSeconds, '-' ], { stdio: [ @@ -105,9 +136,39 @@ async function cacher(id, quality, ready) { ], }); - video.pipe(ffmpegProcess.stdio[3]); - audio.pipe(ffmpegProcess.stdio[4]); + var debounce = false + + function start(et) { + if (fs.existsSync(vidpath)) { + videoCache[id + quality] = { + "path": vidpath, + "size": et * 2, + "downloaded": true, + "download%": 0, + "lastUsed": Date.now(), + "duration": (vidInfo.videoDetails.lengthSeconds + 1) * 1000 + } + ready(vidpath, fs.readFileSync(vidpath)) + } else { + setTimeout(() => { + start(et) + }, 1000); + } + } + + video.on("progress", (chunk, ct, et) => { + if (!debounce) { + debounce = true + start(et) + } + }) + + video.pipe(ffmpegProcess.stdio[3]) + audio.pipe(ffmpegProcess.stdio[4]) + + ffmpegProcess.stdio[1].pipe(fs.createWriteStream(vidpath)) + .on("finish", () => { videoCache[id + quality] = { "path": vidpath, @@ -117,8 +178,6 @@ async function cacher(id, quality, ready) { "lastUsed": Date.now(), "duration": (vidInfo.videoDetails.lengthSeconds + 1) * 1000 } - - ready(vidpath, fs.readFileSync(vidpath)) }) } @@ -413,6 +472,8 @@ app.get("/watch", async (req, res) => { html = html.replace("{VIDEO_QUALITY}", quality) + html = html.replace("{DURATION}", msth(vidInfo.lengthSeconds * 1000)) + html = html.replace("{CSS_HEADER}", cssHeader) for (let index = 0; index < 2; index++) { diff --git a/resources/player.html b/resources/player.html index 03e95c2..a69f3ed 100644 --- a/resources/player.html +++ b/resources/player.html @@ -30,6 +30,7 @@
+

Duration: {DURATION}

SD HD