test_subtitles.py 17 KB

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