Skip to content

Commit b9f1715

Browse files
authored
Merge pull request #1 from floostack/master
Update fork repository
2 parents 4697624 + aa6374b commit b9f1715

File tree

8 files changed

+529
-88
lines changed

8 files changed

+529
-88
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 FlooStack
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

ffmpeg/ffmpeg.go

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
type Transcoder struct {
2222
config *Config
2323
input string
24-
output string
25-
options []string
26-
metadata *Metadata
24+
output []string
25+
options [][]string
26+
metadata transcoder.Metadata
2727
inputPipeReader *io.ReadCloser
2828
outputPipeReader *io.ReadCloser
2929
inputPipeWriter *io.WriteCloser
@@ -50,16 +50,36 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
5050
}
5151

5252
// Get file metadata
53-
_, err := t.getMetadata()
53+
_, err := t.GetMetadata()
5454
if err != nil {
5555
return nil, err
5656
}
5757

58-
// Get executable flags
58+
// Append input file and standard options
5959
args := append([]string{"-i", t.input}, opts.GetStrArguments()...)
60+
outputLength := len(t.output)
61+
optionsLength := len(t.options)
6062

61-
// Append output flag
62-
args = append(args, []string{t.output}...)
63+
if outputLength == 1 && optionsLength == 0 {
64+
// Just append the 1 output file we've got
65+
args = append(args, t.output[0])
66+
} else {
67+
for index, out := range t.output {
68+
// Get executable flags
69+
// If we are at the last output file but still have several options, append them all at once
70+
if index == outputLength-1 && outputLength < optionsLength {
71+
for i := index; i < len(t.options); i++ {
72+
args = append(args, t.options[i]...)
73+
}
74+
// Otherwise just append the current options
75+
} else {
76+
args = append(args, t.options[index]...)
77+
}
78+
79+
// Append output flag
80+
args = append(args, out)
81+
}
82+
}
6383

6484
// Initialize command
6585
cmd := exec.Command(t.config.FfmpegBinPath, args...)
@@ -106,7 +126,7 @@ func (t *Transcoder) Input(arg string) transcoder.Transcoder {
106126

107127
// Output ...
108128
func (t *Transcoder) Output(arg string) transcoder.Transcoder {
109-
t.output = arg
129+
t.output = append(t.output, arg)
110130
return t
111131
}
112132

@@ -128,9 +148,15 @@ func (t *Transcoder) OutputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoder.
128148
return t
129149
}
130150

131-
// WithOptions ...
151+
// WithOptions Sets the options object
132152
func (t *Transcoder) WithOptions(opts transcoder.Options) transcoder.Transcoder {
133-
t.options = opts.GetStrArguments()
153+
t.options = [][]string{opts.GetStrArguments()}
154+
return t
155+
}
156+
157+
// WithAdditionalOptions Appends an additional options object
158+
func (t *Transcoder) WithAdditionalOptions(opts transcoder.Options) transcoder.Transcoder {
159+
t.options = append(t.options, opts.GetStrArguments())
134160
return t
135161
}
136162

@@ -144,14 +170,29 @@ func (t *Transcoder) validate() error {
144170
return errors.New("missing input option")
145171
}
146172

147-
if t.output == "" {
173+
outputLength := len(t.output)
174+
175+
if outputLength == 0 {
148176
return errors.New("missing output option")
149177
}
150178

179+
// length of output files being greater than length of options would produce an invalid ffmpeg command
180+
// unless there is only 1 output file, which obviously wouldn't be a problem
181+
if outputLength > len(t.options) && outputLength != 1 {
182+
return errors.New("number of options and output files does not match")
183+
}
184+
185+
for index, output := range t.output {
186+
if output == "" {
187+
return fmt.Errorf("output at index %d is an empty string", index)
188+
}
189+
}
190+
151191
return nil
152192
}
153193

154-
func (t *Transcoder) getMetadata() (metadata *Metadata, err error) {
194+
// GetMetadata Returns metadata for the specified input file
195+
func (t *Transcoder) GetMetadata() ( transcoder.Metadata, error) {
155196

156197
if t.config.FfprobeBinPath != "" {
157198
var outb, errb bytes.Buffer
@@ -173,6 +214,8 @@ func (t *Transcoder) getMetadata() (metadata *Metadata, err error) {
173214
return nil, fmt.Errorf("error executing (%s) with args (%s) | error: %s | message: %s %s", t.config.FfprobeBinPath, args, err, outb.String(), errb.String())
174215
}
175216

217+
var metadata Metadata
218+
176219
if err = json.Unmarshal([]byte(outb.String()), &metadata); err != nil {
177220
return nil, err
178221
}
@@ -256,7 +299,7 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
256299
}
257300

258301
timesec := utils.DurToSec(currentTime)
259-
dursec, _ := strconv.ParseFloat(t.metadata.Format.Duration, 64)
302+
dursec, _ := strconv.ParseFloat(t.metadata.GetFormat().GetDuration(), 64)
260303

261304
progress := (timesec * 100) / dursec
262305
Progress.Progress = progress

0 commit comments

Comments
 (0)