-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathwebvtt.py
More file actions
412 lines (338 loc) · 13.1 KB
/
Copy pathwebvtt.py
File metadata and controls
412 lines (338 loc) · 13.1 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import os
import io
from shutil import rmtree, copy
from webvtt.webvtt import HlsWebVTT
import webvtt
from webvtt.structures import Caption, Style, TimestampMap
from .generic import GenericParserTestCase
from webvtt.errors import MalformedFileError
BASE_DIR = os.path.dirname(__file__)
OUTPUT_DIR = os.path.join(BASE_DIR, 'output')
class WebVTTTestCase(GenericParserTestCase):
def tearDown(self):
if os.path.exists(OUTPUT_DIR):
rmtree(OUTPUT_DIR)
def test_create_caption(self):
caption = Caption('00:00:00.500', '00:00:07.000', ['Caption test line 1', 'Caption test line 2'])
self.assertEqual(caption.start, '00:00:00.500')
self.assertEqual(caption.start_in_seconds, 0.5)
self.assertEqual(caption.end, '00:00:07.000')
self.assertEqual(caption.end_in_seconds, 7)
self.assertEqual(caption.lines, ['Caption test line 1', 'Caption test line 2'])
def test_write_captions(self):
os.makedirs(OUTPUT_DIR)
copy(self._get_file('one_caption.vtt'), OUTPUT_DIR)
out = io.StringIO()
vtt = webvtt.read(os.path.join(OUTPUT_DIR, 'one_caption.vtt'))
new_caption = Caption('00:00:07.000', '00:00:11.890', ['New caption text line1', 'New caption text line2'])
vtt.captions.append(new_caption)
vtt.write(out)
out.seek(0)
lines = [line.rstrip() for line in out.readlines()]
expected_lines = [
'WEBVTT',
'',
'00:00:00.500 --> 00:00:07.000',
'Caption text #1',
'',
'00:00:07.000 --> 00:00:11.890',
'New caption text line1',
'New caption text line2'
]
self.assertListEqual(lines, expected_lines)
def test_save_captions(self):
os.makedirs(OUTPUT_DIR)
copy(self._get_file('one_caption.vtt'), OUTPUT_DIR)
vtt = webvtt.read(os.path.join(OUTPUT_DIR, 'one_caption.vtt'))
new_caption = Caption('00:00:07.000', '00:00:11.890', ['New caption text line1', 'New caption text line2'])
vtt.captions.append(new_caption)
vtt.save()
with open(os.path.join(OUTPUT_DIR, 'one_caption.vtt'), 'r', encoding='utf-8') as f:
lines = [line.rstrip() for line in f.readlines()]
expected_lines = [
'WEBVTT',
'',
'00:00:00.500 --> 00:00:07.000',
'Caption text #1',
'',
'00:00:07.000 --> 00:00:11.890',
'New caption text line1',
'New caption text line2'
]
self.assertListEqual(lines, expected_lines)
def test_srt_conversion(self):
os.makedirs(OUTPUT_DIR)
copy(self._get_file('one_caption.srt'), OUTPUT_DIR)
vtt = webvtt.from_srt(os.path.join(OUTPUT_DIR, 'one_caption.srt'))
vtt.save()
self.assertTrue(os.path.exists(os.path.join(OUTPUT_DIR, 'one_caption.vtt')))
with open(os.path.join(OUTPUT_DIR, 'one_caption.vtt'), 'r', encoding='utf-8') as f:
lines = [line.rstrip() for line in f.readlines()]
expected_lines = [
'WEBVTT',
'',
'00:00:00.500 --> 00:00:07.000',
'Caption text #1',
]
self.assertListEqual(lines, expected_lines)
def test_sbv_conversion(self):
os.makedirs(OUTPUT_DIR)
copy(self._get_file('two_captions.sbv'), OUTPUT_DIR)
vtt = webvtt.from_sbv(os.path.join(OUTPUT_DIR, 'two_captions.sbv'))
vtt.save()
self.assertTrue(os.path.exists(os.path.join(OUTPUT_DIR, 'two_captions.vtt')))
with open(os.path.join(OUTPUT_DIR, 'two_captions.vtt'), 'r', encoding='utf-8') as f:
lines = [line.rstrip() for line in f.readlines()]
expected_lines = [
'WEBVTT',
'',
'00:00:00.378 --> 00:00:11.378',
'Caption text #1',
'',
'00:00:11.378 --> 00:00:12.305',
'Caption text #2 (line 1)',
'Caption text #2 (line 2)',
]
self.assertListEqual(lines, expected_lines)
def test_save_to_other_location(self):
target_path = os.path.join(OUTPUT_DIR, 'test_folder')
os.makedirs(target_path)
webvtt.read(self._get_file('one_caption.vtt')).save(target_path)
self.assertTrue(os.path.exists(os.path.join(target_path, 'one_caption.vtt')))
def test_save_specific_filename(self):
target_path = os.path.join(OUTPUT_DIR, 'test_folder')
os.makedirs(target_path)
output_file = os.path.join(target_path, 'custom_name.vtt')
webvtt.read(self._get_file('one_caption.vtt')).save(output_file)
self.assertTrue(os.path.exists(output_file))
def test_save_specific_filename_no_extension(self):
target_path = os.path.join(OUTPUT_DIR, 'test_folder')
os.makedirs(target_path)
output_file = os.path.join(target_path, 'custom_name')
webvtt.read(self._get_file('one_caption.vtt')).save(output_file)
self.assertTrue(os.path.exists(os.path.join(target_path, 'custom_name.vtt')))
def test_caption_timestamp_update(self):
c = Caption('00:00:00.500', '00:00:07.000')
c.start = '00:00:01.750'
c.end = '00:00:08.250'
self.assertEqual(c.start, '00:00:01.750')
self.assertEqual(c.end, '00:00:08.250')
def test_caption_timestamp_format(self):
c = Caption('01:02:03.400', '02:03:04.500')
self.assertEqual(c.start, '01:02:03.400')
self.assertEqual(c.end, '02:03:04.500')
c = Caption('02:03.400', '03:04.500')
self.assertEqual(c.start, '00:02:03.400')
self.assertEqual(c.end, '00:03:04.500')
def test_caption_text(self):
c = Caption(text=['Caption line #1', 'Caption line #2'])
self.assertEqual(
c.text,
'Caption line #1\nCaption line #2'
)
def test_caption_receive_text(self):
c = Caption(text='Caption line #1\nCaption line #2')
self.assertEqual(
len(c.lines),
2
)
self.assertEqual(
c.text,
'Caption line #1\nCaption line #2'
)
def test_update_text(self):
c = Caption(text='Caption line #1')
c.text = 'Caption line #1 updated'
self.assertEqual(
c.text,
'Caption line #1 updated'
)
def test_update_text_multiline(self):
c = Caption(text='Caption line #1')
c.text = 'Caption line #1\nCaption line #2'
self.assertEqual(
len(c.lines),
2
)
self.assertEqual(
c.text,
'Caption line #1\nCaption line #2'
)
def test_update_text_wrong_type(self):
c = Caption(text='Caption line #1')
self.assertRaises(
AttributeError,
setattr,
c,
'text',
123
)
def test_manipulate_lines(self):
c = Caption(text=['Caption line #1', 'Caption line #2'])
c.lines[0] = 'Caption line #1 updated'
self.assertEqual(
c.lines[0],
'Caption line #1 updated'
)
def test_read_file_buffer(self):
with open(self._get_file('sample.vtt'), 'r', encoding='utf-8') as f:
vtt = webvtt.read_buffer(f)
self.assertIsInstance(vtt.captions, list)
def test_read_memory_buffer(self):
payload = ''
with open(self._get_file('sample.vtt'), 'r', encoding='utf-8') as f:
payload = f.read()
buffer = io.StringIO(payload)
vtt = webvtt.read_buffer(buffer)
self.assertIsInstance(vtt.captions, list)
def test_read_malformed_buffer(self):
malformed_payloads = ['', 'MOCK MELFORMED CONTENT']
for payload in malformed_payloads:
buffer = io.StringIO(payload)
with self.assertRaises(MalformedFileError):
webvtt.read_buffer(buffer)
def test_timestamp_map(self):
tag_one = 'X-TIMESTAMP-MAP=LOCAL:1086:59:52.424,MPEGTS:183000'
tag_two = 'X-TIMESTAMP-MAP=MPEGTS:183000,LOCAL:1086:59:52.424'
timestamp_map_one = TimestampMap(tag_one)
timestamp_map_two = TimestampMap(tag_two)
self.assertEqual(
timestamp_map_one.local,
'1086:59:52.424'
)
self.assertEqual(
timestamp_map_two.local,
'1086:59:52.424'
)
self.assertEqual(
timestamp_map_one.mpegts,
'183000'
)
self.assertEqual(
timestamp_map_two.mpegts,
'183000'
)
def test_read_hls_webvtt(self):
vtt = HlsWebVTT.read(self._get_file('sample_hls.vtt'))
self.assertIsInstance(vtt.captions, list)
self.assertIsInstance(vtt.timestamp_map, TimestampMap)
self.assertTrue(vtt.timestamp_map.local)
self.assertTrue(vtt.timestamp_map.mpegts)
def test_read_non_hls_webvtt_with_hls_webvtt_parser(self):
vtt = HlsWebVTT.read(self._get_file('sample.vtt'))
self.assertIsInstance(vtt.captions, list)
self.assertIsNone(vtt.timestamp_map)
def test_captions(self):
vtt = webvtt.read(self._get_file('sample.vtt'))
self.assertIsInstance(vtt.captions, list)
def test_captions_prevent_write(self):
vtt = webvtt.read(self._get_file('sample.vtt'))
self.assertRaises(
AttributeError,
setattr,
vtt,
'captions',
[]
)
def test_sequence_iteration(self):
vtt = webvtt.read(self._get_file('sample.vtt'))
self.assertIsInstance(vtt[0], Caption)
self.assertEqual(len(vtt), len(vtt.captions))
def test_save_no_filename(self):
vtt = webvtt.WebVTT()
self.assertRaises(
webvtt.errors.MissingFilenameError,
vtt.save
)
def test_malformed_start_timestamp(self):
self.assertRaises(
webvtt.errors.MalformedCaptionError,
Caption,
'01:00'
)
def test_set_styles_from_text(self):
style = Style()
style.text = '::cue(b) {\n color: peachpuff;\n}'
self.assertListEqual(
style.lines,
['::cue(b) {', ' color: peachpuff;', '}']
)
def test_get_styles_as_text(self):
style = Style()
style.lines = ['::cue(b) {', ' color: peachpuff;', '}']
self.assertEqual(
style.text,
'::cue(b) {color: peachpuff;}'
)
def test_save_identifiers(self):
os.makedirs(OUTPUT_DIR)
copy(self._get_file('using_identifiers.vtt'), OUTPUT_DIR)
vtt = webvtt.read(os.path.join(OUTPUT_DIR, 'using_identifiers.vtt'))
vtt.save(os.path.join(OUTPUT_DIR, 'new_using_identifiers.vtt'))
with open(os.path.join(OUTPUT_DIR, 'new_using_identifiers.vtt'), 'r', encoding='utf-8') as f:
lines = [line.rstrip() for line in f.readlines()]
expected_lines = [
'WEBVTT',
'',
'00:00:00.500 --> 00:00:07.000',
'Caption text #1',
'',
'second caption',
'00:00:07.000 --> 00:00:11.890',
'Caption text #2',
'',
'00:00:11.890 --> 00:00:16.320',
'Caption text #3',
'',
'4',
'00:00:16.320 --> 00:00:21.580',
'Caption text #4',
'',
'00:00:21.580 --> 00:00:23.880',
'Caption text #5',
'',
'00:00:23.880 --> 00:00:27.280',
'Caption text #6'
]
self.assertListEqual(lines, expected_lines)
def test_save_updated_identifiers(self):
os.makedirs(OUTPUT_DIR)
copy(self._get_file('using_identifiers.vtt'), OUTPUT_DIR)
vtt = webvtt.read(os.path.join(OUTPUT_DIR, 'using_identifiers.vtt'))
vtt.captions[0].identifier = 'first caption'
vtt.captions[1].identifier = None
vtt.captions[3].identifier = '44'
last_caption = Caption('00:00:27.280', '00:00:29.200', 'Caption text #7')
last_caption.identifier = 'last caption'
vtt.captions.append(last_caption)
vtt.save(os.path.join(OUTPUT_DIR, 'new_using_identifiers.vtt'))
with open(os.path.join(OUTPUT_DIR, 'new_using_identifiers.vtt'), 'r', encoding='utf-8') as f:
lines = [line.rstrip() for line in f.readlines()]
expected_lines = [
'WEBVTT',
'',
'first caption',
'00:00:00.500 --> 00:00:07.000',
'Caption text #1',
'',
'00:00:07.000 --> 00:00:11.890',
'Caption text #2',
'',
'00:00:11.890 --> 00:00:16.320',
'Caption text #3',
'',
'44',
'00:00:16.320 --> 00:00:21.580',
'Caption text #4',
'',
'00:00:21.580 --> 00:00:23.880',
'Caption text #5',
'',
'00:00:23.880 --> 00:00:27.280',
'Caption text #6',
'',
'last caption',
'00:00:27.280 --> 00:00:29.200',
'Caption text #7'
]
self.assertListEqual(lines, expected_lines)