From a205aae0575b92c28f71ff99270a67b286196384 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Thu, 12 Sep 2024 17:34:20 +0200 Subject: Support low quality (format 22 or 18, i.e. video+audio) streaming --- grimtube.go | 24 ++++++++++++++++++++++++ templates/search.html | 4 ++-- templates/syncstream.html | 6 ++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 templates/syncstream.html diff --git a/grimtube.go b/grimtube.go index 9f8061b..1cfccc0 100644 --- a/grimtube.go +++ b/grimtube.go @@ -9,6 +9,8 @@ import ( "log" "strconv" "path" + "os/exec" + "strings" "git.sr.ht/~ark/ytparser" ) @@ -46,6 +48,27 @@ func serve(w http.ResponseWriter, templatePath string, data interface{}) { } } +func stream(w http.ResponseWriter, r *http.Request) { + values := r.URL.Query() + if !values.Has("url") { + w.WriteHeader(http.StatusBadRequest) + return + } + out, err := exec.Command("yt-dlp", "--print", "url", "-f", "22,18", values.Get("url")).Output() + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + sout := string(out) + data := struct { + Url string + }{ + strings.Trim(sout, "\n"), + } + serve(w, "templates/syncstream.html", data) +} + func index(w http.ResponseWriter, r *http.Request) { term := "" page := 0 @@ -180,6 +203,7 @@ func main() { http.HandleFunc("/", index) http.HandleFunc("/search", search) http.HandleFunc("/embed", embed) + http.HandleFunc("/stream", loadurls) log.Printf("Listening on port %d\n", *port) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) diff --git a/templates/search.html b/templates/search.html index 6a9ce28..70c3490 100644 --- a/templates/search.html +++ b/templates/search.html @@ -26,10 +26,10 @@ {{range .Items}} - + - {{.Title}} + {{.Title}}
{{.ChannelTitle}} | Atom feed
diff --git a/templates/syncstream.html b/templates/syncstream.html new file mode 100644 index 0000000..6a8ec1d --- /dev/null +++ b/templates/syncstream.html @@ -0,0 +1,6 @@ +{{define "title"}}Search{{end}} +{{define "content"}} + +{{end}} -- cgit v1.2.3