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

Commit f704851

Browse files
committed
完成改写,支持多输入
1 parent ded0ae3 commit f704851

File tree

5 files changed

+50
-121
lines changed

5 files changed

+50
-121
lines changed

example/testfile/015871542567

Lines changed: 0 additions & 81 deletions
This file was deleted.

example/testfile/018674002817

Lines changed: 0 additions & 1 deletion
This file was deleted.

example/testfile/output.mp3

-27.5 KB
Binary file not shown.

ffmpeg/ffmpeg.go

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func New(cfg *Config) transcoder.Transcoder {
3939
func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, error) {
4040

4141
var stderrIn io.ReadCloser
42+
var err error
4243

4344
out := make(chan transcoder.Progress)
4445

@@ -49,20 +50,24 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
4950
return nil, err
5051
}
5152

52-
// Get file metadata
53-
_, err := t.GetMetadata()
54-
if err != nil {
55-
return nil, err
56-
}
53+
//Get file metadata
54+
//_, err := t.GetMetadata()
55+
//if err != nil {
56+
// return nil, err
57+
//}
5758

5859
// Append input file and standard options
59-
arg := []string{"-i"}
60+
arg := make([]string, 0)
6061
arg = append(arg, t.input...)
61-
args := append(arg, opts.GetStrArguments()...)
62+
args := make([]string, 0)
63+
for _, input := range t.input {
64+
args = append(args, opts.GetStrArguments()...)
65+
args = append(args, []string{"-i", input}...)
66+
}
6267
outputLength := len(t.output)
6368
optionsLength := len(t.options)
6469

65-
if outputLength == 1 && optionsLength == 0 {
70+
if optionsLength == 0 {
6671
// Just append the 1 output file we've got
6772
args = append(args, t.output[0])
6873
} else {
@@ -90,7 +95,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
9095
if t.config.ProgressEnabled && !t.config.Verbose {
9196
stderrIn, err = cmd.StderrPipe()
9297
if err != nil {
93-
return nil, fmt.Errorf("Failed getting transcoding progress (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err)
98+
return nil, fmt.Errorf("failed getting transcoding progress (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err)
9499
}
95100
}
96101

@@ -101,7 +106,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
101106
// Start process
102107
err = cmd.Start()
103108
if err != nil {
104-
return nil, fmt.Errorf("Failed starting transcoding (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err)
109+
return nil, fmt.Errorf("failed starting transcoding (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err)
105110
}
106111

107112
if t.config.ProgressEnabled && !t.config.Verbose {
@@ -122,7 +127,16 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
122127

123128
// Input ...
124129
func (t *Transcoder) Input(arg string) transcoder.Transcoder {
125-
t.input = append(t.input,arg)
130+
//args := make([]string, 0)
131+
//optionsLength := len(t.options)
132+
//if optionsLength != 0 {
133+
// args = append(args, t.options[0]...)
134+
// t.options = t.options[1:]
135+
//}
136+
//args = append(args, "-i")
137+
//args = append(args, arg)
138+
//arg = strings.Join(args, " ")
139+
t.input = append(t.input, arg)
126140
return t
127141
}
128142

@@ -194,41 +208,37 @@ func (t *Transcoder) validate() error {
194208
}
195209

196210
// GetMetadata Returns metadata for the specified input file
197-
func (t *Transcoder) GetMetadata() ( transcoder.Metadata, error) {
211+
func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) {
198212

199213
if t.config.FfprobeBinPath != "" {
200214
var outb, errb bytes.Buffer
215+
var metadata []Metadata
216+
var metapice Metadata
201217

202-
input := t.input
203-
204-
if t.inputPipeReader != nil {
205-
input = []string{"pipe:"}
206-
}
207-
208-
args := []string{"-i"}
209-
controlStr := []string{"-print_format", "json", "-show_format", "-show_streams", "-show_error"}
210-
args = append(args, input...)
211-
args = append(args, controlStr...)
212-
//args := []string{"-i", input, "-print_format", "json", "-show_format", "-show_streams", "-show_error"}
218+
for _, input := range t.input {
213219

214-
cmd := exec.Command(t.config.FfprobeBinPath, args...)
215-
cmd.Stdout = &outb
216-
cmd.Stderr = &errb
220+
if t.inputPipeReader != nil {
221+
input = "pipe:"
222+
}
217223

218-
err := cmd.Run()
219-
if err != nil {
220-
return nil, fmt.Errorf("error executing (%s) with args (%s) | error: %s | message: %s %s", t.config.FfprobeBinPath, args, err, outb.String(), errb.String())
221-
}
224+
args := []string{"-i", input, "-print_format", "json", "-show_format", "-show_streams", "-show_error"}
222225

223-
var metadata Metadata
226+
cmd := exec.Command(t.config.FfprobeBinPath, args...)
227+
cmd.Stdout = &outb
228+
cmd.Stderr = &errb
229+
err := cmd.Run()
230+
if err != nil {
231+
return nil, fmt.Errorf("error executing (%s) with args (%s) | error: %s | message: %s %s", t.config.FfprobeBinPath, args, err, outb.String(), errb.String())
232+
}
233+
if err := json.Unmarshal([]byte(outb.String()), &metapice); err != nil {
234+
return nil, err
235+
}
224236

225-
if err = json.Unmarshal([]byte(outb.String()), &metadata); err != nil {
226-
return nil, err
237+
t.metadata = metapice
238+
metadata = append(metadata, metapice)
227239
}
228240

229-
t.metadata = metadata
230-
231-
return metadata, nil
241+
return metapice, nil
232242
}
233243

234244
return nil, errors.New("ffprobe binary not found")
@@ -324,11 +334,11 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
324334
func (t *Transcoder) closePipes() {
325335
if t.inputPipeReader != nil {
326336
ipr := *t.inputPipeReader
327-
ipr.Close()
337+
_ = ipr.Close()
328338
}
329339

330340
if t.outputPipeWriter != nil {
331341
opr := *t.outputPipeWriter
332-
opr.Close()
342+
_ = opr.Close()
333343
}
334344
}

ffmpeg/options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type Options struct {
6565
PixFmt *string `flag:"-pix_fmt"`
6666
WhiteListProtocols []string `flag:"-protocol_whitelist"`
6767
Overwrite *bool `flag:"-y"`
68+
FilterComplex *string `flag:"-filter_complex"`
6869
ExtraArgs map[string]interface{}
6970
}
7071

@@ -102,7 +103,7 @@ func (opts Options) GetStrArguments() []string {
102103
values = append(values, k, fmt.Sprintf("%v", v))
103104
}
104105
}
105-
106+
106107
if vi, ok := value.(*int); ok {
107108
values = append(values, flag, fmt.Sprintf("%d", *vi))
108109
}

0 commit comments

Comments
 (0)