File size: 1,231 Bytes
67a6eaa
bbf5927
b22128f
bbf5927
 
 
b22128f
 
bbf5927
4dd9405
bbf5927
b22128f
bbf5927
 
 
b22128f
bbf5927
 
b22128f
bbf5927
 
 
 
 
 
 
 
 
 
 
 
 
b22128f
bbf5927
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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)