forked from bakape/thumbnailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.go
87 lines (76 loc) · 1.77 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
package thumbnailer
// #cgo pkg-config: libavcodec libavutil libavformat libswscale
// #cgo CFLAGS: -std=c11
// #include "video.h"
import "C"
import (
"bytes"
"errors"
"unsafe"
)
var (
// ErrNoStreams denotes no decodeable audio or video streams were found in
// a media container
ErrNoStreams = errors.New("no decodeable video or audio streams found")
// ErrGetFrame denotes an unknown failure to retrieve a video frame
ErrGetFrame = errors.New("failed to get frame")
)
// Thumbnail extracts the first frame of the video
func (c *FFContext) Thumbnail() (thumb Image, err error) {
ci, err := c.codecContext(FFVideo)
if err != nil {
return
}
var img C.struct_Buffer
ret := C.extract_video_image(&img, c.avFormatCtx, ci.ctx, ci.stream)
switch {
case ret != 0:
err = ffError(ret)
case img.data == nil:
err = ErrGetFrame
default:
p := unsafe.Pointer(img.data)
thumb.Data = copyCBuffer(p, C.int(img.size))
C.free(p)
thumb.Width = uint(img.width)
thumb.Height = uint(img.height)
}
return
}
func processVideo(source Source, opts Options) (
src Source, thumb Thumbnail, err error,
) {
src = source
c, err := NewFFContext(bytes.NewReader(src.Data))
if err != nil {
return
}
defer c.Close()
src.Length = c.Duration()
src.HasAudio, err = c.HasStream(FFAudio)
if err != nil {
return
}
src.HasVideo, err = c.HasStream(FFVideo)
if err != nil {
return
}
if !src.HasVideo {
// As of writing ffmpeg does not support cover art in neither MP4-like
// containers or OGG, so consider these unthumbnailable
if !src.HasAudio {
err = ErrNoStreams
}
return
}
c.ExtractMeta(&src)
original := src.Data
src.Image, err = c.Thumbnail()
if err != nil {
return
}
src, thumb, err = processImage(src, opts)
ReturnBuffer(src.Data)
src.Data = original
return
}