Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -27,26 +27,26 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
27 |
return tmp_path, None
|
28 |
|
29 |
# Generate SRT file based on user preferences
|
30 |
-
def generate_srt(
|
31 |
-
|
32 |
|
33 |
# Calculate how long each segment will be displayed
|
34 |
-
segment_duration = audio_duration /
|
35 |
|
36 |
current_time = 0
|
37 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
38 |
-
for i
|
39 |
-
words
|
40 |
-
lines =
|
41 |
-
|
42 |
|
43 |
start_time = current_time
|
44 |
end_time = min(start_time + segment_duration, audio_duration) # Ensure it doesn't exceed audio duration
|
45 |
|
46 |
start_time_str = format_srt_time(start_time)
|
47 |
end_time_str = format_srt_time(end_time)
|
48 |
-
srt_file.write(f"{i + 1}\n{start_time_str} --> {end_time_str}\n")
|
49 |
-
srt_file.write(
|
50 |
|
51 |
current_time += segment_duration # Update current time for the next segment
|
52 |
|
@@ -63,7 +63,10 @@ def format_srt_time(seconds):
|
|
63 |
|
64 |
# Text to audio and SRT functionality
|
65 |
async def text_to_audio_and_srt(text, voice, rate, pitch, words_per_line, lines_per_paragraph):
|
66 |
-
|
|
|
|
|
|
|
67 |
if warning:
|
68 |
return None, None, warning
|
69 |
|
@@ -74,10 +77,10 @@ async def text_to_audio_and_srt(text, voice, rate, pitch, words_per_line, lines_
|
|
74 |
base_name = os.path.splitext(audio_path)[0]
|
75 |
srt_path = f"{base_name}_subtitle.srt"
|
76 |
|
77 |
-
# Split input text into
|
78 |
-
|
79 |
|
80 |
-
generate_srt(
|
81 |
|
82 |
return audio_path, srt_path, None
|
83 |
|
|
|
27 |
return tmp_path, None
|
28 |
|
29 |
# Generate SRT file based on user preferences
|
30 |
+
def generate_srt(words, audio_duration, srt_path, words_per_line, lines_per_paragraph):
|
31 |
+
total_words = len(words)
|
32 |
|
33 |
# Calculate how long each segment will be displayed
|
34 |
+
segment_duration = audio_duration / (total_words // words_per_line // lines_per_paragraph) # Calculate duration based on total segments
|
35 |
|
36 |
current_time = 0
|
37 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
38 |
+
for i in range(0, total_words, words_per_line):
|
39 |
+
# Gather lines based on the defined words per line
|
40 |
+
lines = words[i:i + words_per_line]
|
41 |
+
line_text = ' '.join(lines)
|
42 |
|
43 |
start_time = current_time
|
44 |
end_time = min(start_time + segment_duration, audio_duration) # Ensure it doesn't exceed audio duration
|
45 |
|
46 |
start_time_str = format_srt_time(start_time)
|
47 |
end_time_str = format_srt_time(end_time)
|
48 |
+
srt_file.write(f"{(i // words_per_line) + 1}\n{start_time_str} --> {end_time_str}\n")
|
49 |
+
srt_file.write(f"{line_text}\n\n")
|
50 |
|
51 |
current_time += segment_duration # Update current time for the next segment
|
52 |
|
|
|
63 |
|
64 |
# Text to audio and SRT functionality
|
65 |
async def text_to_audio_and_srt(text, voice, rate, pitch, words_per_line, lines_per_paragraph):
|
66 |
+
# Clean up input text: remove extra spaces and newlines
|
67 |
+
cleaned_text = ' '.join(text.split())
|
68 |
+
|
69 |
+
audio_path, warning = await text_to_speech(cleaned_text, voice, rate, pitch)
|
70 |
if warning:
|
71 |
return None, None, warning
|
72 |
|
|
|
77 |
base_name = os.path.splitext(audio_path)[0]
|
78 |
srt_path = f"{base_name}_subtitle.srt"
|
79 |
|
80 |
+
# Split input text into words
|
81 |
+
words = cleaned_text.split()
|
82 |
|
83 |
+
generate_srt(words, audio_duration, srt_path, words_per_line, lines_per_paragraph)
|
84 |
|
85 |
return audio_path, srt_path, None
|
86 |
|