Spaces:
Paused
Paused
Update app_srt.py
Browse files- app_srt.py +66 -9
app_srt.py
CHANGED
@@ -8,6 +8,38 @@ import time
|
|
8 |
import torch
|
9 |
import torchaudio
|
10 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
#download for mecab
|
13 |
os.system('python -m unidic download')
|
@@ -478,7 +510,7 @@ def trim_audio(intervals, input_file_path, output_file_path):
|
|
478 |
segment = audio[start_time*1000:end_time*1000]
|
479 |
|
480 |
# construct the output file path
|
481 |
-
output_file_path_i = f"{output_file_path}_{i}.wav"
|
482 |
|
483 |
# export the segment to a file
|
484 |
segment.export(output_file_path_i, format='wav')
|
@@ -507,14 +539,37 @@ def merge_audios(input_dir):
|
|
507 |
combined.export(output_file, format="wav")
|
508 |
return "AI配音版.wav"
|
509 |
|
510 |
-
def convert_from_srt(filename,
|
511 |
subtitle_list = read_srt(filename)
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
518 |
return merge_audios("output")
|
519 |
|
520 |
with gr.Blocks() as app:
|
@@ -549,11 +604,13 @@ with gr.Blocks() as app:
|
|
549 |
max_choices=1,
|
550 |
value="en",
|
551 |
)
|
|
|
|
|
552 |
btn = gr.Button("一键开启AI配音吧💕", variant="primary")
|
553 |
with gr.Column():
|
554 |
out1 = gr.Audio(label="为您生成的AI完整配音")
|
555 |
|
556 |
-
btn.click(convert_from_srt, [inp1, inp2, inp3], [out1])
|
557 |
|
558 |
gr.Markdown("### <center>注意❗:请不要生成会对任何个人或组织造成侵害的内容。</center>")
|
559 |
gr.HTML('''
|
|
|
8 |
import torch
|
9 |
import torchaudio
|
10 |
import gradio as gr
|
11 |
+
import shutil
|
12 |
+
|
13 |
+
# mp4 to wav and denoising
|
14 |
+
|
15 |
+
import ffmpeg
|
16 |
+
import urllib.request
|
17 |
+
urllib.request.urlretrieve("https://download.openxlab.org.cn/models/Kevin676/rvc-models/weight/UVR-HP2.pth", "uvr5/uvr_model/UVR-HP2.pth")
|
18 |
+
urllib.request.urlretrieve("https://download.openxlab.org.cn/models/Kevin676/rvc-models/weight/UVR-HP5.pth", "uvr5/uvr_model/UVR-HP5.pth")
|
19 |
+
|
20 |
+
from uvr5.vr import AudioPre
|
21 |
+
weight_uvr5_root = "uvr5/uvr_model"
|
22 |
+
uvr5_names = []
|
23 |
+
for name in os.listdir(weight_uvr5_root):
|
24 |
+
if name.endswith(".pth") or "onnx" in name:
|
25 |
+
uvr5_names.append(name.replace(".pth", ""))
|
26 |
+
|
27 |
+
func = AudioPre
|
28 |
+
|
29 |
+
pre_fun_hp2 = func(
|
30 |
+
agg=int(10),
|
31 |
+
model_path=os.path.join(weight_uvr5_root, "UVR-HP2.pth"),
|
32 |
+
device="cuda",
|
33 |
+
is_half=True,
|
34 |
+
)
|
35 |
+
pre_fun_hp5 = func(
|
36 |
+
agg=int(10),
|
37 |
+
model_path=os.path.join(weight_uvr5_root, "UVR-HP5.pth"),
|
38 |
+
device="cuda",
|
39 |
+
is_half=True,
|
40 |
+
)
|
41 |
+
|
42 |
+
# mp4 to wav and denoising ending
|
43 |
|
44 |
#download for mecab
|
45 |
os.system('python -m unidic download')
|
|
|
510 |
segment = audio[start_time*1000:end_time*1000]
|
511 |
|
512 |
# construct the output file path
|
513 |
+
output_file_path_i = f"./sliced/{output_file_path}_{i}.wav"
|
514 |
|
515 |
# export the segment to a file
|
516 |
segment.export(output_file_path_i, format='wav')
|
|
|
539 |
combined.export(output_file, format="wav")
|
540 |
return "AI配音版.wav"
|
541 |
|
542 |
+
def convert_from_srt(filename, video_full, language, split_model, multilingual):
|
543 |
subtitle_list = read_srt(filename)
|
544 |
+
|
545 |
+
ffmpeg.input(video_full).output("audio_full.wav", ac=2, ar=44100).run()
|
546 |
+
|
547 |
+
if split_model=="UVR-HP2":
|
548 |
+
pre_fun = pre_fun_hp2
|
549 |
+
else:
|
550 |
+
pre_fun = pre_fun_hp5
|
551 |
+
|
552 |
+
pre_fun._path_audio_("audio_full.wav", f"./denoised/{split_model}/{filename}/", f"./denoised/{split_model}/{filename}/", "wav")
|
553 |
+
|
554 |
+
shutil.rmtree("output")
|
555 |
+
shutil.rmtree("sliced")
|
556 |
+
if multilingual==False:
|
557 |
+
for i in subtitle_list:
|
558 |
+
os.makedirs("output", exist_ok=True)
|
559 |
+
os.makedirs("sliced", exist_ok=True)
|
560 |
+
trim_audio([[i.start_time, i.end_time]], f"./denoised/{split_model}/{filename}/vocal_{filename}.wav_10.wav", f"./sliced/sliced_audio_{i.index}")
|
561 |
+
print(f"正在合成第{i.index}条语音")
|
562 |
+
print(f"语音内容:{i.text}")
|
563 |
+
predict(i.text, language, f"./sliced/sliced_audio_{i.index}_0.wav", i.text + " " + str(i.index))
|
564 |
+
else:
|
565 |
+
for i in subtitle_list:
|
566 |
+
os.makedirs("output", exist_ok=True)
|
567 |
+
os.makedirs("sliced", exist_ok=True)
|
568 |
+
trim_audio([[i.start_time, i.end_time]], f"./denoised/{split_model}/{filename}/vocal_{filename}.wav_10.wav", f"./sliced/sliced_audio_{i.index}")
|
569 |
+
print(f"正在合成第{i.index}条语音")
|
570 |
+
print(f"语音内容:{i.text.splitlines()[1]}")
|
571 |
+
predict(i.text.splitlines()[1], language, f"./sliced/sliced_audio_{i.index}_0.wav", i.text.splitlines()[1] + " " + str(i.index))
|
572 |
+
|
573 |
return merge_audios("output")
|
574 |
|
575 |
with gr.Blocks() as app:
|
|
|
604 |
max_choices=1,
|
605 |
value="en",
|
606 |
)
|
607 |
+
inp4 = gr.Dropdown(label="请选择用于分离伴奏的模型", info="UVR-HP5去除背景音乐效果更好,但会对人声造成一定的损伤", choices=["UVR-HP2", "UVR-HP5"], value="UVR-HP5")
|
608 |
+
inp5 = gr.Checkbox(label="SRT文件是否为双语字幕", info="若为双语字幕,请打勾选择")
|
609 |
btn = gr.Button("一键开启AI配音吧💕", variant="primary")
|
610 |
with gr.Column():
|
611 |
out1 = gr.Audio(label="为您生成的AI完整配音")
|
612 |
|
613 |
+
btn.click(convert_from_srt, [inp1, inp2, inp3, inp4, inp5], [out1])
|
614 |
|
615 |
gr.Markdown("### <center>注意❗:请不要生成会对任何个人或组织造成侵害的内容。</center>")
|
616 |
gr.HTML('''
|