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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -27,27 +27,28 @@ async def text_to_speech(text, voice, rate, pitch):
27
  return tmp_path, None
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
- 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
 
@@ -72,8 +73,11 @@ async def text_to_audio_and_srt(text, voice, rate, pitch, words_per_line, lines_
72
  # Generate SRT file based on the entire text
73
  base_name = os.path.splitext(audio_path)[0]
74
  srt_path = f"{base_name}_subtitle.srt"
75
- words = text.split()
76
- generate_srt(words, audio_duration, srt_path, words_per_line, lines_per_paragraph, rate)
 
 
 
77
 
78
  return audio_path, srt_path, None
79
 
 
27
  return tmp_path, None
28
 
29
  # Generate SRT file based on user preferences
30
+ def generate_srt(paragraphs, audio_duration, srt_path, words_per_line, lines_per_paragraph):
31
+ total_paragraphs = len(paragraphs)
32
+
33
+ # Calculate how long each segment will be displayed
34
+ segment_duration = audio_duration / total_paragraphs # Total audio duration divided by total paragraphs
35
+
36
  current_time = 0
 
37
  with open(srt_path, 'w', encoding='utf-8') as srt_file:
38
+ for i, paragraph in enumerate(paragraphs):
39
+ words = paragraph.split()
40
+ lines = [words[j:j + words_per_line] for j in range(0, len(words), words_per_line)]
41
  lines = [' '.join(line) for line in lines]
42
 
43
  start_time = current_time
44
+ end_time = min(start_time + segment_duration, audio_duration) # Ensure it doesn't exceed audio duration
45
 
46
  start_time_str = format_srt_time(start_time)
47
  end_time_str = format_srt_time(end_time)
48
+ srt_file.write(f"{i + 1}\n{start_time_str} --> {end_time_str}\n")
49
  srt_file.write('\n'.join(lines) + '\n\n')
50
 
51
+ current_time += segment_duration # Update current time for the next segment
52
 
53
  return srt_path
54
 
 
73
  # Generate SRT file based on the entire text
74
  base_name = os.path.splitext(audio_path)[0]
75
  srt_path = f"{base_name}_subtitle.srt"
76
+
77
+ # Split input text into paragraphs based on larger gaps (two consecutive newlines)
78
+ paragraphs = [p.strip() for p in text.split('\n\n') if p.strip()]
79
+
80
+ generate_srt(paragraphs, audio_duration, srt_path, words_per_line, lines_per_paragraph)
81
 
82
  return audio_path, srt_path, None
83