Update srt_utils.py
Browse files- srt_utils.py +49 -1
srt_utils.py
CHANGED
|
@@ -216,9 +216,57 @@ def apply_netflix_style_filter(srt_content):
|
|
| 216 |
if current_group:
|
| 217 |
grouped_events.append(current_group)
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
# Generate Output SRT
|
| 220 |
output_srt = ""
|
| 221 |
-
for i, group in enumerate(
|
| 222 |
if not group: continue
|
| 223 |
|
| 224 |
start_time = seconds_to_srt_time(group[0]['start'])
|
|
|
|
| 216 |
if current_group:
|
| 217 |
grouped_events.append(current_group)
|
| 218 |
|
| 219 |
+
# --- POST-PROCESSING: Merge Orphans ---
|
| 220 |
+
# Attempt to merge single/short words into previous block if they are close
|
| 221 |
+
|
| 222 |
+
merged_events = []
|
| 223 |
+
if grouped_events:
|
| 224 |
+
merged_events.append(grouped_events[0])
|
| 225 |
+
|
| 226 |
+
for i in range(1, len(grouped_events)):
|
| 227 |
+
prev_group = merged_events[-1]
|
| 228 |
+
curr_group = grouped_events[i]
|
| 229 |
+
|
| 230 |
+
# Check if current group is "orphan-candidate"
|
| 231 |
+
# Criteria: 1 word OR very short text (< 10 chars)
|
| 232 |
+
curr_text = get_group_text(curr_group)
|
| 233 |
+
is_orphan = len(curr_group) == 1 or len(curr_text) < 10
|
| 234 |
+
|
| 235 |
+
if is_orphan:
|
| 236 |
+
# Check gap
|
| 237 |
+
gap = curr_group[0]['start'] - prev_group[-1]['end']
|
| 238 |
+
|
| 239 |
+
# If gap is small enough (user said "very close")
|
| 240 |
+
# Let's say < 1.0s is reasonably close for a "continuation"
|
| 241 |
+
if gap < 1.0:
|
| 242 |
+
# Check if merging breaks limits
|
| 243 |
+
# We need to simulate the merge
|
| 244 |
+
combined_text = get_group_text(prev_group + curr_group)
|
| 245 |
+
formatted = format_text_lines(combined_text, MAX_CHARS_PER_LINE)
|
| 246 |
+
lines = formatted.split('\n')
|
| 247 |
+
|
| 248 |
+
# Valid if max 2 lines and lines aren't too long (format_text_lines handles length balancing,
|
| 249 |
+
# but we check if it forced 3 lines or something weird, though helper only does max 2 usually)
|
| 250 |
+
# format_text_lines guarantees max 2 lines usually, unless it fails fallback.
|
| 251 |
+
|
| 252 |
+
# Check char limit on lines just to be safe
|
| 253 |
+
valid_merge = True
|
| 254 |
+
for line in lines:
|
| 255 |
+
if len(line) > MAX_CHARS_PER_LINE + 5: # Tolerance
|
| 256 |
+
valid_merge = False
|
| 257 |
+
break
|
| 258 |
+
|
| 259 |
+
if valid_merge:
|
| 260 |
+
# MERGE!
|
| 261 |
+
prev_group.extend(curr_group)
|
| 262 |
+
continue
|
| 263 |
+
|
| 264 |
+
# If not merged, append
|
| 265 |
+
merged_events.append(curr_group)
|
| 266 |
+
|
| 267 |
# Generate Output SRT
|
| 268 |
output_srt = ""
|
| 269 |
+
for i, group in enumerate(merged_events, 1):
|
| 270 |
if not group: continue
|
| 271 |
|
| 272 |
start_time = seconds_to_srt_time(group[0]['start'])
|