Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -26,19 +26,23 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
26 |
await communicate.save(tmp_path)
|
27 |
return tmp_path, None
|
28 |
|
29 |
-
# 生成SRT
|
30 |
def generate_srt(words, audio_duration, srt_path, fps=24):
|
31 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
32 |
-
segment_duration = audio_duration / len(words) # Average duration
|
33 |
current_time = 0
|
34 |
|
35 |
-
for i
|
|
|
|
|
|
|
|
|
36 |
start_time = current_time
|
37 |
end_time = start_time + segment_duration
|
38 |
|
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{
|
42 |
|
43 |
current_time += segment_duration # Update current time
|
44 |
|
|
|
26 |
await communicate.save(tmp_path)
|
27 |
return tmp_path, None
|
28 |
|
29 |
+
# 生成SRT文件,支持两行字幕
|
30 |
def generate_srt(words, audio_duration, srt_path, fps=24):
|
31 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
32 |
+
segment_duration = audio_duration / (len(words) // 2) # Average duration for each two words
|
33 |
current_time = 0
|
34 |
|
35 |
+
for i in range(0, len(words), 2):
|
36 |
+
# Get two words for each subtitle entry
|
37 |
+
line1 = words[i]
|
38 |
+
line2 = words[i + 1] if (i + 1) < len(words) else ""
|
39 |
+
|
40 |
start_time = current_time
|
41 |
end_time = start_time + segment_duration
|
42 |
|
43 |
start_time_str = format_srt_time(start_time)
|
44 |
end_time_str = format_srt_time(end_time)
|
45 |
+
srt_file.write(f"{i // 2 + 1}\n{start_time_str} --> {end_time_str}\n{line1} {line2}\n\n")
|
46 |
|
47 |
current_time += segment_duration # Update current time
|
48 |
|