/
main.go
57 lines (44 loc) · 944 Bytes
/
main.go
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
package main
import (
"net/http"
"strconv"
"github.com/grafov/m3u8"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
const m3u8URL = "TARGET_URL"
func main() {
goji.Get("/trim", trimM3u8)
goji.Serve()
}
func trimM3u8(c web.C, w http.ResponseWriter, r *http.Request) {
q := r.URL.Query().Get("sec")
sec, err := strconv.Atoi(q)
if err != nil {
sec = 0
}
resp, err := http.Get(m3u8URL)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("faild to get m3u8 file"))
return
}
defer resp.Body.Close()
p, err := m3u8.NewMediaPlaylist(1024, 1024)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("faild to NewMediaPlaylist"))
return
}
defer p.Close()
err = p.DecodeFrom(resp.Body, true)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("faild to decode m3u8 file"))
return
}
if sec != 0 {
p.Segments = append(p.Segments[(sec / int(p.TargetDuration)):])
}
w.Write(p.Encode().Bytes())
}