-
Notifications
You must be signed in to change notification settings - Fork 62
/
main.go
178 lines (159 loc) · 4.54 KB
/
main.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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import "flag"
import "fmt"
import "io"
import "net/http"
import "net/url"
import "log"
import "os"
import "time"
import "github.com/golang/groupcache/lru"
import "strings"
import "github.com/kz26/m3u8"
const VERSION = "1.0.5"
var USER_AGENT string
var client = &http.Client{}
func doRequest(c *http.Client, req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", USER_AGENT)
resp, err := c.Do(req)
return resp, err
}
type Download struct {
URI string
totalDuration time.Duration
}
func downloadSegment(fn string, dlc chan *Download, recTime time.Duration) {
out, err := os.Create(fn)
if err != nil {
log.Fatal(err)
}
defer out.Close()
for v := range dlc {
req, err := http.NewRequest("GET", v.URI, nil)
if err != nil {
log.Fatal(err)
}
resp, err := doRequest(client, req)
if err != nil {
log.Print(err)
continue
}
if resp.StatusCode != 200 {
log.Printf("Received HTTP %v for %v\n", resp.StatusCode, v.URI)
continue
}
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
log.Printf("Downloaded %v\n", v.URI)
if recTime != 0 {
log.Printf("Recorded %v of %v\n", v.totalDuration, recTime)
} else {
log.Printf("Recorded %v\n", v.totalDuration)
}
}
}
func getPlaylist(urlStr string, recTime time.Duration, useLocalTime bool, dlc chan *Download) {
startTime := time.Now()
var recDuration time.Duration = 0
cache := lru.New(1024)
playlistUrl, err := url.Parse(urlStr)
if err != nil {
log.Fatal(err)
}
for {
req, err := http.NewRequest("GET", urlStr, nil)
if err != nil {
log.Fatal(err)
}
resp, err := doRequest(client, req)
if err != nil {
log.Print(err)
time.Sleep(time.Duration(3) * time.Second)
}
playlist, listType, err := m3u8.DecodeFrom(resp.Body, true)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
if listType == m3u8.MEDIA {
mpl := playlist.(*m3u8.MediaPlaylist)
for _, v := range mpl.Segments {
if v != nil {
var msURI string
if strings.HasPrefix(v.URI, "http") {
msURI, err = url.QueryUnescape(v.URI)
if err != nil {
log.Fatal(err)
}
} else {
msUrl, err := playlistUrl.Parse(v.URI)
if err != nil {
log.Print(err)
continue
}
msURI, err = url.QueryUnescape(msUrl.String())
if err != nil {
log.Fatal(err)
}
}
_, hit := cache.Get(msURI)
if !hit {
cache.Add(msURI, nil)
if useLocalTime {
recDuration = time.Now().Sub(startTime)
} else {
recDuration += time.Duration(int64(v.Duration * 1000000000))
}
dlc <- &Download{msURI, recDuration}
}
if recTime != 0 && recDuration != 0 && recDuration >= recTime {
close(dlc)
return
}
}
}
if mpl.Closed {
close(dlc)
return
} else {
time.Sleep(time.Duration(int64(mpl.TargetDuration * 1000000000)))
}
} else {
log.Fatal("Not a valid media playlist")
}
}
}
func main() {
duration := flag.Duration("t", time.Duration(0), "Recording duration (0 == infinite)")
useLocalTime := flag.Bool("l", false, "Use local time to track duration instead of supplied metadata")
flag.StringVar(&USER_AGENT, "ua", fmt.Sprintf("gohls/%v", VERSION), "User-Agent for HTTP client")
flag.Parse()
os.Stderr.Write([]byte(fmt.Sprintf("gohls %v - HTTP Live Streaming (HLS) downloader\n", VERSION)))
os.Stderr.Write([]byte("Copyright (C) 2013-2014 Kevin Zhang. Licensed for use under the GNU GPL version 3.\n"))
if flag.NArg() < 2 {
os.Stderr.Write([]byte("Usage: gohls [-l=bool] [-t duration] [-ua user-agent] media-playlist-url output-file\n"))
flag.PrintDefaults()
os.Exit(2)
}
if !strings.HasPrefix(flag.Arg(0), "http") {
log.Fatal("Media playlist url must begin with http/https")
}
msChan := make(chan *Download, 1024)
go getPlaylist(flag.Arg(0), *duration, *useLocalTime, msChan)
downloadSegment(flag.Arg(1), msChan, *duration)
}