-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
137 lines (124 loc) · 2.9 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
package main
import (
"bufio"
"flag"
"fmt"
"github.com/vvampirius/hls-downloader/downloader"
"log"
"os"
"path/filepath"
"strings"
"time"
)
const VERSION = `0.10.2`
var (
ErrorLog = log.New(os.Stderr, `error#`, log.Lshortfile)
DebugLog = log.New(os.Stdout, `debug#`, log.Lshortfile)
)
func helpText() {
fmt.Println(`https://github.com/vvampirius/hls-downloader`)
fmt.Println(`Download HTTP Live Streaming (HLS) content`)
fmt.Printf("\nUsage: %s [options] [<m3u url> <output filename>]\n\n", os.Args[0])
flag.PrintDefaults()
}
func readUrl() string {
run := true
for run {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter m3u URL: ")
text, err := reader.ReadString('\n')
text = strings.TrimSuffix(text, "\n")
if err != nil {
log.Println(err.Error())
run = false
}
if text == `` {
continue
}
return text
}
return ``
}
func readOutputFilename() string {
for {
fmt.Print(`Filename: `)
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err.Error())
}
line = strings.TrimSuffix(line, "\n")
if line == `` {
return fmt.Sprintf("%d.mp4", time.Now().Unix())
}
if ext := filepath.Ext(line); ext == `` || len(ext) > 4 {
log.Println(`Added mp4 extension`)
line = line + `.mp4`
}
if _, err := os.Stat(line); err == nil {
log.Printf("'%s' already exists\n", line)
continue
}
return line
}
}
func main() {
help := flag.Bool("h", false, "print this help")
ver := flag.Bool("v", false, "Show version")
noffmpeg := flag.Bool("noffmpeg", false, "Do not use ffmpeg")
flag.Parse()
if *help {
helpText()
os.Exit(0)
}
if *ver {
fmt.Println(VERSION)
os.Exit(0)
}
var m3uUrl, outputFilename string
switch flag.NArg() {
case 2:
m3uUrl, outputFilename = flag.Arg(0), flag.Arg(1)
if _, err := os.Stat(outputFilename); err == nil {
ErrorLog.Fatalln(`File exist!`)
}
case 0:
outputFilename = readOutputFilename()
m3uUrl = readUrl()
default:
os.Stdout = os.Stderr
helpText()
os.Exit(1)
}
useFfmpeg := true
if *noffmpeg {
useFfmpeg = false
}
notifyChan, err := downloader.NewDownloader().Download(m3uUrl, outputFilename, useFfmpeg, nil)
if err != nil {
ErrorLog.Println(err.Error())
os.Exit(1)
}
i, segmentNum := 0, 0
for d := range notifyChan {
if i != 0 && segmentNum != d.CurrentSegment.Num {
fmt.Println()
}
if d.Playlist != nil {
fmt.Printf("\r[%d / %d] [%s / %s] [%.1f Mb] [%.1f / %.1f Kb]\t",
d.CurrentSegment.Num, d.Playlist.SegmentsCount, time.Duration(d.DownloadedDuration*float32(time.Second)),
time.Duration(d.Playlist.SegmentsDuration*float32(time.Second)), float64(d.GotBytes)/1024/1024,
float32(d.CurrentSegment.GotBytes)/1024, float32(d.CurrentSegment.Size)/1024)
} else {
fmt.Printf("\rno playlist loaded")
}
i++
segmentNum = d.CurrentSegment.Num
err = d.Error
}
fmt.Println()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}