Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

wip: cleanup, try fix some videos on ios

Christian Feif9b51185633c
diff --git a/client/components/video-element.js b/client/components/video-element.js
index 46b0665..fd09f1e 100644
--- a/client/components/video-element.js
+++ b/client/components/video-element.js
@@ -89,8 +89,9 @@ class VideoElement extends HTMLElement {
}
watchVideoHandler (event) {
event.preventDefault()
- this.querySelector('.play.video-placeholder').outerHTML = /*html*/`<video controls width="400">
- <source src="/videos/${this.video.id}" type="video/${this.video.format}" />
+ this.querySelector('.play.video-placeholder').outerHTML = /*html*/`
+ <video controls playsinline style="user-select: none; width: 400px; width: -webkit-fill-available;">
+ <source src="/videos/${this.video.id}.${this.video.format || 'mp4'}" type="video/${this.video.format || 'mp4'}" />
<track default kind="captions" srclang="en" src="/captions/${this.video.id}" />
<p>
Your browser does not support the video tag.
diff --git a/lib/server.js b/lib/server.js
index 6ed4506..9c96490 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -235,7 +235,7 @@ async function deleteVideoHandler(req, res, repo, connections = []) {
}
function watchVideoHandler(req, res, repo) {
- const id = req.url.replace('/videos/', '')
+ const id = req.url.replace('/videos/', '').replace(/\.(webm|mp4)$/, '')
const video = repo.getVideo(id)
const location = video.location || `./data/videos/${id}.mp4`
const contentType = video.format ? `video/${video.format}` : "video/mp4"
@@ -245,30 +245,33 @@ function watchVideoHandler(req, res, repo) {
return
}
- /*https://github.com/bootstrapping-microservices/video-streaming-example/blob/master/index.js*/
- const options = {}
+ // https://github.com/bootstrapping-microservices/video-streaming-example/blob/master/index.js
+ // https://blog.logrocket.com/streaming-video-in-safari/
- let start
- let end
+ const options = {};
- const range = req.headers.range
+ let start;
+ let end;
+
+ const range = req.headers.range;
if (range) {
- const bytesPrefix = "bytes="
+ const bytesPrefix = "bytes=";
if (range.startsWith(bytesPrefix)) {
- const bytesRange = range.substring(bytesPrefix.length)
- const parts = bytesRange.split("-")
+ const bytesRange = range.substring(bytesPrefix.length);
+ const parts = bytesRange.split("-");
if (parts.length === 2) {
- const rangeStart = parts[0] && parts[0].trim()
+ const rangeStart = parts[0] && parts[0].trim();
if (rangeStart && rangeStart.length > 0) {
- options.start = start = parseInt(rangeStart)
+ options.start = start = parseInt(rangeStart);
}
- const rangeEnd = parts[1] && parts[1].trim()
+ const rangeEnd = parts[1] && parts[1].trim();
if (rangeEnd && rangeEnd.length > 0) {
- options.end = end = parseInt(rangeEnd)
+ options.end = end = parseInt(rangeEnd);
}
}
}
}
+
res.setHeader("content-type", contentType)
const stat = fs.statSync(location)
@@ -279,34 +282,32 @@ function watchVideoHandler(req, res, repo) {
res.statusCode = 200
res.setHeader("accept-ranges", "bytes")
res.setHeader("content-length", contentLength)
- res.end()
+ return res.end()
+ }
+ let retrievedLength = contentLength;
+ if (start !== undefined && end !== undefined) {
+ retrievedLength = end - start + 1;
+ } else if (start !== undefined) {
+ retrievedLength = contentLength - start;
}
- else {
- let retrievedLength = contentLength;
- if (start !== undefined && end !== undefined) {
- retrievedLength = end - start + 1;
- } else if (start !== undefined) {
- retrievedLength = contentLength - start;
- }
-
- res.statusCode = start !== undefined || end !== undefined ? 206 : 200
-
- res.setHeader("content-length", retrievedLength)
- if (range !== undefined) {
- res.setHeader("content-range", `bytes ${start || 0}-${end || (contentLength-1)}/${contentLength}`)
- res.setHeader("accept-ranges", "bytes")
- }
+ res.statusCode = (start !== undefined || end !== undefined) ? 206 : 200
- const fileStream = fs.createReadStream(location, options)
- fileStream.on("error", error => {
- console.error(`Error reading file ${location}.`, error)
- res.writeHead(500)
- res.end()
- })
-
- fileStream.pipe(res)
+ res.setHeader("content-length", retrievedLength)
+
+ if (range !== undefined) {
+ res.setHeader("accept-ranges", "bytes")
+ res.setHeader("content-range", `bytes ${start || 0}-${end || (contentLength-1)}/${contentLength}`)
}
+
+ const fileStream = fs.createReadStream(location, options)
+ fileStream.on("error", error => {
+ console.error(`Error reading file ${location}.`, error)
+ res.writeHead(500)
+ res.end()
+ })
+
+ fileStream.pipe(res)
}
export function searchVideosHandler(req, res, repo) {