55 lines
1.4 KiB
Python
55 lines
1.4 KiB
Python
|
|
# RozK
|
||
|
|
|
||
|
|
from . import libav
|
||
|
|
from .codec import Codec
|
||
|
|
from .stream import NullStream, Stream
|
||
|
|
from .packet import Packet
|
||
|
|
|
||
|
|
class Context:
|
||
|
|
__slots__ = '_ref'
|
||
|
|
|
||
|
|
def __init__(self):
|
||
|
|
self._ref = libav.alloc_context()
|
||
|
|
if not self._ref:
|
||
|
|
raise MemoryError
|
||
|
|
|
||
|
|
def __del__(self):
|
||
|
|
if self._ref:
|
||
|
|
libav.free_context(self._ref)
|
||
|
|
|
||
|
|
@property
|
||
|
|
def _as_parameter_(self):
|
||
|
|
return self._ref
|
||
|
|
|
||
|
|
def open_input(self, url):
|
||
|
|
if not self._ref:
|
||
|
|
return
|
||
|
|
errcode = libav.open_input(self._ref, url)
|
||
|
|
if errcode < 0:
|
||
|
|
raise Exception(f"Failed to open: {url}")
|
||
|
|
errcode = libav.find_stream_info(self._ref)
|
||
|
|
if errcode < 0:
|
||
|
|
libav.close_input(self._ref)
|
||
|
|
raise Exception("Failed to find stream info")
|
||
|
|
|
||
|
|
def close_input(self):
|
||
|
|
if self._ref:
|
||
|
|
libav.close_input(self._ref)
|
||
|
|
|
||
|
|
def find_stream(self, type):
|
||
|
|
if not self._ref:
|
||
|
|
return NullStream()
|
||
|
|
index, codec_ref = libav.find_best_stream(self._ref, type)
|
||
|
|
if index < 0 or not codec_ref:
|
||
|
|
return NullStream()
|
||
|
|
return Stream(index, Codec(codec_ref))
|
||
|
|
|
||
|
|
def read_packet(self):
|
||
|
|
if not self._ref:
|
||
|
|
return None
|
||
|
|
packet = Packet()
|
||
|
|
errcode = libav.read_frame(self._ref, packet)
|
||
|
|
if errcode < 0:
|
||
|
|
return None
|
||
|
|
return packet
|