summaryrefslogtreecommitdiff
path: root/ytstreamer.go
blob: 5dd94b9bb659051385569ad2cb031bb993bbec7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main

import (
	"log"
	"net/http"
	"os/exec"
	"io"
	"strings"
)

func combine(spl []string) (io.ReadCloser, error) {
	cmd := exec.Command("ffmpeg", "-i", spl[0], "-i", spl[1], "-c:v", "copy", "-c:a", "copy", "-f", "matroska", "-")
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return nil, err
	}
	if err := cmd.Start(); err != nil {
		return nil, err
	}
	return stdout, nil
}

func stream(w http.ResponseWriter, r *http.Request) {
	values := r.URL.Query()
	if !values.Has("yturl") {
		w.WriteHeader(http.StatusBadRequest)
		return
	}
	ytUrl := values.Get("yturl")
	cmd := exec.Command("yt-dlp", "-g", ytUrl)
	burls, err := cmd.Output()
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Println(err)
		return
	}
	urls := string(burls)
	urls = strings.Trim(urls, "\n")
	log.Println(urls)
	spl := strings.Split(urls, "\n")
	log.Printf("spl: %d", len(spl))
	switch len(spl) {
	case 1:
		w.WriteHeader(http.StatusNoContent)
		return
	case 2:
		stdout, err := combine(spl)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			log.Println(err)
			return
		}
		w.Header().Set("Content-Type", "video/mp4")
		w.Header().Set("Connection", "keep-alive")
		w.Header().Set("Transfer-Encoding", "chunked")
		_, err = io.Copy(w, stdout)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			log.Println(err)
			return
		}
	default:
			w.WriteHeader(http.StatusBadRequest)
			log.Println(err)
			return
	}
}

func main() {
	http.HandleFunc("/", stream)

	log.Fatal(http.ListenAndServe(":8080", nil))
}