Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,27 +27,28 @@ 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 |
-
|
|
|
|
|
|
|
| 34 |
current_time = 0
|
| 35 |
-
|
| 36 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
| 37 |
-
for i in
|
| 38 |
-
|
| 39 |
-
lines = [
|
| 40 |
lines = [' '.join(line) for line in lines]
|
| 41 |
|
| 42 |
start_time = current_time
|
| 43 |
-
end_time = min(start_time + segment_duration
|
| 44 |
|
| 45 |
start_time_str = format_srt_time(start_time)
|
| 46 |
end_time_str = format_srt_time(end_time)
|
| 47 |
-
srt_file.write(f"{i
|
| 48 |
srt_file.write('\n'.join(lines) + '\n\n')
|
| 49 |
|
| 50 |
-
current_time += segment_duration
|
| 51 |
|
| 52 |
return srt_path
|
| 53 |
|
|
@@ -72,8 +73,11 @@ async def text_to_audio_and_srt(text, voice, rate, pitch, words_per_line, lines_
|
|
| 72 |
# Generate SRT file based on the entire text
|
| 73 |
base_name = os.path.splitext(audio_path)[0]
|
| 74 |
srt_path = f"{base_name}_subtitle.srt"
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
return audio_path, srt_path, None
|
| 79 |
|
|
|
|
| 27 |
return tmp_path, None
|
| 28 |
|
| 29 |
# Generate SRT file based on user preferences
|
| 30 |
+
def generate_srt(paragraphs, audio_duration, srt_path, words_per_line, lines_per_paragraph):
|
| 31 |
+
total_paragraphs = len(paragraphs)
|
| 32 |
+
|
| 33 |
+
# Calculate how long each segment will be displayed
|
| 34 |
+
segment_duration = audio_duration / total_paragraphs # Total audio duration divided by total paragraphs
|
| 35 |
+
|
| 36 |
current_time = 0
|
|
|
|
| 37 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
| 38 |
+
for i, paragraph in enumerate(paragraphs):
|
| 39 |
+
words = paragraph.split()
|
| 40 |
+
lines = [words[j:j + words_per_line] for j in range(0, len(words), words_per_line)]
|
| 41 |
lines = [' '.join(line) for line in 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 + 1}\n{start_time_str} --> {end_time_str}\n")
|
| 49 |
srt_file.write('\n'.join(lines) + '\n\n')
|
| 50 |
|
| 51 |
+
current_time += segment_duration # Update current time for the next segment
|
| 52 |
|
| 53 |
return srt_path
|
| 54 |
|
|
|
|
| 73 |
# Generate SRT file based on the entire text
|
| 74 |
base_name = os.path.splitext(audio_path)[0]
|
| 75 |
srt_path = f"{base_name}_subtitle.srt"
|
| 76 |
+
|
| 77 |
+
# Split input text into paragraphs based on larger gaps (two consecutive newlines)
|
| 78 |
+
paragraphs = [p.strip() for p in text.split('\n\n') if p.strip()]
|
| 79 |
+
|
| 80 |
+
generate_srt(paragraphs, audio_duration, srt_path, words_per_line, lines_per_paragraph)
|
| 81 |
|
| 82 |
return audio_path, srt_path, None
|
| 83 |
|