Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,9 +4,6 @@ import asyncio
|
|
4 |
import tempfile
|
5 |
import os
|
6 |
from moviepy.editor import 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():
|
@@ -30,18 +27,21 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
30 |
return tmp_path, None
|
31 |
|
32 |
# SRT文件生成
|
33 |
-
def generate_srt(
|
34 |
srt_path = os.path.join(tempfile.gettempdir(), "output_subtitles.srt")
|
35 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
36 |
-
|
37 |
-
|
|
|
38 |
end_time = start_time + audio_clip.duration
|
39 |
|
40 |
# Convert to SRT format
|
41 |
start_time_str = format_srt_time(start_time)
|
42 |
end_time_str = format_srt_time(end_time)
|
43 |
-
srt_file.write(f"{i + 1}\n{start_time_str} --> {end_time_str}\n{
|
44 |
-
|
|
|
|
|
45 |
return srt_path
|
46 |
|
47 |
def format_srt_time(seconds):
|
@@ -55,42 +55,24 @@ def format_srt_time(seconds):
|
|
55 |
|
56 |
# 文字转音频和SRT功能
|
57 |
async def text_to_audio_and_srt(text, voice, rate, pitch):
|
58 |
-
#
|
59 |
-
max_chars_per_line = 60 # 适当设置每行最大字符数
|
60 |
-
max_lines_per_page = 5 # 每页最大行数
|
61 |
-
|
62 |
-
# 按页拆分文本
|
63 |
words = text.split()
|
64 |
-
lines = []
|
65 |
-
current_line = ""
|
66 |
-
pages = []
|
67 |
-
|
68 |
-
for word in words:
|
69 |
-
if len(current_line) + len(word) + 1 > max_chars_per_line:
|
70 |
-
lines.append(current_line)
|
71 |
-
current_line = word
|
72 |
-
if len(lines) == max_lines_per_page:
|
73 |
-
pages.append("\n".join(lines))
|
74 |
-
lines = []
|
75 |
-
else:
|
76 |
-
current_line = f"{current_line} {word}".strip()
|
77 |
-
|
78 |
-
lines.append(current_line)
|
79 |
-
if lines:
|
80 |
-
pages.append("\n".join(lines))
|
81 |
-
|
82 |
-
# 为每页生成独立音频
|
83 |
audio_clips = []
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
87 |
if warning:
|
88 |
return None, None, warning
|
|
|
89 |
audio_clip = AudioFileClip(audio)
|
90 |
audio_clips.append(audio_clip)
|
|
|
91 |
|
92 |
# 生成SRT文件
|
93 |
-
srt_path = generate_srt(
|
94 |
return audio, srt_path, None
|
95 |
|
96 |
# Gradio接口函数
|
|
|
4 |
import tempfile
|
5 |
import os
|
6 |
from moviepy.editor import AudioFileClip
|
|
|
|
|
|
|
7 |
|
8 |
# 获取所有可用的语音
|
9 |
async def get_voices():
|
|
|
27 |
return tmp_path, None
|
28 |
|
29 |
# SRT文件生成
|
30 |
+
def generate_srt(words, audio_clips):
|
31 |
srt_path = os.path.join(tempfile.gettempdir(), "output_subtitles.srt")
|
32 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
33 |
+
current_time = 0
|
34 |
+
for i, (word, audio_clip) in enumerate(zip(words, audio_clips)):
|
35 |
+
start_time = current_time
|
36 |
end_time = start_time + audio_clip.duration
|
37 |
|
38 |
# Convert to SRT format
|
39 |
start_time_str = format_srt_time(start_time)
|
40 |
end_time_str = format_srt_time(end_time)
|
41 |
+
srt_file.write(f"{i + 1}\n{start_time_str} --> {end_time_str}\n{word}\n\n")
|
42 |
+
|
43 |
+
current_time += audio_clip.duration # Update current time
|
44 |
+
|
45 |
return srt_path
|
46 |
|
47 |
def format_srt_time(seconds):
|
|
|
55 |
|
56 |
# 文字转音频和SRT功能
|
57 |
async def text_to_audio_and_srt(text, voice, rate, pitch):
|
58 |
+
# Split text into words
|
|
|
|
|
|
|
|
|
59 |
words = text.split()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
audio_clips = []
|
61 |
+
subtitle_chunks = []
|
62 |
+
|
63 |
+
# Generate audio for each 3-4 word chunk
|
64 |
+
for i in range(0, len(words), 3): # Change the step to 3 or 4 based on your preference
|
65 |
+
chunk = ' '.join(words[i:i + 3]) # Create a chunk of 3 words
|
66 |
+
audio, warning = await text_to_speech(chunk, voice, rate, pitch)
|
67 |
if warning:
|
68 |
return None, None, warning
|
69 |
+
|
70 |
audio_clip = AudioFileClip(audio)
|
71 |
audio_clips.append(audio_clip)
|
72 |
+
subtitle_chunks.append(chunk)
|
73 |
|
74 |
# 生成SRT文件
|
75 |
+
srt_path = generate_srt(subtitle_chunks, audio_clips)
|
76 |
return audio, srt_path, None
|
77 |
|
78 |
# Gradio接口函数
|