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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -29,25 +29,25 @@ async def text_to_speech(text, voice, rate, pitch):
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
53
 
@@ -55,7 +55,7 @@ def format_srt_time(seconds):
55
  millis = int((seconds - int(seconds)) * 1000)
56
  seconds = int(seconds)
57
  minutes = seconds // 60
58
- hours = minutes // 60
59
  minutes %= 60
60
  seconds %= 60
61
  return f"{hours:02}:{minutes:02}:{seconds:02},{millis:03}"
 
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
+ average_word_duration = (60 / (150 + speech_rate)) # Average duration of each word in seconds, based on typical speech rates
33
+ segment_duration = average_word_duration * words_per_line # Duration for each line
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 * lines_per_paragraph): # Increment by lines per paragraph
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 + segment_duration * lines_per_paragraph, 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 * lines_per_paragraph) + 1}\n{start_time_str} --> {end_time_str}\n")
48
  srt_file.write('\n'.join(lines) + '\n\n')
49
 
50
+ current_time += segment_duration * lines_per_paragraph # Update current time for the next segment
51
 
52
  return srt_path
53
 
 
55
  millis = int((seconds - int(seconds)) * 1000)
56
  seconds = int(seconds)
57
  minutes = seconds // 60
58
+ hours = seconds // 3600
59
  minutes %= 60
60
  seconds %= 60
61
  return f"{hours:02}:{minutes:02}:{seconds:02},{millis:03}"