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

support input options #32

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
51 changes: 35 additions & 16 deletions ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
@@ -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")
}

8 changes: 5 additions & 3 deletions transcoder.go
Original file line number Diff line number Diff line change
@@ -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)
}