diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index 51097b0..ea84fbc 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -21,8 +21,8 @@ import ( type Transcoder struct { config *Config input string - output string - options []string + output []string + options [][]string metadata transcoder.Metadata inputPipeReader *io.ReadCloser outputPipeReader *io.ReadCloser @@ -55,11 +55,31 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, return nil, err } - // Get executable flags + // Append input file and standard options args := append([]string{"-i", t.input}, opts.GetStrArguments()...) + outputLength := len(t.output) + optionsLength := len(t.options) - // Append output flag - args = append(args, []string{t.output}...) + if outputLength == 1 && optionsLength == 0 { + // Just append the 1 output file we've got + args = append(args, t.output[0]) + } else { + for index, out := range t.output { + // Get executable flags + // If we are at the last output file but still have several options, append them all at once + if index == outputLength-1 && outputLength < optionsLength { + for i := index; i < len(t.options); i++ { + args = append(args, t.options[i]...) + } + // Otherwise just append the current options + } else { + args = append(args, t.options[index]...) + } + + // Append output flag + args = append(args, out) + } + } // Initialize command cmd := exec.Command(t.config.FfmpegBinPath, args...) @@ -106,7 +126,7 @@ func (t *Transcoder) Input(arg string) transcoder.Transcoder { // Output ... func (t *Transcoder) Output(arg string) transcoder.Transcoder { - t.output = arg + t.output = append(t.output, arg) return t } @@ -128,9 +148,15 @@ func (t *Transcoder) OutputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoder. return t } -// WithOptions ... +// WithOptions Sets the options object func (t *Transcoder) WithOptions(opts transcoder.Options) transcoder.Transcoder { - t.options = opts.GetStrArguments() + t.options = [][]string{opts.GetStrArguments()} + return t +} + +// WithAdditionalOptions Appends an additional options object +func (t *Transcoder) WithAdditionalOptions(opts transcoder.Options) transcoder.Transcoder { + t.options = append(t.options, opts.GetStrArguments()) return t } @@ -144,10 +170,24 @@ func (t *Transcoder) validate() error { return errors.New("missing input option") } - if t.output == "" { + outputLength := len(t.output) + + if outputLength == 0 { return errors.New("missing output option") } + // length of output files being greater than length of options would produce an invalid ffmpeg command + // unless there is only 1 output file, which obviously wouldn't be a problem + if outputLength > len(t.options) && outputLength != 1 { + return errors.New("number of options and output files does not match") + } + + for index, output := range t.output { + if output == "" { + return fmt.Errorf("output at index %d is an empty string", index) + } + } + return nil } diff --git a/transcoder.go b/transcoder.go index e8e260f..9e58206 100644 --- a/transcoder.go +++ b/transcoder.go @@ -12,5 +12,6 @@ type Transcoder interface { Output(o string) Transcoder OutputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder WithOptions(opts Options) Transcoder + WithAdditionalOptions(opts Options) Transcoder GetMetadata() (Metadata, error) }