video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2020-10-09 a8b447be656145c9ba2a2d8319a10ae8f726de1f
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
package goffmpeg
 
/*
#include <stdlib.h>
#include "libcffmpeg.h"
*/
import "C"
import "unsafe"
 
///////////////for encoder
 
// GoEncoder encoder
type GoEncoder struct {
    enc C.cencoder
}
 
// NewEncoder encoder
func NewEncoder(w, h, fps, br, sFlag, gi int) *GoEncoder {
    if w <= 0 || h <= 0 {
        return nil
    }
 
    return &GoEncoder{
        enc: C.wrap_fn_create_encoder(unsafe.Pointer(libcffmpeg), C.int(w), C.int(h), C.int(fps), C.int(br), C.int(DstFormat), C.int(sFlag), C.int(gi)),
    }
}
 
// NewEncoderWithPixFmt origin pix_fmt
func NewEncoderWithPixFmt(w, h, fps, br, pixFmt, sFlag, gi int) *GoEncoder {
    if w <= 0 || h <= 0 {
        return nil
    }
 
    return &GoEncoder{
        enc: C.wrap_fn_create_encoder(unsafe.Pointer(libcffmpeg), C.int(w), C.int(h), C.int(fps), C.int(br), C.int(pixFmt), C.int(sFlag), C.int(gi)),
    }
}
 
// Free free
func (e *GoEncoder) Free() {
    if e.enc != nil {
        C.wrap_fn_destroy_encoder(unsafe.Pointer(libcffmpeg), e.enc)
    }
}
 
// Encode pic
func (e *GoEncoder) Encode(in []byte, w, h int) ([]byte, int, bool) {
 
    var size C.int
    var key C.int
 
    p := C.wrap_fn_encode(unsafe.Pointer(libcffmpeg), e.enc, unsafe.Pointer(&in[0]), C.int(w), C.int(h), &size, &key)
    defer C.free(p)
    if p != nil && size > 0 {
        b := C.GoBytes(p, size)
 
        isKey := false
        if key > 0 {
            isKey = true
        }
        return b, int(size), isKey
    }
    return nil, 0, false
}
 
// Encode2 pic
func (e *GoEncoder) Encode2(in []byte, w, h int) (unsafe.Pointer, []byte, int, bool) {
 
    var size C.int
    var key C.int
 
    p := C.wrap_fn_encode(unsafe.Pointer(libcffmpeg), e.enc, unsafe.Pointer(&in[0]), C.int(w), C.int(h), &size, &key)
    if p != nil && size > 0 {
 
        isKey := false
        if key > 0 {
            isKey = true
        }
        const maxLen = 0x7fffffff
        length := int(size)
        data := (*[maxLen]byte)(unsafe.Pointer(p))[:length:length]
 
        return p, data, length, isKey
    }
    return nil, nil, 0, false
}