logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

test_subtitles.py (15801B)


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