mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-21 12:49:06 +00:00
server: implement wav/mp3 audio download
If set, will save audio transcription to disk. MP3 conversion is on-request, but cached to disk as well only if it is successfull. Closes #148
This commit is contained in:
35
server/reflector/processors/audio_file_writer.py
Normal file
35
server/reflector/processors/audio_file_writer.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from reflector.processors.base import Processor
|
||||
import av
|
||||
import wave
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class AudioFileWriterProcessor(Processor):
|
||||
"""
|
||||
Write audio frames to a file.
|
||||
"""
|
||||
|
||||
INPUT_TYPE = av.AudioFrame
|
||||
OUTPUT_TYPE = av.AudioFrame
|
||||
|
||||
def __init__(self, path: Path | str):
|
||||
super().__init__()
|
||||
if isinstance(path, str):
|
||||
path = Path(path)
|
||||
self.path = path
|
||||
self.fd = None
|
||||
|
||||
async def _push(self, data: av.AudioFrame):
|
||||
if not self.fd:
|
||||
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self.fd = wave.open(self.path.as_posix(), "wb")
|
||||
self.fd.setnchannels(len(data.layout.channels))
|
||||
self.fd.setsampwidth(data.format.bytes)
|
||||
self.fd.setframerate(data.sample_rate)
|
||||
self.fd.writeframes(data.to_ndarray().tobytes())
|
||||
await self.emit(data)
|
||||
|
||||
async def _flush(self):
|
||||
if self.fd:
|
||||
self.fd.close()
|
||||
self.fd = None
|
||||
Reference in New Issue
Block a user