-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathvideo.go
410 lines (351 loc) · 10.1 KB
/
video.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package vidio
import (
"fmt"
"image"
"io"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
)
type Video struct {
filename string // Video Filename.
width int // Width of frames.
height int // Height of frames.
depth int // Depth of frames.
bitrate int // Bitrate for video encoding.
frames int // Total number of frames.
stream int // Stream Index.
duration float64 // Duration of video in seconds.
fps float64 // Frames per second.
codec string // Codec used for video encoding.
hasstreams bool // Flag storing whether file has additional data streams.
framebuffer []byte // Raw frame data.
metadata map[string]string // Video metadata.
pipe io.ReadCloser // Stdout pipe for ffmpeg process.
cmd *exec.Cmd // ffmpeg command.
}
func (video *Video) FileName() string {
return video.filename
}
func (video *Video) Width() int {
return video.width
}
func (video *Video) Height() int {
return video.height
}
// Channels of video frames.
func (video *Video) Depth() int {
return video.depth
}
// Bitrate of video in bits/s.
func (video *Video) Bitrate() int {
return video.bitrate
}
// Total number of frames in video.
func (video *Video) Frames() int {
return video.frames
}
// Returns the zero-indexed video stream index.
func (video *Video) Stream() int {
return video.stream
}
// Video duration in seconds.
func (video *Video) Duration() float64 {
return video.duration
}
// Frames per second of video.
func (video *Video) FPS() float64 {
return video.fps
}
func (video *Video) Codec() string {
return video.codec
}
// Returns true if file has any audio, subtitle, data or attachment streams.
func (video *Video) HasStreams() bool {
return video.hasstreams
}
func (video *Video) FrameBuffer() []byte {
return video.framebuffer
}
// Raw Metadata from ffprobe output for the video file.
func (video *Video) MetaData() map[string]string {
return video.metadata
}
func (video *Video) SetFrameBuffer(buffer []byte) error {
size := video.width * video.height * video.depth
if len(buffer) < size {
return fmt.Errorf("vidio: buffer size %d is smaller than frame size %d", len(buffer), size)
}
video.framebuffer = buffer
return nil
}
func NewVideo(filename string) (*Video, error) {
streams, err := NewVideoStreams(filename)
if streams == nil {
return nil, err
}
return streams[0], err
}
// Read all video streams from the given file.
func NewVideoStreams(filename string) ([]*Video, error) {
if !exists(filename) {
return nil, fmt.Errorf("vidio: video file %s does not exist", filename)
}
// Check if ffmpeg and ffprobe are installed on the users machine.
if err := installed("ffmpeg"); err != nil {
return nil, err
}
if err := installed("ffprobe"); err != nil {
return nil, err
}
videoData, err := ffprobe(filename, "v")
if err != nil {
return nil, err
}
if len(videoData) == 0 {
return nil, fmt.Errorf("vidio: no video data found in %s", filename)
}
// Loop over all stream types. a: Audio, s: Subtitle, d: Data, t: Attachments
hasstream := false
for _, c := range "asdt" {
data, err := ffprobe(filename, string(c))
if err != nil {
return nil, err
}
if len(data) > 0 {
hasstream = true
break
}
}
streams := make([]*Video, len(videoData))
for i, data := range videoData {
video := &Video{
filename: filename,
depth: 4,
stream: i,
hasstreams: hasstream,
metadata: data,
}
video.addVideoData(data)
streams[i] = video
}
return streams, nil
}
// Adds Video data to the video struct from the ffprobe output.
func (video *Video) addVideoData(data map[string]string) {
if width, ok := data["width"]; ok {
video.width = int(parse(width))
}
if height, ok := data["height"]; ok {
video.height = int(parse(height))
}
if rotation, ok := data["tag:rotate"]; ok && (rotation == "90" || rotation == "270") {
video.width, video.height = video.height, video.width
}
if duration, ok := data["duration"]; ok {
video.duration = float64(parse(duration))
}
if frames, ok := data["nb_frames"]; ok {
video.frames = int(parse(frames))
}
if fps, ok := data["r_frame_rate"]; ok {
split := strings.Split(fps, "/")
if len(split) == 2 && split[0] != "" && split[1] != "" {
video.fps = parse(split[0]) / parse(split[1])
}
}
if bitrate, ok := data["bit_rate"]; ok {
video.bitrate = int(parse(bitrate))
}
if codec, ok := data["codec_name"]; ok {
video.codec = codec
}
}
// Once the user calls Read() for the first time on a Video struct,
// the ffmpeg command which is used to read the video is started.
func (video *Video) init() error {
// If user exits with Ctrl+C, stop ffmpeg process.
video.cleanup()
// ffmpeg command to pipe video data to stdout in 8-bit RGBA format.
cmd := exec.Command(
"ffmpeg",
"-i", video.filename,
"-f", "image2pipe",
"-loglevel", "quiet",
"-pix_fmt", "rgba",
"-vcodec", "rawvideo",
"-map", fmt.Sprintf("0:v:%d", video.stream),
"-",
)
video.cmd = cmd
pipe, err := cmd.StdoutPipe()
if err != nil {
return err
}
video.pipe = pipe
if err := cmd.Start(); err != nil {
return err
}
if video.framebuffer == nil {
video.framebuffer = make([]byte, video.width*video.height*video.depth)
}
return nil
}
// Reads the next frame from the video and stores in the framebuffer.
// If the last frame has been read, returns false, otherwise true.
func (video *Video) Read() bool {
// If cmd is nil, video reading has not been initialized.
if video.cmd == nil {
if err := video.init(); err != nil {
return false
}
}
if _, err := io.ReadFull(video.pipe, video.framebuffer); err != nil {
video.Close()
return false
}
return true
}
// Reads the N-th frame from the video and stores it in the framebuffer. If the index is out of range or
// the operation failes, the function will return an error. The frames are indexed from 0.
func (video *Video) ReadFrame(n int) error {
if n >= video.frames {
return fmt.Errorf("vidio: provided frame index %d is not in frame count range", n)
}
if video.framebuffer == nil {
video.framebuffer = make([]byte, video.width*video.height*video.depth)
}
selectExpression, err := buildSelectExpression(n)
if err != nil {
return fmt.Errorf("vidio: failed to parse the specified frame index: %w", err)
}
cmd := exec.Command(
"ffmpeg",
"-i", video.filename,
"-f", "image2pipe",
"-loglevel", "quiet",
"-pix_fmt", "rgba",
"-vcodec", "rawvideo",
"-map", fmt.Sprintf("0:v:%d", video.stream),
"-vf", selectExpression,
"-vsync", "0",
"-",
)
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("vidio: failed to access the ffmpeg stdout pipe: %w", err)
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("vidio: failed to start the ffmpeg cmd: %w", err)
}
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-interruptChan
if stdoutPipe != nil {
stdoutPipe.Close()
}
if cmd != nil {
cmd.Process.Kill()
}
os.Exit(1)
}()
if _, err := io.ReadFull(stdoutPipe, video.framebuffer); err != nil {
return fmt.Errorf("vidio: failed to read the ffmpeg cmd result to the image buffer: %w", err)
}
if err := stdoutPipe.Close(); err != nil {
return fmt.Errorf("vidio: failed to close the ffmpeg stdout pipe: %w", err)
}
if err := cmd.Wait(); err != nil {
return fmt.Errorf("vidio: failed to free resources after the ffmpeg cmd: %w", err)
}
return nil
}
// Read the N-amount of frames with the given indexes and return them as a slice of RGBA image pointers. If one of
// the indexes is out of range, the function will return an error. The frames are indexes from 0.
func (video *Video) ReadFrames(n ...int) ([]*image.RGBA, error) {
if len(n) == 0 {
return nil, fmt.Errorf("vidio: no frames indexes specified")
}
for _, nValue := range n {
if nValue >= video.frames {
return nil, fmt.Errorf("vidio: provided frame index %d is not in frame count range", nValue)
}
}
selectExpression, err := buildSelectExpression(n...)
if err != nil {
return nil, fmt.Errorf("vidio: failed to parse the specified frame index: %w", err)
}
cmd := exec.Command(
"ffmpeg",
"-i", video.filename,
"-f", "image2pipe",
"-loglevel", "quiet",
"-pix_fmt", "rgba",
"-vcodec", "rawvideo",
"-map", fmt.Sprintf("0:v:%d", video.stream),
"-vf", selectExpression,
"-vsync", "0",
"-",
)
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("vidio: failed to access the ffmpeg stdout pipe: %w", err)
}
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("vidio: failed to start the ffmpeg cmd: %w", err)
}
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-interruptChan
if stdoutPipe != nil {
stdoutPipe.Close()
}
if cmd != nil {
cmd.Process.Kill()
}
os.Exit(1)
}()
frames := make([]*image.RGBA, len(n))
for frameIndex := range frames {
frames[frameIndex] = image.NewRGBA(image.Rect(0, 0, video.width, video.height))
if _, err := io.ReadFull(stdoutPipe, frames[frameIndex].Pix); err != nil {
return nil, fmt.Errorf("vidio: failed to read the ffmpeg cmd result to the image buffer: %w", err)
}
}
if err := stdoutPipe.Close(); err != nil {
return nil, fmt.Errorf("vidio: failed to close the ffmpeg stdout pipe: %w", err)
}
if err := cmd.Wait(); err != nil {
return nil, fmt.Errorf("vidio: failed to free resources after the ffmpeg cmd: %w", err)
}
return frames, nil
}
// Closes the pipe and stops the ffmpeg process.
func (video *Video) Close() {
if video.pipe != nil {
video.pipe.Close()
}
if video.cmd != nil {
video.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 (video *Video) cleanup() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
if video.pipe != nil {
video.pipe.Close()
}
if video.cmd != nil {
video.cmd.Process.Kill()
}
os.Exit(1)
}()
}