video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-09-16 927a49bc04984400cb9b968e41d299cc977e4988
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
package goffmpeg
 
/*
#cgo LDFLAGS: -ldl
#include <stdlib.h>
#include "libcffmpeg.h"
*/
import "C"
import (
    "errors"
    "fmt"
    "unsafe"
)
 
const (
    // ScaleFastBilinear SWS_FAST_BILINEAR
    ScaleFastBilinear = 1
    // ScaleBilinear SWS_BILINEAR
    ScaleBilinear = 2
    // ScaleBicubic SWS_BICUBIC
    ScaleBicubic = 4
    // ScaleX SWS_X
    ScaleX = 8
    // ScalePoint SWS_POINT
    ScalePoint = 0x10
    // ScaleArea SWS_AREA
    ScaleArea = 0x20
    // ScaleBicublin SWS_BICUBLIN
    ScaleBicublin = 0x40
    // ScaleGauss SWS_GAUSS
    ScaleGauss = 0x80
    // ScaleSinc SWS_SINC
    ScaleSinc = 0x100
    // ScaleLancZos SWS_LANCZOS
    ScaleLancZos = 0x200
    // ScaleSpline SWS_SPLINE
    ScaleSpline = 0x400
)
 
var libcffmpeg C.libcffmpeg
 
// InitFFmpeg init ffmepg
func InitFFmpeg(soFileGo string) error {
    soFile := C.CString(soFileGo)
    defer C.free(unsafe.Pointer(soFile))
    lib := C.init_libcffmpeg(soFile)
    if lib == nil {
        fmt.Println("open error: ", soFileGo)
        return errors.New("init ffmpeg error")
    }
    libcffmpeg = lib
    return nil
}
 
// FreeFFmpeg free ffmpeg
func FreeFFmpeg() {
    if libcffmpeg != nil {
        C.release_libcffmpeg(libcffmpeg)
    }
}
 
// Config config
type Config struct {
    Scale  int
    Width  int
    Height int
    GB     bool
    CPU    bool
}
 
// GoFFMPEG handle for c
type GoFFMPEG struct {
    ffmpeg C.cffmpeg
}
 
// New 2nd new
func New(conf Config) *GoFFMPEG {
    f := C.wrap_fn_create()
    if f == nil {
        return nil
    }
    if conf.Scale != 0 && conf.Width != 0 && conf.Height != 0 {
        C.wrap_fn_scale(f, C.int(conf.Width), C.int(conf.Height), C.int(conf.Scale))
    }
    if conf.GB {
        C.wrap_fn_run_gb28181(f)
    }
    if conf.CPU {
        C.wrap_fn_use_cpu(f)
    }
 
    return &GoFFMPEG{
        ffmpeg: f,
    }
}
 
// Free free handle
func (h *GoFFMPEG) Free() {
    C.wrap_fn_destroy(h.ffmpeg)
}
 
// Run ffmpeg
func (h *GoFFMPEG) Run(input string) {
    in := C.CString(input)
    defer C.free(unsafe.Pointer(in))
 
    C.wrap_fn_run(h.ffmpeg, in)
}
 
// DecodeJPEG decode jpeg file
func DecodeJPEG(input string) ([]byte, int, int) {
    in := C.CString(input)
    defer C.free(unsafe.Pointer(in))
 
    var width C.int
    var height C.int
    p := C.wrap_fn_decode_jpeg(in, &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, 0, 0
}
 
// FireRecorder fire recorder
func (h *GoFFMPEG) FireRecorder(sid string, id int64) {
    csid := C.CString(sid)
    defer C.free(unsafe.Pointer(csid))
    C.wrap_fn_fire_recorder(h.ffmpeg, csid, C.long(id))
}
 
// BuildRecorder build recorder
func (h *GoFFMPEG) BuildRecorder(sid, output string, mind, maxd int) {
    out := C.CString(output)
    defer C.free(unsafe.Pointer(out))
    csid := C.CString(sid)
    defer C.free(unsafe.Pointer(csid))
 
    C.wrap_fn_recorder(h.ffmpeg, csid, out, C.int(mind), C.int(maxd))
}
 
// GetInfoRecorder info
func (h *GoFFMPEG) GetInfoRecorder() (string, int, string) {
    var i C.int = -1
 
    var id *C.char
    var idl C.int
 
    var p *C.char
    var pl C.int
 
    C.wrap_fn_info_recorder(h.ffmpeg, &i, &id, &idl, &p, &pl)
    // if p == nil {
    //     return -1, ""
    // }
    gID := C.GoString(id)
    C.free(unsafe.Pointer(id))
    path := C.GoString(p)
    C.free(unsafe.Pointer(p))
 
    // fmt.Println("Go get info : ", path, " len: ", l)
 
    return gID, int(i), path
}
 
// BuildDecoder build decoder
func (h *GoFFMPEG) BuildDecoder() {
    C.wrap_fn_decoder(h.ffmpeg)
}
 
// GetPicDecoder get pic from decoder
func (h *GoFFMPEG) GetPicDecoder() ([]byte, int, int, int64) {
    var width C.int
    var height C.int
    var fid C.long
 
    p := C.wrap_fn_decoder_pic(h.ffmpeg, &width, &height, &fid)
    if width == 0 && height == 0 {
        return nil, 0, 0, 0
    }
    defer C.free(unsafe.Pointer(p))
    d := C.GoBytes(p, width*height*3)
    wid := int(width)
    hei := int(height)
    gfid := int64(fid)
    return d, wid, hei, gfid
}
 
//GetAVPacket get AVPacket
func (h *GoFFMPEG) GetAVPacket() ([]byte, int, int) {
    var key C.int
    var size C.int
 
    p := C.wrap_fn_get_avpacket(h.ffmpeg, &size, &key)
    if size <= 0 {
        return nil, 0, -1
    }
    defer C.free(unsafe.Pointer(p))
    d := C.GoBytes(p, size)
    s := int(size)
    k := int(key)
 
    return d, s, k
}
 
///////////////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(C.int(w), C.int(h), C.int(fps), C.int(br), C.int(sFlag), C.int(gi)),
    }
}
 
// Free free
func (e *GoEncoder) Free() {
    C.wrap_fn_destroy_encoder(e.enc)
}
 
// Encode pic
func (e *GoEncoder) Encode(in []byte, w, h int) ([]byte, int, bool) {
 
    var size C.int
    var key C.int
    cin := C.CBytes(in)
    defer C.free(cin)
 
    p := C.wrap_fn_encode(e.enc, cin, 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
}