Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -26,25 +26,24 @@ 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) //
|
33 |
current_time = 0
|
34 |
|
35 |
-
for i in range(0, len(words),
|
36 |
-
|
37 |
-
|
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 //
|
46 |
|
47 |
-
current_time += segment_duration # Update current time
|
48 |
|
49 |
return srt_path
|
50 |
|
|
|
26 |
await communicate.save(tmp_path)
|
27 |
return tmp_path, None
|
28 |
|
29 |
+
# 生成SRT文件,支持每行5个单词和2行字幕
|
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) // 5) # Average duration for each 5 words
|
33 |
current_time = 0
|
34 |
|
35 |
+
for i in range(0, len(words), 10): # Every 10 words for two lines
|
36 |
+
line1 = ' '.join(words[i:i + 5]) # First 5 words
|
37 |
+
line2 = ' '.join(words[i + 5:i + 10]) if (i + 5 < len(words)) else "" # Next 5 words
|
|
|
38 |
|
39 |
start_time = current_time
|
40 |
+
end_time = start_time + segment_duration * 2 # Adjust duration for two lines
|
41 |
|
42 |
start_time_str = format_srt_time(start_time)
|
43 |
end_time_str = format_srt_time(end_time)
|
44 |
+
srt_file.write(f"{i // 10 + 1}\n{start_time_str} --> {end_time_str}\n{line1} {line2}\n\n")
|
45 |
|
46 |
+
current_time += segment_duration * 2 # Update current time for the next segment
|
47 |
|
48 |
return srt_path
|
49 |
|