-
Notifications
You must be signed in to change notification settings - Fork 2
/
m3u8.go
127 lines (107 loc) · 2.55 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
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
package geektime
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func parseM3u8(m3u8Path string) ([]string, error) {
f, err := os.Open(m3u8Path)
if err != nil {
return nil, err
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
var tsFilenames []string
for _, l := range strings.Split(string(data), "\n") {
l = strings.TrimSpace(l)
if l == "" || strings.HasPrefix(l, "#") {
continue
}
tsFilenames = append(tsFilenames, l)
}
return tsFilenames, nil
}
func listM3U8Paths(dir string) []string {
var m3u8Paths []string
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, ".m3u8") {
m3u8Paths = append(m3u8Paths, path)
}
return nil
})
return m3u8Paths
}
func doesDownloadAllTS(m3u8Path string) (bool, error) {
ts, err := parseM3u8(m3u8Path)
if err != nil {
return false, err
}
dir := filepath.Dir(m3u8Path)
for _, t := range ts {
if ok, err := fileExists(filepath.Join(dir, t)); err == nil {
if !ok {
return false, nil
}
} else {
return false, err
}
}
return true, nil
}
// M3U8Converter converts the videos with the m3u8 format to the mp4
type M3U8Converter struct {
outputDir, inputDir string
}
// NewM3U8Converter creates the converter to convert the m3u8-video to the mp4
func NewM3U8Converter(inputDir, outputDir string) (*M3U8Converter, error) {
if inputDir == "" {
return nil, errors.New("empty input dir")
}
if outputDir == "" {
return nil, errors.New("empty output dir")
}
err := makeSureDirExist(outputDir)
if err != nil {
return nil, err
}
return &M3U8Converter{outputDir, inputDir}, nil
}
// ToMP4 converts to the mp4 format
func (c *M3U8Converter) ToMP4() {
c.convert(m3u8ToMP4, func(m3u8Path string) string {
return filepath.Join(c.outputDir, filepath.Base(filepath.Dir(m3u8Path))) + ".mp4"
})
}
// ToMP3 converts to the mp3 format
func (c *M3U8Converter) ToMP3() {
c.convert(m3u8ToMP3, func(m3u8Path string) string {
return filepath.Join(c.outputDir, filepath.Base(filepath.Dir(m3u8Path))) + ".mp3"
})
}
func (c *M3U8Converter) convert(
convert func(in, out string) (string, error),
filename func(m3u8Path string) string,
) {
for _, m := range listM3U8Paths(c.inputDir) {
outputPath := filename(m)
fmt.Printf("convert %s, to %s\n", m, outputPath)
ret, err := convert(m, outputPath)
msg := "success!"
if err != nil {
msg = err.Error()
}
fmt.Printf("result:%s, message:%s\n", ret, msg)
}
}