hivecorp commited on
Commit
595c4b7
·
verified ·
1 Parent(s): 7bc6887

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -8
app.py CHANGED
@@ -26,24 +26,23 @@ 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 2 lines of subtitles, each having up to 5 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) // 5) # Average duration for each 5 words
33
  current_time = 0
34
 
35
- for i in range(0, len(words), 10): # Every 10 words for two lines
36
- line1 = ' '.join(words[i:i + 5]) # First 5 words
37
- line2 = ' '.join(words[i + 5:i + 10]) if (i + 5 < len(words)) else "" # Next 5 words
38
 
39
  start_time = current_time
40
- end_time = start_time + segment_duration * 2 # Adjust duration for two lines
41
 
42
  start_time_str = format_srt_time(start_time)
43
  end_time_str = format_srt_time(end_time)
44
- srt_file.write(f"{i // 10 + 1}\n{start_time_str} --> {end_time_str}\n{line1} {line2}\n\n")
45
 
46
- current_time += segment_duration * 2 # Update current time for the next segment
47
 
48
  return srt_path
49
 
 
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
 
47
  return srt_path
48