Skip to content

Commit cb57beb

Browse files
author
xh
committed
make it possible to use multiple options and output files and make options set in the transcoder object available in general
1 parent 9a7b7ac commit cb57beb

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

ffmpeg/ffmpeg.go

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
type Transcoder struct {
2222
config *Config
2323
input string
24-
output string
25-
options []string
24+
output []string
25+
options [][]string
2626
metadata *Metadata
2727
inputPipeReader *io.ReadCloser
2828
outputPipeReader *io.ReadCloser
@@ -55,11 +55,31 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
5555
return nil, err
5656
}
5757

58-
// Get executable flags
58+
// Append input file and standard options
5959
args := append([]string{"-i", t.input}, opts.GetStrArguments()...)
60+
outputLength := len(t.output)
61+
optionsLength := len(t.options)
6062

61-
// Append output flag
62-
args = append(args, []string{t.output}...)
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+
}
6383

6484
// Initialize command
6585
cmd := exec.Command(t.config.FfmpegBinPath, args...)
@@ -106,7 +126,7 @@ func (t *Transcoder) Input(arg string) transcoder.Transcoder {
106126

107127
// Output ...
108128
func (t *Transcoder) Output(arg string) transcoder.Transcoder {
109-
t.output = arg
129+
t.output = append(t.output, arg)
110130
return t
111131
}
112132

@@ -128,14 +148,15 @@ func (t *Transcoder) OutputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoder.
128148
return t
129149
}
130150

131-
// WithOptions ...
151+
// WithOptions Sets the options object
132152
func (t *Transcoder) WithOptions(opts transcoder.Options) transcoder.Transcoder {
133-
t.options = opts.GetStrArguments()
153+
t.options = [][]string{opts.GetStrArguments()}
134154
return t
135155
}
136156

157+
// WithAdditionalOptions Appends an additional options object
137158
func (t *Transcoder) WithAdditionalOptions(opts transcoder.Options) transcoder.Transcoder {
138-
t.options = append(t.options, opts.GetStrArguments()...)
159+
t.options = append(t.options, opts.GetStrArguments())
139160
return t
140161
}
141162

@@ -149,10 +170,24 @@ func (t *Transcoder) validate() error {
149170
return errors.New("missing input option")
150171
}
151172

152-
if t.output == "" {
173+
outputLength := len(t.output)
174+
175+
if outputLength == 0 {
153176
return errors.New("missing output option")
154177
}
155178

179+
// length of output files being greater than length of options would produce an invalid ffmpeg command
180+
// unless there is only 1 output file, which obviously wouldn't be a problem
181+
if outputLength > len(t.options) && outputLength != 1 {
182+
return errors.New("number of options and output files does not match")
183+
}
184+
185+
for index, output := range t.output {
186+
if output == "" {
187+
return errors.New(fmt.Sprintf("output at index %d is an empty string", index))
188+
}
189+
}
190+
156191
return nil
157192
}
158193

transcoder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ type Transcoder interface {
1010
Output(o string) Transcoder
1111
OutputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder
1212
WithOptions(opts Options) Transcoder
13+
WithAdditionalOptions(opts Options) Transcoder
1314
}

0 commit comments

Comments
 (0)