Spaces:
Sleeping
Sleeping
Create utils_audio.py
Browse files- utils_audio.py +14 -0
utils_audio.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, subprocess, tempfile, uuid
|
| 2 |
+
|
| 3 |
+
def convert_to_wav(src_path: str, sr: int = 16000, mono: bool = True) -> str:
|
| 4 |
+
"""Convert mp3/mp4/wav to a normalized wav for downstream tools."""
|
| 5 |
+
tmpdir = tempfile.mkdtemp(prefix="audio_")
|
| 6 |
+
out_path = os.path.join(tmpdir, f"{uuid.uuid4().hex}.wav")
|
| 7 |
+
cmd = [
|
| 8 |
+
"ffmpeg", "-y", "-i", src_path,
|
| 9 |
+
"-ar", str(sr),
|
| 10 |
+
"-ac", "1" if mono else "2",
|
| 11 |
+
out_path
|
| 12 |
+
]
|
| 13 |
+
subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 14 |
+
return out_path
|