-
Notifications
You must be signed in to change notification settings - Fork 20
/
m3u8.go
134 lines (120 loc) · 3.29 KB
/
m3u8.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package service
import (
"bytes"
"fmt"
"github.com/grafov/m3u8"
"net/url"
"path"
"strings"
"time"
"github.com/snowie2000/livetv/global"
"github.com/snowie2000/livetv/util"
)
var startUp int64 = 0
func PlaceHolderHLS() string {
// t := (time.Now().Unix() - startUp) / 60
baseUrl, _ := global.GetConfig("base_url")
if !strings.HasSuffix(baseUrl, "/") {
baseUrl += "/"
}
placeholder := baseUrl + "placeholder.ts"
tpl := `#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:1
#EXT-X-TARGETDURATION:30
#EXT-X-DISCONTINUITY:0
#EXTINF:30.000000,
%s?t=1
#EXTINF:30.000000,
%s?t=2
#EXTINF:30.000000,
%s?t=3
#EXT-X-ENDLIST
`
return fmt.Sprintf(tpl, placeholder, placeholder, placeholder)
}
func cleanUrl(Url string) string {
parsedURL, err := url.Parse(Url)
if err != nil {
return Url
}
// Resolve the path using path resolution
parsedURL.Path = path.Clean(parsedURL.Path) // Remove trailing segments
// Get the final clean URL as a string
cleanURL := parsedURL.String()
return cleanURL
}
func processMediaPlaylist(playlistUrl string, pl *m3u8.MediaPlaylist, prefixURL string, proxyToken string, proxy bool, fnTransform func(raw string, ts string) string) string {
baseUrl := global.GetBaseURL(playlistUrl)
handleUri := func(uri string) string {
if uri == "" {
return uri
}
if !global.IsValidURL(uri) {
uri = cleanUrl(global.MergeUrl(baseUrl, uri))
}
if proxy {
tsLink := global.MergeUrl(prefixURL, "/live.ts?token="+proxyToken+"&k="+util.CompressString(uri))
if fnTransform != nil {
tsLink = fnTransform(uri, tsLink)
}
uri = tsLink
}
return uri
}
var i uint = 0
if pl.Count() >= pl.WinSize() {
i = pl.Count() - pl.WinSize()
}
for ; i < pl.Count(); i++ {
pl.Segments[i].URI = handleUri(pl.Segments[i].URI)
}
// remove unused segments
for pl.Count() > pl.WinSize() {
pl.Remove()
}
return pl.Encode().String()
}
func processMasterPlaylist(playlistUrl string, pl *m3u8.MasterPlaylist, prefixURL string, proxyToken string, proxy bool, fnTransform func(raw string, ts string) string) string {
baseUrl := global.GetBaseURL(playlistUrl)
handleUri := func(uri string) string {
if uri == "" {
return uri
}
if !global.IsValidURL(uri) {
uri = cleanUrl(global.MergeUrl(baseUrl, uri))
}
if proxy {
plLink := global.MergeUrl(prefixURL, "/playlist.m3u8?token="+proxyToken+"&k="+util.CompressString(uri))
if fnTransform != nil {
plLink = fnTransform(uri, plLink)
}
uri = plLink
}
return uri
}
for _, v := range pl.Variants {
v.URI = handleUri(v.URI)
if v.VariantParams.Alternatives != nil && len(v.VariantParams.Alternatives) > 0 {
for _, alter := range v.VariantParams.Alternatives {
alter.URI = handleUri(alter.URI)
}
}
}
return pl.Encode().String()
}
func M3U8Process(playlistUrl string, data string, prefixURL string, proxyToken string, proxy bool, fnTransform func(raw string, ts string) string) string {
p, listType, err := m3u8.DecodeFrom(bytes.NewBufferString(data), true)
if err == nil {
switch listType {
case m3u8.MASTER:
return processMasterPlaylist(playlistUrl, p.(*m3u8.MasterPlaylist), prefixURL, proxyToken, proxy, fnTransform)
case m3u8.MEDIA:
return processMediaPlaylist(playlistUrl, p.(*m3u8.MediaPlaylist), prefixURL, proxyToken, proxy, fnTransform)
}
}
return ""
}
func init() {
startUp = time.Now().Unix()
}