innoai commited on
Commit
c54507d
·
verified ·
1 Parent(s): f72f6be

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import edge_tts
3
+ import asyncio
4
+ import tempfile
5
+ import os
6
+ from moviepy.editor import TextClip, concatenate_videoclips, CompositeVideoClip, AudioFileClip
7
+
8
+ # 获取所有可用的语音
9
+ async def get_voices():
10
+ voices = await edge_tts.list_voices()
11
+ return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
12
+
13
+ # 文字转语音功能
14
+ async def text_to_speech(text, voice, rate, pitch):
15
+ if not text.strip():
16
+ return None, gr.Warning("Please enter the text to convert.")
17
+ if not voice:
18
+ return None, gr.Warning("Please select a voice.")
19
+
20
+ voice_short_name = voice.split(" - ")[0]
21
+ rate_str = f"{rate:+d}%"
22
+ pitch_str = f"{pitch:+d}Hz"
23
+ communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
24
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
25
+ tmp_path = tmp_file.name
26
+ await communicate.save(tmp_path)
27
+ return tmp_path, None
28
+
29
+ # 文字转视频功能
30
+ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
31
+ # 字体文件路径
32
+ font_path = os.path.abspath(text_font)
33
+
34
+ # 计算每页可以容纳的行数和每行可以容纳的字符数
35
+ max_chars_per_line = video_width // (text_size // 2) # 字体宽度假设为字体大小的一半
36
+ max_lines_per_page = video_height // (text_size + 15) # 10是行间距
37
+
38
+ # 按页拆分文本
39
+ words = text.split()
40
+ lines = []
41
+ current_line = ""
42
+ pages = []
43
+
44
+ for word in words:
45
+ if len(current_line) + len(word) + 1 > max_chars_per_line:
46
+ lines.append(current_line)
47
+ current_line = word
48
+ if len(lines) == max_lines_per_page:
49
+ pages.append("\n".join(lines))
50
+ lines = []
51
+ else:
52
+ current_line = f"{current_line} {word}".strip()
53
+
54
+ lines.append(current_line)
55
+ if lines:
56
+ pages.append("\n".join(lines))
57
+
58
+ # 为每页生成独立音频
59
+ audio_clips = []
60
+ video_clips = []
61
+ for i, page in enumerate(pages):
62
+ # 将每页的文本连贯朗读生成一个音频文件
63
+ audio_text = page.replace("\n", " ") # 移除换行符以防止 TTS 停顿
64
+ audio, warning = asyncio.run(text_to_speech(audio_text, voice, rate, pitch))
65
+ if warning:
66
+ return None, warning
67
+ audio_clip = AudioFileClip(audio)
68
+ audio_clips.append(audio_clip)
69
+
70
+ # 生成视频片段
71
+ text_clip = TextClip(page, fontsize=text_size, font=font_path, color=text_color, size=(video_width, video_height), bg_color=bg_color, method='caption', align='center')
72
+ text_clip = text_clip.set_duration(audio_clip.duration).set_audio(audio_clip)
73
+ video_clips.append(text_clip)
74
+
75
+ # 合并所有视频片段
76
+ final_video = concatenate_videoclips(video_clips)
77
+ final_video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
78
+ final_video.write_videofile(final_video_path, fps=24, codec="libx264")
79
+ return final_video_path, None
80
+
81
+ # Gradio接口函数
82
+ def tts_interface(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
83
+ video, warning = text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size)
84
+ return None, video, warning
85
+
86
+ # 创建Gradio应用
87
+ async def create_demo():
88
+ voices = await get_voices()
89
+
90
+ demo = gr.Interface(
91
+ fn=tts_interface,
92
+ inputs=[
93
+ gr.Textbox(label="Input Text", lines=5),
94
+ gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=""),
95
+ gr.Slider(minimum=-50, maximum=50, value=0, label="Rate Adjustment (%)", step=1),
96
+ gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1),
97
+ gr.Slider(minimum=640, maximum=1920, value=1080, label="Video Width", step=10),
98
+ gr.Slider(minimum=480, maximum=1080, value=720, label="Video Height", step=10),
99
+ gr.ColorPicker(value="#000000", label="Background Color"),
100
+ gr.ColorPicker(value="#FFFFFF", label="Text Color"),
101
+ gr.Textbox(label="Text Font", value="msyh.ttc"), # 请确保字体文件路径正确
102
+ gr.Slider(minimum=10, maximum=100, value=24, label="Text Size", step=1)
103
+ ],
104
+ outputs=[
105
+ gr.Audio(label="Generated Audio", type="filepath"),
106
+ gr.Video(label="Generated Video"),
107
+ gr.Markdown(label="Warning", visible=False)
108
+ ],
109
+ title="Edge TTS Text to Speech and Video",
110
+ description="Convert text to speech and video using Microsoft Edge TTS. Adjust rate and pitch: 0 is the default value, positive values increase, and negative values decrease.",
111
+ analytics_enabled=False,
112
+ allow_flagging=False,
113
+ )
114
+
115
+ return demo
116
+
117
+ # 运行应用
118
+ if __name__ == "__main__":
119
+ demo = asyncio.run(create_demo())
120
+ demo.launch(server_port=19856)