20 lines
453 B
Python
20 lines
453 B
Python
from pydub import AudioSegment
|
|
import io
|
|
|
|
def convert_audio(raw_bytes: bytes, fmt: str):
|
|
# raw mono 32-bit float fake waveform
|
|
seg = AudioSegment(
|
|
raw_bytes,
|
|
frame_rate=22050,
|
|
sample_width=4,
|
|
channels=1
|
|
)
|
|
buf=io.BytesIO()
|
|
seg.export(buf, format=fmt)
|
|
mime={
|
|
"wav":"audio/wav",
|
|
"mp3":"audio/mpeg",
|
|
"ogg":"audio/ogg"
|
|
}.get(fmt,"audio/wav")
|
|
return buf.getvalue(), mime
|