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

Commit b31707a

Browse files
committed
v0.1 add multi input support
1 parent 0fa8397 commit b31707a

File tree

11 files changed

+104
-99
lines changed

11 files changed

+104
-99
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
main.go
2-
build
2+
build
3+
example

README.md

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,20 @@
1-
# Golang Transcoding Library
2-
3-
<br />
4-
5-
<div align="center">
6-
<!-- Build status -->
7-
<a href="https://circleci.com/gh/floostack/transcoder">
8-
<img src="https://circleci.com/gh/floostack/transcoder.svg?style=svg" alt="Build Status" />
9-
</a>
10-
11-
<!-- Code Quality -->
12-
<a href="https://www.codacy.com/manual/floostack/transcoder?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=floostack/transcoder&amp;utm_campaign=Badge_Grade">
13-
<img src="https://app.codacy.com/project/badge/Grade/f8ee19ef723b4134bb8bb1f9c439959e" alt="Build Status" />
14-
</a>
15-
16-
</div>
17-
18-
<br />
19-
20-
<div align="center">
21-
<sub>Created by <a href="https://floostack.com">FlooStack</a>.</sub>
22-
</div>
1+
# Transcoder plus
232

243
## Features
254

265
<dl>
276
<dt>Ease use</dt>
287
<dd>Implement your own business logic around easy interfaces</dd>
29-
308
<dt>Transcoding progress</dt>
319
<dd>Obtain progress events generated by transcoding application process</dd>
3210
</dl>
3311

34-
## Download from Github
12+
add:支持多文件输入
13+
14+
## Download from GitLab
3515

3616
```shell
37-
go get github.com/floostack/transcoder
17+
go get -u http://gitlab.yiban.io/we-work-go-team/transcoder-plus
3818
```
3919

4020
## Example
@@ -45,31 +25,41 @@ package main
4525
import (
4626
"log"
4727

48-
ffmpeg "github.com/floostack/transcoder/ffmpeg"
28+
"transcoderplus/ffmpeg"
4929
)
5030

