From 9a7b7ac512a7d065fd4ed0744b323a195f94459f Mon Sep 17 00:00:00 2001 From: xh Date: Wed, 14 Oct 2020 15:28:10 +0200 Subject: [PATCH 1/3] Adds method to append additional options --- ffmpeg/ffmpeg.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index 013bc6a..14fc2e8 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -134,6 +134,11 @@ func (t *Transcoder) WithOptions(opts transcoder.Options) transcoder.Transcoder return t } +func (t *Transcoder) WithAdditionalOptions(opts transcoder.Options) transcoder.Transcoder { + t.options = append(t.options, opts.GetStrArguments()...) + return t +} + // validate ... func (t *Transcoder) validate() error { if t.config.FfmpegBinPath == "" { From cb57beb39e9cc666f2008f10ca936cf7c63f6f05 Mon Sep 17 00:00:00 2001 From: xh Date: Thu, 15 Oct 2020 11:56:09 +0200 Subject: [PATCH 2/3] make it possible to use multiple options and output files and make options set in the transcoder object available in general --- ffmpeg/ffmpeg.go | 55 +++++++++++++++++++++++++++++++++++++++--------- transcoder.go | 1 + 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index 14fc2e8..3ff0487 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 *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,14 +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()...) + t.options = append(t.options, opts.GetStrArguments()) return t } @@ -149,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 errors.New(fmt.Sprintf("output at index %d is an empty string", index)) + } + } + return nil } diff --git a/transcoder.go b/transcoder.go index a4da0b6..012055c 100644 --- a/transcoder.go +++ b/transcoder.go @@ -10,4 +10,5 @@ type Transcoder interface { Output(o string) Transcoder OutputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder WithOptions(opts Options) Transcoder + WithAdditionalOptions(opts Options) Transcoder } From 10c7d3155d72f80992e34b4abd339798b0ec8465 Mon Sep 17 00:00:00 2001 From: xh Date: Thu, 15 Oct 2020 14:31:00 +0200 Subject: [PATCH 3/3] replace error.New(fmt.Sprintf) with fmt.Errorf --- ffmpeg/ffmpeg.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index 3ff0487..9ac83ab 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -184,7 +184,7 @@ func (t *Transcoder) validate() error { for index, output := range t.output { if output == "" { - return errors.New(fmt.Sprintf("output at index %d is an empty string", index)) + return fmt.Errorf("output at index %d is an empty string", index) } }