hivecorp commited on
Commit
0bcb2e0
·
verified ·
1 Parent(s): 6926ae7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -12
app.py CHANGED
@@ -4,6 +4,7 @@ import edge_tts
4
  import os
5
  import asyncio
6
  import uuid
 
7
 
8
  # Function to get the length of an audio file in seconds
9
  def get_audio_length(audio_file):
@@ -19,6 +20,32 @@ def format_time(seconds):
19
  secs = seconds % 60
20
  return f"{hrs:02}:{mins:02}:{secs:02},{millis:03}"
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # Function to generate SRT with accurate timing per batch and cross-check timing
23
  async def generate_accurate_srt(batch_text, batch_num, start_offset):
24
  audio_file = f"batch_{batch_num}_audio.wav"
@@ -30,26 +57,23 @@ async def generate_accurate_srt(batch_text, batch_num, start_offset):
30
  # Get the actual length of the audio file
31
  actual_length = get_audio_length(audio_file)
32
 
33
- # Initialize SRT content
34
- srt_content = ""
35
- words = batch_text.split()
36
- num_segments = max(1, len(words) // 15) # Group words into segments of ~15 words each
37
- segment_duration = actual_length / num_segments # Duration for each segment
38
  start_time = start_offset
39
- min_display_duration = 1.5 # Set a minimum display time of 1.5 seconds per subtitle
40
 
41
- # Build SRT content with accurate timing, ensuring no segment exceeds audio length
42
- for i in range(0, len(words), 15):
43
- segment_words = words[i:i+15]
44
- end_time = start_time + max(segment_duration, min_display_duration)
45
 
46
  # If end_time exceeds actual audio length of the batch, adjust it
47
  if end_time > start_offset + actual_length:
48
  end_time = start_offset + actual_length
49
 
50
- srt_content += f"{i // 15 + 1 + (batch_num * 100)}\n"
51
  srt_content += f"{format_time(start_time)} --> {format_time(end_time)}\n"
52
- srt_content += " ".join(segment_words) + "\n\n"
53
 
54
  # Update start time for next segment
55
  start_time = end_time
 
4
  import os
5
  import asyncio
6
  import uuid
7
+ import re
8
 
9
  # Function to get the length of an audio file in seconds
10
  def get_audio_length(audio_file):
 
20
  secs = seconds % 60
21
  return f"{hrs:02}:{mins:02}:{secs:02},{millis:03}"
22
 
23
+ # Function to split text into segments by punctuation or limit to 7-8 words
24
+ def split_text_into_segments(text):
25
+ segments = []
26
+ # Split by punctuation (., !, ?)
27
+ raw_segments = re.split(r'([.!?])', text)
28
+ for i in range(0, len(raw_segments) - 1, 2):
29
+ # Combine segment with following punctuation
30
+ sentence = raw_segments[i].strip() + raw_segments[i + 1]
31
+ words = sentence.split()
32
+
33
+ # If segment is longer than 8 words, split into 7-8 word chunks
34
+ if len(words) > 8:
35
+ for j in range(0, len(words), 8):
36
+ segments.append(" ".join(words[j:j+8]))
37
+ else:
38
+ segments.append(sentence.strip())
39
+
40
+ # Handle remaining text after the last punctuation
41
+ if len(raw_segments) % 2 == 1:
42
+ remaining_text = raw_segments[-1].strip()
43
+ words = remaining_text.split()
44
+ for j in range(0, len(words), 8):
45
+ segments.append(" ".join(words[j:j+8]))
46
+
47
+ return segments
48
+
49
  # Function to generate SRT with accurate timing per batch and cross-check timing
50
  async def generate_accurate_srt(batch_text, batch_num, start_offset):
51
  audio_file = f"batch_{batch_num}_audio.wav"
 
57
  # Get the actual length of the audio file
58
  actual_length = get_audio_length(audio_file)
59
 
60
+ # Split the text into segments based on punctuation and word count
61
+ segments = split_text_into_segments(batch_text)
62
+ segment_duration = actual_length / len(segments) # Duration per segment
 
 
63
  start_time = start_offset
 
64
 
65
+ # Initialize SRT content
66
+ srt_content = ""
67
+ for index, segment in enumerate(segments):
68
+ end_time = start_time + segment_duration
69
 
70
  # If end_time exceeds actual audio length of the batch, adjust it
71
  if end_time > start_offset + actual_length:
72
  end_time = start_offset + actual_length
73
 
74
+ srt_content += f"{index + 1 + (batch_num * 100)}\n"
75
  srt_content += f"{format_time(start_time)} --> {format_time(end_time)}\n"
76
+ srt_content += segment + "\n\n"
77
 
78
  # Update start time for next segment
79
  start_time = end_time