forked from Threadfin/Threadfin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm3u.go
More file actions
341 lines (255 loc) · 7.7 KB
/
m3u.go
File metadata and controls
341 lines (255 loc) · 7.7 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package src
import (
"encoding/json"
"fmt"
"math"
"net/url"
"os/exec"
"path"
"regexp"
"sort"
"strconv"
"strings"
m3u "threadfin/src/internal/m3u-parser"
)
// Playlisten parsen
func parsePlaylist(filename, fileType string) (channels []interface{}, err error) {
content, err := readByteFromFile(filename)
var id = strings.TrimSuffix(getFilenameFromPath(filename), path.Ext(getFilenameFromPath(filename)))
var playlistName = getProviderParameter(id, fileType, "name")
if err == nil {
switch fileType {
case "m3u":
channels, err = m3u.MakeInterfaceFromM3U(content)
case "hdhr":
channels, err = makeInteraceFromHDHR(content, playlistName, id)
}
}
return
}
// Streams filtern
func filterThisStream(s interface{}) (status bool, liveEvent bool) {
status = false
var stream = s.(map[string]string)
var regexpYES = `[{]+[^.]+[}]`
var regexpNO = `!+[{]+[^.]+[}]`
liveEvent = false
for _, filter := range Data.Filter {
if filter.Rule == "" {
continue
}
liveEvent = filter.LiveEvent
var group, name, search string
var exclude, include string
var match = false
var streamValues = strings.Replace(stream["_values"], "\r", "", -1)
if v, ok := stream["group-title"]; ok {
group = v
}
if v, ok := stream["name"]; ok {
name = v
}
// Unerwünschte Streams !{DEU}
r := regexp.MustCompile(regexpNO)
val := r.FindStringSubmatch(filter.Rule)
if len(val) == 1 {
exclude = val[0][2 : len(val[0])-1]
filter.Rule = strings.Replace(filter.Rule, " "+val[0], "", -1)
filter.Rule = strings.Replace(filter.Rule, val[0], "", -1)
}
// Muss zusätzlich erfüllt sein {DEU}
r = regexp.MustCompile(regexpYES)
val = r.FindStringSubmatch(filter.Rule)
if len(val) == 1 {
include = val[0][1 : len(val[0])-1]
filter.Rule = strings.Replace(filter.Rule, " "+val[0], "", -1)
filter.Rule = strings.Replace(filter.Rule, val[0], "", -1)
}
switch filter.CaseSensitive {
case false:
streamValues = strings.ToLower(streamValues)
filter.Rule = strings.ToLower(filter.Rule)
exclude = strings.ToLower(exclude)
include = strings.ToLower(include)
group = strings.ToLower(group)
name = strings.ToLower(name)
}
switch filter.Type {
case "group-title":
search = name
if group == filter.Rule {
match = true
}
case "custom-filter":
search = streamValues
if strings.Contains(search, filter.Rule) {
match = true
}
}
if match == true {
if len(exclude) > 0 {
var status = checkConditions(search, exclude, "exclude")
if status == false {
return false, liveEvent
}
}
if len(include) > 0 {
var status = checkConditions(search, include, "include")
if status == false {
return false, liveEvent
}
}
return true, liveEvent
}
}
return false, liveEvent
}
// Bedingungen für den Filter
func checkConditions(streamValues, conditions, coType string) (status bool) {
switch coType {
case "exclude":
status = true
case "include":
status = false
}
conditions = strings.Replace(conditions, ", ", ",", -1)
conditions = strings.Replace(conditions, " ,", ",", -1)
var keys = strings.Split(conditions, ",")
for _, key := range keys {
if strings.Contains(streamValues, key) {
switch coType {
case "exclude":
return false
case "include":
return true
}
}
}
return
}
// Threadfin M3U Datei erstellen
func buildM3U(groups []string) (m3u string, err error) {
var imgc = Data.Cache.Images
var m3uChannels = make(map[float64]XEPGChannelStruct)
var channelNumbers []float64
for _, dxc := range Data.XEPG.Channels {
var xepgChannel XEPGChannelStruct
err := json.Unmarshal([]byte(mapToJSON(dxc)), &xepgChannel)
if err == nil {
var channelNumber, err = strconv.ParseFloat(strings.TrimSpace(xepgChannel.XChannelID), 64)
if xepgChannel.TvgName == "" {
xepgChannel.TvgName = xepgChannel.Name
}
if xepgChannel.XActive && !xepgChannel.XHideChannel {
if len(groups) > 0 {
if indexOfString(xepgChannel.XGroupTitle, groups) == -1 {
goto Done
}
}
if err == nil {
m3uChannels[channelNumber] = xepgChannel
channelNumbers = append(channelNumbers, channelNumber)
}
}
}
Done:
}
// M3U Inhalt erstellen
sort.Float64s(channelNumbers)
var xmltvURL = fmt.Sprintf("%s://%s/xmltv/threadfin.xml", System.ServerProtocol.XML, System.Domain)
if Settings.ForceHttps && Settings.HttpsThreadfinDomain != "" {
xmltvURL = fmt.Sprintf("https://%s/xmltv/threadfin.xml", Settings.HttpsThreadfinDomain)
}
m3u = fmt.Sprintf(`#EXTM3U url-tvg="%s" x-tvg-url="%s"`+"\n", xmltvURL, xmltvURL)
for _, channelNumber := range channelNumbers {
var channel = m3uChannels[channelNumber]
group := channel.XGroupTitle
if channel.XCategory != "" {
group = channel.XCategory
}
if Settings.ForceHttps && Settings.HttpsThreadfinDomain != "" {
u, err := url.Parse(channel.URL)
if err == nil {
u.Scheme = "https"
host_split := strings.Split(u.Host, ":")
if len(host_split) > 0 {
u.Host = host_split[0]
}
if u.RawQuery != "" {
channel.URL = fmt.Sprintf("https://%s:%d%s?%s", u.Host, Settings.HttpsPort, u.Path, u.RawQuery)
} else {
channel.URL = fmt.Sprintf("https://%s:%d%s", u.Host, Settings.HttpsPort, u.Path)
}
}
}
logo := ""
if channel.TvgLogo != "" {
logo = imgc.Image.GetURL(channel.TvgLogo, Settings.HttpThreadfinDomain, Settings.Port, Settings.ForceHttps, Settings.HttpsPort, Settings.HttpsThreadfinDomain)
}
var parameter = fmt.Sprintf(`#EXTINF:0 channelID="%s" tvg-chno="%s" tvg-name="%s" tvg-id="%s" tvg-logo="%s" group-title="%s",%s`+"\n", channel.XEPG, channel.XChannelID, channel.XName, channel.XChannelID, logo, group, channel.XName)
var stream, err = createStreamingURL("M3U", channel.FileM3UID, channel.XChannelID, channel.XName, channel.URL, channel.BackupChannel1, channel.BackupChannel2, channel.BackupChannel3)
if err == nil {
if !strings.Contains(m3u, stream) {
m3u = m3u + parameter + stream + "\n"
}
}
}
if len(groups) == 0 {
var filename = System.Folder.Data + "threadfin.m3u"
err = writeByteToFile(filename, []byte(m3u))
}
return
}
func probeChannel(request RequestStruct) (string, string, string, error) {
ffmpegPath := Settings.FFmpegPath
ffprobePath := strings.Replace(ffmpegPath, "ffmpeg", "ffprobe", 1)
cmd := exec.Command(ffprobePath, "-v", "error", "-show_streams", "-of", "json", request.ProbeURL)
output, err := cmd.Output()
if err != nil {
return "", "", "", fmt.Errorf("failed to execute ffprobe: %v", err)
}
var ffprobeOutput FFProbeOutput
err = json.Unmarshal(output, &ffprobeOutput)
if err != nil {
return "", "", "", fmt.Errorf("failed to parse ffprobe output: %v", err)
}
var resolution, frameRate, audioChannels string
for _, stream := range ffprobeOutput.Streams {
if stream.CodecType == "video" {
resolution = fmt.Sprintf("%dp", stream.Height)
frameRateParts := strings.Split(stream.RFrameRate, "/")
if len(frameRateParts) == 2 {
frameRate = fmt.Sprintf("%d", parseFrameRate(frameRateParts))
} else {
frameRate = stream.RFrameRate
}
}
if stream.CodecType == "audio" {
audioChannels = stream.ChannelLayout
if audioChannels == "" {
switch stream.Channels {
case 1:
audioChannels = "Mono"
case 2:
audioChannels = "Stereo"
case 6:
audioChannels = "5.1"
case 8:
audioChannels = "7.1"
default:
audioChannels = fmt.Sprintf("%d channels", stream.Channels)
}
}
}
}
return resolution, frameRate, audioChannels, nil
}
func parseFrameRate(parts []string) int {
numerator, denom := 1, 1
fmt.Sscanf(parts[0], "%d", &numerator)
fmt.Sscanf(parts[1], "%d", &denom)
if denom == 0 {
return 0
}
return int(math.Round(float64(numerator) / float64(denom)))
}