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 subtitles vtt

Christian Feidb34576094f5
diff --git a/client/components/video-element.js b/client/components/video-element.js
index e002264..02aa242 100644
--- a/client/components/video-element.js
+++ b/client/components/video-element.js
@@ -91,6 +91,11 @@ class VideoElement extends HTMLElement {
event.preventDefault()
this.querySelector('.play-video-placeholder').outerHTML = /*html*/`<video controls width="280">
<source src="/videos/${this.video.id}" type="video/mp4" />
+ <track
+ default
+ kind="captions"
+ srclang="en"
+ src="/captions/${this.video.id}" />
<p>
Your browser does not support the video tag.
Download the video instead <a href="/videos/${this.video.id}" target="_blank">here</a>
diff --git a/lib/server.js b/lib/server.js
index 9e360bb..7dac3a1 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -40,6 +40,8 @@ export function createServer ({repo, connections = []}) {
return ignoreVideoHandler(req, res, repo, connections)
if (url.pathname.match('\/videos\/.*') && req.method === 'GET')
return watchVideoHandler(req, res)
+ if (url.pathname.match('\/captions\/.*') && req.method === 'GET')
+ return captionsHandler(req, res)
/* client */
if (url.pathname === '/main.css')
@@ -137,7 +139,7 @@ export function createServer ({repo, connections = []}) {
}
function watchVideoHandler(req, res) {
- const videoPath = req.url.replace('/videos/', './data/videos/') + '.mp4'
+ const videoPath = './data' + req.url + '.mp4'
if (!fs.existsSync(videoPath)) {
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.end('Video not found')
@@ -200,13 +202,27 @@ export function createServer ({repo, connections = []}) {
fileStream.on("error", error => {
console.log(`Error reading file ${videoPath}.`)
console.log(error)
- res.sendStatus(500)
+ res.writeHead(500)
+ res.end()
})
fileStream.pipe(res)
}
}
+ function captionsHandler(req, res) {
+ // return vtt file of video
+ const captionsPath = './data' + req.url.replace('captions', 'videos') + '.en.vtt'
+ console.log(captionsPath)
+ if (!fs.existsSync(captionsPath)) {
+ res.writeHead(404)
+ res.end()
+ } else {
+ const fileStream = fs.createReadStream(captionsPath)
+ fileStream.pipe(res)
+ }
+ }
+
async function ignoreVideoHandler(req, res, repo, connections = []) {
const body = await getBody(req)
let { id } = JSON.parse(body)
diff --git a/lib/youtube.js b/lib/youtube.js
index e6981e2..7a9734c 100644
--- a/lib/youtube.js
+++ b/lib/youtube.js
@@ -43,7 +43,7 @@ export async function getVideoSubtitles (id, callback = () => {}) {
if (cookies) cookiesOption = `--cookies ./cookies.txt`
console.log({cookiesOption})
if (!fs.existsSync(transcriptPath)) {
- for await (const line of spawn('yt-dlp', `${id} ${cookiesOption} -o ./data/videos/${id} --skip-download --write-subs --write-auto-subs --sub-lang en --sub-format ttml --convert-subs srt`.split(' '))) {
+ for await (const line of spawn('yt-dlp', `${id} ${cookiesOption} -o ./data/videos/${id} --skip-download --write-subs --write-auto-subs --sub-lang en --sub-format vtt --convert-subs srt -k`.split(' '))) {
console.log(line)
callback(line)
}