-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.go
140 lines (125 loc) · 3.36 KB
/
parser.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
package astiffmpeg
import (
"bytes"
"strconv"
"strings"
"time"
"github.com/asticode/go-astikit"
)
// StdErrParser represents an object capable of parsing stderr
type StdErrParser interface {
Period() time.Duration
Process(t time.Time, b *bytes.Buffer)
}
// DefaultStdErrParser creates the default stderr parser
func DefaultStdErrParser(period time.Duration, fn func(r DefaultStdErrResults)) StdErrParser {
return &defaultStdErrParser{
fn: fn,
period: period,
}
}
type defaultStdErrParser struct {
fn func(r DefaultStdErrResults)
period time.Duration
}
func (p defaultStdErrParser) Period() time.Duration {
return p.period
}
func (p defaultStdErrParser) Process(t time.Time, b *bytes.Buffer) {
// Split on \n
var lines = bytes.Split(b.Bytes(), []byte("\n"))
if len(lines) == 0 {
return
}
// Split on \r
var items = bytes.Split(lines[len(lines)-1], []byte("\r"))
if len(items) < 2 {
return
}
// Parse results
r := p.parseResults(items[len(items)-2])
// Execute callback
p.fn(r)
}
// DefaultStdErrResults represents default stderr results
type DefaultStdErrResults struct {
Bitrate *float64 // bits/s
FPS *int
Frame *int
Q *float64
Size *int // bits
Speed *float64
Time *time.Duration
}
// frame=17448 fps=254 q=31.0 size= 176032kB time=00:11:38.14 bitrate=2065.5kbits/s speed=10.2x
func (p defaultStdErrParser) parseResults(b []byte) (r DefaultStdErrResults) {
// Split on =
var k string
for idx, i := range bytes.Split(b, []byte("=")) {
// Split on space
var items = bytes.Split(bytes.TrimSpace(i), []byte(" "))
// Parse key/value
if len(items[0]) > 0 && len(k) > 0 {
v := string(items[0])
switch k {
case "bitrate":
// There may be other suffix, but we only support this one for now
v = strings.TrimSuffix(v, "kbits/s")
if p, err := strconv.ParseFloat(v, 64); err == nil {
r.Bitrate = astikit.Float64Ptr(p * 1000)
}
case "frame":
if p, err := strconv.Atoi(v); err == nil {
r.Frame = astikit.IntPtr(p)
}
case "fps":
if p, err := strconv.ParseFloat(v, 64); err == nil {
r.FPS = astikit.IntPtr(int(p))
}
case "q":
if p, err := strconv.ParseFloat(v, 64); err == nil {
r.Q = astikit.Float64Ptr(p)
}
case "size":
if n, err := numberFromString(v); err == nil {
r.Size = astikit.IntPtr(int(n.float64()))
}
case "speed":
if p, err := strconv.ParseFloat(strings.TrimSuffix(v, "x"), 64); err == nil {
r.Speed = astikit.Float64Ptr(p)
}
case "time":
// Split on .
var d time.Duration
ps := strings.Split(v, ".")
if len(ps) > 1 {
if p, err := strconv.Atoi(ps[1]); err == nil {
// For now we make the assumption that milliseconds are in this format ".99" and not ".999"
d += time.Duration(p*10) * time.Millisecond
}
}
// Split on :
ps = strings.Split(ps[0], ":")
if len(ps) >= 3 {
if p, err := strconv.Atoi(ps[0]); err == nil {
d += time.Duration(p) * time.Hour
}
if p, err := strconv.Atoi(ps[1]); err == nil {
d += time.Duration(p) * time.Minute
}
if p, err := strconv.Atoi(ps[2]); err == nil {
d += time.Duration(p) * time.Second
}
}
r.Time = astikit.DurationPtr(d)
}
}
// Get key
if len(items) > 1 && len(items[1]) > 0 {
k = string(items[1])
} else if len(items) == 1 && idx == 0 {
k = string(items[0])
}
}
return
}