Spaces:
Runtime error
Runtime error
| # reference: https://huggingface.co/spaces/r3gm/Audio_separator | |
| import subprocess, shlex, sys # noqa | |
| from urllib.parse import urlparse | |
| import librosa | |
| import numpy as np | |
| from pathlib import Path | |
| def convert_to_stereo_and_wav(audio_path: Path) -> Path: | |
| # loading takes time since resampling at 44100 Hz | |
| wave, sr = librosa.load(str(audio_path), mono=False, sr=44100) | |
| # check if mono | |
| if type(wave[0]) != np.ndarray or audio_path.suffix != ".wav": # noqa | |
| stereo_path = audio_path.with_name(audio_path.stem + "_stereo.wav") | |
| command = shlex.split( | |
| f'ffmpeg -y -loglevel error -i "{str(audio_path)}" -ac 2 -f wav "{str(stereo_path)}"' | |
| ) | |
| sub_params = { | |
| "stdout": subprocess.PIPE, | |
| "stderr": subprocess.PIPE, | |
| "creationflags": subprocess.CREATE_NO_WINDOW | |
| if sys.platform == "win32" | |
| else 0, | |
| } | |
| process_wav = subprocess.Popen(command, **sub_params) | |
| output, errors = process_wav.communicate() | |
| if process_wav.returncode != 0 or not stereo_path.exists(): | |
| raise Exception("Error processing audio to stereo wav") | |
| return stereo_path | |
| else: | |
| return Path(audio_path) | |