This commit is contained in:
2025-10-03 17:07:17 +02:00
parent dfd0902256
commit b31704420f
3 changed files with 15 additions and 6 deletions

View File

@ -5,7 +5,7 @@ from .packet import Packet
from .frame import Frame
class Decoder:
__slots__ = '_context', '_index'
__slots__ = '_context'
def __init__(self, stream):
self._context = libav.codec_alloc_context(stream.codec)
@ -19,7 +19,6 @@ class Decoder:
if errcode < 0:
libav.codec_free_context(self._context)
raise Exception("Failed to open codec context")
self._index = 0
def __del__(self):
if self._context:
@ -46,3 +45,6 @@ class Decoder:
errstring = libav.strerror(errcode)
raise Exception(f"Failed to send packet: {errstring}")
return self._receive()
def flush(self):
return self.decode(None)

View File

@ -22,6 +22,12 @@ class Demuxer:
self.video_stream = self._find_stream(libav.AVMEDIA_TYPE_VIDEO)
self.audio_stream = self._find_stream(libav.AVMEDIA_TYPE_AUDIO)
@property
def nb_streams(self):
if not self._context:
return 0
return self._context.contents.nb_streams
def _find_stream(self, type):
index, codec_ref = libav.format_find_best_stream(self._context, type)
if index < 0 or not codec_ref:

9
pve.py
View File

@ -5,8 +5,9 @@ from mp4.decoder import Decoder
demuxer = Demuxer('test.mp4')
print(demuxer.video_stream.codec.name)
print(demuxer.audio_stream.codec.name)
print(f"nb_streams = {demuxer.nb_streams}")
print(f"video codec = {demuxer.video_stream.codec.name}")
print(f"audio codec = {demuxer.audio_stream.codec.name}")
video_decoder = Decoder(demuxer.video_stream)
audio_decoder = Decoder(demuxer.audio_stream)
@ -26,10 +27,10 @@ while True:
else:
print("unkown packet")
video_frames = video_decoder.decode(None)
video_frames = video_decoder.flush()
print(f"flushed {len(video_frames)} video frames")
audio_frames = audio_decoder.decode(None)
audio_frames = audio_decoder.flush()
print(f"flushed {len(audio_frames)} audio frames")
demuxer.close()