-
Notifications
You must be signed in to change notification settings - Fork 204
/
m3u8-downloader.go
executable file
·456 lines (420 loc) · 11.7 KB
/
m3u8-downloader.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// @author:llychao<[email protected]>
// @contributor: Junyi<[email protected]>
// @date:2020-02-18
// @功能:golang m3u8 video Downloader
package main
import (
"bufio"
"bytes"
"crypto/aes"
"crypto/cipher"
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/levigross/grequests"
)
const (
// HEAD_TIMEOUT 请求头超时时间
HEAD_TIMEOUT = 5 * time.Second
// PROGRESS_WIDTH 进度条长度
PROGRESS_WIDTH = 20
// TS_NAME_TEMPLATE ts视频片段命名规则
TS_NAME_TEMPLATE = "%05d.ts"
)
var (
// 命令行参数
urlFlag = flag.String("u", "", "m3u8下载地址(http(s)://url/xx/xx/index.m3u8)")
nFlag = flag.Int("n", 24, "num:下载线程数(默认24)")
htFlag = flag.String("ht", "v1", "hostType:设置getHost的方式(v1: `http(s):// + url.Host + filepath.Dir(url.Path)`; v2: `http(s)://+ u.Host`")
oFlag = flag.String("o", "movie", "movieName:自定义文件名(默认为movie)不带后缀")
cFlag = flag.String("c", "", "cookie:自定义请求cookie")
rFlag = flag.Bool("r", true, "autoClear:是否自动清除ts文件")
sFlag = flag.Int("s", 0, "InsecureSkipVerify:是否允许不安全的请求(默认0)")
spFlag = flag.String("sp", "", "savePath:文件保存的绝对路径(默认为当前路径,建议默认值)")
logger *log.Logger
ro = &grequests.RequestOptions{
UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
RequestTimeout: HEAD_TIMEOUT,
Headers: map[string]string{
"Connection": "keep-alive",
"Accept": "*/*",
"Accept-Encoding": "*",
"Accept-Language": "zh-CN,zh;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5",
},
}
)
// TsInfo 用于保存 ts 文件的下载地址和文件名
type TsInfo struct {
Name string
Url string
}
func init() {
logger = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
}
func main() {
Run()
}
func Run() {
msgTpl := "[功能]:多线程下载直播流m3u8视屏\n[提醒]:下载失败,请使用 -ht=v2 \n[提醒]:下载失败,m3u8 地址可能存在嵌套\n[提醒]:进度条中途下载失败,可重复执行"
fmt.Println(msgTpl)
runtime.GOMAXPROCS(runtime.NumCPU())
now := time.Now()
// 1、解析命令行参数
flag.Parse()
m3u8Url := *urlFlag
maxGoroutines := *nFlag
hostType := *htFlag
movieName := *oFlag
autoClearFlag := *rFlag
cookie := *cFlag
insecure := *sFlag
savePath := *spFlag
ro.Headers["Referer"] = getHost(m3u8Url, "v2")
if insecure != 0 {
ro.InsecureSkipVerify = true
}
// http 自定义 cookie
if cookie != "" {
ro.Headers["Cookie"] = cookie
}
if !strings.HasPrefix(m3u8Url, "http") || m3u8Url == "" {
flag.Usage()
return
}
var download_dir string
pwd, _ := os.Getwd()
if savePath != "" {
pwd = savePath
}
// 初始化下载ts的目录,后面所有的ts文件会保存在这里
download_dir = filepath.Join(pwd, movieName)
if isExist, _ := pathExists(download_dir); !isExist {
os.MkdirAll(download_dir, os.ModePerm)
}
// 2、解析m3u8
m3u8Host := getHost(m3u8Url, hostType)
m3u8Body := getM3u8Body(m3u8Url)
//m3u8Body := getFromFile()
ts_key := getM3u8Key(m3u8Host, m3u8Body)
if ts_key != "" {
fmt.Printf("待解密 ts 文件 key : %s \n", ts_key)
}
ts_list := getTsList(m3u8Host, m3u8Body)
fmt.Println("待下载 ts 文件数量:", len(ts_list))
// 3、下载ts文件到download_dir
downloader(ts_list, maxGoroutines, download_dir, ts_key)
if ok := checkTsDownDir(download_dir); !ok {
fmt.Printf("\n[Failed] 请检查url地址有效性 \n")
return
}
// 4、合并ts切割文件成mp4文件
mv := mergeTs(download_dir)
if autoClearFlag {
//自动清除ts文件目录
os.RemoveAll(download_dir)
}
//5、输出下载视频信息
DrawProgressBar("Merging", float32(1), PROGRESS_WIDTH, mv)
fmt.Printf("\n[Success] 下载保存路径:%s | 共耗时: %6.2fs\n", mv, time.Now().Sub(now).Seconds())
}
// 获取m3u8地址的host
func getHost(Url, ht string) (host string) {
u, err := url.Parse(Url)
checkErr(err)
switch ht {
case "v1":
host = u.Scheme + "://" + u.Host + filepath.Dir(u.EscapedPath())
case "v2":
host = u.Scheme + "://" + u.Host
}
return
}
// 获取m3u8地址的内容体
func getM3u8Body(Url string) string {
r, err := grequests.Get(Url, ro)
checkErr(err)
return r.String()
}
// 获取m3u8加密的密钥
func getM3u8Key(host, html string) (key string) {
lines := strings.Split(html, "\n")
key = ""
for _, line := range lines {
if strings.Contains(line, "#EXT-X-KEY") {
uri_pos := strings.Index(line, "URI")
quotation_mark_pos := strings.LastIndex(line, "\"")
key_url := strings.Split(line[uri_pos:quotation_mark_pos], "\"")[1]
if !strings.Contains(line, "http") {
key_url = fmt.Sprintf("%s/%s", host, key_url)
}
res, err := grequests.Get(key_url, ro)
checkErr(err)
if res.StatusCode == 200 {
key = res.String()
}
}
}
return
}
func getTsList(host, body string) (tsList []TsInfo) {
lines := strings.Split(body, "\n")
index := 0
var ts TsInfo
for _, line := range lines {
if !strings.HasPrefix(line, "#") && line != "" {
//有可能出现的二级嵌套格式的m3u8,请自行转换!
index++
if strings.HasPrefix(line, "http") {
ts = TsInfo{
Name: fmt.Sprintf(TS_NAME_TEMPLATE, index),
Url: line,
}
tsList = append(tsList, ts)
} else {
line = strings.TrimPrefix(line, "/")
ts = TsInfo{
Name: fmt.Sprintf(TS_NAME_TEMPLATE, index),
Url: fmt.Sprintf("%s/%s", host, line),
}
tsList = append(tsList, ts)
}
}
}
return
}
func getFromFile() string {
data, _ := ioutil.ReadFile("./ts.txt")
return string(data)
}
// 下载ts文件
// @modify: 2020-08-13 修复ts格式SyncByte合并不能播放问题
func downloadTsFile(ts TsInfo, download_dir, key string, retries int) {
defer func() {
if r := recover(); r != nil {
//fmt.Println("网络不稳定,正在进行断点持续下载")
downloadTsFile(ts, download_dir, key, retries-1)
}
}()
curr_path_file := fmt.Sprintf("%s/%s", download_dir, ts.Name)
if isExist, _ := pathExists(curr_path_file); isExist {
//logger.Println("[warn] File: " + ts.Name + "already exist")
return
}
res, err := grequests.Get(ts.Url, ro)
if err != nil || !res.Ok {
if retries > 0 {
downloadTsFile(ts, download_dir, key, retries-1)
return
} else {
//logger.Printf("[warn] File :%s", ts.Url)
return
}
}
// 校验长度是否合法
var origData []byte
origData = res.Bytes()
contentLen := 0
contentLenStr := res.Header.Get("Content-Length")
if contentLenStr != "" {
contentLen, _ = strconv.Atoi(contentLenStr)
}
if len(origData) == 0 || (contentLen > 0 && len(origData) < contentLen) || res.Error != nil {
//logger.Println("[warn] File: " + ts.Name + "res origData invalid or err:", res.Error)
downloadTsFile(ts, download_dir, key, retries-1)
return
}
// 解密出视频 ts 源文件
if key != "" {
//解密 ts 文件,算法:aes 128 cbc pack5
origData, err = AesDecrypt(origData, []byte(key))
if err != nil {
downloadTsFile(ts, download_dir, key, retries-1)
return
}
}
// https://en.wikipedia.org/wiki/MPEG_transport_stream
// Some TS files do not start with SyncByte 0x47, they can not be played after merging,
// Need to remove the bytes before the SyncByte 0x47(71).
syncByte := uint8(71) //0x47
bLen := len(origData)
for j := 0; j < bLen; j++ {
if origData[j] == syncByte {
origData = origData[j:]
break
}
}
ioutil.WriteFile(curr_path_file, origData, 0666)
}
// downloader m3u8 下载器
func downloader(tsList []TsInfo, maxGoroutines int, downloadDir string, key string) {
retry := 5 //单个ts 下载重试次数
var wg sync.WaitGroup
limiter := make(chan struct{}, maxGoroutines) //chan struct 内存占用 0 bool 占用 1
tsLen := len(tsList)
downloadCount := 0
for _, ts := range tsList {
wg.Add(1)
limiter <- struct{}{}
go func(ts TsInfo, downloadDir, key string, retryies int) {
defer func() {
wg.Done()
<-limiter
}()
downloadTsFile(ts, downloadDir, key, retryies)
downloadCount++
DrawProgressBar("Downloading", float32(downloadCount)/float32(tsLen), PROGRESS_WIDTH, ts.Name)
return
}(ts, downloadDir, key, retry)
}
wg.Wait()
}
func checkTsDownDir(dir string) bool {
if isExist, _ := pathExists(filepath.Join(dir, fmt.Sprintf(TS_NAME_TEMPLATE, 0))); !isExist {
return true
}
return false
}
// 合并ts文件
func mergeTs(downloadDir string) string {
mvName := downloadDir + ".mp4"
outMv, _ := os.Create(mvName)
defer outMv.Close()
writer := bufio.NewWriter(outMv)
err := filepath.Walk(downloadDir, func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if f.IsDir() || filepath.Ext(path) != ".ts" {
return nil
}
bytes, _ := ioutil.ReadFile(path)
_, err = writer.Write(bytes)
return err
})
checkErr(err)
_ = writer.Flush()
return mvName
}
// 进度条
func DrawProgressBar(prefix string, proportion float32, width int, suffix ...string) {
pos := int(proportion * float32(width))
s := fmt.Sprintf("[%s] %s%*s %6.2f%% \t%s",
prefix, strings.Repeat("■", pos), width-pos, "", proportion*100, strings.Join(suffix, ""))
fmt.Print("\r" + s)
}
// ============================== shell相关 ==============================
// 判断文件是否存在
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// 执行 shell
func execUnixShell(s string) {
cmd := exec.Command("bash", "-c", s)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
panic(err)
}
fmt.Printf("%s", out.String())
}
func execWinShell(s string) error {
cmd := exec.Command("cmd", "/C", s)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return err
}
fmt.Printf("%s", out.String())
return nil
}
// windows 合并文件
func win_merge_file(path string) {
pwd, _ := os.Getwd()
os.Chdir(path)
execWinShell("copy /b *.ts merge.tmp")
execWinShell("del /Q *.ts")
os.Rename("merge.tmp", "merge.mp4")
os.Chdir(pwd)
}
// unix 合并文件
func unix_merge_file(path string) {
pwd, _ := os.Getwd()
os.Chdir(path)
//cmd := `ls *.ts |sort -t "\." -k 1 -n |awk '{print $0}' |xargs -n 1 -I {} bash -c "cat {} >> new.tmp"`
cmd := `cat *.ts >> merge.tmp`
execUnixShell(cmd)
execUnixShell("rm -rf *.ts")
os.Rename("merge.tmp", "merge.mp4")
os.Chdir(pwd)
}
// ============================== 加解密相关 ==============================
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
func AesEncrypt(origData, key []byte, ivs ...[]byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
var iv []byte
if len(ivs) == 0 {
iv = key
} else {
iv = ivs[0]
}
origData = PKCS7Padding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, iv[:blockSize])
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
func AesDecrypt(crypted, key []byte, ivs ...[]byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
var iv []byte
if len(ivs) == 0 {
iv = key
} else {
iv = ivs[0]
}
blockMode := cipher.NewCBCDecrypter(block, iv[:blockSize])
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
origData = PKCS7UnPadding(origData)
return origData, nil
}
func checkErr(e error) {
if e != nil {
logger.Panic(e)
}
}