-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjob.go
169 lines (131 loc) · 2.99 KB
/
job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package ffmpeg
import (
"context"
"io"
"os"
"os/exec"
"strings"
"sync"
)
type jobInput struct {
input InputMedia
options []CliOption
}
type jobOutput struct {
output OutputMedia
options []CliOption
}
// Job represents an ffmpeg job
type Job struct {
cfg *Configuration
globalOptions []CliOption
inputs []*jobInput
outputs []*jobOutput
}
// NewJob creates a new job
func (c *Configuration) NewJob(options ...CliOption) *Job {
return &Job{
cfg: c,
globalOptions: options,
}
}
// AddInput adds an input
func (j *Job) AddInput(input InputMedia, options ...CliOption) {
j.inputs = append(j.inputs, &jobInput{
input: input,
options: options,
})
}
// AddInputFile adds an input file
func (j *Job) AddInputFile(url string, options ...CliOption) (*Metadata, error) {
input, metadata, err := j.cfg.Probe(url)
if err != nil {
return nil, err
}
j.AddInput(input, options...)
return metadata, nil
}
func (j *Job) addOutput(output OutputMedia, options []CliOption) {
j.outputs = append(j.outputs, &jobOutput{
output: output,
options: options,
})
}
// AddOutputFile adds an output file
func (j *Job) AddOutputFile(file string, options ...CliOption) {
j.addOutput(mediaFile(file), options)
}
// Start starts the job
func (j *Job) Start(ctx context.Context) (*os.Process, <-chan Status, error) {
return j.StartDebug(ctx, nil)
}
// StartDebug starts the job and writes all output to w
func (j *Job) StartDebug(ctx context.Context, w io.Writer) (*os.Process, <-chan Status, error) {
var args []string
for _, option := range j.globalOptions {
args = append(args, option.args()...)
}
args = append(args, "-hide_banner", "-stats")
args, extra, err := j.buildArgs(args)
if err != nil {
return nil, nil, err
}
cmd := exec.CommandContext(ctx, j.cfg.ffmpeg, args...)
cmd.ExtraFiles = extra
var mu sync.Mutex
var finished bool
statusChan := make(chan Status, 1)
sendStatusUnsafe := func(status Status) {
// flush channel to prevent the user from reading out of date progress
select {
case <-statusChan:
default:
}
statusChan <- status
}
sendStatus := func(status Status) {
mu.Lock()
defer mu.Unlock()
if finished {
return
}
sendStatusUnsafe(status)
}
sendFinalStatus := func(status Status) {
mu.Lock()
defer mu.Unlock()
defer close(statusChan)
finished = true
sendStatusUnsafe(status)
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, nil, err
}
if w != nil {
w.Write([]byte(strings.Join(cmd.Args, " ") + "\n\n"))
}
go parseProgress(stderr, w, sendStatus)
if err := cmd.Start(); err != nil {
return nil, nil, err
}
go func() {
defer j.cleanup()
if err := cmd.Wait(); err != nil {
sendFinalStatus(&Error{
Arguments: args,
error: err,
})
} else {
sendFinalStatus(&Done{})
}
}()
return cmd.Process, statusChan, nil
}
func flattenOptions(options []CliOption) []string {
var args []string
for _, option := range options {
args = append(args, option.args()...)
}
return args
}