all repos — fixyoutube-go @ a07abe81886576fd49175b5d58b10eba22d53029

A better way to embed YouTube videos everywhere (inspired by FixTweet).

add captions
Marco Andronaco andronacomarco@gmail.com
Sun, 15 Dec 2024 20:04:15 +0100
commit

a07abe81886576fd49175b5d58b10eba22d53029

parent

df3fa40015c39d0e0a70606293bfec8a7a31fa55

5 files changed, 33 insertions(+), 2 deletions(-)

jump to
M fixyoutube.gofixyoutube.go

@@ -77,6 +77,7 @@ r.HandleFunc("GET /", indexHandler)

r.HandleFunc("GET /watch", watchHandler) r.HandleFunc("GET /shorts/{videoID}", shortHandler) r.HandleFunc("GET /proxy/{videoID}", proxyHandler) + r.HandleFunc("GET /sub/{videoID}/{language}", subHandler) r.HandleFunc("GET /refresh/{videoID}", refreshHandler) r.HandleFunc("GET /cache", cacheHandler) r.HandleFunc("GET /{videoID}", shortHandler)
M go.modgo.mod

@@ -6,7 +6,7 @@ toolchain go1.23.3

require ( github.com/birabittoh/myks v0.0.2 - github.com/birabittoh/rabbitpipe v0.0.7 + github.com/birabittoh/rabbitpipe v0.0.8 github.com/joho/godotenv v1.5.1 github.com/sirupsen/logrus v1.9.3 golang.org/x/time v0.8.0
M go.sumgo.sum

@@ -2,6 +2,8 @@ github.com/birabittoh/myks v0.0.2 h1:EBukMUsAflwiqdNo4LE7o2WQdEvawty5ewCZWY+IXSU=

github.com/birabittoh/myks v0.0.2/go.mod h1:klNWaeUWm7TmhnBHBMt9vALwCHW11/Xw1BpCNkCx7hs= github.com/birabittoh/rabbitpipe v0.0.7 h1:QSp/2DZSytCDeWf/o8dFSMkxjuZGwiQid7v/yjpfA00= github.com/birabittoh/rabbitpipe v0.0.7/go.mod h1:6cEDb0WpwrRm2vt5IO3s2gPjzhZZLP7gYx+l9e3gx1k= +github.com/birabittoh/rabbitpipe v0.0.8 h1:rBcAUBFLxPp5FXp4N5H/CscGg71anBz1IkUnWsxgfCk= +github.com/birabittoh/rabbitpipe v0.0.8/go.mod h1:6cEDb0WpwrRm2vt5IO3s2gPjzhZZLP7gYx+l9e3gx1k= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
M handlers.gohandlers.go

@@ -32,6 +32,10 @@ adminUser string

adminPass string ) +func defaultError(w http.ResponseWriter, code int) { + http.Error(w, http.StatusText(code), code) +} + func parseFormat(f rabbitpipe.Format) (res string) { isAudio := f.AudioChannels > 0

@@ -73,7 +77,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {

err := indexTemplate.Execute(w, nil) if err != nil { logger.Error("Failed to fill index template.") - http.Error(w, err.Error(), http.StatusInternalServerError) + defaultError(w, http.StatusInternalServerError) return } }

@@ -146,6 +150,27 @@ h.Set("Status", "200")

h.Set("Content-Type", "video/mp4") h.Set("Content-Length", strconv.FormatInt(vb.Length, 10)) io.Copy(w, vb.Buffer) +} + +func subHandler(w http.ResponseWriter, r *http.Request) { + videoID := r.PathValue("videoID") + language := r.PathValue("language") + + captions, err := invidious.RP.GetCaptions(videoID, language) + if err != nil { + logger.Error("Failed to get captions: ", err) + defaultError(w, http.StatusNotFound) + return + } + + w.Header().Set("Content-Type", "text/vtt") + w.Header().Set("Content-Length", strconv.Itoa(len(captions))) + + _, err = w.Write(captions) + if err != nil { + defaultError(w, http.StatusInternalServerError) + return + } } func downloadHandler(w http.ResponseWriter, r *http.Request) {
M templates/video.htmltemplates/video.html

@@ -42,6 +42,9 @@ <main class="container" style="max-width: 35rem">

{{ if gt (len .FormatStreams) 0 }} <video style="width: 100%" autoplay controls> <source src="{{ (index .FormatStreams 0).URL }}" type="video/mp4" /> + {{ range .Captions }} + <track kind="subtitles" label="{{ .Label }}" src="/sub/{{ $.VideoID }}/{{ .Label }}" srclang="{{ .LanguageCode }}" /> + {{ end }} </video> {{ end }} <h2>{{ .Title }}</h2>