-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogress.go
73 lines (63 loc) · 1.55 KB
/
progress.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
package ffmpeg
import (
"bufio"
"bytes"
"io"
"regexp"
"strconv"
"strings"
"github.com/ssttevee/go-ffmpeg/internal/util"
)
var equalsPattern = regexp.MustCompile(`(\w+)=\s*([^ ]+)`)
func splitProgressLine(data []byte, atEOF bool) (advance int, token []byte, spliterror error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, '\n'); i >= 0 {
// We have a full newline-terminated line.
return i + 1, data[0:i], nil
}
if i := bytes.IndexByte(data, '\r'); i >= 0 {
// We have a cr terminated line
return i + 1, data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
}
func parseProgress(stderr io.Reader, tee io.Writer, updateStatus func(Status)) {
scanner := bufio.NewScanner(stderr)
scanner.Split(splitProgressLine)
for scanner.Scan() {
line := scanner.Text()
if tee != nil {
tee.Write([]byte(line + "\n"))
}
if !strings.HasPrefix(line, "frame=") {
continue
}
matches := equalsPattern.FindAllStringSubmatch(line, -1)
if matches == nil {
continue
}
var progress Progress
for _, match := range matches {
if len(match) > 1 {
switch match[1] {
case "frame":
progress.Frame, _ = strconv.ParseInt(match[2], 10, 64)
case "fps":
progress.Fps, _ = strconv.ParseFloat(match[2], 64)
case "time":
progress.Time = util.ParseDuration(match[2]).Seconds()
case "bitrate":
progress.Bitrate = match[2]
case "speed":
progress.Speed, _ = strconv.ParseFloat(match[2][:len(match[2])-1], 64)
}
}
}
updateStatus(&progress)
}
}