-
Notifications
You must be signed in to change notification settings - Fork 3
/
combine_subtitles.py
34 lines (26 loc) · 964 Bytes
/
combine_subtitles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pysrt
from pysrt import SubRipFile
def combine_subtitles(subtitles):
combined_subtitles = []
current_subtitle = None
for subtitle in subtitles:
if current_subtitle is None:
current_subtitle = subtitle
else:
time_gap = subtitle.start - current_subtitle.end
if time_gap < 200: # Adjust the threshold as per your requirement
current_subtitle.text += ' ' + subtitle.text
current_subtitle.end = subtitle.end
else:
combined_subtitles.append(current_subtitle)
current_subtitle = subtitle
if current_subtitle is not None:
combined_subtitles.append(current_subtitle)
return combined_subtitles
# Load the SRT file
subs = pysrt.open('test_zh1.srt')
combined_subs = combine_subtitles(subs)
subtitles = SubRipFile()
for sub in combined_subs:
subtitles.append(sub)
subtitles.save('combine_output.srt')