@@ -10,11 +10,9 @@ import (
10
10
"os"
11
11
"os/exec"
12
12
"regexp"
13
- "strconv"
14
13
"strings"
15
14
16
- "github.com/floostack/transcoder"
17
- "github.com/floostack/transcoder/utils"
15
+ "github.com/Igor57/transcoder"
18
16
)
19
17
20
18
// Transcoder ...
@@ -35,28 +33,116 @@ func New(cfg *Config) transcoder.Transcoder {
35
33
return & Transcoder {config : cfg }
36
34
}
37
35
38
- // Start ...
39
- func (t * Transcoder ) Start ( opts transcoder.Options ) (<- chan transcoder.Progress , error ) {
36
+ // StartAndReturnCmd ...
37
+ func (t * Transcoder ) StartAndReturnCmd ( optsBeforeInput transcoder. Options , opts transcoder.Options ) (<- chan transcoder.Progress , * exec. Cmd , error ) {
40
38
41
39
var stderrIn io.ReadCloser
42
-
40
+ var err error
43
41
out := make (chan transcoder.Progress )
44
42
45
43
defer t .closePipes ()
46
44
47
45
// Validates config
48
46
if err := t .validate (); err != nil {
49
- return nil , err
47
+ return nil , nil , err
50
48
}
51
49
52
50
// Get file metadata
53
- _ , err := t .GetMetadata ()
51
+ /*
52
+ _, err := t.GetMetadata()
53
+ if err != nil {
54
+ return nil, err
55
+ }
56
+ */
57
+ args := append (optsBeforeInput .GetStrArguments (), []string {"-i" , t .input }... )
58
+ // Append input file and standard options
59
+ args = append (args , opts .GetStrArguments ()... )
60
+ outputLength := len (t .output )
61
+ optionsLength := len (t .options )
62
+
63
+ if outputLength == 1 && optionsLength == 0 {
64
+ // Just append the 1 output file we've got
65
+ args = append (args , t .output [0 ])
66
+ } else {
67
+ for index , out := range t .output {
68
+ // Get executable flags
69
+ // If we are at the last output file but still have several options, append them all at once
70
+ if index == outputLength - 1 && outputLength < optionsLength {
71
+ for i := index ; i < len (t .options ); i ++ {
72
+ args = append (args , t .options [i ]... )
73
+ }
74
+ // Otherwise just append the current options
75
+ } else {
76
+ args = append (args , t .options [index ]... )
77
+ }
78
+
79
+ // Append output flag
80
+ args = append (args , out )
81
+ }
82
+ }
83
+
84
+ // Initialize command
85
+ fmt .Println (strings .Join (args , " " ))
86
+ cmd := exec .Command (t .config .FfmpegBinPath , args ... )
87
+
88
+ // If progresss enabled, get stderr pipe and start progress process
89
+ if t .config .ProgressEnabled && ! t .config .Verbose {
90
+ stderrIn , err = cmd .StderrPipe ()
91
+ if err != nil {
92
+ return nil , nil , fmt .Errorf ("Failed getting transcoding progress (%s) with args (%s) with error %s" , t .config .FfmpegBinPath , args , err )
93
+ }
94
+ }
95
+
96
+ if t .config .Verbose {
97
+ cmd .Stderr = os .Stdout
98
+ }
99
+
100
+ // Start process
101
+ err = cmd .Start ()
54
102
if err != nil {
103
+ return nil , nil , fmt .Errorf ("Failed starting transcoding (%s) with args (%s) with error %s" , t .config .FfmpegBinPath , args , err )
104
+ }
105
+
106
+ if t .config .ProgressEnabled && ! t .config .Verbose {
107
+ go func () {
108
+ t .progress (stderrIn , out )
109
+ }()
110
+
111
+ go func () {
112
+ defer close (out )
113
+ err = cmd .Wait ()
114
+ }()
115
+ } else {
116
+ err = cmd .Wait ()
117
+ }
118
+
119
+ return out , cmd , nil
120
+ }
121
+
122
+ // Start ...
123
+ func (t * Transcoder ) Start (optsBeforeInput transcoder.Options , opts transcoder.Options ) (<- chan transcoder.Progress , error ) {
124
+
125
+ var stderrIn io.ReadCloser
126
+ var err error
127
+ out := make (chan transcoder.Progress )
128
+
129
+ defer t .closePipes ()
130
+
131
+ // Validates config
132
+ if err := t .validate (); err != nil {
55
133
return nil , err
56
134
}
57
135
136
+ // Get file metadata
137
+ /*
138
+ _, err := t.GetMetadata()
139
+ if err != nil {
140
+ return nil, err
141
+ }
142
+ */
143
+ args := append (optsBeforeInput .GetStrArguments (), []string {"-i" , t .input }... )
58
144
// Append input file and standard options
59
- args : = append ([] string { "-i" , t . input } , opts .GetStrArguments ()... )
145
+ args = append (args , opts .GetStrArguments ()... )
60
146
outputLength := len (t .output )
61
147
optionsLength := len (t .options )
62
148
@@ -82,6 +168,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
82
168
}
83
169
84
170
// Initialize command
171
+ fmt .Println (strings .Join (args , " " ))
85
172
cmd := exec .Command (t .config .FfmpegBinPath , args ... )
86
173
87
174
// If progresss enabled, get stderr pipe and start progress process
@@ -192,7 +279,7 @@ func (t *Transcoder) validate() error {
192
279
}
193
280
194
281
// GetMetadata Returns metadata for the specified input file
195
- func (t * Transcoder ) GetMetadata () ( transcoder.Metadata , error ) {
282
+ func (t * Transcoder ) GetMetadata () (transcoder.Metadata , error ) {
196
283
197
284
if t .config .FfprobeBinPath != "" {
198
285
var outb , errb bytes.Buffer
@@ -298,11 +385,11 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
298
385
}
299
386
}
300
387
301
- timesec := utils .DurToSec (currentTime )
302
- dursec , _ := strconv .ParseFloat (t .metadata .GetFormat ().GetDuration (), 64 )
388
+ // timesec := utils.DurToSec(currentTime)
389
+ // dursec, _ := strconv.ParseFloat(t.metadata.GetFormat().GetDuration(), 64)
303
390
304
- progress := (timesec * 100 ) / dursec
305
- Progress .Progress = progress
391
+ // progress := (timesec * 100) / dursec
392
+ // Progress.Progress = progress
306
393
307
394
Progress .CurrentBitrate = currentBitrate
308
395
Progress .FramesProcessed = framesProcessed
0 commit comments