hivecorp commited on
Commit
ff60c7c
·
verified ·
1 Parent(s): fbe2197

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -28,24 +28,25 @@ async def text_to_speech(text, voice, rate, pitch):
28
 
29
  # Generate SRT file based on user preferences
30
  def generate_srt(words, audio_duration, srt_path, words_per_line, lines_per_paragraph, speech_rate):
31
- segment_duration = audio_duration / (len(words) / lines_per_paragraph) # Average duration for each segment
 
32
  adjusted_duration = segment_duration * (60 / (100 + speech_rate)) # Adjust duration based on speech rate
33
  current_time = 0
34
 
35
  with open(srt_path, 'w', encoding='utf-8') as srt_file:
36
- for i in range(0, len(words), words_per_line * lines_per_paragraph): # Every segment according to specified preferences
37
  segment_words = words[i:i + (words_per_line * lines_per_paragraph)]
38
  lines = [segment_words[j:j + words_per_line] for j in range(0, len(segment_words), words_per_line)]
39
  lines = [' '.join(line) for line in lines]
40
-
41
  start_time = current_time
42
- end_time = start_time + adjusted_duration
43
 
44
  start_time_str = format_srt_time(start_time)
45
  end_time_str = format_srt_time(end_time)
46
- srt_file.write(f"{i // (words_per_line * lines_per_paragraph) + 1}\n{start_time_str} --> {end_time_str}\n")
47
  srt_file.write('\n'.join(lines) + '\n\n')
48
-
49
  current_time += adjusted_duration # Update current time for the next segment
50
 
51
  return srt_path
 
28
 
29
  # Generate SRT file based on user preferences
30
  def generate_srt(words, audio_duration, srt_path, words_per_line, lines_per_paragraph, speech_rate):
31
+ total_words = len(words)
32
+ segment_duration = audio_duration / (total_words / words_per_line) # Average duration for each segment
33
  adjusted_duration = segment_duration * (60 / (100 + speech_rate)) # Adjust duration based on speech rate
34
  current_time = 0
35
 
36
  with open(srt_path, 'w', encoding='utf-8') as srt_file:
37
+ for i in range(0, total_words, words_per_line): # Increment by words per line
38
  segment_words = words[i:i + (words_per_line * lines_per_paragraph)]
39
  lines = [segment_words[j:j + words_per_line] for j in range(0, len(segment_words), words_per_line)]
40
  lines = [' '.join(line) for line in lines]
41
+
42
  start_time = current_time
43
+ end_time = min(start_time + adjusted_duration, audio_duration) # Ensure it doesn't exceed audio duration
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 // words_per_line + 1}\n{start_time_str} --> {end_time_str}\n")
48
  srt_file.write('\n'.join(lines) + '\n\n')
49
+
50
  current_time += adjusted_duration # Update current time for the next segment
51
 
52
  return srt_path