[cleanup] Misc (#11311)
[yt-dlp3.git] / test / test_subtitles.py
blobf3b00561791d373f23c10b36d5a8795a12f6fcf4
1 #!/usr/bin/env python3
3 # Allow direct execution
4 import os
5 import sys
6 import unittest
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11 from test.helper import FakeYDL, is_download_test, md5
12 from yt_dlp.extractor import (
13 NPOIE,
14 NRKTVIE,
15 PBSIE,
16 CeskaTelevizeIE,
17 ComedyCentralIE,
18 DailymotionIE,
19 DemocracynowIE,
20 LyndaIE,
21 RaiPlayIE,
22 RTVEALaCartaIE,
23 TedTalkIE,
24 ThePlatformFeedIE,
25 ThePlatformIE,
26 VikiIE,
27 VimeoIE,
28 WallaIE,
29 YoutubeIE,
33 @is_download_test
34 class BaseTestSubtitles(unittest.TestCase):
35 url = None
36 IE = None
38 def setUp(self):
39 self.DL = FakeYDL()
40 self.ie = self.IE()
41 self.DL.add_info_extractor(self.ie)
42 if not self.IE.working():
43 print(f'Skipping: {self.IE.ie_key()} marked as not _WORKING')
44 self.skipTest('IE marked as not _WORKING')
46 def getInfoDict(self):
47 return self.DL.extract_info(self.url, download=False)
49 def getSubtitles(self):
50 info_dict = self.getInfoDict()
51 subtitles = info_dict['requested_subtitles']
52 if not subtitles:
53 return subtitles
54 for sub_info in subtitles.values():
55 if sub_info.get('data') is None:
56 uf = self.DL.urlopen(sub_info['url'])
57 sub_info['data'] = uf.read().decode()
58 return {l: sub_info['data'] for l, sub_info in subtitles.items()}
61 @is_download_test
62 class TestYoutubeSubtitles(BaseTestSubtitles):
63 # Available subtitles for QRS8MkLhQmM:
64 # Language formats
65 # ru vtt, ttml, srv3, srv2, srv1, json3
66 # fr vtt, ttml, srv3, srv2, srv1, json3
67 # en vtt, ttml, srv3, srv2, srv1, json3
68 # nl vtt, ttml, srv3, srv2, srv1, json3
69 # de vtt, ttml, srv3, srv2, srv1, json3
70 # ko vtt, ttml, srv3, srv2, srv1, json3
71 # it vtt, ttml, srv3, srv2, srv1, json3
72 # zh-Hant vtt, ttml, srv3, srv2, srv1, json3
73 # hi vtt, ttml, srv3, srv2, srv1, json3
74 # pt-BR vtt, ttml, srv3, srv2, srv1, json3
75 # es-MX vtt, ttml, srv3, srv2, srv1, json3
76 # ja vtt, ttml, srv3, srv2, srv1, json3
77 # pl vtt, ttml, srv3, srv2, srv1, json3
78 url = 'QRS8MkLhQmM'
79 IE = YoutubeIE
81 def test_youtube_allsubtitles(self):
82 self.DL.params['writesubtitles'] = True
83 self.DL.params['allsubtitles'] = True
84 subtitles = self.getSubtitles()
85 self.assertEqual(len(subtitles.keys()), 13)
86 self.assertEqual(md5(subtitles['en']), 'ae1bd34126571a77aabd4d276b28044d')
87 self.assertEqual(md5(subtitles['it']), '0e0b667ba68411d88fd1c5f4f4eab2f9')
88 for lang in ['fr', 'de']:
89 self.assertTrue(subtitles.get(lang) is not None, f'Subtitles for \'{lang}\' not extracted')
91 def _test_subtitles_format(self, fmt, md5_hash, lang='en'):
92 self.DL.params['writesubtitles'] = True
93 self.DL.params['subtitlesformat'] = fmt
94 subtitles = self.getSubtitles()
95 self.assertEqual(md5(subtitles[lang]), md5_hash)
97 def test_youtube_subtitles_ttml_format(self):
98 self._test_subtitles_format('ttml', 'c97ddf1217390906fa9fbd34901f3da2')
100 def test_youtube_subtitles_vtt_format(self):
101 self._test_subtitles_format('vtt', 'ae1bd34126571a77aabd4d276b28044d')
103 def test_youtube_subtitles_json3_format(self):
104 self._test_subtitles_format('json3', '688dd1ce0981683867e7fe6fde2a224b')
106 def _test_automatic_captions(self, url, lang):
107 self.url = url
108 self.DL.params['writeautomaticsub'] = True
109 self.DL.params['subtitleslangs'] = [lang]
110 subtitles = self.getSubtitles()
111 self.assertTrue(subtitles[lang] is not None)
113 def test_youtube_automatic_captions(self):
114 # Available automatic captions for 8YoUxe5ncPo:
115 # Language formats (all in vtt, ttml, srv3, srv2, srv1, json3)
116 # gu, zh-Hans, zh-Hant, gd, ga, gl, lb, la, lo, tt, tr,
117 # lv, lt, tk, th, tg, te, fil, haw, yi, ceb, yo, de, da,
118 # el, eo, en, eu, et, es, ru, rw, ro, bn, be, bg, uk, jv,
119 # bs, ja, or, xh, co, ca, cy, cs, ps, pt, pa, vi, pl, hy,
120 # hr, ht, hu, hmn, hi, ha, mg, uz, ml, mn, mi, mk, ur,
121 # mt, ms, mr, ug, ta, my, af, sw, is, am,
122 # *it*, iw, sv, ar,
123 # su, zu, az, id, ig, nl, no, ne, ny, fr, ku, fy, fa, fi,
124 # ka, kk, sr, sq, ko, kn, km, st, sk, si, so, sn, sm, sl,
125 # ky, sd
126 # ...
127 self._test_automatic_captions('8YoUxe5ncPo', 'it')
129 @unittest.skip('Video unavailable')
130 def test_youtube_translated_subtitles(self):
131 # This video has a subtitles track, which can be translated (#4555)
132 self._test_automatic_captions('Ky9eprVWzlI', 'it')
134 def test_youtube_nosubtitles(self):
135 self.DL.expect_warning('video doesn\'t have subtitles')
136 # Available automatic captions for 8YoUxe5ncPo:
137 # ...
138 # 8YoUxe5ncPo has no subtitles
139 self.url = '8YoUxe5ncPo'
140 self.DL.params['writesubtitles'] = True
141 self.DL.params['allsubtitles'] = True
142 subtitles = self.getSubtitles()
143 self.assertFalse(subtitles)
146 @is_download_test
147 class TestDailymotionSubtitles(BaseTestSubtitles):
148 url = 'http://www.dailymotion.com/video/xczg00'
149 IE = DailymotionIE
151 def test_allsubtitles(self):
152 self.DL.params['writesubtitles'] = True
153 self.DL.params['allsubtitles'] = True
154 subtitles = self.getSubtitles()
155 self.assertTrue(len(subtitles.keys()) >= 6)
156 self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
157 self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
158 for lang in ['es', 'fr', 'de']:
159 self.assertTrue(subtitles.get(lang) is not None, f'Subtitles for \'{lang}\' not extracted')
161 def test_nosubtitles(self):
162 self.DL.expect_warning('video doesn\'t have subtitles')
163 self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
164 self.DL.params['writesubtitles'] = True
165 self.DL.params['allsubtitles'] = True
166 subtitles = self.getSubtitles()
167 self.assertFalse(subtitles)
170 @is_download_test
171 @unittest.skip('IE broken')
172 class TestTedSubtitles(BaseTestSubtitles):
173 url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
174 IE = TedTalkIE
176 def test_allsubtitles(self):
177 self.DL.params['writesubtitles'] = True
178 self.DL.params['allsubtitles'] = True
179 subtitles = self.getSubtitles()
180 self.assertTrue(len(subtitles.keys()) >= 28)
181 self.assertEqual(md5(subtitles['en']), '4262c1665ff928a2dada178f62cb8d14')
182 self.assertEqual(md5(subtitles['fr']), '66a63f7f42c97a50f8c0e90bc7797bb5')
183 for lang in ['es', 'fr', 'de']:
184 self.assertTrue(subtitles.get(lang) is not None, f'Subtitles for \'{lang}\' not extracted')
187 @is_download_test
188 class TestVimeoSubtitles(BaseTestSubtitles):
189 url = 'http://vimeo.com/76979871'
190 IE = VimeoIE
192 def test_allsubtitles(self):
193 self.DL.params['writesubtitles'] = True
194 self.DL.params['allsubtitles'] = True
195 subtitles = self.getSubtitles()
196 self.assertEqual(set(subtitles.keys()), {'de', 'en', 'es', 'fr'})
197 self.assertEqual(md5(subtitles['en']), '386cbc9320b94e25cb364b97935e5dd1')
198 self.assertEqual(md5(subtitles['fr']), 'c9b69eef35bc6641c0d4da8a04f9dfac')
200 def test_nosubtitles(self):
201 self.DL.expect_warning('video doesn\'t have subtitles')
202 self.url = 'http://vimeo.com/68093876'
203 self.DL.params['writesubtitles'] = True
204 self.DL.params['allsubtitles'] = True
205 subtitles = self.getSubtitles()
206 self.assertFalse(subtitles)
209 @is_download_test
210 @unittest.skip('IE broken')
211 class TestWallaSubtitles(BaseTestSubtitles):
212 url = 'http://vod.walla.co.il/movie/2705958/the-yes-men'
213 IE = WallaIE
215 def test_allsubtitles(self):
216 self.DL.expect_warning('Automatic Captions not supported by this server')
217 self.DL.params['writesubtitles'] = True
218 self.DL.params['allsubtitles'] = True
219 subtitles = self.getSubtitles()
220 self.assertEqual(set(subtitles.keys()), {'heb'})
221 self.assertEqual(md5(subtitles['heb']), 'e758c5d7cb982f6bef14f377ec7a3920')
223 def test_nosubtitles(self):
224 self.DL.expect_warning('video doesn\'t have subtitles')
225 self.url = 'http://vod.walla.co.il/movie/2642630/one-direction-all-for-one'
226 self.DL.params['writesubtitles'] = True
227 self.DL.params['allsubtitles'] = True
228 subtitles = self.getSubtitles()
229 self.assertFalse(subtitles)
232 @is_download_test
233 @unittest.skip('IE broken')
234 class TestCeskaTelevizeSubtitles(BaseTestSubtitles):
235 url = 'http://www.ceskatelevize.cz/ivysilani/10600540290-u6-uzasny-svet-techniky'
236 IE = CeskaTelevizeIE
238 def test_allsubtitles(self):
239 self.DL.expect_warning('Automatic Captions not supported by this server')
240 self.DL.params['writesubtitles'] = True
241 self.DL.params['allsubtitles'] = True
242 subtitles = self.getSubtitles()
243 self.assertEqual(set(subtitles.keys()), {'cs'})
244 self.assertTrue(len(subtitles['cs']) > 20000)
246 def test_nosubtitles(self):
247 self.DL.expect_warning('video doesn\'t have subtitles')
248 self.url = 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220'
249 self.DL.params['writesubtitles'] = True
250 self.DL.params['allsubtitles'] = True
251 subtitles = self.getSubtitles()
252 self.assertFalse(subtitles)
255 @is_download_test
256 @unittest.skip('IE broken')
257 class TestLyndaSubtitles(BaseTestSubtitles):
258 url = 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html'
259 IE = LyndaIE
261 def test_allsubtitles(self):
262 self.DL.params['writesubtitles'] = True
263 self.DL.params['allsubtitles'] = True
264 subtitles = self.getSubtitles()
265 self.assertEqual(set(subtitles.keys()), {'en'})
266 self.assertEqual(md5(subtitles['en']), '09bbe67222259bed60deaa26997d73a7')
269 @is_download_test
270 @unittest.skip('IE broken')
271 class TestNPOSubtitles(BaseTestSubtitles):
272 url = 'http://www.npo.nl/nos-journaal/28-08-2014/POW_00722860'
273 IE = NPOIE
275 def test_allsubtitles(self):
276 self.DL.params['writesubtitles'] = True
277 self.DL.params['allsubtitles'] = True
278 subtitles = self.getSubtitles()
279 self.assertEqual(set(subtitles.keys()), {'nl'})
280 self.assertEqual(md5(subtitles['nl']), 'fc6435027572b63fb4ab143abd5ad3f4')
283 @is_download_test
284 @unittest.skip('IE broken')
285 class TestMTVSubtitles(BaseTestSubtitles):
286 url = 'http://www.cc.com/video-clips/p63lk0/adam-devine-s-house-party-chasing-white-swans'
287 IE = ComedyCentralIE
289 def getInfoDict(self):
290 return super().getInfoDict()['entries'][0]
292 def test_allsubtitles(self):
293 self.DL.params['writesubtitles'] = True
294 self.DL.params['allsubtitles'] = True
295 subtitles = self.getSubtitles()
296 self.assertEqual(set(subtitles.keys()), {'en'})
297 self.assertEqual(md5(subtitles['en']), '78206b8d8a0cfa9da64dc026eea48961')
300 @is_download_test
301 class TestNRKSubtitles(BaseTestSubtitles):
302 url = 'http://tv.nrk.no/serie/ikke-gjoer-dette-hjemme/DMPV73000411/sesong-2/episode-1'
303 IE = NRKTVIE
305 def test_allsubtitles(self):
306 self.DL.params['writesubtitles'] = True
307 self.DL.params['allsubtitles'] = True
308 subtitles = self.getSubtitles()
309 self.assertEqual(set(subtitles.keys()), {'nb-ttv'})
310 self.assertEqual(md5(subtitles['nb-ttv']), '67e06ff02d0deaf975e68f6cb8f6a149')
313 @is_download_test
314 class TestRaiPlaySubtitles(BaseTestSubtitles):
315 IE = RaiPlayIE
317 def test_subtitles_key(self):
318 self.url = 'http://www.raiplay.it/video/2014/04/Report-del-07042014-cb27157f-9dd0-4aee-b788-b1f67643a391.html'
319 self.DL.params['writesubtitles'] = True
320 self.DL.params['allsubtitles'] = True
321 subtitles = self.getSubtitles()
322 self.assertEqual(set(subtitles.keys()), {'it'})
323 self.assertEqual(md5(subtitles['it']), 'b1d90a98755126b61e667567a1f6680a')
325 def test_subtitles_array_key(self):
326 self.url = 'https://www.raiplay.it/video/2020/12/Report---04-01-2021-2e90f1de-8eee-4de4-ac0e-78d21db5b600.html'
327 self.DL.params['writesubtitles'] = True
328 self.DL.params['allsubtitles'] = True
329 subtitles = self.getSubtitles()
330 self.assertEqual(set(subtitles.keys()), {'it'})
331 self.assertEqual(md5(subtitles['it']), '4b3264186fbb103508abe5311cfcb9cd')
334 @is_download_test
335 @unittest.skip('IE broken - DRM only')
336 class TestVikiSubtitles(BaseTestSubtitles):
337 url = 'http://www.viki.com/videos/1060846v-punch-episode-18'
338 IE = VikiIE
340 def test_allsubtitles(self):
341 self.DL.params['writesubtitles'] = True
342 self.DL.params['allsubtitles'] = True
343 subtitles = self.getSubtitles()
344 self.assertEqual(set(subtitles.keys()), {'en'})
345 self.assertEqual(md5(subtitles['en']), '53cb083a5914b2d84ef1ab67b880d18a')
348 @is_download_test
349 class TestThePlatformSubtitles(BaseTestSubtitles):
350 # from http://www.3playmedia.com/services-features/tools/integrations/theplatform/
351 # (see http://theplatform.com/about/partners/type/subtitles-closed-captioning/)
352 url = 'theplatform:JFUjUE1_ehvq'
353 IE = ThePlatformIE
355 def test_allsubtitles(self):
356 self.DL.params['writesubtitles'] = True
357 self.DL.params['allsubtitles'] = True
358 subtitles = self.getSubtitles()
359 self.assertEqual(set(subtitles.keys()), {'en'})
360 self.assertEqual(md5(subtitles['en']), '97e7670cbae3c4d26ae8bcc7fdd78d4b')
363 @is_download_test
364 @unittest.skip('IE broken')
365 class TestThePlatformFeedSubtitles(BaseTestSubtitles):
366 url = 'http://feed.theplatform.com/f/7wvmTC/msnbc_video-p-test?form=json&pretty=true&range=-40&byGuid=n_hardball_5biden_140207'
367 IE = ThePlatformFeedIE
369 def test_allsubtitles(self):
370 self.DL.params['writesubtitles'] = True
371 self.DL.params['allsubtitles'] = True
372 subtitles = self.getSubtitles()
373 self.assertEqual(set(subtitles.keys()), {'en'})
374 self.assertEqual(md5(subtitles['en']), '48649a22e82b2da21c9a67a395eedade')
377 @is_download_test
378 class TestRtveSubtitles(BaseTestSubtitles):
379 url = 'http://www.rtve.es/alacarta/videos/los-misterios-de-laura/misterios-laura-capitulo-32-misterio-del-numero-17-2-parte/2428621/'
380 IE = RTVEALaCartaIE
382 def test_allsubtitles(self):
383 print('Skipping, only available from Spain')
384 return
385 self.DL.params['writesubtitles'] = True
386 self.DL.params['allsubtitles'] = True
387 subtitles = self.getSubtitles()
388 self.assertEqual(set(subtitles.keys()), {'es'})
389 self.assertEqual(md5(subtitles['es']), '69e70cae2d40574fb7316f31d6eb7fca')
392 @is_download_test
393 class TestDemocracynowSubtitles(BaseTestSubtitles):
394 url = 'http://www.democracynow.org/shows/2015/7/3'
395 IE = DemocracynowIE
397 def test_allsubtitles(self):
398 self.DL.params['writesubtitles'] = True
399 self.DL.params['allsubtitles'] = True
400 subtitles = self.getSubtitles()
401 self.assertEqual(set(subtitles.keys()), {'en'})
402 self.assertEqual(md5(subtitles['en']), 'a3cc4c0b5eadd74d9974f1c1f5101045')
404 def test_subtitles_in_page(self):
405 self.url = 'http://www.democracynow.org/2015/7/3/this_flag_comes_down_today_bree'
406 self.DL.params['writesubtitles'] = True
407 self.DL.params['allsubtitles'] = True
408 subtitles = self.getSubtitles()
409 self.assertEqual(set(subtitles.keys()), {'en'})
410 self.assertEqual(md5(subtitles['en']), 'a3cc4c0b5eadd74d9974f1c1f5101045')
413 @is_download_test
414 class TestPBSSubtitles(BaseTestSubtitles):
415 url = 'https://www.pbs.org/video/how-fantasy-reflects-our-world-picecq/'
416 IE = PBSIE
418 def test_allsubtitles(self):
419 self.DL.params['writesubtitles'] = True
420 self.DL.params['allsubtitles'] = True
421 subtitles = self.getSubtitles()
422 self.assertEqual(set(subtitles.keys()), {'en'})
424 def test_subtitles_dfxp_format(self):
425 self.DL.params['writesubtitles'] = True
426 self.DL.params['subtitlesformat'] = 'dfxp'
427 subtitles = self.getSubtitles()
428 self.assertIn(md5(subtitles['en']), ['643b034254cdc3768ff1e750b6b5873b'])
430 def test_subtitles_vtt_format(self):
431 self.DL.params['writesubtitles'] = True
432 self.DL.params['subtitlesformat'] = 'vtt'
433 subtitles = self.getSubtitles()
434 self.assertIn(
435 md5(subtitles['en']), ['937a05711555b165d4c55a9667017045', 'f49ea998d6824d94959c8152a368ff73'])
437 def test_subtitles_srt_format(self):
438 self.DL.params['writesubtitles'] = True
439 self.DL.params['subtitlesformat'] = 'srt'
440 subtitles = self.getSubtitles()
441 self.assertIn(md5(subtitles['en']), ['2082c21b43759d9bf172931b2f2ca371'])
443 def test_subtitles_sami_format(self):
444 self.DL.params['writesubtitles'] = True
445 self.DL.params['subtitlesformat'] = 'sami'
446 subtitles = self.getSubtitles()
447 self.assertIn(md5(subtitles['en']), ['4256b16ac7da6a6780fafd04294e85cd'])
450 if __name__ == '__main__':
451 unittest.main()