-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
66 lines (57 loc) · 2.05 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
package main
import (
"encoding/hex"
"flag"
"log"
"os"
"strings"
)
const defaultUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
var (
logger = log.New(os.Stdout, "", log.LstdFlags)
logErr = log.New(os.Stderr, "", log.LstdFlags)
)
var (
flagInput = flag.String("i", "", "`file or URL` to read")
flagOutput = flag.String("o", "", "Write to `file`")
flagTmpDir = flag.String("t", "", "Temp directory to store downloaded parts")
flagThread = flag.Int("thread", 1, "Concurrent downloading threads. Suggestion: <= 32 (no hard limit imposed)")
flagUA = flag.String("UA", defaultUA, "Send User-Agent to server")
flagBaseURL = flag.String("baseurl", "", "Base URL to reference (useful when m3u8 file is local file)")
flagKey = flag.String("key", "", "Decryption key, overrides key declared in m3u8 (in 32-char hex form)")
flagRaw = flag.Bool("raw", false, "Don't attempt to decrypt. Usually you should also turn on nomerge")
flagNoMerge = flag.Bool("nomerge", false, "Don't attempt to merge segments (segments stay in the tmp directory)")
flagRetry = flag.Int("retry", 3, "retry `num` times after failure")
)
var customKey []byte
func main() {
flag.Parse()
if !*flagNoMerge && *flagOutput == "" {
logErr.Fatalln("output file must be specified")
}
var err error
if *flagKey != "" {
customKey, err = hex.DecodeString(*flagKey)
if err != nil {
logErr.Fatalln("invalid key given:", err)
}
if len(customKey) != aes128BlockSize {
logErr.Fatalln("please provide key in 32-char hex form")
}
}
if *flagBaseURL == "" {
*flagBaseURL = (*flagInput)[:strings.LastIndex(*flagInput, "/")+1]
} else if (*flagBaseURL)[len(*flagBaseURL)-1] != '/' {
*flagBaseURL = *flagBaseURL + "/"
}
logger.Println("using base URL:", *flagBaseURL)
logger.Println("decoding m3u8 from:", *flagInput)
pl, err := decode(*flagInput)
if err != nil {
logErr.Fatalln("decode m3u8 file failed:", err)
}
err = download(pl)
if err != nil {
logErr.Fatalln("download failed:", err)
}
}