malleswar commited on
Commit
43afe4c
·
verified ·
1 Parent(s): f846199

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py CHANGED
@@ -114,6 +114,40 @@ def process_video(video_file, language="hindi", progress=gr.Progress()):
114
 
115
  print(f"Transcription complete: {len(transcription_result['chunks'])} segments")
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  # Create SRT in output directory (persistent)
118
  progress(0.6, desc="Creating SRT...")
119
  srt_path = os.path.join(output_dir, "subtitles.srt")
 
114
 
115
  print(f"Transcription complete: {len(transcription_result['chunks'])} segments")
116
 
117
+ # Post-process: Split overly long chunks for better readability
118
+ processed_chunks = []
119
+ max_chunk_duration = 5.0 # Maximum 5 seconds per subtitle
120
+ max_words_per_chunk = 15 # Maximum words per subtitle
121
+
122
+ for chunk in transcription_result['chunks']:
123
+ start, end = chunk['timestamp']
124
+ text = chunk['text'].strip()
125
+ duration = end - start
126
+ words = text.split()
127
+
128
+ # If chunk is short enough, keep as is
129
+ if duration <= max_chunk_duration and len(words) <= max_words_per_chunk:
130
+ processed_chunks.append(chunk)
131
+ else:
132
+ # Split long chunk into smaller pieces
133
+ words_per_split = max(5, len(words) // max(2, int(duration / max_chunk_duration)))
134
+ time_per_word = duration / len(words)
135
+
136
+ for i in range(0, len(words), words_per_split):
137
+ split_words = words[i:i + words_per_split]
138
+ split_start = start + (i * time_per_word)
139
+ split_end = start + ((i + len(split_words)) * time_per_word)
140
+
141
+ processed_chunks.append({
142
+ 'timestamp': (split_start, split_end),
143
+ 'text': ' '.join(split_words)
144
+ })
145
+
146
+ print(f"After splitting long chunks: {len(processed_chunks)} segments")
147
+
148
+ # Use processed chunks instead of original
149
+ transcription_result['chunks'] = processed_chunks
150
+
151
  # Create SRT in output directory (persistent)
152
  progress(0.6, desc="Creating SRT...")
153
  srt_path = os.path.join(output_dir, "subtitles.srt")