Fix duration

This commit is contained in:
2025-01-24 14:47:43 +01:00
parent d0d019f1b6
commit 43562391b7

View File

@@ -22,6 +22,7 @@ class AudioFileWriterProcessor(Processor):
self.out_container = None self.out_container = None
self.out_stream = None self.out_stream = None
self.last_packet = None self.last_packet = None
self.duration = 0
async def _push(self, data: av.AudioFrame): async def _push(self, data: av.AudioFrame):
if not self.out_container: if not self.out_container:
@@ -39,9 +40,11 @@ class AudioFileWriterProcessor(Processor):
) )
else: else:
raise ValueError("Only mp3 and wav files are supported") raise ValueError("Only mp3 and wav files are supported")
for packet in self.out_stream.encode(data): for packet in self.out_stream.encode(data):
self.out_container.mux(packet) self.out_container.mux(packet)
self.last_packet = packet self.last_packet = packet
self.duration += packet.duration
await self.emit(data) await self.emit(data)
async def _flush(self): async def _flush(self):
@@ -49,13 +52,11 @@ class AudioFileWriterProcessor(Processor):
for packet in self.out_stream.encode(): for packet in self.out_stream.encode():
self.out_container.mux(packet) self.out_container.mux(packet)
self.last_packet = packet self.last_packet = packet
self.duration += packet.duration
try: try:
if self.last_packet is not None: if self.last_packet is not None:
duration = round( duration = round(
float( float(self.duration * self.last_packet.time_base * 1000),
(self.last_packet.pts * self.last_packet.duration)
* self.last_packet.time_base
),
2, 2,
) )
except Exception: except Exception: