Spaces:
Runtime error
Runtime error
Create utils.py
Browse files- lib/vc/utils.py +84 -0
lib/vc/utils.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import wave
|
3 |
+
import subprocess
|
4 |
+
import yt_dlp
|
5 |
+
import ffmpeg
|
6 |
+
import logging
|
7 |
+
from fairseq import checkpoint_utils
|
8 |
+
logger = logging.getLogger(__name__)
|
9 |
+
|
10 |
+
def load_hubert(config):
|
11 |
+
path_check = os.path.exists("assets/hubert/hubert_base.pt")
|
12 |
+
if path_check is False:
|
13 |
+
logger.warn("hubert_base.pt is missing. Please check the documentation for to get it.")
|
14 |
+
else:
|
15 |
+
logger.info("hubert_base.pt found.")
|
16 |
+
models, _, _ = checkpoint_utils.load_model_ensemble_and_task(
|
17 |
+
[os.path.join("assets", "hubert", "hubert_base.pt")],
|
18 |
+
suffix="",
|
19 |
+
)
|
20 |
+
hubert_model = models[0]
|
21 |
+
hubert_model = hubert_model.to(config.device)
|
22 |
+
if config.is_half:
|
23 |
+
hubert_model = hubert_model.half()
|
24 |
+
else:
|
25 |
+
hubert_model = hubert_model.float()
|
26 |
+
hubert_model.eval()
|
27 |
+
return hubert_model
|
28 |
+
|
29 |
+
def download_audio(url, audio_provider):
|
30 |
+
logs = []
|
31 |
+
if url == "":
|
32 |
+
logs.append("URL required!")
|
33 |
+
yield None, "\n".join(logs)
|
34 |
+
return None, "\n".join(logs)
|
35 |
+
if not os.path.exists("yt"):
|
36 |
+
os.mkdir("yt")
|
37 |
+
if audio_provider == "Youtube":
|
38 |
+
logs.append("Downloading the audio...")
|
39 |
+
yield None, "\n".join(logs)
|
40 |
+
ydl_opts = {
|
41 |
+
'noplaylist': True,
|
42 |
+
'format': 'bestaudio/best',
|
43 |
+
'postprocessors': [{
|
44 |
+
'key': 'FFmpegExtractAudio',
|
45 |
+
'preferredcodec': 'wav',
|
46 |
+
}],
|
47 |
+
"outtmpl": 'yt/audio',
|
48 |
+
}
|
49 |
+
audio_path = "yt/audio.wav"
|
50 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
51 |
+
ydl.download([url])
|
52 |
+
logs.append("Download Complete.")
|
53 |
+
yield audio_path, "\n".join(logs)
|
54 |
+
|
55 |
+
def cut_vocal_and_inst(split_model):
|
56 |
+
logs = []
|
57 |
+
logs.append("Starting the audio splitting process...")
|
58 |
+
yield "\n".join(logs), None, None, None
|
59 |
+
command = f"demucs --two-stems=vocals -n {split_model} yt/audio.wav -o output"
|
60 |
+
result = subprocess.Popen(command.split(), stdout=subprocess.PIPE, text=True)
|
61 |
+
for line in result.stdout:
|
62 |
+
logs.append(line)
|
63 |
+
yield "\n".join(logs), None, None, None
|
64 |
+
logger.info(result.stdout)
|
65 |
+
vocal = f"output/{split_model}/audio/vocals.wav"
|
66 |
+
inst = f"output/{split_model}/audio/no_vocals.wav"
|
67 |
+
logs.append("Audio splitting complete.")
|
68 |
+
yield "\n".join(logs), vocal, inst, vocal
|
69 |
+
|
70 |
+
def combine_vocal_and_inst(audio_data, vocal_volume, inst_volume, split_model):
|
71 |
+
if not os.path.exists("output/result"):
|
72 |
+
os.mkdir("output/result")
|
73 |
+
vocal_path = "output/result/output.wav"
|
74 |
+
output_path = "output/result/combine.mp3"
|
75 |
+
inst_path = f"output/{split_model}/audio/no_vocals.wav"
|
76 |
+
with wave.open(vocal_path, "w") as wave_file:
|
77 |
+
wave_file.setnchannels(1)
|
78 |
+
wave_file.setsampwidth(2)
|
79 |
+
wave_file.setframerate(audio_data[0])
|
80 |
+
wave_file.writeframes(audio_data[1].tobytes())
|
81 |
+
command = f'ffmpeg -y -i {inst_path} -i {vocal_path} -filter_complex [0:a]volume={inst_volume}[i];[1:a]volume={vocal_volume}[v];[i][v]amix=inputs=2:duration=longest[a] -map [a] -b:a 320k -c:a libmp3lame {output_path}'
|
82 |
+
result = subprocess.run(command.split(), stdout=subprocess.PIPE)
|
83 |
+
logger.info(result.stdout.decode())
|
84 |
+
return output_path
|