7
7
"errors"
8
8
"fmt"
9
9
"io"
10
+ "io/ioutil"
10
11
"os"
11
12
"os/exec"
12
13
"regexp"
@@ -56,10 +57,19 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
56
57
}
57
58
58
59
// 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 ()... )
60
68
outputLength := len (t .output )
61
69
optionsLength := len (t .options )
62
70
71
+
72
+
63
73
if outputLength == 1 && optionsLength == 0 {
64
74
// Just append the 1 output file we've got
65
75
args = append (args , t .output [0 ])
@@ -81,17 +91,22 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
81
91
}
82
92
}
83
93
94
+ //progressOpts := []string{"-loglevel panic", "-nostats", "-progress -"}
95
+ //args = append(args, progressOpts...)
96
+ //a := strings.Join(args, " ")
97
+ //print(a)
84
98
// Initialize command
85
99
cmd := exec .Command (t .config .FfmpegBinPath , args ... )
86
100
87
101
// If progresss enabled, get stderr pipe and start progress process
88
102
if t .config .ProgressEnabled && ! t .config .Verbose {
89
- stderrIn , err = cmd .StderrPipe ()
103
+ stderrIn , err = cmd .StdoutPipe ()
90
104
if err != nil {
91
105
return nil , fmt .Errorf ("Failed getting transcoding progress (%s) with args (%s) with error %s" , t .config .FfmpegBinPath , args , err )
92
106
}
93
107
}
94
108
109
+
95
110
if t .config .Verbose {
96
111
cmd .Stderr = os .Stdout
97
112
}
@@ -105,6 +120,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
105
120
if t .config .ProgressEnabled && ! t .config .Verbose {
106
121
go func () {
107
122
t .progress (stderrIn , out )
123
+ //t.progress2(stderrIn)
108
124
}()
109
125
110
126
go func () {
@@ -118,6 +134,11 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
118
134
return out , nil
119
135
}
120
136
137
+ func (t * Transcoder ) progress2 (stream io.ReadCloser ) {
138
+ msg , _ := ioutil .ReadAll (stream )
139
+ fmt .Printf ("%s\n " , msg )
140
+ }
141
+
121
142
// Input ...
122
143
func (t * Transcoder ) Input (arg string ) transcoder.Transcoder {
123
144
t .input = arg
@@ -258,9 +279,13 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
258
279
buf := make ([]byte , 2 )
259
280
scanner .Buffer (buf , bufio .MaxScanTokenSize )
260
281
282
+ //var re = regexp.MustCompile(`=\s+`)
283
+
284
+ progress := & Progress {}
261
285
for scanner .Scan () {
262
286
Progress := new (Progress )
263
287
line := scanner .Text ()
288
+ //fmt.Println(line)
264
289
265
290
if strings .Contains (line , "frame=" ) && strings .Contains (line , "time=" ) && strings .Contains (line , "bitrate=" ) {
266
291
var re = regexp .MustCompile (`=\s+` )
@@ -311,6 +336,104 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
311
336
312
337
out <- * Progress
313
338
}
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
+ }
314
437
}
315
438
}
316
439
0 commit comments