Spaces:
Running
Running
Update app.py
Browse filesdivison by zero update function
app.py
CHANGED
@@ -29,27 +29,30 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
29 |
# Generate SRT file with specified lines of subtitles
|
30 |
def generate_srt(words, audio_duration, srt_path, num_lines):
|
31 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
34 |
|
35 |
-
|
|
|
36 |
lines = []
|
37 |
for j in range(num_lines):
|
38 |
-
line = ' '.join(words[i + j * 5:i + (j + 1) * 5])
|
39 |
if line:
|
40 |
lines.append(line)
|
41 |
-
|
42 |
-
start_time = current_time
|
43 |
-
end_time = start_time + segment_duration # Adjust duration for the current segment
|
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 // (5 * num_lines) + 1}\n{start_time_str} --> {end_time_str}\n" + "\n".join(lines) + "\n\n")
|
48 |
-
|
49 |
-
current_time += segment_duration # Update current time for the next segment
|
50 |
|
51 |
return srt_path
|
52 |
|
|
|
53 |
def format_srt_time(seconds):
|
54 |
millis = int((seconds - int(seconds)) * 1000)
|
55 |
seconds = int(seconds)
|
|
|
29 |
# Generate SRT file with specified lines of subtitles
|
30 |
def generate_srt(words, audio_duration, srt_path, num_lines):
|
31 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
32 |
+
divisor = len(words) // (5 * num_lines)
|
33 |
+
if divisor == 0:
|
34 |
+
segment_duration = audio_duration # Use full duration as fallback
|
35 |
+
else:
|
36 |
+
segment_duration = audio_duration / divisor # Calculate duration per segment
|
37 |
|
38 |
+
current_time = 0
|
39 |
+
for i in range(0, len(words), 5 * num_lines):
|
40 |
lines = []
|
41 |
for j in range(num_lines):
|
42 |
+
line = ' '.join(words[i + j * 5:i + (j + 1) * 5])
|
43 |
if line:
|
44 |
lines.append(line)
|
|
|
|
|
|
|
45 |
|
46 |
+
start_time = current_time
|
47 |
+
end_time = start_time + segment_duration
|
48 |
start_time_str = format_srt_time(start_time)
|
49 |
end_time_str = format_srt_time(end_time)
|
50 |
srt_file.write(f"{i // (5 * num_lines) + 1}\n{start_time_str} --> {end_time_str}\n" + "\n".join(lines) + "\n\n")
|
51 |
+
current_time += segment_duration
|
|
|
52 |
|
53 |
return srt_path
|
54 |
|
55 |
+
|
56 |
def format_srt_time(seconds):
|
57 |
millis = int((seconds - int(seconds)) * 1000)
|
58 |
seconds = int(seconds)
|