mirror of
https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI.git
synced 2025-01-01 12:35:04 +08:00
optimize(ffmpeg): replace cmdline ffmpeg to pyav
except uvr5
This commit is contained in:
parent
90a4d9474a
commit
94277fd71e
@ -1,22 +1,49 @@
|
|||||||
import ffmpeg
|
|
||||||
import librosa
|
import librosa
|
||||||
import numpy as np
|
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):
|
def load_audio(file, sr):
|
||||||
try:
|
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 = (
|
||||||
file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
||||||
) # 防止小白拷路径头尾带了空格和"和回车
|
) # 防止小白拷路径头尾带了空格和"和回车
|
||||||
out, _ = (
|
with open(file, "rb") as f:
|
||||||
ffmpeg.input(file, threads=0)
|
with BytesIO() as out:
|
||||||
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
|
audio2(f, out, "f32le", sr)
|
||||||
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
|
return np.frombuffer(out.getvalue(), np.float32).flatten()
|
||||||
)
|
|
||||||
return np.frombuffer(out, np.float32).flatten()
|
|
||||||
|
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
audio = file[1] / 32768.0
|
audio = file[1] / 32768.0
|
||||||
|
@ -6,8 +6,9 @@ logger = logging.getLogger(__name__)
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import soundfile as sf
|
import soundfile as sf
|
||||||
import torch
|
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 (
|
from infer.lib.infer_pack.models import (
|
||||||
SynthesizerTrnMs256NSFsid,
|
SynthesizerTrnMs256NSFsid,
|
||||||
SynthesizerTrnMs256NSFsid_nono,
|
SynthesizerTrnMs256NSFsid_nono,
|
||||||
@ -285,17 +286,17 @@ class VC:
|
|||||||
tgt_sr,
|
tgt_sr,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
path = "%s/%s.wav" % (opt_root, os.path.basename(path))
|
path = "%s/%s.%s" % (opt_root, os.path.basename(path), format1)
|
||||||
|
with BytesIO() as wavf:
|
||||||
sf.write(
|
sf.write(
|
||||||
path,
|
wavf,
|
||||||
audio_opt,
|
audio_opt,
|
||||||
tgt_sr,
|
tgt_sr,
|
||||||
|
format="wav"
|
||||||
)
|
)
|
||||||
if os.path.exists(path):
|
wavf.seek(0, 0)
|
||||||
os.system(
|
with open(path, "wb") as outf:
|
||||||
"ffmpeg -i %s -vn %s -q:a 2 -y"
|
wav2(wavf, outf, format1)
|
||||||
% (path, path[:-4] + ".%s" % format1)
|
|
||||||
)
|
|
||||||
except:
|
except:
|
||||||
info += traceback.format_exc()
|
info += traceback.format_exc()
|
||||||
infos.append("%s->%s" % (os.path.basename(path), info))
|
infos.append("%s->%s" % (os.path.basename(path), info))
|
||||||
|
@ -43,3 +43,4 @@ torchcrepe==0.0.20
|
|||||||
fastapi==0.88
|
fastapi==0.88
|
||||||
ffmpy==0.3.1
|
ffmpy==0.3.1
|
||||||
python-dotenv>=1.0.0
|
python-dotenv>=1.0.0
|
||||||
|
av
|
||||||
|
@ -44,3 +44,4 @@ torchcrepe==0.0.20
|
|||||||
fastapi==0.88
|
fastapi==0.88
|
||||||
ffmpy==0.3.1
|
ffmpy==0.3.1
|
||||||
python-dotenv>=1.0.0
|
python-dotenv>=1.0.0
|
||||||
|
av
|
||||||
|
Loading…
Reference in New Issue
Block a user