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

Commit d91fbf5

Browse files
authored
support input options (#32)
1 parent 9367de1 commit d91fbf5

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,19 @@ import (
5050

5151
func main() {
5252

53+
54+
hwaccel := "cuvid"
55+
videoCodec := "h264_cuvid"
56+
57+
inputOpts := ffmpeg.Options{
58+
Hwaccel: &hwaccel,
59+
VideoCodec: &videoCodec,
60+
}
61+
5362
format := "mp4"
5463
overwrite := true
5564

56-
opts := ffmpeg.Options{
65+
outputOpts := ffmpeg.Options{
5766
OutputFormat: &format,
5867
Overwrite: &overwrite,
5968
}
@@ -68,8 +77,9 @@ func main() {
6877
New(ffmpegConf).
6978
Input("/tmp/avi").
7079
Output("/tmp/mp4").
71-
WithOptions(opts).
72-
Start(opts)
80+
WithInputOptions(inputOpts).
81+
WithOutputOptions(outputOpts).
82+
Start()
7383

7484
if err != nil {
7585
log.Fatal(err)

ffmpeg/ffmpeg.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ type Transcoder struct {
2323
config *Config
2424
input string
2525
output []string
26-
options [][]string
26+
inputOptions []string
27+
outputOptions [][]string
2728
metadata transcoder.Metadata
2829
inputPipeReader *io.ReadCloser
2930
outputPipeReader *io.ReadCloser
@@ -38,7 +39,7 @@ func New(cfg *Config) transcoder.Transcoder {
3839
}
3940

4041
// Start ...
41-
func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, error) {
42+
func (t *Transcoder) Start() (<-chan transcoder.Progress, error) {
4243

4344
var stderrIn io.ReadCloser
4445

@@ -58,24 +59,30 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
5859
}
5960

6061
// Append input file and standard options
61-
args := append([]string{"-i", t.input}, opts.GetStrArguments()...)
62+
var args []string
63+
64+
if len(t.inputOptions) > 0 {
65+
args = append(args, t.inputOptions...)
66+
}
67+
68+
args = append(args, []string{"-i", t.input}...)
6269
outputLength := len(t.output)
63-
optionsLength := len(t.options)
70+
outputOptionsLength := len(t.outputOptions)
6471

65-
if outputLength == 1 && optionsLength == 0 {
72+
if outputLength == 1 && outputOptionsLength == 0 {
6673
// Just append the 1 output file we've got
6774
args = append(args, t.output[0])
6875
} else {
6976
for index, out := range t.output {
7077
// Get executable flags
7178
// If we are at the last output file but still have several options, append them all at once
72-
if index == outputLength-1 && outputLength < optionsLength {
73-
for i := index; i < len(t.options); i++ {
74-
args = append(args, t.options[i]...)
79+
if index == outputLength-1 && outputLength < outputOptionsLength {
80+
for i := index; i < len(t.outputOptions); i++ {
81+
args = append(args, t.outputOptions[i]...)
7582
}
7683
// Otherwise just append the current options
7784
} else {
78-
args = append(args, t.options[index]...)
85+
args = append(args, t.outputOptions[index]...)
7986
}
8087

8188
// Append output flag
@@ -158,15 +165,27 @@ func (t *Transcoder) OutputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoder.
158165
return t
159166
}
160167

161-
// WithOptions Sets the options object
162-
func (t *Transcoder) WithOptions(opts transcoder.Options) transcoder.Transcoder {
163-
t.options = [][]string{opts.GetStrArguments()}
168+
// WithInputOptions Sets the options object
169+
func (t *Transcoder) WithInputOptions(opts transcoder.Options) transcoder.Transcoder {
170+
t.inputOptions = opts.GetStrArguments()
171+
return t
172+
}
173+
174+
// WithAdditionalInputOptions Appends an additional options object
175+
func (t *Transcoder) WithAdditionalInputOptions(opts transcoder.Options) transcoder.Transcoder {
176+
t.inputOptions = append(t.inputOptions, opts.GetStrArguments()...)
177+
return t
178+
}
179+
180+
// WithOutputOptions Sets the options object
181+
func (t *Transcoder) WithOutputOptions(opts transcoder.Options) transcoder.Transcoder {
182+
t.outputOptions = [][]string{opts.GetStrArguments()}
164183
return t
165184
}
166185

167-
// WithAdditionalOptions Appends an additional options object
168-
func (t *Transcoder) WithAdditionalOptions(opts transcoder.Options) transcoder.Transcoder {
169-
t.options = append(t.options, opts.GetStrArguments())
186+
// WithAdditionalOutputOptions Appends an additional options object
187+
func (t *Transcoder) WithAdditionalOutputOptions(opts transcoder.Options) transcoder.Transcoder {
188+
t.outputOptions = append(t.outputOptions, opts.GetStrArguments())
170189
return t
171190
}
172191

@@ -196,7 +215,7 @@ func (t *Transcoder) validate() error {
196215

197216
// length of output files being greater than length of options would produce an invalid ffmpeg command
198217
// unless there is only 1 output file, which obviously wouldn't be a problem
199-
if outputLength > len(t.options) && outputLength != 1 {
218+
if outputLength > len(t.outputOptions) && outputLength != 1 {
200219
return errors.New("number of options and output files does not match")
201220
}
202221

transcoder.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77

88
// Transcoder ...
99
type Transcoder interface {
10-
Start(opts Options) (<-chan Progress, error)
10+
Start() (<-chan Progress, error)
1111
Input(i string) Transcoder
1212
InputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder
1313
Output(o string) Transcoder
1414
OutputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder
15-
WithOptions(opts Options) Transcoder
16-
WithAdditionalOptions(opts Options) Transcoder
15+
WithInputOptions(opts Options) Transcoder
16+
WithAdditionalInputOptions(opts Options) Transcoder
17+
WithOutputOptions(opts Options) Transcoder
18+
WithAdditionalOutputOptions(opts Options) Transcoder
1719
WithContext(ctx *context.Context) Transcoder
1820
GetMetadata() (Metadata, error)
1921
}

0 commit comments

Comments
 (0)