Aphasia_Classification / utils_audio.py
Ellie5757575757's picture
Create utils_audio.py
08677ee verified
raw
history blame contribute delete
539 Bytes
import os, subprocess, tempfile, uuid
def convert_to_wav(src_path: str, sr: int = 16000, mono: bool = True) -> str:
"""Convert mp3/mp4/wav to a normalized wav for downstream tools."""
tmpdir = tempfile.mkdtemp(prefix="audio_")
out_path = os.path.join(tmpdir, f"{uuid.uuid4().hex}.wav")
cmd = [
"ffmpeg", "-y", "-i", src_path,
"-ar", str(sr),
"-ac", "1" if mono else "2",
out_path
]
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return out_path