Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -19,7 +19,7 @@ def format_time(seconds):
|
|
19 |
secs = seconds % 60
|
20 |
return f"{hrs:02}:{mins:02}:{secs:02},{millis:03}"
|
21 |
|
22 |
-
# Function to generate SRT with accurate timing per batch
|
23 |
async def generate_accurate_srt(batch_text, batch_num, start_offset):
|
24 |
audio_file = f"batch_{batch_num}_audio.wav"
|
25 |
|
@@ -38,25 +38,32 @@ async def generate_accurate_srt(batch_text, batch_num, start_offset):
|
|
38 |
start_time = start_offset
|
39 |
min_display_duration = 1.5 # Set a minimum display time of 1.5 seconds per subtitle
|
40 |
|
41 |
-
# Build SRT content with accurate timing
|
42 |
-
for i in range(0, len(words), 15):
|
43 |
segment_words = words[i:i+15]
|
44 |
-
end_time = start_time + max(segment_duration, min_display_duration)
|
|
|
|
|
|
|
|
|
|
|
45 |
srt_content += f"{i // 15 + 1 + (batch_num * 100)}\n"
|
46 |
srt_content += f"{format_time(start_time)} --> {format_time(end_time)}\n"
|
47 |
srt_content += " ".join(segment_words) + "\n\n"
|
|
|
|
|
48 |
start_time = end_time
|
49 |
|
50 |
return srt_content, audio_file, start_time # Return updated start time for cumulative tracking
|
51 |
|
52 |
-
# Batch processing function with cumulative timing and
|
53 |
async def batch_process_srt_and_audio(script_text, progress=gr.Progress()):
|
54 |
batches = [script_text[i:i+500] for i in range(0, len(script_text), 500)]
|
55 |
all_srt_content = ""
|
56 |
combined_audio = AudioSegment.empty()
|
57 |
start_offset = 0.0 # Track cumulative time offset for SRT timing
|
58 |
|
59 |
-
#
|
60 |
for batch_num, batch_text in enumerate(batches):
|
61 |
srt_content, audio_file, end_offset = await generate_accurate_srt(batch_text, batch_num, start_offset)
|
62 |
all_srt_content += srt_content
|
@@ -72,15 +79,28 @@ async def batch_process_srt_and_audio(script_text, progress=gr.Progress()):
|
|
72 |
# Update progress
|
73 |
progress((batch_num + 1) / len(batches))
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
# Generate unique names for the final files
|
76 |
unique_id = uuid.uuid4()
|
77 |
final_audio_path = f"final_audio_{unique_id}.wav"
|
78 |
final_srt_path = f"final_subtitles_{unique_id}.srt"
|
79 |
|
80 |
-
# Export combined audio and SRT with unique names
|
81 |
combined_audio.export(final_audio_path, format="wav")
|
82 |
with open(final_srt_path, "w") as srt_file:
|
83 |
-
srt_file.write(
|
84 |
|
85 |
return final_srt_path, final_audio_path
|
86 |
|
|
|
19 |
secs = seconds % 60
|
20 |
return f"{hrs:02}:{mins:02}:{secs:02},{millis:03}"
|
21 |
|
22 |
+
# Function to generate SRT with accurate timing per batch and cross-check timing
|
23 |
async def generate_accurate_srt(batch_text, batch_num, start_offset):
|
24 |
audio_file = f"batch_{batch_num}_audio.wav"
|
25 |
|
|
|
38 |
start_time = start_offset
|
39 |
min_display_duration = 1.5 # Set a minimum display time of 1.5 seconds per subtitle
|
40 |
|
41 |
+
# Build SRT content with accurate timing, ensuring no segment exceeds audio length
|
42 |
+
for i in range(0, len(words), 15):
|
43 |
segment_words = words[i:i+15]
|
44 |
+
end_time = start_time + max(segment_duration, min_display_duration)
|
45 |
+
|
46 |
+
# If end_time exceeds actual audio length of the batch, adjust it
|
47 |
+
if end_time > start_offset + actual_length:
|
48 |
+
end_time = start_offset + actual_length
|
49 |
+
|
50 |
srt_content += f"{i // 15 + 1 + (batch_num * 100)}\n"
|
51 |
srt_content += f"{format_time(start_time)} --> {format_time(end_time)}\n"
|
52 |
srt_content += " ".join(segment_words) + "\n\n"
|
53 |
+
|
54 |
+
# Update start time for next segment
|
55 |
start_time = end_time
|
56 |
|
57 |
return srt_content, audio_file, start_time # Return updated start time for cumulative tracking
|
58 |
|
59 |
+
# Batch processing function with cumulative timing, progress indicator, and final SRT validation
|
60 |
async def batch_process_srt_and_audio(script_text, progress=gr.Progress()):
|
61 |
batches = [script_text[i:i+500] for i in range(0, len(script_text), 500)]
|
62 |
all_srt_content = ""
|
63 |
combined_audio = AudioSegment.empty()
|
64 |
start_offset = 0.0 # Track cumulative time offset for SRT timing
|
65 |
|
66 |
+
# Process each batch sequentially to ensure proper timing and cumulative offset tracking
|
67 |
for batch_num, batch_text in enumerate(batches):
|
68 |
srt_content, audio_file, end_offset = await generate_accurate_srt(batch_text, batch_num, start_offset)
|
69 |
all_srt_content += srt_content
|
|
|
79 |
# Update progress
|
80 |
progress((batch_num + 1) / len(batches))
|
81 |
|
82 |
+
# Final cross-check: Adjust any subtitle that exceeds the total audio length
|
83 |
+
total_audio_length = combined_audio.duration_seconds
|
84 |
+
validated_srt_content = ""
|
85 |
+
for line in all_srt_content.strip().splitlines():
|
86 |
+
if '-->' in line:
|
87 |
+
start_str, end_str = line.split(' --> ')
|
88 |
+
start_time = sum(x * float(t) for x, t in zip([3600, 60, 1, 0.001], start_str.replace(',', ':').split(':')))
|
89 |
+
end_time = sum(x * float(t) for x, t in zip([3600, 60, 1, 0.001], end_str.replace(',', ':').split(':')))
|
90 |
+
if end_time > total_audio_length:
|
91 |
+
end_time = total_audio_length
|
92 |
+
line = f"{format_time(start_time)} --> {format_time(end_time)}"
|
93 |
+
validated_srt_content += line + "\n"
|
94 |
+
|
95 |
# Generate unique names for the final files
|
96 |
unique_id = uuid.uuid4()
|
97 |
final_audio_path = f"final_audio_{unique_id}.wav"
|
98 |
final_srt_path = f"final_subtitles_{unique_id}.srt"
|
99 |
|
100 |
+
# Export combined audio and validated SRT with unique names
|
101 |
combined_audio.export(final_audio_path, format="wav")
|
102 |
with open(final_srt_path, "w") as srt_file:
|
103 |
+
srt_file.write(validated_srt_content)
|
104 |
|
105 |
return final_srt_path, final_audio_path
|
106 |
|