Thanks to visit codestin.com
Credit goes to github.com

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

Multiple options and outputs #11

Merged
merged 4 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
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
58 changes: 49 additions & 9 deletions ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions transcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}