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

Commit 33aa4e6

Browse files
committed
update
1 parent aad936c commit 33aa4e6

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

ffmpeg/ffmpeg.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"io"
10+
"log"
1011
"os"
1112
"os/exec"
1213
"regexp"
@@ -24,10 +25,10 @@ type Transcoder struct {
2425
output []string
2526
options [][]string
2627
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
3132
}
3233

3334
// New ...
@@ -40,8 +41,6 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
4041

4142
var stderrIn io.ReadCloser
4243

43-
out := make(chan transcoder.Progress)
44-
4544
defer t.closePipes()
4645

4746
// Validates config
@@ -95,7 +94,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
9594
if t.config.ProgressEnabled && !t.config.Verbose {
9695
stderrIn, err = cmd.StderrPipe()
9796
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)
9998
}
10099
}
101100

@@ -106,9 +105,10 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
106105
// Start process
107106
err = cmd.Start()
108107
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)
110109
}
111110

111+
out := make(chan transcoder.Progress)
112112
if t.config.ProgressEnabled && !t.config.Verbose {
113113
go func() {
114114
t.progress(stderrIn, out)
@@ -117,12 +117,18 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
117117
go func() {
118118
defer close(out)
119119
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+
}
120123
}()
121124
} else {
122125
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+
}
123129
}
124130

125-
return out, nil
131+
return out, err
126132
}
127133

128134
// Input ...
@@ -138,17 +144,17 @@ func (t *Transcoder) Output(arg string) transcoder.Transcoder {
138144
}
139145

140146
// 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 {
143149
t.inputPipeWriter = w
144150
t.inputPipeReader = r
145151
}
146152
return t
147153
}
148154

149155
// 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 {
152158
t.outputPipeWriter = w
153159
t.outputPipeReader = r
154160
}
@@ -201,7 +207,7 @@ func (t *Transcoder) validate() error {
201207
// GetMetadata Returns metadata for the specified input file
202208
func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) {
203209

204-
if t.config.FfprobeBinPath != "" {
210+
if len(t.config.FfprobeBinPath) > 0 {
205211
var outb, errb bytes.Buffer
206212

207213
input := t.input
@@ -225,7 +231,7 @@ func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) {
225231

226232
var metadata Metadata
227233

228-
if err = json.Unmarshal([]byte(outb.String()), &metadata); err != nil {
234+
if err = json.Unmarshal(outb.Bytes(), &metadata); err != nil {
229235
return nil, err
230236
}
231237

@@ -326,12 +332,10 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
326332
// closePipes Closes pipes if opened
327333
func (t *Transcoder) closePipes() {
328334
if t.inputPipeReader != nil {
329-
ipr := *t.inputPipeReader
330-
ipr.Close()
335+
t.inputPipeReader.Close()
331336
}
332337

333338
if t.outputPipeWriter != nil {
334-
opr := *t.outputPipeWriter
335-
opr.Close()
339+
t.outputPipeWriter.Close()
336340
}
337341
}

transcoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
type Transcoder interface {
99
Start(opts Options) (<-chan Progress, error)
1010
Input(i string) Transcoder
11-
InputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder
11+
InputPipe(w io.WriteCloser, r io.ReadCloser) Transcoder
1212
Output(o string) Transcoder
13-
OutputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder
13+
OutputPipe(w io.WriteCloser, r io.ReadCloser) Transcoder
1414
WithOptions(opts Options) Transcoder
1515
WithAdditionalOptions(opts Options) Transcoder
1616
GetMetadata() (Metadata, error)

0 commit comments

Comments
 (0)