-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffmpeg.go
308 lines (255 loc) · 5.54 KB
/
ffmpeg.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package ffmpegbin
import (
"errors"
"fmt"
"io"
"github.com/nickalie/go-binwrapper"
)
type FFmpeg struct {
*binwrapper.BinWrapper
inputFiles []string
input io.Reader
outputFile string
output io.Writer
format string
audioBitrate int
videoBitrate int
videoCodec string
audioCodec string
seek int
vframes *int
duration int
rate int
loop int
// webp
lossless bool
compressionLevel *int
qscale *int
filterComplex string
mapping string
movflags string
preset Preset
removeMetadata bool
noVideo bool
// extra arg
extraParrams []ExtraParram
}
type ExtraParram struct {
Key string
Values []string
}
type Preset string
const (
PresetUltraFast Preset = "ultrafast"
PresetSuperFast Preset = "superfast"
PresetVeryFast Preset = "veryfast"
PresetFaster Preset = "faster"
PresetFast Preset = "fast"
PresetMedium Preset = "medium"
PresetSlow Preset = "slow"
PresetSlower Preset = "slower"
PresetVerySlow Preset = "veryslow"
PresetPlacebo Preset = "placebo"
)
func (p Preset) String() string {
return string(p)
}
func NewFFmpeg() *FFmpeg {
bin := binwrapper.NewBinWrapper().ExecPath("ffmpeg").AutoExe().Debug()
return &FFmpeg{
BinWrapper: bin,
}
}
func (f *FFmpeg) InputFile(file string) *FFmpeg {
f.input = nil
f.inputFiles = append(f.inputFiles, file)
return f
}
func (f *FFmpeg) Input(reader io.Reader) *FFmpeg {
f.inputFiles = []string{}
f.input = reader
return f
}
func (f *FFmpeg) OutputFile(file string) *FFmpeg {
f.output = nil
f.outputFile = file
return f
}
func (f *FFmpeg) Output(writer io.Writer) *FFmpeg {
f.outputFile = ""
f.output = writer
return f
}
func (f *FFmpeg) Format(format string) *FFmpeg {
f.format = format
return f
}
func (f *FFmpeg) AudioBitrate(bitrate int) *FFmpeg {
f.audioBitrate = bitrate
return f
}
func (f *FFmpeg) VideoBitrate(bitrate int) *FFmpeg {
f.videoBitrate = bitrate
return f
}
func (f *FFmpeg) VideoCodec(codec string) *FFmpeg {
f.videoCodec = codec
return f
}
func (f *FFmpeg) AudioCodec(codec string) *FFmpeg {
f.audioCodec = codec
return f
}
func (f *FFmpeg) Seek(seek int) *FFmpeg {
f.seek = seek
return f
}
func (f *FFmpeg) VFrames(vframes int) *FFmpeg {
f.vframes = &vframes
return f
}
func (f *FFmpeg) Duration(duration int) *FFmpeg {
f.duration = duration
return f
}
func (f *FFmpeg) Rate(rate int) *FFmpeg {
f.rate = rate
return f
}
func (f *FFmpeg) Loop(loop int) *FFmpeg {
f.loop = loop
return f
}
func (f *FFmpeg) Lossless(lossless bool) *FFmpeg {
f.lossless = lossless
return f
}
func (f *FFmpeg) CompressionLevel(compressionLevel int) *FFmpeg {
f.compressionLevel = &compressionLevel
return f
}
func (f *FFmpeg) QScale(qscale int) *FFmpeg {
f.qscale = &qscale
return f
}
func (f *FFmpeg) FilterComplex(filterComplex string) *FFmpeg {
f.filterComplex = filterComplex
return f
}
func (f *FFmpeg) Map(m string) *FFmpeg {
f.mapping = m
return f
}
func (f *FFmpeg) Movflags(movflags string) *FFmpeg {
f.movflags = movflags
return f
}
func (f *FFmpeg) Preset(preset Preset) *FFmpeg {
f.preset = preset
return f
}
func (f *FFmpeg) RemoveMetadata(removeMetadata bool) *FFmpeg {
f.removeMetadata = removeMetadata
return f
}
func (f *FFmpeg) NoVideo(noVideo bool) *FFmpeg {
f.noVideo = noVideo
return f
}
func (f *FFmpeg) ExtraParram(key string, values ...string) *FFmpeg {
f.extraParrams = append(f.extraParrams, ExtraParram{
Key: key,
Values: values,
})
return f
}
// Ex: cat VID_20191112_093257.mp4 | ffmpeg -i - -ss 10 -t 6 -vcodec libx264 -acodec aac -b:a 10000 -b:v 10000 -movflags frag_keyframe+empty_moov+faststart -f mp4 pipe: > bbbbb.mp4
func (f *FFmpeg) Run() error {
defer f.BinWrapper.Reset()
if f.seek >= 0 {
f.Arg("-ss", fmt.Sprintf("%d", f.seek))
}
if f.duration > 0 {
f.Arg("-t", fmt.Sprintf("%d", f.duration))
}
// input
if f.input != nil {
f.Arg("-i", "-")
f.StdIn(f.input)
} else if len(f.inputFiles) > 0 {
for _, inputFile := range f.inputFiles {
f.Arg("-i", inputFile)
}
} else {
return errors.New("Undefined input")
}
// arg
f.Arg("-y")
if f.audioBitrate > 0 {
f.Arg("-b:a", fmt.Sprintf("%d", f.audioBitrate))
}
if f.videoBitrate > 0 {
f.Arg("-b:v", fmt.Sprintf("%d", f.videoBitrate))
}
if f.videoCodec != "" {
f.Arg("-vcodec", f.videoCodec)
}
if f.audioCodec != "" {
f.Arg("-acodec", f.audioCodec)
}
if f.vframes != nil {
f.Arg("-vframes", fmt.Sprintf("%d", *(f.vframes)))
}
if f.rate > 0 {
f.Arg("-r", fmt.Sprintf("%d", f.rate))
}
if f.loop > 0 {
f.Arg("-loop", fmt.Sprintf("%d", f.loop))
}
// webp
if f.lossless {
f.Arg("-lossless", "1")
}
if f.compressionLevel != nil {
f.Arg("-compression_level", fmt.Sprintf("%d", *f.compressionLevel))
}
if f.qscale != nil {
f.Arg("-qscale", fmt.Sprintf("%d", *f.qscale))
}
if f.filterComplex != "" {
f.Arg("-filter_complex", f.filterComplex)
}
if f.mapping != "" {
f.Arg("-map", f.mapping)
}
if f.removeMetadata {
f.Arg("-map_metadata", "-1")
}
if f.noVideo {
f.Arg("-vn")
}
if f.format != "" {
f.Arg("-f", f.format)
}
if f.preset != "" {
f.Arg("-preset", f.preset.String())
}
for _, extraParram := range f.extraParrams {
f.Arg(extraParram.Key, extraParram.Values...)
}
// output
if f.outputFile != "" {
f.Arg(f.outputFile)
} else if f.output != nil {
f.Arg("-movflags", f.movflags+"+frag_keyframe+empty_moov").Arg("pipe:")
f.SetStdOut(f.output)
} else {
return errors.New("Undefined output")
}
// run
err := f.BinWrapper.Run()
if err != nil {
return errors.New(err.Error() + ". " + string(f.StdErr()))
}
return nil
}