7
7
"errors"
8
8
"fmt"
9
9
"io"
10
+ "log"
10
11
"os"
11
12
"os/exec"
12
13
"regexp"
@@ -24,10 +25,10 @@ type Transcoder struct {
24
25
output []string
25
26
options [][]string
26
27
metadata transcoder.Metadata
27
- inputPipeReader * io.ReadCloser
28
- outputPipeReader * io.ReadCloser
29
- inputPipeWriter * io.WriteCloser
30
- outputPipeWriter * io.WriteCloser
28
+ inputPipeReader io.ReadCloser
29
+ outputPipeReader io.ReadCloser
30
+ inputPipeWriter io.WriteCloser
31
+ outputPipeWriter io.WriteCloser
31
32
}
32
33
33
34
// New ...
@@ -40,8 +41,6 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
40
41
41
42
var stderrIn io.ReadCloser
42
43
43
- out := make (chan transcoder.Progress )
44
-
45
44
defer t .closePipes ()
46
45
47
46
// Validates config
@@ -95,7 +94,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
95
94
if t .config .ProgressEnabled && ! t .config .Verbose {
96
95
stderrIn , err = cmd .StderrPipe ()
97
96
if err != nil {
98
- return nil , fmt .Errorf ("Failed getting transcoding progress (%s) with args (%s) with error %s " , t .config .FfmpegBinPath , args , err )
97
+ return nil , fmt .Errorf ("failed getting transcoding progress (%s) with args (%s) with error %w " , t .config .FfmpegBinPath , args , err )
99
98
}
100
99
}
101
100
@@ -106,9 +105,10 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
106
105
// Start process
107
106
err = cmd .Start ()
108
107
if err != nil {
109
- return nil , fmt .Errorf ("Failed starting transcoding (%s) with args (%s) with error %s " , t .config .FfmpegBinPath , args , err )
108
+ return nil , fmt .Errorf ("failed starting transcoding (%s) with args (%s) with error %w " , t .config .FfmpegBinPath , args , err )
110
109
}
111
110
111
+ out := make (chan transcoder.Progress )
112
112
if t .config .ProgressEnabled && ! t .config .Verbose {
113
113
go func () {
114
114
t .progress (stderrIn , out )
@@ -117,12 +117,18 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
117
117
go func () {
118
118
defer close (out )
119
119
err = cmd .Wait ()
120
+ if err != nil {
121
+ log .Printf ("failed to transcoding (%s) with args (%s) with error %v" , t .config .FfmpegBinPath , args , err )
122
+ }
120
123
}()
121
124
} else {
122
125
err = cmd .Wait ()
126
+ if err != nil {
127
+ return nil , fmt .Errorf ("failed to transcoding (%s) with args (%s) with error %w" , t .config .FfmpegBinPath , args , err )
128
+ }
123
129
}
124
130
125
- return out , nil
131
+ return out , err
126
132
}
127
133
128
134
// Input ...
@@ -138,17 +144,17 @@ func (t *Transcoder) Output(arg string) transcoder.Transcoder {
138
144
}
139
145
140
146
// InputPipe ...
141
- func (t * Transcoder ) InputPipe (w * io.WriteCloser , r * io.ReadCloser ) transcoder.Transcoder {
142
- if & t .input == nil {
147
+ func (t * Transcoder ) InputPipe (w io.WriteCloser , r io.ReadCloser ) transcoder.Transcoder {
148
+ if len ( t .input ) == 0 {
143
149
t .inputPipeWriter = w
144
150
t .inputPipeReader = r
145
151
}
146
152
return t
147
153
}
148
154
149
155
// OutputPipe ...
150
- func (t * Transcoder ) OutputPipe (w * io.WriteCloser , r * io.ReadCloser ) transcoder.Transcoder {
151
- if & t .output == nil {
156
+ func (t * Transcoder ) OutputPipe (w io.WriteCloser , r io.ReadCloser ) transcoder.Transcoder {
157
+ if len ( t .output ) == 0 {
152
158
t .outputPipeWriter = w
153
159
t .outputPipeReader = r
154
160
}
@@ -201,7 +207,7 @@ func (t *Transcoder) validate() error {
201
207
// GetMetadata Returns metadata for the specified input file
202
208
func (t * Transcoder ) GetMetadata () (transcoder.Metadata , error ) {
203
209
204
- if t .config .FfprobeBinPath != "" {
210
+ if len ( t .config .FfprobeBinPath ) > 0 {
205
211
var outb , errb bytes.Buffer
206
212
207
213
input := t .input
@@ -225,7 +231,7 @@ func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) {
225
231
226
232
var metadata Metadata
227
233
228
- if err = json .Unmarshal ([] byte ( outb .String () ), & metadata ); err != nil {
234
+ if err = json .Unmarshal (outb .Bytes ( ), & metadata ); err != nil {
229
235
return nil , err
230
236
}
231
237
@@ -326,12 +332,10 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
326
332
// closePipes Closes pipes if opened
327
333
func (t * Transcoder ) closePipes () {
328
334
if t .inputPipeReader != nil {
329
- ipr := * t .inputPipeReader
330
- ipr .Close ()
335
+ t .inputPipeReader .Close ()
331
336
}
332
337
333
338
if t .outputPipeWriter != nil {
334
- opr := * t .outputPipeWriter
335
- opr .Close ()
339
+ t .outputPipeWriter .Close ()
336
340
}
337
341
}
0 commit comments