Skip to content

Commit 9e50c7f

Browse files
committed
add StartAndReturnCmd for abort option
1 parent 0fa8397 commit 9e50c7f

File tree

5 files changed

+115
-19
lines changed

5 files changed

+115
-19
lines changed

ffmpeg/ffmpeg.go

Lines changed: 101 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ import (
1010
"os"
1111
"os/exec"
1212
"regexp"
13-
"strconv"
1413
"strings"
1514

16-
"github.com/floostack/transcoder"
17-
"github.com/floostack/transcoder/utils"
15+
"github.com/Igor57/transcoder"
1816
)
1917

2018
// Transcoder ...
@@ -35,28 +33,116 @@ func New(cfg *Config) transcoder.Transcoder {
3533
return &Transcoder{config: cfg}
3634
}
3735

38-
// Start ...
39-
func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress, error) {
36+
// StartAndReturnCmd ...
37+
func (t *Transcoder) StartAndReturnCmd(optsBeforeInput transcoder.Options, opts transcoder.Options) (<-chan transcoder.Progress, *exec.Cmd, error) {
4038

4139
var stderrIn io.ReadCloser
42-
40+
var err error
4341
out := make(chan transcoder.Progress)
4442

4543
defer t.closePipes()
4644

4745
// Validates config
4846
if err := t.validate(); err != nil {
49-
return nil, err
47+
return nil, nil, err
5048
}
5149

5250
// Get file metadata
53-
_, err := t.GetMetadata()
51+
/*
52+
_, err := t.GetMetadata()
53+
if err != nil {
54+
return nil, err
55+
}
56+
*/
57+
args := append(optsBeforeInput.GetStrArguments(), []string{"-i", t.input}...)
58+
// Append input file and standard options
59+
args = append(args, opts.GetStrArguments()...)
60+
outputLength := len(t.output)
61+
optionsLength := len(t.options)
62+
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+
}
83+
84+
// Initialize command
85+
fmt.Println(strings.Join(args, " "))
86+
cmd := exec.Command(t.config.FfmpegBinPath, args...)
87+
88+
// If progresss enabled, get stderr pipe and start progress process
89+
if t.config.ProgressEnabled && !t.config.Verbose {
90+
stderrIn, err = cmd.StderrPipe()
91+
if err != nil {
92+
return nil, nil, fmt.Errorf("Failed getting transcoding progress (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err)
93+
}
94+
}
95+
96+
if t.config.Verbose {
97+
cmd.Stderr = os.Stdout
98+
}
99+
100+
// Start process
101+
err = cmd.Start()
54102
if err != nil {
103+
return nil, nil, fmt.Errorf("Failed starting transcoding (%s) with args (%s) with error %s", t.config.FfmpegBinPath, args, err)
104+
}
105+
106+
if t.config.ProgressEnabled && !t.config.Verbose {
107+
go func() {
108+
t.progress(stderrIn, out)
109+
}()
110+
111+
go func() {
112+
defer close(out)
113+
err = cmd.Wait()
114+
}()
115+
} else {
116+
err = cmd.Wait()
117+
}
118+
119+
return out, cmd, nil
120+
}
121+
122+
// Start ...
123+
func (t *Transcoder) Start(optsBeforeInput transcoder.Options, opts transcoder.Options) (<-chan transcoder.Progress, error) {
124+
125+
var stderrIn io.ReadCloser
126+
var err error
127+
out := make(chan transcoder.Progress)
128+
129+
defer t.closePipes()
130+
131+
// Validates config
132+
if err := t.validate(); err != nil {
55133
return nil, err
56134
}
57135

136+
// Get file metadata
137+
/*
138+
_, err := t.GetMetadata()
139+
if err != nil {
140+
return nil, err
141+
}
142+
*/
143+
args := append(optsBeforeInput.GetStrArguments(), []string{"-i", t.input}...)
58144
// Append input file and standard options
59-
args := append([]string{"-i", t.input}, opts.GetStrArguments()...)
145+
args = append(args, opts.GetStrArguments()...)
60146
outputLength := len(t.output)
61147
optionsLength := len(t.options)
62148

@@ -82,6 +168,7 @@ func (t *Transcoder) Start(opts transcoder.Options) (<-chan transcoder.Progress,
82168
}
83169

84170
// Initialize command
171+
fmt.Println(strings.Join(args, " "))
85172
cmd := exec.Command(t.config.FfmpegBinPath, args...)
86173

87174
// If progresss enabled, get stderr pipe and start progress process
@@ -192,7 +279,7 @@ func (t *Transcoder) validate() error {
192279
}
193280

194281
// GetMetadata Returns metadata for the specified input file
195-
func (t *Transcoder) GetMetadata() ( transcoder.Metadata, error) {
282+
func (t *Transcoder) GetMetadata() (transcoder.Metadata, error) {
196283

197284
if t.config.FfprobeBinPath != "" {
198285
var outb, errb bytes.Buffer
@@ -298,11 +385,11 @@ func (t *Transcoder) progress(stream io.ReadCloser, out chan transcoder.Progress
298385
}
299386
}
300387

301-
timesec := utils.DurToSec(currentTime)
302-
dursec, _ := strconv.ParseFloat(t.metadata.GetFormat().GetDuration(), 64)
388+
//timesec := utils.DurToSec(currentTime)
389+
//dursec, _ := strconv.ParseFloat(t.metadata.GetFormat().GetDuration(), 64)
303390

304-
progress := (timesec * 100) / dursec
305-
Progress.Progress = progress
391+
//progress := (timesec * 100) / dursec
392+
//Progress.Progress = progress
306393

307394
Progress.CurrentBitrate = currentBitrate
308395
Progress.FramesProcessed = framesProcessed

ffmpeg/metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ffmpeg
22

3-
import "github.com/floostack/transcoder"
3+
import "github.com/Igor57/transcoder"
44

55
// Metadata ...
66
type Metadata struct {

ffmpeg/options.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import (
77

88
// Options defines allowed FFmpeg arguments
99
type Options struct {
10+
Listen *int `flag:"-listen"`
11+
FilterComplex *string `flag:"-filter_complex"`
12+
ScThreshold *int `flag:"-sc_threshold"`
13+
Maps []string `flag:"-map"`
14+
HlsFlags *string `flag:"-hls_flags"`
15+
UseLocaltimeMkdir *int `flag:"-use_localtime_mkdir"`
16+
VarStreamMap *string `flag:"-var_stream_map"`
1017
Aspect *string `flag:"-aspect"`
1118
Resolution *string `flag:"-s"`
1219
VideoBitRate *string `flag:"-b:v"`
@@ -102,7 +109,7 @@ func (opts Options) GetStrArguments() []string {
102109
values = append(values, k, fmt.Sprintf("%v", v))
103110
}
104111
}
105-
112+
106113
if vi, ok := value.(*int); ok {
107114
values = append(values, flag, fmt.Sprintf("%d", *vi))
108115
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module github.com/floostack/transcoder
1+
module github.com/Igor57/transcoder
22

3-
go 1.13
3+
go 1.15

transcoder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package transcoder
22

33
import (
44
"io"
5+
"os/exec"
56
)
67

78
// Transcoder ...
89
type Transcoder interface {
9-
Start(opts Options) (<-chan Progress, error)
10+
Start(optsBeforeInput Options, opts Options) (<-chan Progress, error)
11+
StartAndReturnCmd(optsBeforeInput Options, opts Options) (<-chan Progress, *exec.Cmd, error)
1012
Input(i string) Transcoder
1113
InputPipe(w *io.WriteCloser, r *io.ReadCloser) Transcoder
1214
Output(o string) Transcoder

0 commit comments

Comments
 (0)