-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathavprobe2.go
222 lines (191 loc) · 4.38 KB
/
avprobe2.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
package main
import (
"os/exec"
"path/filepath"
"bufio"
"strings"
"log"
"regexp"
"fmt"
"errors"
"time"
)
type avprobeStat struct {
Dur time.Duration
W,H int
Vcodec,Acodec string
Fps int
Bitrate int64
Vinfo,Ainfo string
}
func avprobe2(path string) (err error, info avprobeStat) {
out, err := exec.Command("avprobe", path).CombinedOutput()
if err != nil {
err = errors.New(fmt.Sprintf("avprobe: %v", err))
return
}
debug := false
for _, l := range strings.Split(string(out), "\n") {
var re *regexp.Regexp
var ma []string
re, _ = regexp.Compile(`Duration: (.{11})`)
ma = re.FindStringSubmatch(l)
if len(ma) > 1 {
var h,m,s,ms int
fmt.Sscanf(ma[1], "%d:%d:%d.%d", &h, &m, &s, &ms)
dur := time.Duration(0)
dur += time.Duration(h)*3600
dur += time.Duration(m)*60
dur += time.Duration(s)
dur += time.Duration(ms)/100
if debug {
log.Printf("avprobe %s: dur %v => %f", path, ma[1], dur)
}
info.Dur = dur
}
re, _ = regexp.Compile(`Video: .* (\d+x\d+)`)
ma = re.FindStringSubmatch(l)
if len(ma) > 1 {
var w,h int
fmt.Sscanf(ma[1], "%dx%d", &w, &h)
if debug {
log.Printf("avprobe %s: size %v => %dx%d", path, ma[1], w, h)
}
info.W = w
info.H = h
}
re, _ = regexp.Compile(`bitrate: (\d+) kb/s`)
ma = re.FindStringSubmatch(l)
if len(ma) > 1 {
fmt.Sscanf(ma[1], "%d", &info.Bitrate)
if debug {
log.Printf("avprobe %s: bitrate %v => %d", path, ma[1], info.Bitrate)
}
}
re, _ = regexp.Compile(`Video: (\w+)`)
ma = re.FindStringSubmatch(l)
if len(ma) > 1 {
info.Vcodec = ma[1]
if debug {
log.Printf("avprobe %s: vcodec %s", path, info.Vcodec)
}
}
re, _ = regexp.Compile(`Audio: (\w+)`)
ma = re.FindStringSubmatch(l)
if len(ma) > 1 {
info.Acodec = ma[1]
if debug {
log.Printf("avprobe %s: acodec %s", path, info.Acodec)
}
}
re, _ = regexp.Compile(`Video: (.*)`)
ma = re.FindStringSubmatch(l)
if len(ma) > 1 {
info.Vinfo = ma[1]
if debug {
log.Printf("avprobe %s: vinfo %s", path, info.Vinfo)
}
}
re, _ = regexp.Compile(`Audio: (.*)`)
ma = re.FindStringSubmatch(l)
if len(ma) > 1 {
info.Ainfo = ma[1]
if debug {
log.Printf("avprobe %s: ainfo %s", path, info.Ainfo)
}
}
}
return
}
type avconvStat struct {
dur time.Duration
per float64
fps int
speed int64
}
type avconvCb func (st avconvStat) error
func avconvM3u8V2(filename,outpath string, cb func (st avconvStat) error) (err error) {
var info avprobeStat
err, info = avprobe2(filename)
if err != nil {
return
}
if info.Dur == time.Duration(0.0) || info.Vcodec == "" || info.Acodec == "" {
err = errors.New("avprobe invalid info")
return
}
var args []string
args = append(args, "-i", filename)
args = append(args, "-strict", "experimental")
if info.Vcodec == "h264" && info.Acodec == "aac" {
args = append(args, "-acodec", "aac", "-vcodec", "copy", "-bsf", "h264_mp4toannexb")
} else {
args = append(args, "-acodec", "aac", "-vcodec", "libx264")
}
args = append(args, "-f", "mpegts", "-")
var args2 []string
args2 = append(args2, "-i", "-" ,"-d", "10", "-p", outpath)
args2 = append(args2, "-m", filepath.Join(outpath, "conv.m3u8"), "-u", "/")
cmd := exec.Command("avconv", args...)
f, err := cmd.StderrPipe()
if err != nil {
return
}
bf := bufio.NewReader(f)
cmd2 := exec.Command("m3u8-segmenter", args2...)
cmd2.Stdin, err = cmd.StdoutPipe()
err = cmd.Start()
if err != nil {
return
}
err = cmd2.Start()
if err != nil {
return
}
for {
line, err2 := bf.ReadString('\r')
if err2 != nil {
log.Printf("%v", err2)
break
}
log.Printf("%s", line)
var re *regexp.Regexp
var ma []string
var st avconvStat
var found bool
re, _ = regexp.Compile(`time=(\d+.\d+)`)
ma = re.FindStringSubmatch(line)
if len(ma) > 1 {
var tm time.Duration
fmt.Sscanf(ma[1], "%d", &tm)
st.dur = tm*time.Second
found = true
}
re, _ = regexp.Compile(`fps= *(\d+)`)
ma = re.FindStringSubmatch(line)
if len(ma) > 1 {
fmt.Sscanf(ma[1], "%d", &st.fps)
}
re, _ = regexp.Compile(`bitrate= *(\d+)`)
ma = re.FindStringSubmatch(line)
if len(ma) > 1 {
fmt.Sscanf(ma[1], "%d", &st.speed)
st.speed /= 8
}
if found {
st.per = float64(st.dur)/float64(info.Dur)
err = cb(st)
if err != nil {
cmd.Process.Kill()
cmd2.Process.Kill()
return
}
}
}
err = cmd.Wait()
if err != nil {
return
}
err = cmd2.Wait()
return
}