5131
func main() {
52-
53-
format := "mp4"
32+
33+
inputformat := "mulaw"
34+
filtercomplex := "amix=inputs=2"
35+
audiorate := 8000
36+
outputformat := "mp3"
5437
overwrite := true
5538

56-
opts := ffmpeg.Options{
57-
OutputFormat: &format,
58-
Overwrite: &overwrite,
39+
outputopts := ffmpeg.Options{
40+
OutputFormat: &outputformat,
41+
Overwrite: &overwrite,
42+
FilterComplex: &filtercomplex,
43+
}
44+
45+
inputopts := ffmpeg.Options{
46+
OutputFormat: &inputformat,
47+
AudioRate: &audiorate,
5948
}
6049

6150
ffmpegConf := &ffmpeg.Config{
62-
FfmpegBinPath: "/usr/local/bin/ffmpeg",
63-
FfprobeBinPath: "/usr/local/bin/ffprobe",
51+
FfmpegBinPath: "ffmpegPath",
52+
FfprobeBinPath: "ffprobePath",
6453
ProgressEnabled: true,
6554
}
6655

6756
progress, err := ffmpeg.
6857
New(ffmpegConf).
69-
Input("/tmp/avi").
70-
Output("/tmp/mp4").
71-
WithOptions(opts).
72-
Start(opts)
58+
Input("inputfile1Path").
59+
Input("inputfile2Path").
60+
Output("outputfile1Path").
61+
WithOptions(outputopts).
62+
Start(inputopts)
7363

7464
if err != nil {
7565
log.Fatal(err)
@@ -79,4 +69,9 @@ func main() {
7969
log.Printf("%+v", msg)
8070
}
8171
}
72+
8273
```
74+
75+
## 项目源地址
76+
77+
https://github.com/floostack/transcoder

config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package transcoder
1+
package transcoderplus
22

33
// Config ...
44
type Config interface{}

ffmpeg/ffmpeg.go

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,35 @@ import (
1313
"strconv"
1414
"strings"
1515

16-
"github.com/floostack/transcoder"
17-
"github.com/floostack/transcoder/utils"
16+
"transcoderplus"
17+
"transcoderplus/utils"
1818
)
1919

2020
// Transcoder ...
2121
type Transcoder struct {
2222
config *Config
23-
input string
23+
input []string
2424
output []string
2525
options [][]string
26-
metadata transcoder.Metadata
26+
metadata transcoderplus.Metadata
2727
inputPipeReader *io.ReadCloser
2828
outputPipeReader *io.ReadCloser
2929
inputPipeWriter *io.WriteCloser
3030
outputPipeWriter *io.WriteCloser
3131
}
3232

3333
// New ...
34-
func New(cfg *Config) transcoder.Transcoder {
34+
func New(cfg *Config) transcoderplus.Transcoder {
3535
return &Transcoder{config: cfg}
3636
}
3737

3838
// Start ...
39-
func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, error) {
39+
func (t *Transcoder) Start(opts transcoderplus.Options) (<-chan transcoderplus.Progress, error) {
4040

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

43-
out := make(chan transcoder.Progress)
44+
out := make(chan transcoderplus.Progress)
4445

4546
defer t.closePipes()
4647

@@ -49,18 +50,25 @@ 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+
// 不能使用,因为ffprobe不支持mulaw格式的识别
54+
//Get file metadata
55+
//_, err = t.GetMetadata()
56+
//if err != nil {
57+
// return nil, err
58+
//}
5759

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

63-
if outputLength == 1 && optionsLength == 0 {
71+
if optionsLength == 0 {
6472
// Just append the 1 output file we've got
6573
args = append(args, t.output[0])
6674
} else {
@@ -88,7 +96,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
8896
if t.config.ProgressEnabled && !t.config.Verbose {
8997
stderrIn, err = cmd.StderrPipe()
9098
if err != nil {
91-
return nil, fmt.Errorf("Failed getting transcoding progress (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err)
99+
return nil, fmt.Errorf("failed getting transcoding progress (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err)
92100
}
93101
}
94102

@@ -99,7 +107,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
99107
// Start process
100108
err = cmd.Start()
101109
if err != nil {
102-
return nil, fmt.Errorf("Failed starting transcoding (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err)
110+
return nil, fmt.Errorf("failed starting transcoding (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err)
103111
}
104112

105113
if t.config.ProgressEnabled && !t.config.Verbose {
@@ -119,19 +127,19 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
119127
}
120128

121129
// Input ...
122-
func (t *Transcoder) Input(arg string) transcoder.Transcoder {
123-
t.input = arg
130+
func (t *Transcoder) Input(arg string) transcoderplus.Transcoder {
131+
t.input = append(t.input, arg)
124132
return t
125133
}
126134

127135
// Output ...
128-
func (t *Transcoder) Output(arg string) transcoder.Transcoder {
136+
func (t *Transcoder) Output(arg string) transcoderplus.Transcoder {
129137
t.output = append(t.output, arg)
130138
return t
131139
}
132140

133141
// InputPipe ...
134-
func (t *Transcoder) InputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoder.Transcoder {
142+
func (t *Transcoder) InputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoderplus.Transcoder {
135143
if &t.input == nil {
136144
t.inputPipeWriter = w
137145
t.inputPipeReader = r
@@ -140,7 +148,7 @@ func (t *Transcoder) InputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoder.T
140148
}
141149

142150
// OutputPipe ...
143-
func (t *Transcoder) OutputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoder.Transcoder {
151+
func (t *Transcoder) OutputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoderplus.Transcoder {
144152
if &t.output == nil {
145153
t.outputPipeWriter = w
146154
t.outputPipeReader = r
@@ -149,13 +157,13 @@ func (t *Transcoder) OutputPipe(w *io.WriteCloser, r *io.ReadCloser) transcoder.
149157
}
150158

151159
// WithOptions Sets the options object
152-
func (t *Transcoder) WithOptions(opts transcoder.Options) transcoder.Transcoder {
160+
func (t *Transcoder) WithOptions(opts transcoderplus.Options) transcoderplus.Transcoder {
153161
t.options = [][]string{opts.GetStrArguments()}
154162
return t
155163
}
156164

157165
// WithAdditionalOptions Appends an additional options object
158-
func (t *Transcoder) WithAdditionalOptions(opts transcoder.Options) transcoder.Transcoder {
166+
func (t *Transcoder) WithAdditionalOptions(opts transcoderplus.Options) transcoderplus.Transcoder {
159167
t.options = append(t.options, opts.GetStrArguments())
160168
return t
161169
}
@@ -166,7 +174,7 @@ func (t *Transcoder) validate() error {
166174
return errors.New("ffmpeg binary path not found")
167175
}
168176

169-
if t.input == "" {
177+
if t.input == nil {
170178
return errors.New("missing input option")
171179
}
172180

@@ -192,44 +200,43 @@ func (t *Transcoder) validate() error {
192200
}
193201

194202
// GetMetadata Returns metadata for the specified input file
195-
func (t *Transcoder) GetMetadata() ( transcoder.Metadata, error) {
203+
func (t *Transcoder) GetMetadata() (transcoderplus.Metadata, error) {
196204

197205
if t.config.FfprobeBinPath != "" {
198-
var outb, errb bytes.Buffer
199-
200-
input := t.input
201-
202-
if t.inputPipeReader != nil {
203-
input = "pipe:"
204-
}
206+
//var metadata []Metadata
207+
var metapice Metadata
205208

206-
args := []string{"-i", input, "-print_format", "json", "-show_format", "-show_streams", "-show_error"}
209+
input := t.input[0]
207210

208-
cmd := exec.Command(t.config.FfprobeBinPath, args...)
209-
cmd.Stdout = &outb
210-
cmd.Stderr = &errb
211+
var outb, errb bytes.Buffer
211212

212-
err := cmd.Run()
213-
if err != nil {
214-
return nil, fmt.Errorf("error executing (%s) with args (%s) | error: %s | message: %s %s", t.config.FfprobeBinPath, args, err, outb.String(), errb.String())
215-
}
213+
if t.inputPipeReader != nil {
214+
input = "pipe:"
215+
}
216216

217-
var metadata Metadata
217+
args := []string{"-i", input, "-print_format", "json", "-show_format", "-show_streams", "-show_error"}
218218

219-
if err = json.Unmarshal([]byte(outb.String()), &metadata); err != nil {
220-
return nil, err
221-
}
219+
cmd := exec.Command(t.config.FfprobeBinPath, args...)
220+
cmd.Stdout = &outb
221+
cmd.Stderr = &errb
222+
err := cmd.Run()
223+
if err != nil {
224+
return nil, fmt.Errorf("error executing (%s) with args (%s) | error: %s | message: %s %s", t.config.FfprobeBinPath, args, err, outb.String(), errb.String())
225+
}
226+
if err := json.Unmarshal([]byte(outb.String()), &metapice); err != nil {
227+
return nil, err
228+
}
222229

223-
t.metadata = metadata
230+
t.metadata = metapice
224231

225-
return metadata, nil
232+
return metapice, nil
226233
}
227234

228235
return nil, errors.New("ffprobe binary not found")
229236
}
230237

231238
// progress sends through given channel the transcoding status
232-
func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress) {
239+
func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoderplus.Progress) {
233240

234241
defer stream.Close()
235242

@@ -298,6 +305,7 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
298305
}
299306
}
300307

308+
// 都可以删,因为没有ffprobe
301309
timesec := utils.DurToSec(currentTime)
302310
dursec, _ := strconv.ParseFloat(t.metadata.GetFormat().GetDuration(), 64)
303311

@@ -318,11 +326,11 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
318326
func (t *Transcoder) closePipes() {
319327
if t.inputPipeReader != nil {
320328
ipr := *t.inputPipeReader
321-
ipr.Close()
329+
_ = ipr.Close()
322330
}
323331

324332
if t.outputPipeWriter != nil {
325333
opr := *t.outputPipeWriter
326-
opr.Close()
334+
_ = opr.Close()
327335
}
328336
}

0 commit comments

Comments
 (0)