]> git.bitcoin.ninja Git - youtube-dl/blob - test/test_subtitles.py
[extractor/common] Recognize Indian censorship (#5021)
[youtube-dl] / test / test_subtitles.py
1 #!/usr/bin/env python
2 from __future__ import unicode_literals
3
4 # Allow direct execution
5 import os
6 import sys
7 import unittest
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
10 from test.helper import FakeYDL, md5
11
12
13 from youtube_dl.extractor import (
14     BlipTVIE,
15     YoutubeIE,
16     DailymotionIE,
17     TEDIE,
18     VimeoIE,
19     WallaIE,
20     CeskaTelevizeIE,
21 )
22
23
24 class BaseTestSubtitles(unittest.TestCase):
25     url = None
26     IE = None
27
28     def setUp(self):
29         self.DL = FakeYDL()
30         self.ie = self.IE(self.DL)
31
32     def getInfoDict(self):
33         info_dict = self.ie.extract(self.url)
34         return info_dict
35
36     def getSubtitles(self):
37         info_dict = self.getInfoDict()
38         return info_dict['subtitles']
39
40
41 class TestYoutubeSubtitles(BaseTestSubtitles):
42     url = 'QRS8MkLhQmM'
43     IE = YoutubeIE
44
45     def test_youtube_no_writesubtitles(self):
46         self.DL.params['writesubtitles'] = False
47         subtitles = self.getSubtitles()
48         self.assertEqual(subtitles, None)
49
50     def test_youtube_subtitles(self):
51         self.DL.params['writesubtitles'] = True
52         subtitles = self.getSubtitles()
53         self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
54
55     def test_youtube_subtitles_lang(self):
56         self.DL.params['writesubtitles'] = True
57         self.DL.params['subtitleslangs'] = ['it']
58         subtitles = self.getSubtitles()
59         self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
60
61     def test_youtube_allsubtitles(self):
62         self.DL.params['writesubtitles'] = True
63         self.DL.params['allsubtitles'] = True
64         subtitles = self.getSubtitles()
65         self.assertEqual(len(subtitles.keys()), 13)
66
67     def test_youtube_subtitles_sbv_format(self):
68         self.DL.params['writesubtitles'] = True
69         self.DL.params['subtitlesformat'] = 'sbv'
70         subtitles = self.getSubtitles()
71         self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
72
73     def test_youtube_subtitles_vtt_format(self):
74         self.DL.params['writesubtitles'] = True
75         self.DL.params['subtitlesformat'] = 'vtt'
76         subtitles = self.getSubtitles()
77         self.assertEqual(md5(subtitles['en']), '3cb210999d3e021bd6c7f0ea751eab06')
78
79     def test_youtube_list_subtitles(self):
80         self.DL.expect_warning('Video doesn\'t have automatic captions')
81         self.DL.params['listsubtitles'] = True
82         info_dict = self.getInfoDict()
83         self.assertEqual(info_dict, None)
84
85     def test_youtube_automatic_captions(self):
86         self.url = '8YoUxe5ncPo'
87         self.DL.params['writeautomaticsub'] = True
88         self.DL.params['subtitleslangs'] = ['it']
89         subtitles = self.getSubtitles()
90         self.assertTrue(subtitles['it'] is not None)
91
92     def test_youtube_translated_subtitles(self):
93         # This video has a subtitles track, which can be translated
94         self.url = 'Ky9eprVWzlI'
95         self.DL.params['writeautomaticsub'] = True
96         self.DL.params['subtitleslangs'] = ['it']
97         subtitles = self.getSubtitles()
98         self.assertTrue(subtitles['it'] is not None)
99
100     def test_youtube_nosubtitles(self):
101         self.DL.expect_warning('video doesn\'t have subtitles')
102         self.url = 'n5BB19UTcdA'
103         self.DL.params['writesubtitles'] = True
104         self.DL.params['allsubtitles'] = True
105         subtitles = self.getSubtitles()
106         self.assertEqual(len(subtitles), 0)
107
108     def test_youtube_multiple_langs(self):
109         self.url = 'QRS8MkLhQmM'
110         self.DL.params['writesubtitles'] = True
111         langs = ['it', 'fr', 'de']
112         self.DL.params['subtitleslangs'] = langs
113         subtitles = self.getSubtitles()
114         for lang in langs:
115             self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
116
117
118 class TestDailymotionSubtitles(BaseTestSubtitles):
119     url = 'http://www.dailymotion.com/video/xczg00'
120     IE = DailymotionIE
121
122     def test_no_writesubtitles(self):
123         subtitles = self.getSubtitles()
124         self.assertEqual(subtitles, None)
125
126     def test_subtitles(self):
127         self.DL.params['writesubtitles'] = True
128         subtitles = self.getSubtitles()
129         self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
130
131     def test_subtitles_lang(self):
132         self.DL.params['writesubtitles'] = True
133         self.DL.params['subtitleslangs'] = ['fr']
134         subtitles = self.getSubtitles()
135         self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
136
137     def test_allsubtitles(self):
138         self.DL.params['writesubtitles'] = True
139         self.DL.params['allsubtitles'] = True
140         subtitles = self.getSubtitles()
141         self.assertTrue(len(subtitles.keys()) >= 6)
142
143     def test_list_subtitles(self):
144         self.DL.expect_warning('Automatic Captions not supported by this server')
145         self.DL.params['listsubtitles'] = True
146         info_dict = self.getInfoDict()
147         self.assertEqual(info_dict, None)
148
149     def test_automatic_captions(self):
150         self.DL.expect_warning('Automatic Captions not supported by this server')
151         self.DL.params['writeautomaticsub'] = True
152         self.DL.params['subtitleslang'] = ['en']
153         subtitles = self.getSubtitles()
154         self.assertTrue(len(subtitles.keys()) == 0)
155
156     def test_nosubtitles(self):
157         self.DL.expect_warning('video doesn\'t have subtitles')
158         self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
159         self.DL.params['writesubtitles'] = True
160         self.DL.params['allsubtitles'] = True
161         subtitles = self.getSubtitles()
162         self.assertEqual(len(subtitles), 0)
163
164     def test_multiple_langs(self):
165         self.DL.params['writesubtitles'] = True
166         langs = ['es', 'fr', 'de']
167         self.DL.params['subtitleslangs'] = langs
168         subtitles = self.getSubtitles()
169         for lang in langs:
170             self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
171
172
173 class TestTedSubtitles(BaseTestSubtitles):
174     url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
175     IE = TEDIE
176
177     def test_no_writesubtitles(self):
178         subtitles = self.getSubtitles()
179         self.assertEqual(subtitles, None)
180
181     def test_subtitles(self):
182         self.DL.params['writesubtitles'] = True
183         subtitles = self.getSubtitles()
184         self.assertEqual(md5(subtitles['en']), '4262c1665ff928a2dada178f62cb8d14')
185
186     def test_subtitles_lang(self):
187         self.DL.params['writesubtitles'] = True
188         self.DL.params['subtitleslangs'] = ['fr']
189         subtitles = self.getSubtitles()
190         self.assertEqual(md5(subtitles['fr']), '66a63f7f42c97a50f8c0e90bc7797bb5')
191
192     def test_allsubtitles(self):
193         self.DL.params['writesubtitles'] = True
194         self.DL.params['allsubtitles'] = True
195         subtitles = self.getSubtitles()
196         self.assertTrue(len(subtitles.keys()) >= 28)
197
198     def test_list_subtitles(self):
199         self.DL.expect_warning('Automatic Captions not supported by this server')
200         self.DL.params['listsubtitles'] = True
201         info_dict = self.getInfoDict()
202         self.assertEqual(info_dict, None)
203
204     def test_automatic_captions(self):
205         self.DL.expect_warning('Automatic Captions not supported by this server')
206         self.DL.params['writeautomaticsub'] = True
207         self.DL.params['subtitleslang'] = ['en']
208         subtitles = self.getSubtitles()
209         self.assertTrue(len(subtitles.keys()) == 0)
210
211     def test_multiple_langs(self):
212         self.DL.params['writesubtitles'] = True
213         langs = ['es', 'fr', 'de']
214         self.DL.params['subtitleslangs'] = langs
215         subtitles = self.getSubtitles()
216         for lang in langs:
217             self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
218
219
220 class TestBlipTVSubtitles(BaseTestSubtitles):
221     url = 'http://blip.tv/a/a-6603250'
222     IE = BlipTVIE
223
224     def test_list_subtitles(self):
225         self.DL.expect_warning('Automatic Captions not supported by this server')
226         self.DL.params['listsubtitles'] = True
227         info_dict = self.getInfoDict()
228         self.assertEqual(info_dict, None)
229
230     def test_allsubtitles(self):
231         self.DL.expect_warning('Automatic Captions not supported by this server')
232         self.DL.params['writesubtitles'] = True
233         self.DL.params['allsubtitles'] = True
234         subtitles = self.getSubtitles()
235         self.assertEqual(set(subtitles.keys()), set(['en']))
236         self.assertEqual(md5(subtitles['en']), '5b75c300af65fe4476dff79478bb93e4')
237
238
239 class TestVimeoSubtitles(BaseTestSubtitles):
240     url = 'http://vimeo.com/76979871'
241     IE = VimeoIE
242
243     def test_no_writesubtitles(self):
244         subtitles = self.getSubtitles()
245         self.assertEqual(subtitles, None)
246
247     def test_subtitles(self):
248         self.DL.params['writesubtitles'] = True
249         subtitles = self.getSubtitles()
250         self.assertEqual(md5(subtitles['en']), '8062383cf4dec168fc40a088aa6d5888')
251
252     def test_subtitles_lang(self):
253         self.DL.params['writesubtitles'] = True
254         self.DL.params['subtitleslangs'] = ['fr']
255         subtitles = self.getSubtitles()
256         self.assertEqual(md5(subtitles['fr']), 'b6191146a6c5d3a452244d853fde6dc8')
257
258     def test_allsubtitles(self):
259         self.DL.params['writesubtitles'] = True
260         self.DL.params['allsubtitles'] = True
261         subtitles = self.getSubtitles()
262         self.assertEqual(set(subtitles.keys()), set(['de', 'en', 'es', 'fr']))
263
264     def test_list_subtitles(self):
265         self.DL.expect_warning('Automatic Captions not supported by this server')
266         self.DL.params['listsubtitles'] = True
267         info_dict = self.getInfoDict()
268         self.assertEqual(info_dict, None)
269
270     def test_automatic_captions(self):
271         self.DL.expect_warning('Automatic Captions not supported by this server')
272         self.DL.params['writeautomaticsub'] = True
273         self.DL.params['subtitleslang'] = ['en']
274         subtitles = self.getSubtitles()
275         self.assertTrue(len(subtitles.keys()) == 0)
276
277     def test_nosubtitles(self):
278         self.DL.expect_warning('video doesn\'t have subtitles')
279         self.url = 'http://vimeo.com/56015672'
280         self.DL.params['writesubtitles'] = True
281         self.DL.params['allsubtitles'] = True
282         subtitles = self.getSubtitles()
283         self.assertEqual(len(subtitles), 0)
284
285     def test_multiple_langs(self):
286         self.DL.params['writesubtitles'] = True
287         langs = ['es', 'fr', 'de']
288         self.DL.params['subtitleslangs'] = langs
289         subtitles = self.getSubtitles()
290         for lang in langs:
291             self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
292
293
294 class TestWallaSubtitles(BaseTestSubtitles):
295     url = 'http://vod.walla.co.il/movie/2705958/the-yes-men'
296     IE = WallaIE
297
298     def test_list_subtitles(self):
299         self.DL.expect_warning('Automatic Captions not supported by this server')
300         self.DL.params['listsubtitles'] = True
301         info_dict = self.getInfoDict()
302         self.assertEqual(info_dict, None)
303
304     def test_allsubtitles(self):
305         self.DL.expect_warning('Automatic Captions not supported by this server')
306         self.DL.params['writesubtitles'] = True
307         self.DL.params['allsubtitles'] = True
308         subtitles = self.getSubtitles()
309         self.assertEqual(set(subtitles.keys()), set(['heb']))
310         self.assertEqual(md5(subtitles['heb']), 'e758c5d7cb982f6bef14f377ec7a3920')
311
312     def test_nosubtitles(self):
313         self.DL.expect_warning('video doesn\'t have subtitles')
314         self.url = 'http://vod.walla.co.il/movie/2642630/one-direction-all-for-one'
315         self.DL.params['writesubtitles'] = True
316         self.DL.params['allsubtitles'] = True
317         subtitles = self.getSubtitles()
318         self.assertEqual(len(subtitles), 0)
319
320
321 class TestCeskaTelevizeSubtitles(BaseTestSubtitles):
322     url = 'http://www.ceskatelevize.cz/ivysilani/10600540290-u6-uzasny-svet-techniky'
323     IE = CeskaTelevizeIE
324
325     def test_list_subtitles(self):
326         self.DL.expect_warning('Automatic Captions not supported by this server')
327         self.DL.params['listsubtitles'] = True
328         info_dict = self.getInfoDict()
329         self.assertEqual(info_dict, None)
330
331     def test_allsubtitles(self):
332         self.DL.expect_warning('Automatic Captions not supported by this server')
333         self.DL.params['writesubtitles'] = True
334         self.DL.params['allsubtitles'] = True
335         subtitles = self.getSubtitles()
336         self.assertEqual(set(subtitles.keys()), set(['cs']))
337         self.assertTrue(len(subtitles['cs']) > 20000)
338
339     def test_nosubtitles(self):
340         self.DL.expect_warning('video doesn\'t have subtitles')
341         self.url = 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220'
342         self.DL.params['writesubtitles'] = True
343         self.DL.params['allsubtitles'] = True
344         subtitles = self.getSubtitles()
345         self.assertEqual(len(subtitles), 0)
346
347
348 if __name__ == '__main__':
349     unittest.main()