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:
2023-08-15 19:01:39 +02:00
committed by Mathieu Virbel
parent 290b552479
commit a809e5e734
6 changed files with 134 additions and 6 deletions

View 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