hivecorp commited on
Commit
0a64399
·
verified ·
1 Parent(s): 595c4b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -26,21 +26,25 @@ async def text_to_speech(text, voice, rate, pitch):
26
  await communicate.save(tmp_path)
27
  return tmp_path, None
28
 
29
- # Generate SRT file with 1 line of subtitles containing 10-14 words
30
  def generate_srt(words, audio_duration, srt_path):
31
  with open(srt_path, 'w', encoding='utf-8') as srt_file:
32
- segment_duration = audio_duration / (len(words) // 14) # Average duration for each 14 words
33
  current_time = 0
34
 
35
- for i in range(0, len(words), 14): # Every 14 words for one line
36
- line = ' '.join(words[i:i + 14]) # Up to 14 words
 
37
 
 
 
 
38
  start_time = current_time
39
  end_time = start_time + segment_duration # Adjust duration for one line
40
 
41
  start_time_str = format_srt_time(start_time)
42
  end_time_str = format_srt_time(end_time)
43
- srt_file.write(f"{i // 14 + 1}\n{start_time_str} --> {end_time_str}\n{line}\n\n")
44
 
45
  current_time += segment_duration # Update current time for the next segment
46
 
 
26
  await communicate.save(tmp_path)
27
  return tmp_path, None
28
 
29
+ # Generate SRT file with 2 lines of subtitles containing 14-16 words total
30
  def generate_srt(words, audio_duration, srt_path):
31
  with open(srt_path, 'w', encoding='utf-8') as srt_file:
32
+ segment_duration = audio_duration / (len(words) // 16) # Average duration for each 16 words
33
  current_time = 0
34
 
35
+ for i in range(0, len(words), 16): # Every 16 words for two lines
36
+ line1 = ' '.join(words[i:i + 8]) # First line with up to 8 words
37
+ line2 = ' '.join(words[i + 8:i + 16]) # Second line with the next 8 words
38
 
39
+ if line2.strip() == '':
40
+ line2 = '' # Handle case where there aren't enough words for the second line
41
+
42
  start_time = current_time
43
  end_time = start_time + segment_duration # Adjust duration for one line
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 // 16 + 1}\n{start_time_str} --> {end_time_str}\n{line1}\n{line2}\n\n")
48
 
49
  current_time += segment_duration # Update current time for the next segment
50