-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyFrameWriter.pyx
More file actions
66 lines (52 loc) · 1.94 KB
/
Copy pathPyFrameWriter.pyx
File metadata and controls
66 lines (52 loc) · 1.94 KB
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
# distutils: language = c++
from cpython cimport Py_buffer
import ctypes
import numpy as np
cimport numpy as np
from libcpp.vector cimport vector
# Declare the class with cdef
cdef extern from "FrameWriter.h":
cdef cppclass FrameWriter:
int width;
int height;
int n_channel;
unsigned char *data;
FrameWriter();
FrameWriter(const char *filename, const char *codec_name, double fps, int width, int height, const char *ffmpegcmd, unsigned char *buf);
void close_video();
int write_frame();
vector[const char *] getcodecname();
vector[const char *] getcodeclongname();
cdef class PyFrameWriter:
cdef FrameWriter c_writer
def __cinit__(self, const char *filename, const char *codec_name, double fps, int width, int height, const char *ffmpegcmd, np.ndarray im):
cdef np.ndarray[np.uint8_t, ndim=1, mode = 'c'] np_buf = np.ascontiguousarray(im, dtype = np.uint8)
cdef unsigned char* buf = <unsigned char*> np_buf.data
self.c_writer = FrameWriter(filename, codec_name, fps, width, height, ffmpegcmd, buf)
def close_video(self):
return self.c_writer.close_video()
def write_frame(self):
return self.c_writer.write_frame()
def __getbuffer__(self, Py_buffer *buffer, int flags):
cdef Py_ssize_t itemsize = sizeof(unsigned char)
pylen = self.c_writer.width * self.c_writer.height * self.c_writer.n_channel
buffer.buf = self.c_writer.data
buffer.format = 'b' # uint8_t
buffer.internal = NULL # see References
buffer.itemsize = itemsize
buffer.len = <Py_ssize_t>pylen
buffer.ndim = 1
buffer.obj = self
buffer.readonly = 0
buffer.shape = &buffer.len
buffer.strides = &buffer.itemsize
buffer.suboffsets = NULL # for pointer arrays only
def __releasebuffer__(self, Py_buffer *buffer):
pass
def getcodec():
codecs = {}
codecname = getcodecname()
codeclongname = getcodeclongname()
for i in range(len(codecname)):
codecs[codecname[i]] = codeclongname[i]
return codecs