demuxer, decoder not working
This commit is contained in:
		@ -1,19 +1,42 @@
 | 
			
		||||
# RozK
 | 
			
		||||
 | 
			
		||||
from . import libav
 | 
			
		||||
from .context import Context
 | 
			
		||||
from .codec import Codec
 | 
			
		||||
from .stream import NullStream, Stream
 | 
			
		||||
from .packet import Packet
 | 
			
		||||
 | 
			
		||||
class Demuxer:
 | 
			
		||||
    __slots__ = 'context', 'video_stream', 'audio_stream'
 | 
			
		||||
    __slots__ = '_ref', 'video_stream', 'audio_stream'
 | 
			
		||||
 | 
			
		||||
    def __init__(self, path):
 | 
			
		||||
        self.context = Context()
 | 
			
		||||
        self.context.open_input("file:" + path)
 | 
			
		||||
        self.video_stream = self.context.find_stream(libav.AVMEDIA_TYPE_VIDEO)
 | 
			
		||||
        self.audio_stream = self.context.find_stream(libav.AVMEDIA_TYPE_AUDIO)
 | 
			
		||||
        self._ref = libav.format_alloc_context()
 | 
			
		||||
        if not self._ref:
 | 
			
		||||
            raise MemoryError
 | 
			
		||||
        errcode = libav.format_open_input(self._ref, "file:" + path)
 | 
			
		||||
        if errcode < 0:
 | 
			
		||||
            raise Exception(f"Failed to open: {path}")
 | 
			
		||||
        errcode = libav.format_find_stream_info(self._ref)
 | 
			
		||||
        if errcode < 0:
 | 
			
		||||
            libav.format_close_input(self._ref)
 | 
			
		||||
            raise Exception("Failed to find stream info")
 | 
			
		||||
        self.video_stream = self._find_stream(libav.AVMEDIA_TYPE_VIDEO)
 | 
			
		||||
        self.audio_stream = self._find_stream(libav.AVMEDIA_TYPE_AUDIO)
 | 
			
		||||
 | 
			
		||||
    def _find_stream(self, type):
 | 
			
		||||
        index, codec_ref = libav.format_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):
 | 
			
		||||
        return self.context.read_packet()
 | 
			
		||||
        if not self._ref:
 | 
			
		||||
            return None
 | 
			
		||||
        packet = Packet()
 | 
			
		||||
        errcode = libav.read_frame(self._ref, packet)
 | 
			
		||||
        if errcode < 0:
 | 
			
		||||
            return None
 | 
			
		||||
        return packet
 | 
			
		||||
 | 
			
		||||
    def close(self):
 | 
			
		||||
        self.context.close_input()
 | 
			
		||||
        if self._ref:
 | 
			
		||||
            libav.format_close_input(self._ref)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user