-
Notifications
You must be signed in to change notification settings - Fork 14
/
m3u8.go
95 lines (85 loc) · 2.06 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
package twitterdownloader
import (
"errors"
"io"
"io/ioutil"
"os"
"github.com/grafov/m3u8"
)
func saveFile(filename string, reader io.ReadCloser) error {
body, err := ioutil.ReadAll(reader)
if err = ioutil.WriteFile(filename, body, os.ModePerm); err != nil {
return err
}
return nil
}
func playList(reader io.ReadCloser) (string, error) {
// saveFile("playlist.m3u8", reader)
// f, err := os.Open("playlist.m3u8")
// if err != nil {
// panic(err)
// }
p, listType, err := m3u8.DecodeFrom(reader, true)
if err != nil {
panic(err)
}
switch listType {
// case m3u8.MEDIA:
// mediapl := p.(*m3u8.MediaPlaylist)
// // fmt.Printf("%+v\n", mediapl)
case m3u8.MASTER:
masterpl := p.(*m3u8.MasterPlaylist)
// fmt.Printf("%+v\n", masterpl)
var target struct {
index int
bandwidth uint32
}
target.index = -1
target.bandwidth = 0
for k, v := range masterpl.Variants {
// fmt.Println("URI:", v.URI)
// fmt.Println("Resolution:", v.Resolution)
// fmt.Println("Bandwidth:", v.Bandwidth)
if target.bandwidth < v.Bandwidth {
target.bandwidth = v.Bandwidth
target.index = k
}
}
if target.index > 0 {
return masterpl.Variants[target.index].URI, nil
}
return masterpl.Variants[0].URI, nil
}
return "", errors.New("Not find playlist m3u8")
}
func videoList(reader io.ReadCloser) ([]string, error) {
// f, err := os.Open("video.m3u8")
// if err != nil {
// panic(err)
// }
// p, listType, err := m3u8.DecodeFrom(bufio.NewReader(f), true)
p, listType, err := m3u8.DecodeFrom(reader, true)
if err != nil {
panic(err)
}
var s []string
switch listType {
case m3u8.MEDIA:
mediapl := p.(*m3u8.MediaPlaylist)
// fmt.Printf("%+v\n", mediapl)
for _, v := range mediapl.Segments {
if v == nil {
// fmt.Println("Nil")
break
}
// fmt.Println("URI:", v.URI)
// fmt.Println("Resolution:", v.Duration)
s = append(s, v.URI)
}
return s, nil
// case m3u8.MASTER:
// masterpl := p.(*m3u8.MasterPlaylist)
// // fmt.Printf("%+v\n", masterpl)
}
return nil, errors.New("Not find video list")
}