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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re, os
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
headers = {
|
| 6 |
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
|
| 7 |
+
}
|
| 8 |
+
pattern = r'//www\.bilibili\.com/video[^"]*'
|
| 9 |
+
|
| 10 |
+
def get_bilibili_video_id(url):
|
| 11 |
+
match = re.search(r'/video/([a-zA-Z0-9]+)/', url)
|
| 12 |
+
extracted_value = match.group(1)
|
| 13 |
+
return extracted_value
|
| 14 |
+
|
| 15 |
+
# Get bilibili audio
|
| 16 |
+
def find_first_appearance_with_neighborhood(text, pattern):
|
| 17 |
+
match = re.search(pattern, text)
|
| 18 |
+
|
| 19 |
+
if match:
|
| 20 |
+
return match.group()
|
| 21 |
+
else:
|
| 22 |
+
return None
|
| 23 |
+
|
| 24 |
+
def search_bilibili(keyword):
|
| 25 |
+
if keyword.startswith("BV"):
|
| 26 |
+
req = requests.get("https://search.bilibili.com/all?keyword={}&duration=1".format(keyword), headers=headers).text
|
| 27 |
+
else:
|
| 28 |
+
req = requests.get("https://search.bilibili.com/all?keyword={}&duration=1&tids=3&page=1".format(keyword), headers=headers).text
|
| 29 |
+
|
| 30 |
+
video_link = "https:" + find_first_appearance_with_neighborhood(req, pattern)
|
| 31 |
+
|
| 32 |
+
return video_link
|
| 33 |
+
|
| 34 |
+
def get_response(html_url):
|
| 35 |
+
headers = {
|
| 36 |
+
"referer": "https://www.bilibili.com/",
|
| 37 |
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
|
| 38 |
+
}
|
| 39 |
+
response = requests.get(html_url, headers=headers)
|
| 40 |
+
return response
|
| 41 |
+
|
| 42 |
+
def get_video_info(html_url):
|
| 43 |
+
response = get_response(html_url)
|
| 44 |
+
html_data = re.findall('<script>window.__playinfo__=(.*?)</script>', response.text)[0]
|
| 45 |
+
json_data = json.loads(html_data)
|
| 46 |
+
if json_data['data']['dash']['audio'][0]['backupUrl']!=None:
|
| 47 |
+
audio_url = json_data['data']['dash']['audio'][0]['backupUrl'][0]
|
| 48 |
+
else:
|
| 49 |
+
audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
|
| 50 |
+
video_url = json_data['data']['dash']['video'][0]['baseUrl']
|
| 51 |
+
return audio_url, video_url
|
| 52 |
+
|
| 53 |
+
def save_audio(title, html_url):
|
| 54 |
+
audio_url = get_video_info(html_url)[0]
|
| 55 |
+
#video_url = get_video_info(html_url)[1]
|
| 56 |
+
|
| 57 |
+
audio_content = get_response(audio_url).content
|
| 58 |
+
#video_content = get_response(video_url).content
|
| 59 |
+
|
| 60 |
+
with open(title + '.mp3', mode='wb') as f:
|
| 61 |
+
f.write(audio_content)
|
| 62 |
+
print("音乐内容保存完成")
|
| 63 |
+
#with open(title + '.mp4', mode='wb') as f:
|
| 64 |
+
# f.write(video_content)
|
| 65 |
+
#print("视频内容保存完成"
|
| 66 |
+
|
| 67 |
+
from uvr5.vr import AudioPre
|
| 68 |
+
weight_uvr5_root = "uvr5/uvr_model"
|
| 69 |
+
uvr5_names = []
|
| 70 |
+
for name in os.listdir(weight_uvr5_root):
|
| 71 |
+
if name.endswith(".pth") or "onnx" in name:
|
| 72 |
+
uvr5_names.append(name.replace(".pth", ""))
|
| 73 |
+
|
| 74 |
+
func = AudioPre
|
| 75 |
+
pre_fun_hp2 = func(
|
| 76 |
+
agg=int(10),
|
| 77 |
+
model_path=os.path.join(weight_uvr5_root, "UVR-HP2.pth"),
|
| 78 |
+
device="cuda",
|
| 79 |
+
is_half=True,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
pre_fun_hp5 = func(
|
| 83 |
+
agg=int(10),
|
| 84 |
+
model_path=os.path.join(weight_uvr5_root, "UVR-HP5.pth"),
|
| 85 |
+
device="cuda",
|
| 86 |
+
is_half=True,
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
import webrtcvad
|
| 90 |
+
from pydub import AudioSegment
|
| 91 |
+
from pydub.utils import make_chunks
|
| 92 |
+
|
| 93 |
+
import os
|
| 94 |
+
import librosa
|
| 95 |
+
import soundfile
|
| 96 |
+
import gradio as gr
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def vad(audio_name):
|
| 100 |
+
audio = AudioSegment.from_file(audio_name, format="wav")
|
| 101 |
+
# Set the desired sample rate (WebRTC VAD supports only 8000, 16000, 32000, or 48000 Hz)
|
| 102 |
+
audio = audio.set_frame_rate(48000)
|
| 103 |
+
# Set single channel (mono)
|
| 104 |
+
audio = audio.set_channels(1)
|
| 105 |
+
|
| 106 |
+
# Initialize VAD
|
| 107 |
+
vad = webrtcvad.Vad()
|
| 108 |
+
# Set aggressiveness mode (an integer between 0 and 3, 3 is the most aggressive)
|
| 109 |
+
vad.set_mode(3)
|
| 110 |
+
|
| 111 |
+
# Convert pydub audio to bytes
|
| 112 |
+
frame_duration = 30 # Duration of a frame in ms
|
| 113 |
+
frame_width = int(audio.frame_rate * frame_duration / 1000) # width of a frame in samples
|
| 114 |
+
frames = make_chunks(audio, frame_duration)
|
| 115 |
+
|
| 116 |
+
# Perform voice activity detection
|
| 117 |
+
voiced_frames = []
|
| 118 |
+
for frame in frames:
|
| 119 |
+
if len(frame.raw_data) < frame_width * 2: # Ensure frame is correct length
|
| 120 |
+
break
|
| 121 |
+
is_speech = vad.is_speech(frame.raw_data, audio.frame_rate)
|
| 122 |
+
if is_speech:
|
| 123 |
+
voiced_frames.append(frame)
|
| 124 |
+
|
| 125 |
+
# Combine voiced frames back to an audio segment
|
| 126 |
+
voiced_audio = sum(voiced_frames, AudioSegment.silent(duration=0))
|
| 127 |
+
|
| 128 |
+
voiced_audio.export("voiced_audio.wav", format="wav")
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def youtube_downloader(
|
| 134 |
+
video_identifier,
|
| 135 |
+
filename,
|
| 136 |
+
split_model,
|
| 137 |
+
start_time
|
| 138 |
+
):
|
| 139 |
+
print(video_identifier)
|
| 140 |
+
video_info = get_video_info(video_identifier)[0]
|
| 141 |
+
print(video_info)
|
| 142 |
+
audio_content = get_response(video_info).content
|
| 143 |
+
with open(filename.strip() + ".wav", mode="wb") as f:
|
| 144 |
+
f.write(audio_content)
|
| 145 |
+
audio_path = filename.strip() + ".wav"
|
| 146 |
+
start_ms = start_time * 1000
|
| 147 |
+
end_ms = start_ms + 45000
|
| 148 |
+
# make dir output
|
| 149 |
+
os.makedirs("output", exist_ok=True)
|
| 150 |
+
|
| 151 |
+
if split_model=="UVR-HP2":
|
| 152 |
+
pre_fun = pre_fun_hp2
|
| 153 |
+
else:
|
| 154 |
+
pre_fun = pre_fun_hp5
|
| 155 |
+
|
| 156 |
+
audio_orig = AudioSegment.from_file(audio_path)
|
| 157 |
+
if len(audio_orig) > end_ms:
|
| 158 |
+
|
| 159 |
+
# Extract the segment
|
| 160 |
+
segment = audio_orig[start_ms:end_ms]
|
| 161 |
+
segment.export(filename.strip() + ".wav", format="wav")
|
| 162 |
+
pre_fun._path_audio_(filename.strip() + ".wav", f"./output/{split_model}/{filename}/", f"./output/{split_model}/{filename}/", "wav")
|
| 163 |
+
os.remove(filename.strip()+".wav")
|
| 164 |
+
else:
|
| 165 |
+
segment = audio_orig[start_ms:len(audio_orig)]
|
| 166 |
+
segment.export(filename.strip() + ".wav", format="wav")
|
| 167 |
+
pre_fun._path_audio_(filename.strip() + ".wav", f"./output/{split_model}/{filename}/", f"./output/{split_model}/{filename}/", "wav")
|
| 168 |
+
os.remove(filename.strip()+".wav")
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
return f"./output/{split_model}/{filename}/vocal_{filename}.wav_10.wav", f"./output/{split_model}/{filename}/instrument_{filename}.wav_10.wav"
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def youtube_downloader_100s(
|
| 175 |
+
video_identifier,
|
| 176 |
+
filename,
|
| 177 |
+
split_model
|
| 178 |
+
):
|
| 179 |
+
print(video_identifier)
|
| 180 |
+
video_info = get_video_info(video_identifier)[0]
|
| 181 |
+
print(video_info)
|
| 182 |
+
audio_content = get_response(video_info).content
|
| 183 |
+
with open(filename.strip() + ".wav", mode="wb") as f:
|
| 184 |
+
f.write(audio_content)
|
| 185 |
+
audio_path = filename.strip() + ".wav"
|
| 186 |
+
if split_model=="UVR-HP2":
|
| 187 |
+
pre_fun = pre_fun_hp2
|
| 188 |
+
else:
|
| 189 |
+
pre_fun = pre_fun_hp5
|
| 190 |
+
|
| 191 |
+
os.makedirs("output", exist_ok=True)
|
| 192 |
+
audio_orig = AudioSegment.from_file(audio_path)
|
| 193 |
+
|
| 194 |
+
if len(audio_orig) > 120000:
|
| 195 |
+
start_ms = 10000
|
| 196 |
+
end_ms = start_ms + 110000
|
| 197 |
+
|
| 198 |
+
# Extract the segment
|
| 199 |
+
|
| 200 |
+
segment = audio_orig[start_ms:end_ms]
|
| 201 |
+
|
| 202 |
+
segment.export(filename.strip() + ".wav", format="wav")
|
| 203 |
+
|
| 204 |
+
pre_fun._path_audio_(filename.strip() + ".wav", f"./output/{split_model}/{filename}/", f"./output/{split_model}/{filename}/", "wav")
|
| 205 |
+
os.remove(filename.strip()+".wav")
|
| 206 |
+
else:
|
| 207 |
+
pre_fun._path_audio_(filename.strip() + ".wav", f"./output/{split_model}/{filename}/", f"./output/{split_model}/{filename}/", "wav")
|
| 208 |
+
os.remove(filename.strip()+".wav")
|
| 209 |
+
|
| 210 |
+
return f"./output/{split_model}/{filename}/vocal_{filename}.wav_10.wav", f"./output/{split_model}/{filename}/instrument_{filename}.wav_10.wav"
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def convert(start_time, song_name_src, song_name_ref, vocal_vol, inst_vol):
|
| 214 |
+
split_model = "UVR-HP5"
|
| 215 |
+
song_name_ref = song_name_ref.strip().replace(" ", "")
|
| 216 |
+
video_identifier = search_bilibili(song_name_ref)
|
| 217 |
+
song_id = get_bilibili_video_id(video_identifier)
|
| 218 |
+
|
| 219 |
+
song_name_src = song_name_src.strip().replace(" ", "")
|
| 220 |
+
video_identifier_src = search_bilibili(song_name_src)
|
| 221 |
+
song_id_src = get_bilibili_video_id(video_identifier_src)
|
| 222 |
+
|
| 223 |
+
if os.path.isdir(f"./output/{split_model}/{song_id}")==False:
|
| 224 |
+
audio, sr = librosa.load(youtube_downloader_100s(video_identifier, song_id, split_model)[0], sr=24000, mono=True)
|
| 225 |
+
soundfile.write("audio_ref.wav", audio, sr)
|
| 226 |
+
else:
|
| 227 |
+
audio, sr = librosa.load(f"./output/{split_model}/{song_id}/vocal_{song_id}.wav_10.wav", sr=24000, mono=True)
|
| 228 |
+
soundfile.write("audio_ref.wav", audio, sr)
|
| 229 |
+
|
| 230 |
+
vad("audio_ref.wav")
|
| 231 |
+
|
| 232 |
+
#if os.path.isdir(f"./output/{split_model}/{song_id_src}")==False:
|
| 233 |
+
audio_src, sr_src = librosa.load(youtube_downloader(video_identifier_src, song_id_src, split_model, start_time)[0], sr=24000, mono=True)
|
| 234 |
+
soundfile.write("audio_src.wav", audio_src, sr_src)
|
| 235 |
+
#else:
|
| 236 |
+
# audio_src, sr_src = librosa.load(f"./output/{split_model}/{song_id_src}/vocal_{song_id_src}.wav_10.wav", sr=24000, mono=True)
|
| 237 |
+
# soundfile.write("audio_src.wav", audio_src, sr_src)
|
| 238 |
+
if os.path.isfile("output.wav"):
|
| 239 |
+
os.remove("output.wav")
|
| 240 |
+
os.system("python infer.py --src_wav_path audio_src.wav --ref_wav_path voiced_audio.wav --out_path output.wav")
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
audio_vocal = AudioSegment.from_file("output.wav", format="wav")
|
| 244 |
+
|
| 245 |
+
# Load the second audio file
|
| 246 |
+
audio_inst = AudioSegment.from_file(f"output/{split_model}/{song_id_src}/instrument_{song_id_src}.wav_10.wav", format="wav")
|
| 247 |
+
|
| 248 |
+
audio_vocal = audio_vocal + vocal_vol # Increase volume of the first audio by 5 dB
|
| 249 |
+
audio_inst = audio_inst + inst_vol # Decrease volume of the second audio by 5 dB
|
| 250 |
+
|
| 251 |
+
# Concatenate audio files
|
| 252 |
+
combined_audio = audio_vocal.overlay(audio_inst)
|
| 253 |
+
|
| 254 |
+
# Export the concatenated audio to a new file
|
| 255 |
+
combined_audio.export(f"{song_name_src}-AI翻唱.wav", format="wav")
|
| 256 |
+
|
| 257 |
+
return f"{song_name_src}-AI翻唱.wav"
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
app = gr.Blocks()
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
with app:
|
| 265 |
+
gr.Markdown("# <center>🥳💕🎶 NeuCoSVC 终极版:一键生成专属AI歌手,无需训练、一键翻唱</center>")
|
| 266 |
+
gr.Markdown("## <center>🌟 只需 2 个歌曲名,一键翻唱任意歌手的任意歌曲,随时随地,听你想听!Powered by [NeuCoSVC](https://github.com/thuhcsi/NeuCoSVC)</center>")
|
| 267 |
+
gr.Markdown("### <center>🌊 更多精彩应用,敬请关注[滔滔AI](http://www.talktalkai.com);滔滔AI,为爱滔滔!💕</center>")
|
| 268 |
+
with gr.Row():
|
| 269 |
+
with gr.Column():
|
| 270 |
+
with gr.Row():
|
| 271 |
+
inp1 = gr.Textbox(label="请填写想要AI翻唱的歌曲或BV号", info="直接填写BV号的得到的歌曲最匹配,也可以选择填写“歌曲名+歌手名”")
|
| 272 |
+
inp2 = gr.Textbox(label="请填写含有目标音色的歌曲或BV号", info="例如您希望使用AI周杰伦的音色,就在此处填写周杰伦的任意一首歌")
|
| 273 |
+
inp0 = gr.Number(value=0, label="起始时间 (秒)", info="此程序将自动���起始时间开始提取45秒的翻唱歌曲")
|
| 274 |
+
inp3 = gr.Slider(minimum=-3, maximum=3, value=0, step=1, label="调节人声音量,默认为0")
|
| 275 |
+
inp4 = gr.Slider(minimum=-3, maximum=3, value=0, step=1, label="调节伴奏音量,默认为0")
|
| 276 |
+
btn = gr.Button("一键开启AI翻唱之旅吧💕", variant="primary")
|
| 277 |
+
with gr.Column():
|
| 278 |
+
out = gr.Audio(label="AI歌手为您亲情演唱的歌曲", type="filepath", interactive=False)
|
| 279 |
+
|
| 280 |
+
btn.click(convert, [inp0, inp1, inp2, inp3, inp4], out)
|
| 281 |
+
|
| 282 |
+
gr.Markdown("### <center>注意❗:请不要生成会对个人以及组织造成侵害的内容,此程序仅供科研、学习及个人娱乐使用。</center>")
|
| 283 |
+
gr.HTML('''
|
| 284 |
+
<div class="footer">
|
| 285 |
+
<p>🌊🏞️🎶 - 江水东流急,滔滔无尽声。 明·顾璘
|
| 286 |
+
</p>
|
| 287 |
+
</div>
|
| 288 |
+
''')
|
| 289 |
+
|
| 290 |
+
app.queue().launch(share=True, show_error=True)
|