You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

349 lines
13 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. BlipTVIE,
  11. YoutubeIE,
  12. DailymotionIE,
  13. TEDIE,
  14. VimeoIE,
  15. WallaIE,
  16. CeskaTelevizeIE,
  17. )
  18. class BaseTestSubtitles(unittest.TestCase):
  19. url = None
  20. IE = None
  21. def setUp(self):
  22. self.DL = FakeYDL()
  23. self.ie = self.IE()
  24. self.DL.add_info_extractor(self.ie)
  25. def getInfoDict(self):
  26. info_dict = self.DL.extract_info(self.url, download=False)
  27. return info_dict
  28. def getSubtitles(self):
  29. info_dict = self.getInfoDict()
  30. subtitles = info_dict['subtitles']
  31. if not subtitles:
  32. return subtitles
  33. for sub_info in subtitles.values():
  34. if sub_info.get('data') is None:
  35. uf = self.DL.urlopen(sub_info['url'])
  36. sub_info['data'] = uf.read().decode('utf-8')
  37. return dict((l, sub_info['data']) for l, sub_info in subtitles.items())
  38. class TestYoutubeSubtitles(BaseTestSubtitles):
  39. url = 'QRS8MkLhQmM'
  40. IE = YoutubeIE
  41. def test_youtube_no_writesubtitles(self):
  42. self.DL.params['writesubtitles'] = False
  43. subtitles = self.getSubtitles()
  44. self.assertEqual(subtitles, None)
  45. def test_youtube_subtitles(self):
  46. self.DL.params['writesubtitles'] = True
  47. subtitles = self.getSubtitles()
  48. self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
  49. def test_youtube_subtitles_lang(self):
  50. self.DL.params['writesubtitles'] = True
  51. self.DL.params['subtitleslangs'] = ['it']
  52. subtitles = self.getSubtitles()
  53. self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
  54. def test_youtube_allsubtitles(self):
  55. self.DL.params['writesubtitles'] = True
  56. self.DL.params['allsubtitles'] = True
  57. subtitles = self.getSubtitles()
  58. self.assertEqual(len(subtitles.keys()), 13)
  59. def test_youtube_subtitles_sbv_format(self):
  60. self.DL.params['writesubtitles'] = True
  61. self.DL.params['subtitlesformat'] = 'sbv'
  62. subtitles = self.getSubtitles()
  63. self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
  64. def test_youtube_subtitles_vtt_format(self):
  65. self.DL.params['writesubtitles'] = True
  66. self.DL.params['subtitlesformat'] = 'vtt'
  67. subtitles = self.getSubtitles()
  68. self.assertEqual(md5(subtitles['en']), '3cb210999d3e021bd6c7f0ea751eab06')
  69. def test_youtube_list_subtitles(self):
  70. self.DL.expect_warning('Video doesn\'t have automatic captions')
  71. self.DL.params['listsubtitles'] = True
  72. info_dict = self.getInfoDict()
  73. self.assertEqual(info_dict, None)
  74. def test_youtube_automatic_captions(self):
  75. self.url = '8YoUxe5ncPo'
  76. self.DL.params['writeautomaticsub'] = True
  77. self.DL.params['subtitleslangs'] = ['it']
  78. subtitles = self.getSubtitles()
  79. self.assertTrue(subtitles['it'] is not None)
  80. def test_youtube_translated_subtitles(self):
  81. # This video has a subtitles track, which can be translated
  82. self.url = 'Ky9eprVWzlI'
  83. self.DL.params['writeautomaticsub'] = True
  84. self.DL.params['subtitleslangs'] = ['it']
  85. subtitles = self.getSubtitles()
  86. self.assertTrue(subtitles['it'] is not None)
  87. def test_youtube_nosubtitles(self):
  88. self.DL.expect_warning('video doesn\'t have subtitles')
  89. self.url = 'n5BB19UTcdA'
  90. self.DL.params['writesubtitles'] = True
  91. self.DL.params['allsubtitles'] = True
  92. subtitles = self.getSubtitles()
  93. self.assertEqual(len(subtitles), 0)
  94. def test_youtube_multiple_langs(self):
  95. self.url = 'QRS8MkLhQmM'
  96. self.DL.params['writesubtitles'] = True
  97. langs = ['it', 'fr', 'de']
  98. self.DL.params['subtitleslangs'] = langs
  99. subtitles = self.getSubtitles()
  100. for lang in langs:
  101. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  102. class TestDailymotionSubtitles(BaseTestSubtitles):
  103. url = 'http://www.dailymotion.com/video/xczg00'
  104. IE = DailymotionIE
  105. def test_no_writesubtitles(self):
  106. subtitles = self.getSubtitles()
  107. self.assertEqual(subtitles, None)
  108. def test_subtitles(self):
  109. self.DL.params['writesubtitles'] = True
  110. subtitles = self.getSubtitles()
  111. self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
  112. def test_subtitles_lang(self):
  113. self.DL.params['writesubtitles'] = True
  114. self.DL.params['subtitleslangs'] = ['fr']
  115. subtitles = self.getSubtitles()
  116. self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
  117. def test_allsubtitles(self):
  118. self.DL.params['writesubtitles'] = True
  119. self.DL.params['allsubtitles'] = True
  120. subtitles = self.getSubtitles()
  121. self.assertTrue(len(subtitles.keys()) >= 6)
  122. def test_list_subtitles(self):
  123. self.DL.expect_warning('Automatic Captions not supported by this server')
  124. self.DL.params['listsubtitles'] = True
  125. info_dict = self.getInfoDict()
  126. self.assertEqual(info_dict, None)
  127. def test_automatic_captions(self):
  128. self.DL.expect_warning('Automatic Captions not supported by this server')
  129. self.DL.params['writeautomaticsub'] = True
  130. self.DL.params['subtitleslang'] = ['en']
  131. subtitles = self.getSubtitles()
  132. self.assertTrue(len(subtitles.keys()) == 0)
  133. def test_nosubtitles(self):
  134. self.DL.expect_warning('video doesn\'t have subtitles')
  135. self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
  136. self.DL.params['writesubtitles'] = True
  137. self.DL.params['allsubtitles'] = True
  138. subtitles = self.getSubtitles()
  139. self.assertEqual(len(subtitles), 0)
  140. def test_multiple_langs(self):
  141. self.DL.params['writesubtitles'] = True
  142. langs = ['es', 'fr', 'de']
  143. self.DL.params['subtitleslangs'] = langs
  144. subtitles = self.getSubtitles()
  145. for lang in langs:
  146. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  147. class TestTedSubtitles(BaseTestSubtitles):
  148. url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
  149. IE = TEDIE
  150. def test_no_writesubtitles(self):
  151. subtitles = self.getSubtitles()
  152. self.assertFalse(subtitles)
  153. def test_subtitles(self):
  154. self.DL.params['writesubtitles'] = True
  155. subtitles = self.getSubtitles()
  156. self.assertEqual(md5(subtitles['en']), '4262c1665ff928a2dada178f62cb8d14')
  157. def test_subtitles_lang(self):
  158. self.DL.params['writesubtitles'] = True
  159. self.DL.params['subtitleslangs'] = ['fr']
  160. subtitles = self.getSubtitles()
  161. self.assertEqual(md5(subtitles['fr']), '66a63f7f42c97a50f8c0e90bc7797bb5')
  162. def test_allsubtitles(self):
  163. self.DL.params['writesubtitles'] = True
  164. self.DL.params['allsubtitles'] = True
  165. subtitles = self.getSubtitles()
  166. self.assertTrue(len(subtitles.keys()) >= 28)
  167. def test_list_subtitles(self):
  168. self.DL.params['listsubtitles'] = True
  169. info_dict = self.getInfoDict()
  170. self.assertEqual(info_dict, None)
  171. def test_multiple_langs(self):
  172. self.DL.params['writesubtitles'] = True
  173. langs = ['es', 'fr', 'de']
  174. self.DL.params['subtitleslangs'] = langs
  175. subtitles = self.getSubtitles()
  176. for lang in langs:
  177. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  178. class TestBlipTVSubtitles(BaseTestSubtitles):
  179. url = 'http://blip.tv/a/a-6603250'
  180. IE = BlipTVIE
  181. def test_list_subtitles(self):
  182. self.DL.expect_warning('Automatic Captions not supported by this server')
  183. self.DL.params['listsubtitles'] = True
  184. info_dict = self.getInfoDict()
  185. self.assertEqual(info_dict, None)
  186. def test_allsubtitles(self):
  187. self.DL.expect_warning('Automatic Captions not supported by this server')
  188. self.DL.params['writesubtitles'] = True
  189. self.DL.params['allsubtitles'] = True
  190. subtitles = self.getSubtitles()
  191. self.assertEqual(set(subtitles.keys()), set(['en']))
  192. self.assertEqual(md5(subtitles['en']), '5b75c300af65fe4476dff79478bb93e4')
  193. class TestVimeoSubtitles(BaseTestSubtitles):
  194. url = 'http://vimeo.com/76979871'
  195. IE = VimeoIE
  196. def test_no_writesubtitles(self):
  197. subtitles = self.getSubtitles()
  198. self.assertEqual(subtitles, None)
  199. def test_subtitles(self):
  200. self.DL.params['writesubtitles'] = True
  201. subtitles = self.getSubtitles()
  202. self.assertEqual(md5(subtitles['en']), '8062383cf4dec168fc40a088aa6d5888')
  203. def test_subtitles_lang(self):
  204. self.DL.params['writesubtitles'] = True
  205. self.DL.params['subtitleslangs'] = ['fr']
  206. subtitles = self.getSubtitles()
  207. self.assertEqual(md5(subtitles['fr']), 'b6191146a6c5d3a452244d853fde6dc8')
  208. def test_allsubtitles(self):
  209. self.DL.params['writesubtitles'] = True
  210. self.DL.params['allsubtitles'] = True
  211. subtitles = self.getSubtitles()
  212. self.assertEqual(set(subtitles.keys()), set(['de', 'en', 'es', 'fr']))
  213. def test_list_subtitles(self):
  214. self.DL.expect_warning('Automatic Captions not supported by this server')
  215. self.DL.params['listsubtitles'] = True
  216. info_dict = self.getInfoDict()
  217. self.assertEqual(info_dict, None)
  218. def test_automatic_captions(self):
  219. self.DL.expect_warning('Automatic Captions not supported by this server')
  220. self.DL.params['writeautomaticsub'] = True
  221. self.DL.params['subtitleslang'] = ['en']
  222. subtitles = self.getSubtitles()
  223. self.assertTrue(len(subtitles.keys()) == 0)
  224. def test_nosubtitles(self):
  225. self.DL.expect_warning('video doesn\'t have subtitles')
  226. self.url = 'http://vimeo.com/56015672'
  227. self.DL.params['writesubtitles'] = True
  228. self.DL.params['allsubtitles'] = True
  229. subtitles = self.getSubtitles()
  230. self.assertEqual(len(subtitles), 0)
  231. def test_multiple_langs(self):
  232. self.DL.params['writesubtitles'] = True
  233. langs = ['es', 'fr', 'de']
  234. self.DL.params['subtitleslangs'] = langs
  235. subtitles = self.getSubtitles()
  236. for lang in langs:
  237. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  238. class TestWallaSubtitles(BaseTestSubtitles):
  239. url = 'http://vod.walla.co.il/movie/2705958/the-yes-men'
  240. IE = WallaIE
  241. def test_list_subtitles(self):
  242. self.DL.expect_warning('Automatic Captions not supported by this server')
  243. self.DL.params['listsubtitles'] = True
  244. info_dict = self.getInfoDict()
  245. self.assertEqual(info_dict, None)
  246. def test_allsubtitles(self):
  247. self.DL.expect_warning('Automatic Captions not supported by this server')
  248. self.DL.params['writesubtitles'] = True
  249. self.DL.params['allsubtitles'] = True
  250. subtitles = self.getSubtitles()
  251. self.assertEqual(set(subtitles.keys()), set(['heb']))
  252. self.assertEqual(md5(subtitles['heb']), 'e758c5d7cb982f6bef14f377ec7a3920')
  253. def test_nosubtitles(self):
  254. self.DL.expect_warning('video doesn\'t have subtitles')
  255. self.url = 'http://vod.walla.co.il/movie/2642630/one-direction-all-for-one'
  256. self.DL.params['writesubtitles'] = True
  257. self.DL.params['allsubtitles'] = True
  258. subtitles = self.getSubtitles()
  259. self.assertEqual(len(subtitles), 0)
  260. class TestCeskaTelevizeSubtitles(BaseTestSubtitles):
  261. url = 'http://www.ceskatelevize.cz/ivysilani/10600540290-u6-uzasny-svet-techniky'
  262. IE = CeskaTelevizeIE
  263. def test_list_subtitles(self):
  264. self.DL.expect_warning('Automatic Captions not supported by this server')
  265. self.DL.params['listsubtitles'] = True
  266. info_dict = self.getInfoDict()
  267. self.assertEqual(info_dict, None)
  268. def test_allsubtitles(self):
  269. self.DL.expect_warning('Automatic Captions not supported by this server')
  270. self.DL.params['writesubtitles'] = True
  271. self.DL.params['allsubtitles'] = True
  272. subtitles = self.getSubtitles()
  273. self.assertEqual(set(subtitles.keys()), set(['cs']))
  274. self.assertTrue(len(subtitles['cs']) > 20000)
  275. def test_nosubtitles(self):
  276. self.DL.expect_warning('video doesn\'t have subtitles')
  277. self.url = 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220'
  278. self.DL.params['writesubtitles'] = True
  279. self.DL.params['allsubtitles'] = True
  280. subtitles = self.getSubtitles()
  281. self.assertEqual(len(subtitles), 0)
  282. if __name__ == '__main__':
  283. unittest.main()