HD & search fixes! Improved Chromium experience.

This commit is contained in:
Bingus_Violet 2023-11-27 18:32:33 +00:00
parent 94824f020a
commit 540d02af2f

137
index.js
View file

@ -61,6 +61,53 @@ app.listen(PORT, () => {
console.log("Simpletube is now listening on port: " + PORT)
})
function resultHTML(result) {
console.log(result)
function thumbnailCheck() {
if (result.thumbnails) {
return result.thumbnails[0].url
} else {
return result.thumbnail
}
}
function publishCheck() {
if (result.published) {
return result.published.text
} else {
return result.uploaded
}
}
return `
<div class="col-xxl-4 col-sm-6 resultContainer">
<div class="videoResult container-fluid row">
<div class="col-lg-6 thumbparent">
<a class="videoLink" href="/watch?v=${result.id}">
<img class="thumbnail" src="${thumbnailCheck()}">
<p style="display: block; text-align: left;">${result.duration.pretty || result.durationString}</p>
</a>
</div>
<div class="col-lg-6">
<a class="videoLink" href="/watch?v=${result.id}">
<p style="font-size: 1.25rem;">${result.title || "No Title Found"}</p>
<p class="resultDescription">${(result.description || result.views.text).substring(0, 125) || "No Description"}</p>
<p style="display: block;">${publishCheck()}</p>
</a>
</div>
<div style="display: inline-block; width: 100%;">
<a style="color: white; margin: 10px; display: inline-block;" href="/channel?q=${result.channel.id}">
<img src="${result.channel.thumbnail}" class="minipfp">
${result.channel.name}
</a>
</div>
</div>
</div>
`
}
function msth(ms) {
var x = ms / 1000
var seconds = Math.floor(x % 60)
@ -117,13 +164,16 @@ async function cacher(id, quality, ready) {
ready(vidpath, fs.readFileSync(vidpath))
}
var percent = Math.round(ct / et * 100)
if (!debounce) {
videoCache[id + quality]["download%"] = percent
}
if (!debounce && percent > dp && id in videoCache && "path" in videoCache[id]) {
dp = percent
}
})
.on("finish", () => {
if (id in videoCache) {
videoCache[id]["downloaded"] = true
if ((id + quality) in videoCache) {
videoCache[id + quality]["downloaded"] = true
}
})
video.pipe(fs.createWriteStream(vidpath))
@ -176,6 +226,8 @@ async function cacher(id, quality, ready) {
debounce = true
start(et)
}
videoCache[id + quality]["download%"] = Math.round(ct / et * 100)
})
video.pipe(ffmpegProcess.stdio[3])
@ -243,31 +295,7 @@ app.get("/search", async (req, res) => {
for (let index = 0; index < videos.length; index++) {
const result = videos[index];
addedHTML += `
<div class="col-xxl-4 col-sm-6 resultContainer">
<div class="videoResult container-fluid row">
<div class="col-lg-6 thumbparent">
<a class="videoLink" href="/watch?v=${result.id}">
<img class="thumbnail" src="${result.thumbnail}">
<p style="display: block; text-align: left;">${result.durationString}</p>
</a>
</div>
<div class="col-lg-6">
<a class="videoLink" href="/watch?v=${result.id}">
<p style="font-size: 1.25rem;">${result.title || "No Title Found"}</p>
<p class="resultDescription">${result.description.substring(0, 125) + "..." || "No Description"}</p>
</a>
</div>
<div style="display: inline-block; width: 100%;">
<a style="color: white; margin: 10px; display: inline-block;" href="/channel?q=${result.channel.id}">
<img src="${result.channel.thumbnail}" class="minipfp">
${result.channel.name}
</a>
</div>
</div>
</div>
`
addedHTML += resultHTML(result)
}
}
if (addedHTML == "") {
@ -314,24 +342,7 @@ app.get("/channel", async (req, res) => {
for (let index = 0; index < videos.length; index++) {
const result = videos[index];
if (result.title != undefined) {
addedHTML += `
<div class="col-xxl-4 col-sm-6 resultContainer">
<div class="videoResult container-fluid row">
<div class="col-lg-6 thumbparent">
<a class="videoLink" href="/watch?v=${result.id}">
<img class="thumbnail" src="${result.thumbnails[0].url}">
<p style="display: block; text-align: left;">${result.duration.pretty}</p>
</a>
</div>
<div class="col-lg-6">
<a class="videoLink" href="/watch?v=${result.id}">
<p style="font-size: 1.25rem;">${result.title || "No Title Found"}</p>
<p style="display: block;">${result.published.text}</p>
</a>
</div>
</div>
</div>
`
addedHTML += resultHTML(result)
} else {
addedHTML = "<p>Failed to load.</p>"
}
@ -389,7 +400,9 @@ app.get("/video", async (req, res) => {
if (range) {
function ready(vidpath) {
if (fs.existsSync(vidpath)) {
const fileSize = videoCache[id].size
// const fileSize = videoCache[id].size
const fileSize = fs.statSync(vidpath).size
console.log(fileSize, vidpath)
const parts = range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0], 10)
const end = parts[1]
@ -411,9 +424,7 @@ app.get("/video", async (req, res) => {
res.writeHead(206, head)
if (fs.existsSync(vidpath)) {
fs.createReadStream(vidpath, { start: start }).pipe(res)
}
fs.createReadStream(vidpath, { start: start, end: end }).pipe(res)
}
}
@ -424,7 +435,7 @@ app.get("/video", async (req, res) => {
videoCache[id].lastUsed = Date.now()
}
} else {
videoCache[id] = []
videoCache[id + quality] = []
cacher(id, quality, ready)
}
@ -456,7 +467,7 @@ app.get("/video", async (req, res) => {
app.get("/watch", async (req, res) => {
var id = req.query.q || req.query.v || "ubFq-wV3Eic"
var quality = req.query.quality || "sd"
var quality = req.query.quality || "hd"
res.setHeader("Content-Type", "text/html")
@ -482,8 +493,6 @@ app.get("/watch", async (req, res) => {
}
var html = fs.readFileSync(playerPath).toString()
if (quality == "sd") {
@ -508,14 +517,14 @@ app.get("/watch", async (req, res) => {
}
html = html.replace("{VIDEO_DESCRIPTION}", vidInfo.description || "No Description.")
if (!(id in videoCache && videoCache[id]["downloaded"] == true)) {
console.log(videoCache)
if (!((id + quality) in videoCache && videoCache[id + quality].downloaded == true)) {
html = html.replace("{CACHE_WARNING}", `
<p style="color: lightgray">Please note that this video has not been fully cached, and may have trouble loading!
<br>{DOWNLOAD_PERCENT}% cached as of page load. If content fails to load after a minute, reload the page!</p>
`)
if (id in videoCache && "download%" in videoCache[id]) {
html = html.replace("{DOWNLOAD_PERCENT}", videoCache[id]["download%"])
if ((id + quality) in videoCache && "download%" in videoCache[id + quality]) {
html = html.replace("{DOWNLOAD_PERCENT}", videoCache[id + quality]["download%"])
} else {
html = html.replace("{DOWNLOAD_PERCENT}", "0")
}
@ -529,10 +538,10 @@ app.get("/watch", async (req, res) => {
res.send(html)
})
process.on('uncaughtException', (err, origin) => {
fs.writeSync(
process.stderr.fd,
`Caught exception: ${err}\n` +
`Exception origin: ${origin}`,
);
});
// process.on('uncaughtException', (err, origin) => {
// fs.writeSync(
// process.stderr.fd,
// `Caught exception: ${err}\n` +
// `Exception origin: ${origin}`,
// );
// });