File size: 539 Bytes
08677ee
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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