diff --git a/infer/lib/audio.py b/infer/lib/audio.py index 045055c..97dbb90 100644 --- a/infer/lib/audio.py +++ b/infer/lib/audio.py @@ -1,22 +1,49 @@ -import ffmpeg import librosa import numpy as np +import av +from io import BytesIO +def wav2(i, o, format): + inp = av.open(i, 'rb') + if format == "m4a": format = "mp4" + out = av.open(o, 'wb', format=format) + if format == "ogg": format = "libvorbis" + if format == "mp4": format = "aac" + + ostream = out.add_stream(format) + + for frame in inp.decode(audio=0): + for p in ostream.encode(frame): out.mux(p) + + for p in ostream.encode(None): out.mux(p) + + out.close() + inp.close() + +def audio2(i, o, format, sr): + inp = av.open(i, 'rb') + out = av.open(o, 'wb', format=format) + if format == "ogg": format = "libvorbis" + if format == "f32le": format = "pcm_f32le" + + ostream = out.add_stream(format, channels=1) + ostream.sample_rate = sr + + for frame in inp.decode(audio=0): + for p in ostream.encode(frame): out.mux(p) + + out.close() + inp.close() def load_audio(file, sr): try: - # https://github.com/openai/whisper/blob/main/whisper/audio.py#L26 - # This launches a subprocess to decode audio while down-mixing and resampling as necessary. - # Requires the ffmpeg CLI and `ffmpeg-python` package to be installed. file = ( file.strip(" ").strip('"').strip("\n").strip('"').strip(" ") ) # 防止小白拷路径头尾带了空格和"和回车 - out, _ = ( - ffmpeg.input(file, threads=0) - .output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr) - .run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True) - ) - return np.frombuffer(out, np.float32).flatten() + with open(file, "rb") as f: + with BytesIO() as out: + audio2(f, out, "f32le", sr) + return np.frombuffer(out.getvalue(), np.float32).flatten() except AttributeError: audio = file[1] / 32768.0 diff --git a/infer/modules/vc/modules.py b/infer/modules/vc/modules.py index d785155..5beb28f 100644 --- a/infer/modules/vc/modules.py +++ b/infer/modules/vc/modules.py @@ -6,8 +6,9 @@ logger = logging.getLogger(__name__) import numpy as np import soundfile as sf import torch +from io import BytesIO -from infer.lib.audio import load_audio +from infer.lib.audio import load_audio, wav2 from infer.lib.infer_pack.models import ( SynthesizerTrnMs256NSFsid, SynthesizerTrnMs256NSFsid_nono, @@ -285,17 +286,17 @@ class VC: tgt_sr, ) else: - path = "%s/%s.wav" % (opt_root, os.path.basename(path)) - sf.write( - path, - audio_opt, - tgt_sr, - ) - if os.path.exists(path): - os.system( - "ffmpeg -i %s -vn %s -q:a 2 -y" - % (path, path[:-4] + ".%s" % format1) + path = "%s/%s.%s" % (opt_root, os.path.basename(path), format1) + with BytesIO() as wavf: + sf.write( + wavf, + audio_opt, + tgt_sr, + format="wav" ) + wavf.seek(0, 0) + with open(path, "wb") as outf: + wav2(wavf, outf, format1) except: info += traceback.format_exc() infos.append("%s->%s" % (os.path.basename(path), info)) diff --git a/requirements-dml.txt b/requirements-dml.txt index 1e72c1d..a49ed2d 100644 --- a/requirements-dml.txt +++ b/requirements-dml.txt @@ -42,4 +42,5 @@ onnxruntime-directml torchcrepe==0.0.20 fastapi==0.88 ffmpy==0.3.1 -python-dotenv>=1.0.0 \ No newline at end of file +python-dotenv>=1.0.0 +av diff --git a/requirements.txt b/requirements.txt index c2fb58c..f0e7181 100644 --- a/requirements.txt +++ b/requirements.txt @@ -44,3 +44,4 @@ torchcrepe==0.0.20 fastapi==0.88 ffmpy==0.3.1 python-dotenv>=1.0.0 +av