generated from yt-dlp/yt-dlp-sample-plugins
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimplesrt.py
226 lines (168 loc) · 7.58 KB
/
simplesrt.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import os
from datetime import timedelta
from typing import List, Tuple, Union, Iterator
import re
class Subtitle:
"""
A class to represent a single subtitle unit in a subtitle file.
Attributes
----------
start : timedelta
The start time of the subtitle.
end : timedelta
The end time of the subtitle.
text : str
The text content of the subtitle.
Methods
-------
_print_duration(duration: timedelta) -> str:
Returns a formatted string representing the given duration.
__str__() -> str:
Returns a string representation of the subtitle, including start and end times and text content.
__repr__() -> str:
Returns a string representation of the Subtitle object with its attributes.
"""
def __init__(self, start_duration, end_duration, text):
self.start = start_duration
self.end = end_duration
self.text = text.strip()
@staticmethod
def _print_duration(duration) :
hours, remainder = divmod(duration.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return f"{hours:02d}:{minutes:02d}:{seconds:02d},{duration.microseconds // 1000:03d}"
def __str__(self):
return f"{self._print_duration(self.start)} --> {self._print_duration(self.end)}\n{self.text}\n\n"
def __repr__(self):
return f"Subtitle Object start:{self.start}, end:{self.end}, text:'{self.text}'"
class SimpleSrt:
"""
A class to parse and manipulate Simple SubRip (SRT) subtitle files.
Attributes
----------
subs : List[Subtitle]
A list of Subtitle objects representing the parsed subtitles in the input SRT string.
Methods
-------
get_duration(parts: Tuple[int, int, int, int]) -> timedelta:
Returns a timedelta object representing the duration from a tuple of hours, minutes, seconds, and milliseconds.
parse_timecode_string(line: str) -> Union[bool, Tuple[timedelta, timedelta]]:
Parses a timecode string from an SRT file and returns a tuple of start and end timedelta objects.
If the line does not contain a valid timecode, returns False.
parse_srt(subtitle_text: str) -> List[Subtitle]:
Parses the input SRT string and returns a list of Subtitle objects.
Usage
-----
srt = SimpleSrt(srt_string)
subs = srt.subs
"""
def __init__(self, srt_string):
self.subs = self.parse_srt(srt_string)
@staticmethod
def get_duration(parts) :
"""
get_duration(parts: Tuple[int, int, int, int]) -> timedelta:
Returns a timedelta object representing the duration from a tuple of hours, minutes, seconds, and milliseconds.
:param parts: Tuple[int, int, int, int])
:return: timedelta
"""
hour, minute, second, millisecond = parts
return timedelta(hours=hour, minutes=minute, seconds=second, milliseconds=millisecond)
def parse_timecode_string(self, line) -> Union[bool, Tuple[timedelta, timedelta]]:
"""
Parses a timecode string from an SRT file and returns a tuple of start and end timedelta objects.
If the line does not contain a valid timecode, returns False.
:param line: string of srt timecode hh:mm:ss,mss --> hh:mm:ss,mss
:return: tuple of timedelta objects of start and end time
"""
time_frame_pattern = re.compile(r"(\d+):(\d+):(\d+),(\d+) --> (\d+):(\d+):(\d+),(\d+)")
if "-->" in line:
timing = time_frame_pattern.match(line.strip())
if timing is None:
return False
start = self.get_duration([int(x) for x in timing.groups()[0:4]])
end = self.get_duration([int(x) for x in timing.groups()[4:8]])
return start, end
return False
def parse_srt(self, subtitle_text) :
srtlines = [x for x in subtitle_text.split("\n") if len(x.strip()) > 0]
srtlines += ["", ""]
i = 0
while i < len(srtlines):
timecode = self.parse_timecode_string(srtlines[i])
if timecode:
y = 0
text = ""
try:
while not self.parse_timecode_string(srtlines[y + i + 2]):
text += srtlines[y + i + 1] + "\n"
y += 1
except IndexError:
pass
start, end = timecode
yield Subtitle(start, end, text)
i += y + 1
else:
i += 1
def dedupe_yt_srt(subs_iter):
previous_subtitle = None
index = 1
text = ""
for subtitle in subs_iter:
if previous_subtitle is None: # first interation set previous subtitle for comparison
previous_subtitle = subtitle
continue
subtitle.text = subtitle.text.strip() # remove trailing linebreaks
if len(subtitle.text) == 0: # skip over empty subtitles
continue
if (subtitle.start - subtitle.end < timedelta(milliseconds=150) and # very short
subtitle.text in previous_subtitle.text ): # same text as previous
previous_subtitle.end = subtitle.end # lengthen previous subtitle
continue
current_lines = subtitle.text.split("\n")
last_lines = previous_subtitle.text.split("\n")
singleword=False
if current_lines[0] == last_lines[-1]: # if first current is last previous
if len(last_lines)==1:
if len(last_lines[0].split(" "))<2 and len(last_lines[0])>2: # if is just one word
singleword=True
subtitle.text= current_lines[0]+" "+"\n".join(current_lines[1:]) # remove line break after single word
else:
subtitle.text = "\n".join(current_lines[1:]) # discard first line of current
else:
subtitle.text = "\n".join(current_lines[1:]) # discard first line of current
else: # not fusing two lines
if len(subtitle.text.split(" "))<=2: # only one word in subtitle
previous_subtitle.end = subtitle.end # lengthen previous subtitle
title_text=subtitle.text
if title_text[0]!=" ":
title_text=" "+title_text
previous_subtitle.text+=title_text # add text to previous
continue # drop this subtitle
if subtitle.start <= previous_subtitle.end: # remove overlap and let 1ms gap
previous_subtitle.end = subtitle.start - timedelta(milliseconds=1)
if subtitle.start >= subtitle.end: # swap start and end if wrong order
end =subtitle.end
subtitle.end= subtitle.start
subtitle.start = end
if not singleword:
yield previous_subtitle
previous_subtitle = subtitle
index += 1
yield previous_subtitle
def subs_to_text(subs_iter):
index = 1
text = ""
for subtitle in subs_iter:
text += f"{index}\n{subtitle}"# conversion to str handles adding timecode
index += 1
return text
def process_srt(file_path, new_file_path):
text = ""
with open(file_path, "r", encoding="utf8") as file:
srtstring = file.read()
srt = SimpleSrt(srtstring)
subs = dedupe_yt_srt(srt.subs)
text=subs_to_text(subs)
with open(new_file_path, "w", encoding="utf8") as new_file:
new_file.write(text.strip())