-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm3u8.go
190 lines (158 loc) · 2.92 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package parsem3u8
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
var linePattern = regexp.MustCompile(`([a-zA-Z-]+)=("[^"]+"|[^",]+)`)
type MediaType uint
const (
Stream = iota + 1
Vod
)
var videoTypes = []string{
"3g2",
"3gp",
"aaf",
"asf",
"avchd",
"avi",
"drc",
"flv",
"m2v",
"m4p",
"m4v",
"mkv",
"mng",
"mov",
"mp2",
"mp4",
"mpe",
"mpeg",
"mpg",
"mpv",
"mxf",
"nsv",
"ogg",
"ogv",
"qt",
"rm",
"rmvb",
"roq",
"svi",
"vob",
"webm",
"wmv",
"yuv",
}
type Segments []Segment
type Segment struct {
Name string `json:"name"`
Duration float64 `json:"duration"`
Ctags customTags `json:"tags"`
URL string `json:"url"`
Type MediaType `json:"-"`
}
type customTags []customTag
type customTag struct {
Key string `json:"key"`
Value string `json:"value"`
}
func (c customTags) Get(key string) string {
for _, tag := range c {
if tag.Key == strings.ToLower(key) {
return tag.Value
}
}
return ""
}
func (c *customTags) Set(key, value string) error {
for _, tag := range *c {
if tag.Key == strings.ToLower(key) {
tag.Value = value
return nil
}
}
return errors.New("no such key")
}
func ParseFile(filepath string) Segments {
f, err := os.OpenFile(filepath, os.O_RDONLY, 0644)
if err != nil {
log.Fatal(err)
}
return Parse(f)
}
func Parse(reader io.Reader) Segments {
var (
extInf bool
segments Segments
segment Segment
lines []string
)
s := bufio.NewScanner(reader)
for s.Scan() {
lines = append(lines, s.Text())
}
for i := 0; i < len(lines); i++ {
line := strings.TrimSpace(lines[i])
if i == 0 && line != "#EXTM3U" {
log.Panic("not a proper m3u file.")
}
switch {
case line == "":
continue
case strings.HasPrefix(line, "#EXTINF"):
var duration string
if extInf {
log.Fatalf("duplicate EXTINF: %s, line: %d", line, i+1)
}
_, err := fmt.Sscanf(line, "#EXTINF:%s", &duration)
if err != nil {
log.Fatal(err)
}
segment.Duration, err = strconv.ParseFloat(duration, 64)
if err != nil {
log.Fatal(err)
}
splitName := strings.Split(line, ",")
segment.Name = splitName[len(splitName)-1]
segment.Ctags = parseLineParameters(line)
extInf = true
case !strings.HasPrefix(line, "#"):
segment.URL = line
extInf = false
lineType := getType(line)
segment.Type = lineType
segments = append(segments, segment)
}
}
return segments
}
func parseLineParameters(line string) customTags {
r := linePattern.FindAllStringSubmatch(line, -1)
tags := make(customTags, 0)
for _, arr := range r {
tag := customTag{
Key: strings.ToLower(arr[1]),
Value: strings.Trim(arr[2], "\""),
}
tags = append(tags, tag)
}
return tags
}
func getType(text string) MediaType {
ext := strings.Trim(filepath.Ext(text), ".")
for i := 0; i < len(videoTypes); i++ {
if ext == videoTypes[i] {
return Vod
}
}
return Stream
}