From df8dbf5a540e3bdb7541a1b268b027aa60183884 Mon Sep 17 00:00:00 2001 From: r6c Date: Thu, 6 Jan 2022 13:06:38 +0800 Subject: [PATCH] support input options --- README.md | 16 ++++++++++++--- ffmpeg/ffmpeg.go | 51 +++++++++++++++++++++++++++++++++--------------- transcoder.go | 8 +++++--- 3 files changed, 53 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 651b613..967d5b9 100644 --- a/README.md +++ b/README.md @@ -50,10 +50,19 @@ import ( func main() { + + hwaccel := "cuvid" + videoCodec := "h264_cuvid" + + inputOpts := ffmpeg.Options{ + Hwaccel: &hwaccel, + VideoCodec: &videoCodec, + } + format := "mp4" overwrite := true - opts := ffmpeg.Options{ + outputOpts := ffmpeg.Options{ OutputFormat: &format, Overwrite: &overwrite, } @@ -68,8 +77,9 @@ func main() { New(ffmpegConf). Input("/tmp/avi"). Output("/tmp/mp4"). - WithOptions(opts). - Start(opts) + WithInputOptions(inputOpts). + WithOutputOptions(outputOpts). + Start() if err != nil { log.Fatal(err) diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index ce262f8..6328104 100644 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -23,7 +23,8 @@ type Transcoder struct { config *Config input string output []string - options [][]string + inputOptions []string + outputOptions [][]string metadata transcoder.Metadata inputPipeReader *io.ReadCloser outputPipeReader *io.ReadCloser @@ -38,7 +39,7 @@ func New(cfg *Config) transcoder.Transcoder { } // Start ... -func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, error) { +func (t *Transcoder) Start() (<-chan transcoder.Progress, error) { var stderrIn io.ReadCloser @@ -58,24 +59,30 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, } // Append input file and standard options - args := append([]string{"-i", t.input}, opts.GetStrArguments()...) + var args []string + + if len(t.inputOptions) > 0 { + args = append(args, t.inputOptions...) + } + + args = append(args, []string{"-i", t.input}...) outputLength := len(t.output) - optionsLength := len(t.options) + outputOptionsLength := len(t.outputOptions) - if outputLength == 1 && optionsLength == 0 { + if outputLength == 1 && outputOptionsLength == 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]...) + if index == outputLength-1 && outputLength < outputOptionsLength { + for i := index; i < len(t.outputOptions); i++ { + args = append(args, t.outputOptions[i]...) } // Otherwise just append the current options } else { - args = append(args, t.options[index]...) + args = append(args, t.outputOptions[index]...) } // Append output flag @@ -158,15 +165,27 @@ func (t *Transcoder) OutputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoder. return t } -// WithOptions Sets the options object -func (t *Transcoder) WithOptions(opts transcoder.Options) transcoder.Transcoder { - t.options = [][]string{opts.GetStrArguments()} +// WithInputOptions Sets the options object +func (t *Transcoder) WithInputOptions(opts transcoder.Options) transcoder.Transcoder { + t.inputOptions = opts.GetStrArguments() + return t +} + +// WithAdditionalInputOptions Appends an additional options object +func (t *Transcoder) WithAdditionalInputOptions(opts transcoder.Options) transcoder.Transcoder { + t.inputOptions = append(t.inputOptions, opts.GetStrArguments()...) + return t +} + +// WithOutputOptions Sets the options object +func (t *Transcoder) WithOutputOptions(opts transcoder.Options) transcoder.Transcoder { + t.outputOptions = [][]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()) +// WithAdditionalOutputOptions Appends an additional options object +func (t *Transcoder) WithAdditionalOutputOptions(opts transcoder.Options) transcoder.Transcoder { + t.outputOptions = append(t.outputOptions, opts.GetStrArguments()) return t } @@ -196,7 +215,7 @@ func (t *Transcoder) validate() error { // 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 { + if outputLength > len(t.outputOptions) && outputLength != 1 { return errors.New("number of options and output files does not match") } diff --git a/transcoder.go b/transcoder.go index c93bc4a..f53ca4e 100644 --- a/transcoder.go +++ b/transcoder.go @@ -7,13 +7,15 @@ import ( // Transcoder ... type Transcoder interface { - Start(opts Options) (<-chan Progress, error) + Start() (<-chan Progress, error) Input(i string) Transcoder InputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder Output(o string) Transcoder OutputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder - WithOptions(opts Options) Transcoder - WithAdditionalOptions(opts Options) Transcoder + WithInputOptions(opts Options) Transcoder + WithAdditionalInputOptions(opts Options) Transcoder + WithOutputOptions(opts Options) Transcoder + WithAdditionalOutputOptions(opts Options) Transcoder WithContext(ctx *context.Context) Transcoder GetMetadata() (Metadata, error) }