Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,9 @@ import os
|
|
5 |
import asyncio
|
6 |
import uuid
|
7 |
import re
|
|
|
|
|
|
|
8 |
|
9 |
def get_audio_length(audio_file):
|
10 |
audio = AudioSegment.from_file(audio_file)
|
@@ -60,43 +63,79 @@ def smart_text_split(text, words_per_line, lines_per_segment):
|
|
60 |
|
61 |
return segments
|
62 |
|
63 |
-
async def
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
combined_audio = AudioSegment.empty()
|
68 |
-
current_time = 0
|
69 |
-
|
70 |
-
for idx, segment in enumerate(segments, 1):
|
71 |
-
# Generate audio for this segment
|
72 |
-
audio_file = f"temp_segment_{idx}.wav"
|
73 |
tts = edge_tts.Communicate(segment, voice, rate=rate, pitch=pitch)
|
74 |
await tts.save(audio_file)
|
75 |
|
76 |
-
# Get segment duration
|
77 |
segment_audio = AudioSegment.from_file(audio_file)
|
78 |
segment_duration = len(segment_audio)
|
79 |
|
80 |
-
|
81 |
-
srt_content += f"{idx}\n"
|
82 |
-
srt_content += f"{format_time_ms(current_time)} --> {format_time_ms(current_time + segment_duration)}\n"
|
83 |
-
srt_content += segment + "\n\n"
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
# Export final files
|
93 |
unique_id = uuid.uuid4()
|
94 |
audio_path = f"final_audio_{unique_id}.mp3"
|
95 |
srt_path = f"final_subtitles_{unique_id}.srt"
|
96 |
|
97 |
-
|
98 |
with open(srt_path, "w", encoding='utf-8') as f:
|
99 |
-
f.write(
|
100 |
|
101 |
return srt_path, audio_path
|
102 |
|
|
|
5 |
import asyncio
|
6 |
import uuid
|
7 |
import re
|
8 |
+
from concurrent.futures import ThreadPoolExecutor
|
9 |
+
from typing import List, Tuple
|
10 |
+
import math
|
11 |
|
12 |
def get_audio_length(audio_file):
|
13 |
audio = AudioSegment.from_file(audio_file)
|
|
|
63 |
|
64 |
return segments
|
65 |
|
66 |
+
async def process_segment(segment: str, idx: int, voice: str, rate: str, pitch: str) -> Tuple[str, AudioSegment, int]:
|
67 |
+
"""Process a single segment concurrently"""
|
68 |
+
audio_file = f"temp_segment_{idx}_{uuid.uuid4()}.wav"
|
69 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
tts = edge_tts.Communicate(segment, voice, rate=rate, pitch=pitch)
|
71 |
await tts.save(audio_file)
|
72 |
|
|
|
73 |
segment_audio = AudioSegment.from_file(audio_file)
|
74 |
segment_duration = len(segment_audio)
|
75 |
|
76 |
+
srt_content = f"{idx}\n"
|
|
|
|
|
|
|
77 |
|
78 |
+
return srt_content, segment_audio, segment_duration
|
79 |
+
finally:
|
80 |
+
if os.path.exists(audio_file):
|
81 |
+
os.remove(audio_file)
|
82 |
+
|
83 |
+
async def process_chunk_parallel(chunks: List[str], start_idx: int, voice: str, rate: str, pitch: str) -> Tuple[str, AudioSegment]:
|
84 |
+
"""Process a chunk of segments in parallel"""
|
85 |
+
tasks = [
|
86 |
+
process_segment(segment, i + start_idx, voice, rate, pitch)
|
87 |
+
for i, segment in enumerate(chunks, 1)
|
88 |
+
]
|
89 |
+
|
90 |
+
results = await asyncio.gather(*tasks)
|
91 |
+
|
92 |
+
combined_audio = AudioSegment.empty()
|
93 |
+
srt_content = ""
|
94 |
+
current_time = 0
|
95 |
+
|
96 |
+
for srt_part, audio_part, duration in results:
|
97 |
+
srt_content += srt_part
|
98 |
+
srt_content += f"{format_time_ms(current_time)} --> {format_time_ms(current_time + duration)}\n"
|
99 |
+
srt_content += chunks[len(combined_audio.get_dc_offset())] + "\n\n"
|
100 |
|
101 |
+
combined_audio += audio_part
|
102 |
+
current_time += duration
|
103 |
+
|
104 |
+
return srt_content, combined_audio
|
105 |
+
|
106 |
+
async def generate_accurate_srt(text, voice, rate, pitch, words_per_line, lines_per_segment):
|
107 |
+
segments = smart_text_split(text, words_per_line, lines_per_segment)
|
108 |
+
|
109 |
+
# Split segments into chunks for parallel processing
|
110 |
+
chunk_size = 10 # Process 10 segments at a time
|
111 |
+
chunks = [segments[i:i + chunk_size] for i in range(0, len(segments), chunk_size)]
|
112 |
+
|
113 |
+
final_srt = ""
|
114 |
+
final_audio = AudioSegment.empty()
|
115 |
+
|
116 |
+
# Process chunks in parallel
|
117 |
+
chunk_tasks = []
|
118 |
+
for i, chunk in enumerate(chunks):
|
119 |
+
start_idx = i * chunk_size + 1
|
120 |
+
task = process_chunk_parallel(chunk, start_idx, voice, rate, pitch)
|
121 |
+
chunk_tasks.append(task)
|
122 |
+
|
123 |
+
# Gather results
|
124 |
+
chunk_results = await asyncio.gather(*chunk_tasks)
|
125 |
+
|
126 |
+
# Combine results
|
127 |
+
for srt_content, audio_content in chunk_results:
|
128 |
+
final_srt += srt_content
|
129 |
+
final_audio += audio_content
|
130 |
|
131 |
# Export final files
|
132 |
unique_id = uuid.uuid4()
|
133 |
audio_path = f"final_audio_{unique_id}.mp3"
|
134 |
srt_path = f"final_subtitles_{unique_id}.srt"
|
135 |
|
136 |
+
final_audio.export(audio_path, format="mp3", bitrate="320k")
|
137 |
with open(srt_path, "w", encoding='utf-8') as f:
|
138 |
+
f.write(final_srt)
|
139 |
|
140 |
return srt_path, audio_path
|
141 |
|