This commit is contained in:
Bingus_Violet 2023-11-19 23:52:48 -06:00
parent 85da7352d1
commit b6de202102
2 changed files with 67 additions and 5 deletions

View file

@ -46,6 +46,37 @@ app.listen(PORT, () => {
console.log("Simpletube is now listening on port: " + 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) { async function cacher(id, quality, ready) {
vidpath = path.join(cachePath, `${id + quality}.mp4`) vidpath = path.join(cachePath, `${id + quality}.mp4`)
var debounce = true var debounce = true
@ -73,7 +104,6 @@ async function cacher(id, quality, ready) {
var percent = Math.round(ct / et * 100) var percent = Math.round(ct / et * 100)
if (!debounce && percent > dp && id in videoCache && "path" in videoCache[id]) { if (!debounce && percent > dp && id in videoCache && "path" in videoCache[id]) {
dp = percent dp = percent
videoCache[id]["download%"] = dp
} }
}) })
.on("finish", () => { .on("finish", () => {
@ -98,6 +128,7 @@ async function cacher(id, quality, ready) {
'-movflags','frag_keyframe+empty_moov', '-movflags','frag_keyframe+empty_moov',
'-f','mp4', '-f','mp4',
'-loglevel','error', '-loglevel','error',
'-t', vidInfo.videoDetails.lengthSeconds,
'-' '-'
], { ], {
stdio: [ stdio: [
@ -105,9 +136,39 @@ async function cacher(id, quality, ready) {
], ],
}); });
video.pipe(ffmpegProcess.stdio[3]); var debounce = false
audio.pipe(ffmpegProcess.stdio[4]);
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)) ffmpegProcess.stdio[1].pipe(fs.createWriteStream(vidpath))
.on("finish", () => { .on("finish", () => {
videoCache[id + quality] = { videoCache[id + quality] = {
"path": vidpath, "path": vidpath,
@ -117,8 +178,6 @@ async function cacher(id, quality, ready) {
"lastUsed": Date.now(), "lastUsed": Date.now(),
"duration": (vidInfo.videoDetails.lengthSeconds + 1) * 1000 "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("{VIDEO_QUALITY}", quality)
html = html.replace("{DURATION}", msth(vidInfo.lengthSeconds * 1000))
html = html.replace("{CSS_HEADER}", cssHeader) html = html.replace("{CSS_HEADER}", cssHeader)
for (let index = 0; index < 2; index++) { for (let index = 0; index < 2; index++) {

View file

@ -30,6 +30,7 @@
</video> </video>
</div> </div>
<hr> <hr>
<p>Duration: {DURATION}</p>
<a href="?quality=sd&q={VIDEOID}">SD</a> <a href="?quality=sd&q={VIDEOID}">SD</a>
<a href="?quality=hd&q={VIDEOID}">HD</a> <a href="?quality=hd&q={VIDEOID}">HD</a>
<main> <main>