Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,24 +26,23 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
| 26 |
await communicate.save(tmp_path)
|
| 27 |
return tmp_path, None
|
| 28 |
|
| 29 |
-
# Generate SRT file with
|
| 30 |
def generate_srt(words, audio_duration, srt_path):
|
| 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 |
-
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
|
| 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 //
|
| 45 |
|
| 46 |
-
current_time += segment_duration
|
| 47 |
|
| 48 |
return srt_path
|
| 49 |
|
|
|
|
| 26 |
await communicate.save(tmp_path)
|
| 27 |
return tmp_path, None
|
| 28 |
|
| 29 |
+
# Generate SRT file with 1 line of subtitles containing 10-14 words
|
| 30 |
def generate_srt(words, audio_duration, srt_path):
|
| 31 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
| 32 |
+
segment_duration = audio_duration / (len(words) // 14) # Average duration for each 14 words
|
| 33 |
current_time = 0
|
| 34 |
|
| 35 |
+
for i in range(0, len(words), 14): # Every 14 words for one line
|
| 36 |
+
line = ' '.join(words[i:i + 14]) # Up to 14 words
|
|
|
|
| 37 |
|
| 38 |
start_time = current_time
|
| 39 |
+
end_time = start_time + segment_duration # Adjust duration for one line
|
| 40 |
|
| 41 |
start_time_str = format_srt_time(start_time)
|
| 42 |
end_time_str = format_srt_time(end_time)
|
| 43 |
+
srt_file.write(f"{i // 14 + 1}\n{start_time_str} --> {end_time_str}\n{line}\n\n")
|
| 44 |
|
| 45 |
+
current_time += segment_duration # Update current time for the next segment
|
| 46 |
|
| 47 |
return srt_path
|
| 48 |
|