Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -96,11 +96,11 @@ async def process_segment(segment: str, idx: int, voice: str, rate: str, pitch:
|
|
| 96 |
await tts.save(audio_file)
|
| 97 |
|
| 98 |
segment_audio = AudioSegment.from_file(audio_file)
|
|
|
|
|
|
|
| 99 |
segment_duration = len(segment_audio)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
return srt_content, segment_audio, segment_duration
|
| 104 |
finally:
|
| 105 |
if os.path.exists(audio_file):
|
| 106 |
os.remove(audio_file)
|
|
@@ -118,41 +118,41 @@ async def process_chunk_parallel(chunks: List[str], start_idx: int, voice: str,
|
|
| 118 |
srt_content = ""
|
| 119 |
current_time = 0
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
combined_audio += audio_part
|
| 128 |
-
|
|
|
|
| 129 |
|
| 130 |
return srt_content, combined_audio
|
| 131 |
|
| 132 |
async def generate_accurate_srt(text, voice, rate, pitch, words_per_line, lines_per_segment):
|
| 133 |
segments = smart_text_split(text, words_per_line, lines_per_segment)
|
| 134 |
|
| 135 |
-
#
|
| 136 |
-
chunk_size =
|
| 137 |
chunks = [segments[i:i + chunk_size] for i in range(0, len(segments), chunk_size)]
|
| 138 |
|
| 139 |
final_srt = ""
|
| 140 |
final_audio = AudioSegment.empty()
|
| 141 |
|
| 142 |
-
# Process chunks in
|
| 143 |
-
|
| 144 |
-
for
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
# Gather results
|
| 150 |
-
chunk_results = await asyncio.gather(*chunk_tasks)
|
| 151 |
-
|
| 152 |
-
# Combine results
|
| 153 |
-
for srt_content, audio_content in chunk_results:
|
| 154 |
final_srt += srt_content
|
| 155 |
final_audio += audio_content
|
|
|
|
| 156 |
|
| 157 |
# Export final files
|
| 158 |
unique_id = uuid.uuid4()
|
|
|
|
| 96 |
await tts.save(audio_file)
|
| 97 |
|
| 98 |
segment_audio = AudioSegment.from_file(audio_file)
|
| 99 |
+
# Add small silence at the end of each segment
|
| 100 |
+
segment_audio = segment_audio + AudioSegment.silent(duration=250)
|
| 101 |
segment_duration = len(segment_audio)
|
| 102 |
|
| 103 |
+
return "", segment_audio, segment_duration
|
|
|
|
|
|
|
| 104 |
finally:
|
| 105 |
if os.path.exists(audio_file):
|
| 106 |
os.remove(audio_file)
|
|
|
|
| 118 |
srt_content = ""
|
| 119 |
current_time = 0
|
| 120 |
|
| 121 |
+
for idx, (_, audio_part, duration) in enumerate(results, start_idx):
|
| 122 |
+
# Calculate start and end times
|
| 123 |
+
start_time = current_time
|
| 124 |
+
end_time = start_time + duration
|
| 125 |
+
|
| 126 |
+
# Format SRT entry
|
| 127 |
+
srt_content += f"{idx}\n"
|
| 128 |
+
srt_content += f"{format_time_ms(start_time)} --> {format_time_ms(end_time)}\n"
|
| 129 |
+
srt_content += chunks[idx - start_idx] + "\n\n"
|
| 130 |
|
| 131 |
combined_audio += audio_part
|
| 132 |
+
# Add the duration plus a small gap
|
| 133 |
+
current_time = end_time + 100 # 100ms gap between segments
|
| 134 |
|
| 135 |
return srt_content, combined_audio
|
| 136 |
|
| 137 |
async def generate_accurate_srt(text, voice, rate, pitch, words_per_line, lines_per_segment):
|
| 138 |
segments = smart_text_split(text, words_per_line, lines_per_segment)
|
| 139 |
|
| 140 |
+
# Process smaller chunks for better timing control
|
| 141 |
+
chunk_size = 5 # Reduced from 10 to 5 for better timing control
|
| 142 |
chunks = [segments[i:i + chunk_size] for i in range(0, len(segments), chunk_size)]
|
| 143 |
|
| 144 |
final_srt = ""
|
| 145 |
final_audio = AudioSegment.empty()
|
| 146 |
|
| 147 |
+
# Process chunks in sequence for better timing accuracy
|
| 148 |
+
current_index = 1
|
| 149 |
+
for chunk in chunks:
|
| 150 |
+
srt_content, audio_content = await process_chunk_parallel(
|
| 151 |
+
chunk, current_index, voice, rate, pitch
|
| 152 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
final_srt += srt_content
|
| 154 |
final_audio += audio_content
|
| 155 |
+
current_index += len(chunk)
|
| 156 |
|
| 157 |
# Export final files
|
| 158 |
unique_id = uuid.uuid4()
|