-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpdGrabber.go
381 lines (329 loc) · 8.47 KB
/
mpdGrabber.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package mpdgrabber
import (
"fmt"
"io"
"log"
"mime"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/mattetti/go-dash/mpd"
)
var Debug = false
var Logger = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
const UnknownString = "unknown"
type ContentType int
const (
ContentTypeAudio ContentType = iota
ContentTypeVideo
ContentTypeText
)
func (c ContentType) String() string {
switch c {
case ContentTypeAudio:
return "audio"
case ContentTypeVideo:
return "video"
case ContentTypeText:
return "text"
default:
return UnknownString
}
}
func templatedSegments(baseURL *url.URL, representation *mpd.Representation) (segmentUrls []string) {
if representation == nil {
if Debug {
fmt.Println("no representation to look for templated segments")
}
return
}
// I don't think you have 2 templates but the mpd format is bonkers... so maybe?
var template *mpd.SegmentTemplate
if representation.AdaptationSet != nil && representation.AdaptationSet.SegmentTemplate != nil {
template = representation.AdaptationSet.SegmentTemplate
if Debug {
fmt.Println("->using AdaptationSet.SegmentTemplate")
}
} else {
template = representation.SegmentTemplate
if Debug {
fmt.Println("->using Representation.SegmentTemplate")
}
}
if template == nil {
if Debug {
fmt.Println("no SegmentTemplate found")
}
return
}
segmentUrls = templateSubstitution(template.Initialization, representation)
if Debug && len(segmentUrls) > 0 {
fmt.Printf("templated Initialization url: %s\n", segmentUrls[0])
}
segmentUrls = append(segmentUrls, templateSubstitution(template.Media, representation)...)
if Debug {
fmt.Printf("Found templated segments %d\n", len(segmentUrls))
}
for i, segmentURL := range segmentUrls {
segmentUrls[i] = absBaseURL(baseURL, []string{segmentURL}).String()
}
return segmentUrls
}
func templateSubstitution(templateStr *string, representation *mpd.Representation) (urls []string) {
if templateStr == nil {
return urls
}
template := *templateStr
/*
$identifier$ Substitution parameter
$$ An escape sequence, i.e., “$$” is replaced with a single “$”.
$RepresentationID$ The player substitutes this identifier with the value of the attribute Representation@id of the containing Representation.
$Number$ The player substitutes this identifier with the number of the corresponding Segment.
$Bandwidth$ The player substitutes this identifier with the value of Representation@bandwidth attribute value.
$Time$ The player substitutes this identifier with the value of the SegmentTimeline@t attribute for the Segment. You can use either $Number$ or $Time$, but not both at the same time.
*/
if strings.Contains(template, "$RepresentationID$") {
template = strings.Replace(template, "$RepresentationID$", strPtrtoS(representation.ID), -1)
}
// Time-Based SegmentTemplate
// $Time$ identifier, which will be substituted with the value of the t attribute from the SegmentTimeline.
if strings.Contains(template, "$Time$") {
if Debug {
fmt.Println("\t-> Time-based SegmentTemplate")
}
var segTemplate *mpd.SegmentTemplate
if representation.AdaptationSet != nil && representation.AdaptationSet.SegmentTemplate != nil {
segTemplate = representation.AdaptationSet.SegmentTemplate
} else {
segTemplate = representation.SegmentTemplate
}
// segment timeline
if segTemplate.SegmentTimeline != nil {
if Debug {
fmt.Println("\t-> Segment timeline found")
}
/* example
<S t="0" d="96256" r="2" />
<S d="95232" />
<S d="96256" r="2" />
<S d="95232" />
*/
currentT := 0
duration := 0
for _, tlSeg := range segTemplate.SegmentTimeline.Segments {
if tlSeg.StartTime != nil {
currentT = uint64PtrToI(tlSeg.StartTime)
}
duration = int(tlSeg.Duration)
url := strings.Replace(template, "$Time$", strconv.Itoa(currentT), -1)
urls = append(urls, url)
currentT += duration
// repeat count if present
if tlSeg.RepeatCount != nil {
repeat := intPtrToI(tlSeg.RepeatCount)
for i := 0; i < repeat; i++ {
url := strings.Replace(template, "$Time$", strconv.Itoa(currentT), -1)
urls = append(urls, url)
currentT += duration
}
}
}
}
} else if strings.Contains(template, "$Number$") {
} else {
urls = append(urls, template)
}
return
}
// always returns a copy
func absBaseURL(manifestBaseURL *url.URL, elBaseURLs []string) *url.URL {
if len(elBaseURLs) == 0 {
u := *manifestBaseURL
return &u
}
elBaseURL := elBaseURLs[0]
u, err := url.Parse(elBaseURL)
if err != nil {
if Debug {
fmt.Printf("failed to parse the base url %s - %s\n", elBaseURL, err)
}
return manifestBaseURL
}
if u.IsAbs() {
return u
}
return manifestBaseURL.ResolveReference(u)
}
func extractContentType(contentType, mimeType *string) string {
if contentType != nil {
return strings.ToLower(*contentType)
}
if mimeType != nil {
mType := strings.ToLower(*mimeType)
if strings.Contains(mType, "video") {
return "video"
}
if strings.Contains(mType, "audio") {
return "audio"
}
if strings.Contains(mType, "text") {
return "text"
}
}
return UnknownString
}
func downloadFile(url string, path string) (*os.File, error) {
return downloadFileWithClient(http.DefaultClient, url, path)
}
// downloadFile downloads a file from a given url and saves it to a given path
// it returns the file and an error if something goes wrong
// It's the caller's responsibility to close the file.
func downloadFileWithClient(client *http.Client, url string, path string) (*os.File, error) {
if client == nil {
client = http.DefaultClient
}
// check if there is a valid file at `path`
if fileExists(path) {
if Debug {
fmt.Println("-> File already exists at", path)
}
return os.Open(path)
}
// build the request with the proper headers
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
// req.Header.Add("Accept", "application/dash+xml,video/vnd.mpeg.dash.mpd")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
err := fmt.Errorf("bad status: %s", resp.Status)
return nil, err
}
// Create the file
out, err := os.Create(path)
if err != nil {
return nil, err
}
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
out.Close()
os.Remove(path)
return nil, err
}
return out, nil
}
func repCodecs(r *mpd.Representation) string {
if r == nil {
return UnknownString
}
codecs := strPtrtoS(r.Codecs)
if codecs == UnknownString && r.AdaptationSet != nil && r.AdaptationSet.Codecs != nil {
codecs = strPtrtoS(r.AdaptationSet.Codecs)
}
return codecs
}
func guessedExtension(r *mpd.Representation) string {
if r == nil {
return ""
}
// codec check first (especially because of text streams)
codec := repCodecs(r)
if codec != UnknownString {
// stpp is a text stream
if strings.Contains(codec, "stpp") {
return ".ttml"
}
// webvtt is a text stream
if strings.Contains(codec, "webvtt") {
return ".vtt"
}
if strings.Contains(codec, "wvtt") {
return ".vtt"
}
if strings.Contains(codec, "vtt") {
return ".vtt"
}
if strings.Contains(codec, "avc") {
return ".mp4"
}
if strings.Contains(codec, "mp4a") {
return ".mp4"
}
if strings.Contains(codec, "mp3") {
return ".mp3"
}
if strings.Contains(codec, "vorbis") {
return ".ogg"
}
if strings.Contains(codec, "opus") {
return ".opus"
}
if strings.Contains(codec, "vp9") {
return ".webm"
}
if strings.Contains(codec, "vp8") {
return ".webm"
}
}
// mimetype check
mimeType := strPtrtoS(r.MimeType)
if mimeType == UnknownString && r.AdaptationSet != nil && r.AdaptationSet.MimeType != nil {
mimeType = strPtrtoS(r.AdaptationSet.MimeType)
}
if mimeType != UnknownString {
ext, err := mime.ExtensionsByType(mimeType)
if err == nil && len(ext) > 0 {
return ext[0]
}
}
return ""
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
func strPtrtoS(s *string) string {
if s == nil {
return UnknownString
}
return *s
}
func intPtrToI(d *int) int {
if d == nil {
return 0
}
return int(*d)
}
func int64PtrToI(d *int64) int {
if d == nil {
return 0
}
return int(*d)
}
func uint64PtrToI(d *uint64) int {
if d == nil {
return 0
}
return int(*d)
}
func uint32PtrToI(d *uint32) int {
if d == nil {
return 0
}
return int(*d)
}
func boolPtrToB(d *bool) bool {
if d == nil {
return false
}
return *d
}