video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-11-20 dc65b072a13994ff02cc7b54f3b00ba087e7f86a
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
package goffmpeg
 
/*
#include <stdlib.h>
#include "libcffmpeg.h"
*/
import "C"
 
import "unsafe"
 
/////////////// for decoder
 
// Decode decode jpeg file
// return val: -1 open error; -2, find stream error; -3, converter create error
func Decode(input string, gb bool) ([]byte, int, int) {
    in := C.CString(input)
    defer C.free(unsafe.Pointer(in))
 
    withGB := 0
    if gb {
        withGB = 1
    }
 
    var width C.int
    var height C.int
    p := C.wrap_fn_decode(unsafe.Pointer(libcffmpeg), in, C.int(withGB), &width, &height)
    defer C.free(p)
 
    if width > 0 && height > 0 {
        data := C.GoBytes(p, width*height*3)
        wid := int(width)
        hei := int(height)
        return data, wid, hei
    }
    return nil, int(width), int(height)
}
 
// Decode2 decode jpeg file
// return val: -1 open error; -2, find stream error; -3, converter create error
func Decode2(input string, gb bool) (unsafe.Pointer, []byte, int, int) {
    in := C.CString(input)
    defer C.free(unsafe.Pointer(in))
 
    withGB := 0
    if gb {
        withGB = 1
    }
 
    var width C.int
    var height C.int
    p := C.wrap_fn_decode(unsafe.Pointer(libcffmpeg), in, C.int(withGB), &width, &height)
 
    if width > 0 && height > 0 {
        wid := int(width)
        hei := int(height)
        const maxLen = 0x7fffffff
        size := int(width * height * 3)
        data := (*[maxLen]byte)(unsafe.Pointer(p))[:size:size]
 
        return p, data, wid, hei
    }
    return nil, nil, 0, 0
}