Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Commit 37ffe7c

Browse files
committed
Extended version of ffmpeg output parser
1 parent 5cc0e09 commit 37ffe7c

File tree

1 file changed

+125
-2
lines changed

1 file changed

+125
-2
lines changed

ffmpeg/ffmpeg.go

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"io"
10+
"io/ioutil"
1011
"os"
1112
"os/exec"
1213
"regexp"
@@ -56,10 +57,19 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
5657
}
5758

5859
// Append input file and standard options
59-
args := append([]string{"-i", t.input}, opts.GetStrArguments()...)
60+
progressOpts := []string{"-loglevel", "error", "-nostats", "-progress", "-"}
61+
defaultOpts := []string{"-i", t.input}
62+
63+
args := []string{}
64+
args = append(args, progressOpts...)
65+
args = append(args, defaultOpts...)
66+
67+
args = append(args, opts.GetStrArguments()...)
6068
outputLength := len(t.output)
6169
optionsLength := len(t.options)
6270

71+
72+
6373
if outputLength == 1 && optionsLength == 0 {
6474
// Just append the 1 output file we've got
6575
args = append(args, t.output[0])
@@ -81,17 +91,22 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
8191
}
8292
}
8393

94+
//progressOpts := []string{"-loglevel panic", "-nostats", "-progress -"}
95+
//args = append(args, progressOpts...)
96+
//a := strings.Join(args, " ")
97+
//print(a)
8498
// Initialize command
8599
cmd := exec.Command(t.config.FfmpegBinPath, args...)
86100

87101
// If progresss enabled, get stderr pipe and start progress process
88102
if t.config.ProgressEnabled && !t.config.Verbose {
89-
stderrIn, err = cmd.StderrPipe()
103+
stderrIn, err = cmd.StdoutPipe()
90104
if err != nil {
91105
return nil, fmt.Errorf("Failed getting transcoding progress (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err)
92106
}
93107
}
94108

109+
95110
if t.config.Verbose {
96111
cmd.Stderr = os.Stdout
97112
}
@@ -105,6 +120,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
105120
if t.config.ProgressEnabled && !t.config.Verbose {
106121
go func() {
107122
t.progress(stderrIn, out)
123+
//t.progress2(stderrIn)
108124
}()
109125

110126
go func() {
@@ -118,6 +134,11 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
118134
return out, nil
119135
}
120136

137+
func (t *Transcoder) progress2(stream io.ReadCloser) {
138+
msg, _ := ioutil.ReadAll(stream)
139+
fmt.Printf("%s\n", msg)
140+
}
141+
121142
// Input ...
122143
func (t *Transcoder) Input(arg string) transcoder.Transcoder {
123144
t.input = arg
@@ -258,9 +279,13 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
258279
buf := make([]byte, 2)
259280
scanner.Buffer(buf, bufio.MaxScanTokenSize)
260281

282+
//var re = regexp.MustCompile(`=\s+`)
283+
284+
progress := &Progress{}
261285
for scanner.Scan() {
262286
Progress := new(Progress)
263287
line := scanner.Text()
288+
//fmt.Println(line)
264289

265290
if strings.Contains(line, "frame=") && strings.Contains(line, "time=") && strings.Contains(line, "bitrate=") {
266291
var re = regexp.MustCompile(`=\s+`)
@@ -311,6 +336,104 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
311336

312337
out <- *Progress
313338
}
339+
340+
/*
341+
bitrate= 256.1kbits/s
342+
total_size=410220
343+
out_time_us=12816000
344+
out_time_ms=12816000
345+
out_time=00:00:12.816000
346+
dup_frames=0
347+
drop_frames=0
348+
speed= 789x
349+
progress=end
350+
*/
351+
352+
if strings.Contains(line, "bitrate=") {
353+
//st := re.ReplaceAllString(line, `=`)
354+
removedEqualSign := strings.ReplaceAll(line, "=", " ")
355+
f := strings.Fields(removedEqualSign)
356+
357+
progress.CurrentBitrate = f[1]
358+
}
359+
360+
var totalSize int64
361+
if strings.Contains(line, "total_size=") {
362+
//st := re.ReplaceAllString(line, `=`)
363+
removedEqualSign := strings.ReplaceAll(line, "=", " ")
364+
f := strings.Fields(removedEqualSign)
365+
ts, err := strconv.ParseInt(f[1], 10, 64)
366+
if err != nil {
367+
totalSize = 0
368+
} else {
369+
totalSize = ts
370+
}
371+
}
372+
373+
if strings.Contains(line, "out_time") {
374+
//st := re.ReplaceAllString(line, `=`)
375+
removedEqualSign := strings.ReplaceAll(line, "=", " ")
376+
f := strings.Fields(removedEqualSign)
377+
378+
//progress := (timesec * 100) / dursec
379+
//ms, err := strconv.ParseInt(f[1], 10, 64)
380+
progress.CurrentTime = f[1]
381+
}
382+
383+
if strings.Contains(line, "out_time") {
384+
//st := re.ReplaceAllString(line, `=`)
385+
removedEqualSign := strings.ReplaceAll(line, "=", " ")
386+
f := strings.Fields(removedEqualSign)
387+
388+
//progress := (timesec * 100) / dursec
389+
//ms, err := strconv.ParseInt(f[1], 10, 64)
390+
progress.CurrentTime = f[1]
391+
}
392+
393+
if strings.Contains(line, "speed") {
394+
//st := re.ReplaceAllString(line, `=`)
395+
removedEqualSign := strings.ReplaceAll(line, "=", " ")
396+
f := strings.Fields(removedEqualSign)
397+
398+
//progress := (timesec * 100) / dursec
399+
//ms, err := strconv.ParseInt(f[1], 10, 64)
400+
progress.Speed = f[1]
401+
}
402+
403+
if strings.Contains(line, "progress") {
404+
//st := re.ReplaceAllString(line, `=`)
405+
removedEqualSign := strings.ReplaceAll(line, "=", " ")
406+
f := strings.Fields(removedEqualSign)
407+
408+
//progress := (timesec * 100) / dursec
409+
//ms, err := strconv.ParseInt(f[1], 10, 64)
410+
if len(f) == 2 {
411+
if f[1] == "continue" {
412+
size, err := strconv.ParseInt(t.metadata.GetFormat().GetSize(), 10, 64)
413+
if err != nil {
414+
progress.Progress = 0.0
415+
} else {
416+
417+
}
418+
419+
if totalSize > 0 {
420+
progr := (size/totalSize)*100
421+
progress.Progress = float64(progr)
422+
} else {
423+
progress.Progress = 0.0
424+
}
425+
426+
}
427+
if f[1] == "end" {
428+
progress.Progress = 1.0
429+
}
430+
} else {
431+
progress.Progress = 0.0
432+
}
433+
434+
435+
out <- *progress
436+
}
314437
}
315438
}
316439

0 commit comments

Comments
 (0)