mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-21 12:49:06 +00:00
@@ -1,7 +1,8 @@
|
||||
from reflector.processors.base import Processor
|
||||
import av
|
||||
from pathlib import Path
|
||||
|
||||
import av
|
||||
from reflector.processors.base import Processor
|
||||
|
||||
|
||||
class AudioFileWriterProcessor(Processor):
|
||||
"""
|
||||
@@ -15,6 +16,8 @@ class AudioFileWriterProcessor(Processor):
|
||||
super().__init__()
|
||||
if isinstance(path, str):
|
||||
path = Path(path)
|
||||
if path.suffix not in (".mp3", ".wav"):
|
||||
raise ValueError("Only mp3 and wav files are supported")
|
||||
self.path = path
|
||||
self.out_container = None
|
||||
self.out_stream = None
|
||||
@@ -22,10 +25,19 @@ class AudioFileWriterProcessor(Processor):
|
||||
async def _push(self, data: av.AudioFrame):
|
||||
if not self.out_container:
|
||||
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self.out_container = av.open(self.path.as_posix(), "w", format="wav")
|
||||
self.out_stream = self.out_container.add_stream(
|
||||
"pcm_s16le", rate=data.sample_rate
|
||||
)
|
||||
suffix = self.path.suffix
|
||||
if suffix == ".mp3":
|
||||
self.out_container = av.open(self.path.as_posix(), "w", format="mp3")
|
||||
self.out_stream = self.out_container.add_stream(
|
||||
"libmp3lame", rate=data.sample_rate
|
||||
)
|
||||
elif suffix == ".wav":
|
||||
self.out_container = av.open(self.path.as_posix(), "w", format="wav")
|
||||
self.out_stream = self.out_container.add_stream(
|
||||
"pcm_s16le", rate=data.sample_rate
|
||||
)
|
||||
else:
|
||||
raise ValueError("Only mp3 and wav files are supported")
|
||||
for packet in self.out_stream.encode(data):
|
||||
self.out_container.mux(packet)
|
||||
await self.emit(data)
|
||||
|
||||
Reference in New Issue
Block a user