169 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			169 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
# RozK
 | 
						|
# https://www.ffmpeg.org/doxygen/trunk/group__libavf.html
 | 
						|
 | 
						|
import ctypes
 | 
						|
 | 
						|
_avutil = ctypes.cdll.LoadLibrary('libavutil.so')
 | 
						|
_avformat = ctypes.cdll.LoadLibrary('libavformat.so')
 | 
						|
_avcodec = ctypes.cdll.LoadLibrary('libavcodec.so')
 | 
						|
 | 
						|
class AVFrame(ctypes.Structure):
 | 
						|
    pass
 | 
						|
 | 
						|
AVFrame_p = ctypes.POINTER(AVFrame)
 | 
						|
AVFrame_pp = ctypes.POINTER(AVFrame_p)
 | 
						|
 | 
						|
_avutil.av_frame_alloc.restype = AVFrame_p
 | 
						|
_avutil.av_frame_alloc.argtypes = None
 | 
						|
 | 
						|
def frame_alloc():
 | 
						|
    return _avutil.av_frame_alloc()
 | 
						|
 | 
						|
_avutil.av_frame_free.restype = None
 | 
						|
_avutil.av_frame_free.argtypes = [AVFrame_pp]
 | 
						|
 | 
						|
def frame_free(frame):
 | 
						|
    _avutil.av_frame_free(ctypes.byref(frame))
 | 
						|
 | 
						|
class AVFormatContext(ctypes.Structure):
 | 
						|
    pass
 | 
						|
 | 
						|
AVFormatContext_p = ctypes.POINTER(AVFormatContext)
 | 
						|
AVFormatContext_pp = ctypes.POINTER(AVFormatContext_p)
 | 
						|
 | 
						|
_avformat.avformat_alloc_context.restype = AVFormatContext_p
 | 
						|
_avformat.avformat_alloc_context.argtypes = None
 | 
						|
 | 
						|
def format_alloc_context():
 | 
						|
    return _avformat.avformat_alloc_context()
 | 
						|
 | 
						|
_avformat.avformat_free_context.restype = None
 | 
						|
_avformat.avformat_free_context.argtypes = [AVFormatContext_p]
 | 
						|
 | 
						|
def format_free_context(context):
 | 
						|
    _avformat.avformat_free_context(context)
 | 
						|
 | 
						|
_avformat.avformat_open_input.restype = ctypes.c_int
 | 
						|
_avformat.avformat_open_input.argtypes = [
 | 
						|
    AVFormatContext_pp,
 | 
						|
    ctypes.c_char_p, # url
 | 
						|
    ctypes.c_void_p, # format
 | 
						|
    ctypes.POINTER(ctypes.c_void_p)] # options
 | 
						|
 | 
						|
def format_open_input(context, url):
 | 
						|
    return _avformat.avformat_open_input(ctypes.byref(context), url.encode('ascii', 'ignore'), None, None)
 | 
						|
 | 
						|
_avformat.avformat_close_input.restype = None
 | 
						|
_avformat.avformat_close_input.argtypes = [AVFormatContext_pp]
 | 
						|
 | 
						|
def format_close_input(context):
 | 
						|
    _avformat.avformat_close_input(ctypes.byref(context))
 | 
						|
 | 
						|
_avformat.avformat_find_stream_info.restype = ctypes.c_int
 | 
						|
_avformat.avformat_find_stream_info.argtypes = [
 | 
						|
    AVFormatContext_p,
 | 
						|
    ctypes.POINTER(ctypes.c_void_p)] # options
 | 
						|
 | 
						|
def format_find_stream_info(context):
 | 
						|
    return _avformat.avformat_find_stream_info(context, None)
 | 
						|
 | 
						|
AVMEDIA_TYPE_UNKNOWN = -1
 | 
						|
AVMEDIA_TYPE_VIDEO = 0
 | 
						|
AVMEDIA_TYPE_AUDIO = 1
 | 
						|
AVMEDIA_TYPE_DATA = 2
 | 
						|
AVMEDIA_TYPE_SUBTITLE = 3
 | 
						|
AVMEDIA_TYPE_ATTACHMENT = 4
 | 
						|
 | 
						|
class AVCodec(ctypes.Structure):
 | 
						|
    _fields_ = [
 | 
						|
        ("name", ctypes.c_char_p),
 | 
						|
        ("long_name", ctypes.c_char_p)]
 | 
						|
        # ...
 | 
						|
 | 
						|
AVCodec_p = ctypes.POINTER(AVCodec)
 | 
						|
AVCodec_pp = ctypes.POINTER(AVCodec_p)
 | 
						|
 | 
						|
_avformat.av_find_best_stream.restype = ctypes.c_int
 | 
						|
_avformat.av_find_best_stream.argtypes = [
 | 
						|
    AVFormatContext_p,
 | 
						|
    ctypes.c_int, # type
 | 
						|
    ctypes.c_int, # wanted stream
 | 
						|
    ctypes.c_int, # related stream
 | 
						|
    AVCodec_pp,
 | 
						|
    ctypes.c_int] # flags
 | 
						|
 | 
						|
def format_find_best_stream(context, type):
 | 
						|
    codec = AVCodec_p()
 | 
						|
    index = _avformat.av_find_best_stream(context, type, -1, -1, ctypes.byref(codec), 0)
 | 
						|
    return index, codec
 | 
						|
 | 
						|
class AVPacket(ctypes.Structure):
 | 
						|
    _fields_ = [
 | 
						|
        ("buf", ctypes.c_void_p),
 | 
						|
        ("pts", ctypes.c_int64),
 | 
						|
        ("dts", ctypes.c_int64),
 | 
						|
        ("data", ctypes.c_void_p),
 | 
						|
        ("size", ctypes.c_int),
 | 
						|
        ("stream_index", ctypes.c_int)]
 | 
						|
        # ...
 | 
						|
 | 
						|
AVPacket_p = ctypes.POINTER(AVPacket)
 | 
						|
AVPacket_pp = ctypes.POINTER(AVPacket_p)
 | 
						|
 | 
						|
_avformat.av_packet_alloc.restype = AVPacket_p
 | 
						|
_avformat.av_packet_alloc.argtypes = None
 | 
						|
 | 
						|
def packet_alloc():
 | 
						|
    return _avformat.av_packet_alloc()
 | 
						|
 | 
						|
_avformat.av_packet_free.restype = None
 | 
						|
_avformat.av_packet_free.argtypes = [AVPacket_pp]
 | 
						|
 | 
						|
def packet_free(packet):
 | 
						|
    _avformat.av_packet_free(ctypes.byref(packet))
 | 
						|
 | 
						|
_avformat.av_read_frame.restype = ctypes.c_int
 | 
						|
_avformat.av_read_frame.argtypes = [AVFormatContext_p, AVPacket_p]
 | 
						|
 | 
						|
def read_frame(context, packet):
 | 
						|
    return _avformat.av_read_frame(context, packet)
 | 
						|
 | 
						|
class AVCodecContext(ctypes.Structure):
 | 
						|
    pass
 | 
						|
 | 
						|
AVCodecContext_p = ctypes.POINTER(AVCodecContext)
 | 
						|
AVCodecContext_pp = ctypes.POINTER(AVCodecContext_p)
 | 
						|
 | 
						|
_avcodec.avcodec_alloc_context3.restype = AVCodecContext_p
 | 
						|
_avcodec.avcodec_alloc_context3.argtypes = [AVCodec_p]
 | 
						|
 | 
						|
def codec_alloc_context(codec):
 | 
						|
    return _avcodec.avcodec_alloc_context3(codec)
 | 
						|
 | 
						|
_avcodec.avcodec_free_context.restype = None
 | 
						|
_avcodec.avcodec_free_context.argtypes = [AVCodecContext_pp]
 | 
						|
 | 
						|
def codec_free_context(context):
 | 
						|
    _avcodec.avcodec_free_context(ctypes.byref(context))
 | 
						|
 | 
						|
_avcodec.avcodec_open2.restype = ctypes.c_int
 | 
						|
_avcodec.avcodec_open2.argtypes = [
 | 
						|
    AVCodecContext_p,
 | 
						|
    AVCodec_p,
 | 
						|
    ctypes.POINTER(ctypes.c_void_p)] # options
 | 
						|
 | 
						|
def codec_open(context, codec):
 | 
						|
    return _avcodec.avcodec_open2(context, codec, None)
 | 
						|
 | 
						|
_avcodec.avcodec_send_packet.restype = ctypes.c_int
 | 
						|
_avcodec.avcodec_send_packet.argtypes = [AVCodecContext_p, AVPacket_p]
 | 
						|
 | 
						|
def codec_send_packet(context, packet):
 | 
						|
    return _avcodec.avcodec_send_packet(context, packet)
 | 
						|
 | 
						|
_avcodec.avcodec_receive_frame.restype = ctypes.c_int
 | 
						|
_avcodec.avcodec_receive_frame.argtypes = [AVCodecContext_p, AVFrame_p]
 | 
						|
 | 
						|
def codec_receive_frame(context, frame):
 | 
						|
    return _avcodec.avcodec_receive_frame(context, frame)
 |