forked from glut23/webvtt-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsers.py
296 lines (233 loc) · 9.25 KB
/
parsers.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import re
import os
import codecs
from .errors import MalformedFileError, MalformedCaptionError
from .structures import Block, Style, Caption
class TextBasedParser(object):
"""
Parser for plain text caption files.
This is a generic class, do not use directly.
"""
TIMEFRAME_LINE_PATTERN = ''
PARSER_OPTIONS = {}
def __init__(self, parse_options=None):
self.captions = []
self.parse_options = parse_options or {}
def read(self, file):
"""Reads the captions file."""
content = self._get_content_from_file(file_path=file)
self._validate(content)
self._parse(content)
return self
def read_from_buffer(self, buffer):
content = self._read_content_lines(buffer)
self._validate(content)
self._parse(content)
return self
def _get_content_from_file(self, file_path):
encoding = self._read_file_encoding(file_path)
with open(file_path, encoding=encoding) as f:
return self._read_content_lines(f)
def _read_file_encoding(self, file_path):
first_bytes = min(32, os.path.getsize(file_path))
with open(file_path, 'rb') as f:
raw = f.read(first_bytes)
if raw.startswith(codecs.BOM_UTF8):
return 'utf-8-sig'
else:
return 'utf-8'
def _read_content_lines(self, file_obj):
lines = [line.rstrip('\n\r') for line in file_obj.readlines()]
if not lines:
raise MalformedFileError('The file is empty.')
return lines
def _read_content(self, file):
return self._get_content_from_file(file_path=file)
def _parse_timeframe_line(self, line):
"""Parse timeframe line and return start and end timestamps."""
tf = self._validate_timeframe_line(line)
if not tf:
raise MalformedCaptionError('Invalid time format')
return tf.group(1), tf.group(2)
def _validate_timeframe_line(self, line):
return re.match(self.TIMEFRAME_LINE_PATTERN, line)
def _is_timeframe_line(self, line):
"""
This method returns True if the line contains the timeframes.
To be implemented by child classes.
"""
raise NotImplementedError
def _validate(self, lines):
"""
Validates the format of the parsed file.
To be implemented by child classes.
"""
raise NotImplementedError
def _should_skip_line(self, line, index, caption):
"""
This method returns True for a line that should be skipped.
Implement in child classes if needed.
"""
return False
def _parse(self, lines):
self.captions = []
c = None
for index, line in enumerate(lines):
if self._is_timeframe_line(line):
try:
start, end = self._parse_timeframe_line(line)
except MalformedCaptionError as e:
raise MalformedCaptionError('{} in line {}'.format(e, index + 1))
c = Caption(start, end)
elif self._should_skip_line(line, index, c): # allow child classes to skip lines based on the content
continue
elif line:
if c is None:
raise MalformedCaptionError(
'Caption missing timeframe in line {}.'.format(index + 1))
else:
c.add_line(line)
else:
if c is None:
continue
if not c.lines:
if self.PARSER_OPTIONS.get('ignore_empty_captions', False):
c = None
continue
raise MalformedCaptionError('Caption missing text in line {}.'.format(index + 1))
self.captions.append(c)
c = None
if c is not None and c.lines:
self.captions.append(c)
class SRTParser(TextBasedParser):
"""
SRT parser.
"""
TIMEFRAME_LINE_PATTERN = re.compile(r'\s*(\d+:\d{2}:\d{2},\d{3})\s*-->\s*(\d+:\d{2}:\d{2},\d{3})')
PARSER_OPTIONS = {
'ignore_empty_captions': True
}
def _validate(self, lines):
if len(lines) < 2 or lines[0] != '1' or not self._validate_timeframe_line(lines[1]):
raise MalformedFileError('The file does not have a valid format.')
def _is_timeframe_line(self, line):
return '-->' in line
def _should_skip_line(self, line, index, caption):
return caption is None and line.isdigit()
class WebVTTParser(TextBasedParser):
"""
WebVTT parser.
"""
TIMEFRAME_LINE_PATTERN = re.compile(
# matches timestamp format: 0:0:13.540 --> 0:0:17.850
r'\s*((?:\d+:)?\d{1,2}:\d{1,2}.\d{3})\s*-->\s*((?:\d+:)?\d{1,2}:\d{1,2}.\d{3})'
)
COMMENT_PATTERN = re.compile(r'NOTE(?:\s.+|$)')
STYLE_PATTERN = re.compile(r'STYLE[ \t]*$')
def __init__(self):
super().__init__()
self.styles = []
def _compute_blocks(self, lines):
blocks = []
for index, line in enumerate(lines, start=1):
if line:
if not blocks:
blocks.append(Block(index))
if not blocks[-1].lines:
if not line.strip():
continue
blocks[-1].line_number = index
blocks[-1].lines.append(line)
else:
blocks.append(Block(index))
# filter out empty blocks and skip signature
return list(filter(lambda x: x.lines, blocks))[1:]
def _parse_cue_block(self, block):
caption = Caption()
cue_timings = None
additional_blocks = None
for line_number, line in enumerate(block.lines):
if self._is_cue_timings_line(line):
if cue_timings is None:
try:
cue_timings = self._parse_timeframe_line(line)
except MalformedCaptionError as e:
raise MalformedCaptionError(
'{} in line {}'.format(e, block.line_number + line_number))
else:
additional_blocks = self._compute_blocks(
['WEBVTT', ''] + block.lines[line_number:]
)
break
elif line_number == 0:
caption.identifier = line
else:
caption.add_line(line)
caption.start = cue_timings[0]
caption.end = cue_timings[1]
return caption, additional_blocks
def _parse(self, lines):
self.captions = []
blocks = self._compute_blocks(lines)
self._parse_blocks(blocks)
def _is_empty(self, block):
is_empty = True
for line in block.lines:
if line.strip() != "":
is_empty = False
return is_empty
def _parse_blocks(self, blocks):
for block in blocks:
# skip empty blocks
if self._is_empty(block):
continue
if self._is_cue_block(block):
caption, additional_blocks = self._parse_cue_block(block)
self.captions.append(caption)
if additional_blocks:
self._parse_blocks(additional_blocks)
elif self._is_comment_block(block):
continue
elif self._is_style_block(block):
if self.captions:
raise MalformedFileError(
'Style block defined after the first cue in line {}.'
.format(block.line_number))
style = Style()
style.lines = block.lines[1:]
self.styles.append(style)
else:
if len(block.lines) == 1:
raise MalformedCaptionError(
'Standalone cue identifier in line {}.'.format(block.line_number))
else:
raise MalformedCaptionError(
'Missing timing cue in line {}.'.format(block.line_number+1))
def _validate(self, lines):
if not re.match('WEBVTT', lines[0]):
raise MalformedFileError('The file does not have a valid format')
def _is_cue_timings_line(self, line):
return '-->' in line
def _is_cue_block(self, block):
"""Returns True if it is a cue block
(one of the two first lines being a cue timing line)"""
return any(map(self._is_cue_timings_line, block.lines[:2]))
def _is_comment_block(self, block):
"""Returns True if it is a comment block"""
return re.match(self.COMMENT_PATTERN, block.lines[0])
def _is_style_block(self, block):
"""Returns True if it is a style block"""
return re.match(self.STYLE_PATTERN, block.lines[0])
class SBVParser(TextBasedParser):
"""
YouTube SBV parser.
"""
TIMEFRAME_LINE_PATTERN = re.compile(r'\s*(\d+:\d{2}:\d{2}.\d{3}),(\d+:\d{2}:\d{2}.\d{3})')
PARSER_OPTIONS = {
'ignore_empty_captions': True
}
def _validate(self, lines):
if not self._validate_timeframe_line(lines[0]):
raise MalformedFileError('The file does not have a valid format')
def _is_timeframe_line(self, line):
return self._validate_timeframe_line(line)