innoai commited on
Commit
31593f9
·
verified ·
1 Parent(s): f35c431

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -6
app.py CHANGED
@@ -4,6 +4,9 @@ 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():
@@ -67,10 +70,23 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
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)
@@ -98,7 +114,7 @@ async def create_demo():
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=[
@@ -117,4 +133,4 @@ async def create_demo():
117
  # 运行应用
118
  if __name__ == "__main__":
119
  demo = asyncio.run(create_demo())
120
- demo.launch()
 
4
  import tempfile
5
  import os
6
  from moviepy.editor import TextClip, concatenate_videoclips, CompositeVideoClip, AudioFileClip
7
+ from wand.image import Image
8
+ from wand.drawing import Drawing
9
+ from wand.color import Color
10
 
11
  # 获取所有可用的语音
12
  async def get_voices():
 
70
  audio_clip = AudioFileClip(audio)
71
  audio_clips.append(audio_clip)
72
 
73
+ # 使用 wand 生成视频片段
74
+ with Drawing() as draw:
75
+ draw.font = font_path
76
+ draw.font_size = text_size
77
+ draw.fill_color = Color(text_color)
78
+ draw.text_alignment = 'center'
79
+ draw.text_interline_spacing = 10
80
+ with Image(width=video_width, height=video_height, background=Color(bg_color)) as img:
81
+ lines = page.split("\n")
82
+ for j, line in enumerate(lines):
83
+ draw.text(int(video_width / 2), (j + 1) * (text_size + 10), line)
84
+ img.draw(draw)
85
+ img.format = 'png'
86
+ img_path = os.path.join(tempfile.gettempdir(), f"page_{i}.png")
87
+ img.save(filename=img_path)
88
+ text_clip = ImageClip(img_path).set_duration(audio_clip.duration).set_audio(audio_clip)
89
+ video_clips.append(text_clip)
90
 
91
  # 合并所有视频片段
92
  final_video = concatenate_videoclips(video_clips)
 
114
  gr.Slider(minimum=480, maximum=1080, value=720, label="Video Height", step=10),
115
  gr.ColorPicker(value="#000000", label="Background Color"),
116
  gr.ColorPicker(value="#FFFFFF", label="Text Color"),
117
+ gr.Textbox(label="Text Font", value="msyh.ttf"), # 请确保字体文件路径正确
118
  gr.Slider(minimum=10, maximum=100, value=24, label="Text Size", step=1)
119
  ],
120
  outputs=[
 
133
  # 运行应用
134
  if __name__ == "__main__":
135
  demo = asyncio.run(create_demo())
136
+ demo.launch(server_port=19856)