-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathvideowriter.go
315 lines (274 loc) · 8.38 KB
/
videowriter.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
309
310
311
312
313
314
315
package vidio
import (
"fmt"
"io"
"math"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
)
type VideoWriter struct {
filename string // Output filename.
streamfile string // Extra stream data filename.
width int // Frame width.
height int // Frame height.
bitrate int // Output video bitrate.
loop int // Number of times for GIF to loop.
delay int // Delay of final frame of GIF. Default -1 (same delay as previous frame).
macro int // Macroblock size for determining how to resize frames for codecs.
fps float64 // Frames per second for output video. Default 25.
quality float64 // Used if bitrate not given. Default 0.5.
codec string // Codec to encode video with. Default libx264.
pipe io.WriteCloser // Stdout pipe of ffmpeg process.
cmd *exec.Cmd // ffmpeg command.
}
// Optional parameters for VideoWriter.
type Options struct {
Bitrate int // Bitrate.
Loop int // For GIFs only. -1=no loop, 0=infinite loop, >0=number of loops.
Delay int // Delay for final frame of GIFs in centiseconds.
Macro int // Macroblock size for determining how to resize frames for codecs.
FPS float64 // Frames per second for output video.
Quality float64 // If bitrate not given, use quality instead. Must be between 0 and 1. 0:best, 1:worst.
Codec string // Codec for video.
StreamFile string // File path for extra stream data.
}
func (writer *VideoWriter) FileName() string {
return writer.filename
}
// File used to fill in extra stream data.
func (writer *VideoWriter) StreamFile() string {
return writer.streamfile
}
func (writer *VideoWriter) Width() int {
return writer.width
}
func (writer *VideoWriter) Height() int {
return writer.height
}
// Bitrate of video in bits/s.
func (writer *VideoWriter) Bitrate() int {
return writer.bitrate
}
// For GIFs only, defines looping behavior. -1=no loop, 0=infinite loop, >0=number of loops.
func (writer *VideoWriter) Loop() int {
return writer.loop
}
// Delay for final frame of GIFs in centiseconds.
func (writer *VideoWriter) Delay() int {
return writer.delay
}
// Macroblock size for determining how to resize frames for codecs.
func (writer *VideoWriter) Macro() int {
return writer.macro
}
// Frames per second of video.
func (writer *VideoWriter) FPS() float64 {
return writer.fps
}
// Video Codec Quality parameter. Must be between 0 and 1. 0:best, 1:worst.
func (writer *VideoWriter) Quality() float64 {
return writer.quality
}
func (writer *VideoWriter) Codec() string {
return writer.codec
}
// Creates a new VideoWriter struct with default values from the Options struct.
func NewVideoWriter(filename string, width, height int, options *Options) (*VideoWriter, error) {
// Check if ffmpeg is installed on the users machine.
if err := installed("ffmpeg"); err != nil {
return nil, err
}
if options == nil {
options = &Options{}
}
writer := &VideoWriter{
filename: filename,
width: width,
height: height,
bitrate: options.Bitrate,
}
// Default Parameter options logic from:
// https://github.com/imageio/imageio-ffmpeg/blob/master/imageio_ffmpeg/_io.py#L268.
// GIF settings
writer.loop = options.Loop // Default to infinite loop.
if options.Delay == 0 {
writer.delay = -1 // Default to frame delay of previous frame.
} else {
writer.delay = options.Delay
}
if options.Macro == 0 {
writer.macro = 16
} else {
writer.macro = options.Macro
}
if options.FPS == 0 {
writer.fps = 25
} else {
writer.fps = options.FPS
}
if options.Quality == 0 {
writer.quality = 0.5
} else {
writer.quality = math.Max(0, math.Min(options.Quality, 1))
}
if options.Codec == "" {
if strings.HasSuffix(strings.ToLower(filename), ".wmv") {
writer.codec = "msmpeg4"
} else if strings.HasSuffix(strings.ToLower(filename), ".gif") {
writer.codec = "gif"
} else {
writer.codec = "libx264"
}
} else {
writer.codec = options.Codec
}
if options.StreamFile != "" {
if !exists(options.StreamFile) {
return nil, fmt.Errorf("vidio: file %s does not exist", options.StreamFile)
}
writer.streamfile = options.StreamFile
}
return writer, nil
}
// Once the user calls Write() for the first time on a VideoWriter struct,
// the ffmpeg command which is used to write to the video file is started.
func (writer *VideoWriter) init() error {
// If user exits with Ctrl+C, stop ffmpeg process.
writer.cleanup()
// ffmpeg command to write to video file. Takes in bytes from Stdin and encodes them.
command := []string{
"-y", // overwrite output file if it exists.
"-loglevel", "quiet",
"-f", "rawvideo",
"-vcodec", "rawvideo",
"-s", fmt.Sprintf("%dx%d", writer.width, writer.height), // frame w x h.
"-pix_fmt", "rgba",
"-r", fmt.Sprintf("%.02f", writer.fps), // frames per second.
"-i", "-", // The input comes from stdin.
}
gif := strings.HasSuffix(strings.ToLower(writer.filename), ".gif")
// Assumes "writer.streamfile" is a container format.
// gif check is included since they are a common format.
if writer.streamfile != "" && !gif {
command = append(
command,
"-i", writer.streamfile,
"-map", "0:v:0",
"-map", "1:a?", // Add Audio streams if present.
"-c:a", "copy",
"-map", "1:s?", // Add Subtitle streams if present.
"-c:s", "copy",
"-map", "1:d?", // Add Data streams if present.
"-c:d", "copy",
"-map", "1:t?", // Add Attachments streams if present.
"-c:t", "copy",
"-shortest", // Cut longest streams to match audio duration.
)
}
command = append(
command,
"-vcodec", writer.codec,
"-pix_fmt", "yuv420p", // Output is 8-bit RGB, ignore alpha.
)
// Code from the imageio-ffmpeg project.
// https://github.com/imageio/imageio-ffmpeg/blob/master/imageio_ffmpeg/_io.py#L399.
// If bitrate not given, use a default.
if writer.bitrate == 0 {
if writer.codec == "libx264" {
// Quality between 0 an 51. 51 is worst.
command = append(command, "-crf", fmt.Sprintf("%d", int(writer.quality*51)))
} else {
// Quality between 1 and 31. 31 is worst.
command = append(command, "-qscale:v", fmt.Sprintf("%d", int(writer.quality*30)+1))
}
} else {
command = append(command, "-b:v", fmt.Sprintf("%d", writer.bitrate))
}
// For GIFs, add looping and delay parameters.
if gif {
command = append(
command,
"-loop", fmt.Sprintf("%d", writer.loop),
"-final_delay", fmt.Sprintf("%d", writer.delay),
)
}
// Code from the imageio-ffmpeg project:
// https://github.com/imageio/imageio-ffmpeg/blob/master/imageio_ffmpeg/_io.py#L415.
// Resizes the video frames to a size that works with most codecs.
if writer.macro > 1 {
if writer.width%writer.macro > 0 || writer.height%writer.macro > 0 {
width := writer.width
height := writer.height
if writer.width%writer.macro > 0 {
width += writer.macro - (writer.width % writer.macro)
}
if writer.height%writer.macro > 0 {
height += writer.macro - (writer.height % writer.macro)
}
writer.width = width
writer.height = height
command = append(
command,
"-vf", fmt.Sprintf("scale=%d:%d", width, height),
)
}
}
command = append(command, writer.filename)
cmd := exec.Command("ffmpeg", command...)
writer.cmd = cmd
pipe, err := cmd.StdinPipe()
if err != nil {
return err
}
writer.pipe = pipe
if err := cmd.Start(); err != nil {
return err
}
return nil
}
// Writes the given frame to the video file.
func (writer *VideoWriter) Write(frame []byte) error {
// If cmd is nil, video writing has not been set up.
if writer.cmd == nil {
if err := writer.init(); err != nil {
return err
}
}
total := 0
for total < len(frame) {
n, err := writer.pipe.Write(frame[total:])
if err != nil {
return err
}
total += n
}
return nil
}
// Closes the pipe and stops the ffmpeg process.
func (writer *VideoWriter) Close() {
if writer.pipe != nil {
writer.pipe.Close()
}
if writer.cmd != nil {
writer.cmd.Wait()
}
}
// Stops the "cmd" process running when the user presses Ctrl+C.
// https://stackoverflow.com/questions/11268943/is-it-possible-to-capture-a-ctrlc-signal-and-run-a-cleanup-function-in-a-defe.
func (writer *VideoWriter) cleanup() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
if writer.pipe != nil {
writer.pipe.Close()
}
if writer.cmd != nil {
writer.cmd.Process.Kill()
}
os.Exit(1)
}()
}