Skip to content

Commit 1079a33

Browse files
authored
Merge pull request #275 from happyarts/fix/negative-subtitle-duration
Fix negative subtitle durations in adjust_for_pause (#253)
2 parents 80f507f + 640701e commit 1079a33

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

noScribe.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,25 +2789,35 @@ def save_doc():
27892789
speech_chunks = get_speech_timestamps(audio, vad_parameters)
27902790

27912791
def adjust_for_pause(segment):
2792-
"""Adjusts start and end of segment if it falls into a pause
2792+
"""Adjusts start and end of segment if it falls into a pause
27932793
identified by the VAD"""
27942794
pause_extend = 0.2 # extend the pauses by 200ms to make the detection more robust
2795-
2795+
2796+
original_start = segment.start
2797+
original_end = segment.end
2798+
27962799
# iterate through the pauses and adjust segment boundaries accordingly
27972800
for i in range(0, len(speech_chunks)):
27982801
pause_start = (speech_chunks[i]['end'] / sampling_rate) - pause_extend
2799-
if i == (len(speech_chunks) - 1):
2802+
if i == (len(speech_chunks) - 1):
28002803
pause_end = duration + pause_extend # last segment, pause till the end
28012804
else:
28022805
pause_end = (speech_chunks[i+1]['start'] / sampling_rate) + pause_extend
2803-
2806+
28042807
if pause_start > segment.end:
28052808
break # we moved beyond the segment, stop going further
28062809
if segment.start > pause_start and segment.start < pause_end:
28072810
segment.start = pause_end - pause_extend
28082811
if segment.end > pause_start and segment.end < pause_end:
28092812
segment.end = pause_start + pause_extend
2810-
2813+
2814+
# Safety check: if pause adjustments caused start >= end
2815+
# (e.g. short segment fully inside a pause), revert to
2816+
# original boundaries to avoid negative durations (#253)
2817+
if segment.start >= segment.end:
2818+
segment.start = original_start
2819+
segment.end = original_end
2820+
28112821
return segment
28122822

28132823
# Run Faster-Whisper in a spawned subprocess and stream segments

0 commit comments

Comments
 (0)