2023-08-19 18:57:30 +08:00
|
|
|
import os
|
|
|
|
import traceback
|
2023-09-01 15:18:08 +08:00
|
|
|
import logging
|
2023-09-02 11:50:52 +08:00
|
|
|
|
2023-09-01 15:18:08 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
2023-08-19 18:57:30 +08:00
|
|
|
|
|
|
|
import ffmpeg
|
2023-08-28 15:08:31 +08:00
|
|
|
import torch
|
2023-08-19 18:57:30 +08:00
|
|
|
|
|
|
|
from configs.config import Config
|
|
|
|
from infer.modules.uvr5.mdxnet import MDXNetDereverb
|
2023-10-08 18:31:09 +08:00
|
|
|
from infer.modules.uvr5.vr import AudioPre, AudioPreDeEcho
|
2023-08-19 18:57:30 +08:00
|
|
|
|
|
|
|
config = Config()
|
|
|
|
|
|
|
|
|
|
|
|
def uvr(model_name, inp_root, save_root_vocal, paths, save_root_ins, agg, format0):
|
|
|
|
infos = []
|
|
|
|
try:
|
|
|
|
inp_root = inp_root.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
|
|
|
save_root_vocal = (
|
|
|
|
save_root_vocal.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
|
|
|
)
|
|
|
|
save_root_ins = (
|
|
|
|
save_root_ins.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
|
|
|
)
|
|
|
|
if model_name == "onnx_dereverb_By_FoxJoy":
|
|
|
|
pre_fun = MDXNetDereverb(15, config.device)
|
|
|
|
else:
|
|
|
|
func = AudioPre if "DeEcho" not in model_name else AudioPreDeEcho
|
|
|
|
pre_fun = func(
|
|
|
|
agg=int(agg),
|
2023-08-19 18:58:39 +08:00
|
|
|
model_path=os.path.join(
|
|
|
|
os.getenv("weight_uvr5_root"), model_name + ".pth"
|
|
|
|
),
|
2023-08-19 18:57:30 +08:00
|
|
|
device=config.device,
|
2023-10-08 18:31:09 +08:00
|
|
|
is_half=config.is_half
|
2023-08-19 18:57:30 +08:00
|
|
|
)
|
2023-10-08 18:31:09 +08:00
|
|
|
is_hp3 = "HP3" in model_name
|
2023-08-19 18:57:30 +08:00
|
|
|
if inp_root != "":
|
|
|
|
paths = [os.path.join(inp_root, name) for name in os.listdir(inp_root)]
|
|
|
|
else:
|
|
|
|
paths = [path.name for path in paths]
|
|
|
|
for path in paths:
|
|
|
|
inp_path = os.path.join(inp_root, path)
|
|
|
|
need_reformat = 1
|
|
|
|
done = 0
|
|
|
|
try:
|
|
|
|
info = ffmpeg.probe(inp_path, cmd="ffprobe")
|
|
|
|
if (
|
|
|
|
info["streams"][0]["channels"] == 2
|
|
|
|
and info["streams"][0]["sample_rate"] == "44100"
|
|
|
|
):
|
|
|
|
need_reformat = 0
|
|
|
|
pre_fun._path_audio_(
|
2023-10-08 18:31:09 +08:00
|
|
|
inp_path, save_root_ins, save_root_vocal, format0,is_hp3=is_hp3
|
2023-08-19 18:57:30 +08:00
|
|
|
)
|
|
|
|
done = 1
|
|
|
|
except:
|
|
|
|
need_reformat = 1
|
|
|
|
traceback.print_exc()
|
|
|
|
if need_reformat == 1:
|
2023-08-19 18:58:39 +08:00
|
|
|
tmp_path = "%s/%s.reformatted.wav" % (
|
2023-08-30 17:46:45 +08:00
|
|
|
os.path.join(os.environ["TEMP"]),
|
2023-08-19 18:58:39 +08:00
|
|
|
os.path.basename(inp_path),
|
|
|
|
)
|
2023-08-19 18:57:30 +08:00
|
|
|
os.system(
|
|
|
|
"ffmpeg -i %s -vn -acodec pcm_s16le -ac 2 -ar 44100 %s -y"
|
|
|
|
% (inp_path, tmp_path)
|
|
|
|
)
|
|
|
|
inp_path = tmp_path
|
|
|
|
try:
|
|
|
|
if done == 0:
|
2023-10-08 18:11:48 +08:00
|
|
|
pre_fun._path_audio_(
|
2023-08-19 18:57:30 +08:00
|
|
|
inp_path, save_root_ins, save_root_vocal, format0
|
|
|
|
)
|
|
|
|
infos.append("%s->Success" % (os.path.basename(inp_path)))
|
|
|
|
yield "\n".join(infos)
|
|
|
|
except:
|
2023-09-09 12:00:29 +08:00
|
|
|
try:
|
|
|
|
if done == 0:
|
|
|
|
pre_fun._path_audio_(
|
|
|
|
inp_path, save_root_ins, save_root_vocal, format0
|
|
|
|
)
|
|
|
|
infos.append("%s->Success" % (os.path.basename(inp_path)))
|
|
|
|
yield "\n".join(infos)
|
|
|
|
except:
|
|
|
|
infos.append(
|
|
|
|
"%s->%s" % (os.path.basename(inp_path), traceback.format_exc())
|
|
|
|
)
|
|
|
|
yield "\n".join(infos)
|
2023-08-19 18:57:30 +08:00
|
|
|
except:
|
|
|
|
infos.append(traceback.format_exc())
|
|
|
|
yield "\n".join(infos)
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
if model_name == "onnx_dereverb_By_FoxJoy":
|
|
|
|
del pre_fun.pred.model
|
|
|
|
del pre_fun.pred.model_
|
|
|
|
else:
|
|
|
|
del pre_fun.model
|
|
|
|
del pre_fun
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
if torch.cuda.is_available():
|
|
|
|
torch.cuda.empty_cache()
|
2023-09-01 15:18:08 +08:00
|
|
|
logger.info("Executed torch.cuda.empty_cache()")
|
2023-08-19 18:57:30 +08:00
|
|
|
yield "\n".join(infos)
|