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

Commit 5980774

Browse files
authored
Merge pull request #11 from Borloxos/multipleOptionsAndOutputs
Multiple options and outputs
2 parents 2c44ff9 + 25708ec commit 5980774

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

ffmpeg/ffmpeg.go

+49-9
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 transcoder.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,9 +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()}
154+
return t
155+
}
156+
157+
// WithAdditionalOptions Appends an additional options object
158+
func (t *Transcoder) WithAdditionalOptions(opts transcoder.Options) transcoder.Transcoder {
159+
t.options = append(t.options, opts.GetStrArguments())
134160
return t
135161
}
136162

@@ -144,10 +170,24 @@ func (t *Transcoder) validate() error {
144170
return errors.New("missing input option")
145171
}
146172

147-
if t.output == "" {
173+
outputLength := len(t.output)
174+
175+
if outputLength == 0 {
148176
return errors.New("missing output option")
149177
}
150178

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 fmt.Errorf("output at index %d is an empty string", index)
188+
}
189+
}
190+
151191
return nil
152192
}
153193

transcoder.go

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ type Transcoder interface {
1212
Output(o string) Transcoder
1313
OutputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder
1414
WithOptions(opts Options) Transcoder
15+
WithAdditionalOptions(opts Options) Transcoder
1516
GetMetadata() (Metadata, error)
1617
}

0 commit comments

Comments
 (0)