Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,8 +31,8 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
| 31 |
|
| 32 |
# 文字转视频功能
|
| 33 |
def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
|
| 34 |
-
#
|
| 35 |
-
font_path = os.path.abspath(text_font)
|
| 36 |
|
| 37 |
# 计算每页可以容纳的行数和每行可以容纳的字符数
|
| 38 |
max_chars_per_line = video_width // (text_size // 2) # 字体宽度假设为字体大小的一半
|
|
@@ -74,7 +74,7 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
|
|
| 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:
|
|
@@ -82,6 +82,7 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
|
|
| 82 |
for j, line in enumerate(lines):
|
| 83 |
# Calculate y position based on the line index
|
| 84 |
y_position = int((video_height / 2) - ((len(lines) * (text_size + 10)) / 2) + j * (text_size + 10))
|
|
|
|
| 85 |
draw.text(int(video_width / 2), y_position, line)
|
| 86 |
draw(img) # Correctly draw the text onto the image
|
| 87 |
img.format = 'png'
|
|
@@ -89,7 +90,7 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
|
|
| 89 |
img.save(filename=img_path)
|
| 90 |
text_clip = ImageClip(img_path).set_duration(audio_clip.duration).set_audio(audio_clip)
|
| 91 |
video_clips.append(text_clip)
|
| 92 |
-
|
| 93 |
# 合并所有视频片段
|
| 94 |
final_video = concatenate_videoclips(video_clips)
|
| 95 |
final_video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
|
|
|
|
| 31 |
|
| 32 |
# 文字转视频功能
|
| 33 |
def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
|
| 34 |
+
# Use a built-in font for testing purposes
|
| 35 |
+
font_path = os.path.abspath(text_font) if os.path.exists(text_font) else "Arial"
|
| 36 |
|
| 37 |
# 计算每页可以容纳的行数和每行可以容纳的字符数
|
| 38 |
max_chars_per_line = video_width // (text_size // 2) # 字体宽度假设为字体大小的一半
|
|
|
|
| 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:
|
|
|
|
| 82 |
for j, line in enumerate(lines):
|
| 83 |
# Calculate y position based on the line index
|
| 84 |
y_position = int((video_height / 2) - ((len(lines) * (text_size + 10)) / 2) + j * (text_size + 10))
|
| 85 |
+
print(f"Drawing text: {line} at position: ({int(video_width / 2)}, {y_position})") # Debugging line
|
| 86 |
draw.text(int(video_width / 2), y_position, line)
|
| 87 |
draw(img) # Correctly draw the text onto the image
|
| 88 |
img.format = 'png'
|
|
|
|
| 90 |
img.save(filename=img_path)
|
| 91 |
text_clip = ImageClip(img_path).set_duration(audio_clip.duration).set_audio(audio_clip)
|
| 92 |
video_clips.append(text_clip)
|
| 93 |
+
|
| 94 |
# 合并所有视频片段
|
| 95 |
final_video = concatenate_videoclips(video_clips)
|
| 96 |
final_video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
|