This commit is contained in:
2025-10-03 16:55:38 +02:00
parent 2316435af8
commit dfd0902256
7 changed files with 122 additions and 47 deletions

View File

@ -1,12 +1,36 @@
# RozK
# https://www.ffmpeg.org/doxygen/trunk/group__libavf.html
import errno
import ctypes
_avutil = ctypes.cdll.LoadLibrary('libavutil.so')
_avformat = ctypes.cdll.LoadLibrary('libavformat.so')
_avcodec = ctypes.cdll.LoadLibrary('libavcodec.so')
def _errtag(a, b, c, d):
return -(ord(a) | (ord(b) << 8) | (ord(c) << 16) | (ord(d) << 24))
if errno.EAGAIN < 0:
AVERROR_EAGAIN = errno.EAGAIN
else:
AVERROR_EAGAIN = -errno.EAGAIN
AVERROR_EOF = _errtag('E', 'O', 'F', ' ')
AV_ERROR_MAX_STRING_SIZE = 64
_avutil.av_strerror.restype = ctypes.c_int
_avutil.av_strerror.argtypes = [
ctypes.c_int, # errno
ctypes.c_char_p, # errbuf
ctypes.c_size_t] # errbuff_size
def strerror(errno):
errbuf = ctypes.create_string_buffer(AV_ERROR_MAX_STRING_SIZE)
_avutil.av_strerror(errno, errbuf, AV_ERROR_MAX_STRING_SIZE)
return errbuf.value.decode("utf-8")
class AVFrame(ctypes.Structure):
pass
@ -25,9 +49,34 @@ _avutil.av_frame_free.argtypes = [AVFrame_pp]
def frame_free(frame):
_avutil.av_frame_free(ctypes.byref(frame))
class AVFormatContext(ctypes.Structure):
class AVCodecParameters(ctypes.Structure):
pass
AVCodecParameters_p = ctypes.POINTER(AVCodecParameters)
class AVStream(ctypes.Structure):
_fields_ = [
("av_class", ctypes.c_void_p),
("index", ctypes.c_int),
("id", ctypes.c_int),
("codecpar", AVCodecParameters_p)]
# ...
AVStream_p = ctypes.POINTER(AVStream)
AVStream_pp = ctypes.POINTER(AVStream_p)
class AVFormatContext(ctypes.Structure):
_fields_ = [
("av_class", ctypes.c_void_p),
("iformat", ctypes.c_void_p),
("oformat", ctypes.c_void_p),
("priv_data", ctypes.c_void_p),
("pb", ctypes.c_void_p),
("ctx_flags", ctypes.c_int),
("nb_streams", ctypes.c_uint),
("streams", AVStream_pp)]
# ...
AVFormatContext_p = ctypes.POINTER(AVFormatContext)
AVFormatContext_pp = ctypes.POINTER(AVFormatContext_p)
@ -146,6 +195,12 @@ _avcodec.avcodec_free_context.argtypes = [AVCodecContext_pp]
def codec_free_context(context):
_avcodec.avcodec_free_context(ctypes.byref(context))
_avcodec.avcodec_parameters_to_context.restype = ctypes.c_int
_avcodec.avcodec_parameters_to_context.argtypes = [AVCodecContext_p, AVCodecParameters_p]
def codec_parameters_to_context(context, parameters):
return _avcodec.avcodec_parameters_to_context(context, parameters)
_avcodec.avcodec_open2.restype = ctypes.c_int
_avcodec.avcodec_open2.argtypes = [
AVCodecContext_p,