]> git.bitcoin.ninja Git - youtube-dl/blob - test/test_subtitles.py
[viki] Convert to new subtitles system
[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     LyndaIE,
22     NPOIE,
23     ComedyCentralIE,
24     NRKTVIE,
25     RaiIE,
26     VikiIE,
27 )
28
29
30 class BaseTestSubtitles(unittest.TestCase):
31     url = None
32     IE = None
33
34     def setUp(self):
35         self.DL = FakeYDL()
36         self.ie = self.IE()
37         self.DL.add_info_extractor(self.ie)
38
39     def getInfoDict(self):
40         info_dict = self.DL.extract_info(self.url, download=False)
41         return info_dict
42
43     def getSubtitles(self):
44         info_dict = self.getInfoDict()
45         subtitles = info_dict['requested_subtitles']
46         if not subtitles:
47             return subtitles
48         for sub_info in subtitles.values():
49             if sub_info.get('data') is None:
50                 uf = self.DL.urlopen(sub_info['url'])
51                 sub_info['data'] = uf.read().decode('utf-8')
52         return dict((l, sub_info['data']) for l, sub_info in subtitles.items())
53
54
55 class TestYoutubeSubtitles(BaseTestSubtitles):
56     url = 'QRS8MkLhQmM'
57     IE = YoutubeIE
58
59     def test_youtube_subtitles(self):
60         self.DL.params['writesubtitles'] = True
61         subtitles = self.getSubtitles()
62         self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
63
64     def test_youtube_subtitles_lang(self):
65         self.DL.params['writesubtitles'] = True
66         self.DL.params['subtitleslangs'] = ['it']
67         subtitles = self.getSubtitles()
68         self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
69
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
76     def test_youtube_subtitles_sbv_format(self):
77         self.DL.params['writesubtitles'] = True
78         self.DL.params['subtitlesformat'] = 'sbv'
79         subtitles = self.getSubtitles()
80         self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
81
82     def test_youtube_subtitles_vtt_format(self):
83         self.DL.params['writesubtitles'] = True
84         self.DL.params['subtitlesformat'] = 'vtt'
85         subtitles = self.getSubtitles()
86         self.assertEqual(md5(subtitles['en']), '3cb210999d3e021bd6c7f0ea751eab06')
87
88     def test_youtube_list_subtitles(self):
89         self.DL.expect_warning('Video doesn\'t have automatic captions')
90         self.DL.params['listsubtitles'] = True
91         info_dict = self.getInfoDict()
92         self.assertEqual(info_dict, None)
93
94     def test_youtube_automatic_captions(self):
95         self.url = '8YoUxe5ncPo'
96         self.DL.params['writeautomaticsub'] = True
97         self.DL.params['subtitleslangs'] = ['it']
98         subtitles = self.getSubtitles()
99         self.assertTrue(subtitles['it'] is not None)
100
101     def test_youtube_translated_subtitles(self):
102         # This video has a subtitles track, which can be translated
103         self.url = 'Ky9eprVWzlI'
104         self.DL.params['writeautomaticsub'] = True
105         self.DL.params['subtitleslangs'] = ['it']
106         subtitles = self.getSubtitles()
107         self.assertTrue(subtitles['it'] is not None)
108
109     def test_youtube_nosubtitles(self):
110         self.DL.expect_warning('video doesn\'t have subtitles')
111         self.url = 'n5BB19UTcdA'
112         self.DL.params['writesubtitles'] = True
113         self.DL.params['allsubtitles'] = True
114         subtitles = self.getSubtitles()
115         self.assertEqual(len(subtitles), 0)
116
117     def test_youtube_multiple_langs(self):
118         self.url = 'QRS8MkLhQmM'
119         self.DL.params['writesubtitles'] = True
120         langs = ['it', 'fr', 'de']
121         self.DL.params['subtitleslangs'] = langs
122         subtitles = self.getSubtitles()
123         for lang in langs:
124             self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
125
126
127 class TestDailymotionSubtitles(BaseTestSubtitles):
128     url = 'http://www.dailymotion.com/video/xczg00'
129     IE = DailymotionIE
130
131     def test_subtitles(self):
132         self.DL.params['writesubtitles'] = True
133         subtitles = self.getSubtitles()
134         self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
135
136     def test_subtitles_lang(self):
137         self.DL.params['writesubtitles'] = True
138         self.DL.params['subtitleslangs'] = ['fr']
139         subtitles = self.getSubtitles()
140         self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
141
142     def test_allsubtitles(self):
143         self.DL.params['writesubtitles'] = True
144         self.DL.params['allsubtitles'] = True
145         subtitles = self.getSubtitles()
146         self.assertTrue(len(subtitles.keys()) >= 6)
147
148     def test_nosubtitles(self):
149         self.DL.expect_warning('video doesn\'t have subtitles')
150         self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
151         self.DL.params['writesubtitles'] = True
152         self.DL.params['allsubtitles'] = True
153         subtitles = self.getSubtitles()
154         self.assertEqual(len(subtitles), 0)
155
156     def test_multiple_langs(self):
157         self.DL.params['writesubtitles'] = True
158         langs = ['es', 'fr', 'de']
159         self.DL.params['subtitleslangs'] = langs
160         subtitles = self.getSubtitles()
161         for lang in langs:
162             self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
163
164
165 class TestTedSubtitles(BaseTestSubtitles):
166     url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
167     IE = TEDIE
168
169     def test_no_writesubtitles(self):
170         subtitles = self.getSubtitles()
171         self.assertFalse(subtitles)
172
173     def test_subtitles(self):
174         self.DL.params['writesubtitles'] = True
175         subtitles = self.getSubtitles()
176         self.assertEqual(md5(subtitles['en']), '4262c1665ff928a2dada178f62cb8d14')
177
178     def test_subtitles_lang(self):
179         self.DL.params['writesubtitles'] = True
180         self.DL.params['subtitleslangs'] = ['fr']
181         subtitles = self.getSubtitles()
182         self.assertEqual(md5(subtitles['fr']), '66a63f7f42c97a50f8c0e90bc7797bb5')
183
184     def test_allsubtitles(self):
185         self.DL.params['writesubtitles'] = True
186         self.DL.params['allsubtitles'] = True
187         subtitles = self.getSubtitles()
188         self.assertTrue(len(subtitles.keys()) >= 28)
189
190     def test_list_subtitles(self):
191         self.DL.params['listsubtitles'] = True
192         info_dict = self.getInfoDict()
193         self.assertEqual(info_dict, None)
194
195     def test_multiple_langs(self):
196         self.DL.params['writesubtitles'] = True
197         langs = ['es', 'fr', 'de']
198         self.DL.params['subtitleslangs'] = langs
199         subtitles = self.getSubtitles()
200         for lang in langs:
201             self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
202
203
204 class TestBlipTVSubtitles(BaseTestSubtitles):
205     url = 'http://blip.tv/a/a-6603250'
206     IE = BlipTVIE
207
208     def test_list_subtitles(self):
209         self.DL.params['listsubtitles'] = True
210         info_dict = self.getInfoDict()
211         self.assertEqual(info_dict, None)
212
213     def test_allsubtitles(self):
214         self.DL.params['writesubtitles'] = True
215         self.DL.params['allsubtitles'] = True
216         subtitles = self.getSubtitles()
217         self.assertEqual(set(subtitles.keys()), set(['en']))
218         self.assertEqual(md5(subtitles['en']), '5b75c300af65fe4476dff79478bb93e4')
219
220
221 class TestVimeoSubtitles(BaseTestSubtitles):
222     url = 'http://vimeo.com/76979871'
223     IE = VimeoIE
224
225     def test_subtitles(self):
226         self.DL.params['writesubtitles'] = True
227         subtitles = self.getSubtitles()
228         self.assertEqual(md5(subtitles['en']), '8062383cf4dec168fc40a088aa6d5888')
229
230     def test_subtitles_lang(self):
231         self.DL.params['writesubtitles'] = True
232         self.DL.params['subtitleslangs'] = ['fr']
233         subtitles = self.getSubtitles()
234         self.assertEqual(md5(subtitles['fr']), 'b6191146a6c5d3a452244d853fde6dc8')
235
236     def test_allsubtitles(self):
237         self.DL.params['writesubtitles'] = True
238         self.DL.params['allsubtitles'] = True
239         subtitles = self.getSubtitles()
240         self.assertEqual(set(subtitles.keys()), set(['de', 'en', 'es', 'fr']))
241
242     def test_nosubtitles(self):
243         self.DL.expect_warning('video doesn\'t have subtitles')
244         self.url = 'http://vimeo.com/56015672'
245         self.DL.params['writesubtitles'] = True
246         self.DL.params['allsubtitles'] = True
247         subtitles = self.getSubtitles()
248         self.assertEqual(len(subtitles), 0)
249
250     def test_multiple_langs(self):
251         self.DL.params['writesubtitles'] = True
252         langs = ['es', 'fr', 'de']
253         self.DL.params['subtitleslangs'] = langs
254         subtitles = self.getSubtitles()
255         for lang in langs:
256             self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
257
258
259 class TestWallaSubtitles(BaseTestSubtitles):
260     url = 'http://vod.walla.co.il/movie/2705958/the-yes-men'
261     IE = WallaIE
262
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
269     def test_allsubtitles(self):
270         self.DL.expect_warning('Automatic Captions not supported by this server')
271         self.DL.params['writesubtitles'] = True
272         self.DL.params['allsubtitles'] = True
273         subtitles = self.getSubtitles()
274         self.assertEqual(set(subtitles.keys()), set(['heb']))
275         self.assertEqual(md5(subtitles['heb']), 'e758c5d7cb982f6bef14f377ec7a3920')
276
277     def test_nosubtitles(self):
278         self.DL.expect_warning('video doesn\'t have subtitles')
279         self.url = 'http://vod.walla.co.il/movie/2642630/one-direction-all-for-one'
280         self.DL.params['writesubtitles'] = True
281         self.DL.params['allsubtitles'] = True
282         subtitles = self.getSubtitles()
283         self.assertEqual(len(subtitles), 0)
284
285
286 class TestCeskaTelevizeSubtitles(BaseTestSubtitles):
287     url = 'http://www.ceskatelevize.cz/ivysilani/10600540290-u6-uzasny-svet-techniky'
288     IE = CeskaTelevizeIE
289
290     def test_list_subtitles(self):
291         self.DL.expect_warning('Automatic Captions not supported by this server')
292         self.DL.params['listsubtitles'] = True
293         info_dict = self.getInfoDict()
294         self.assertEqual(info_dict, None)
295
296     def test_allsubtitles(self):
297         self.DL.expect_warning('Automatic Captions not supported by this server')
298         self.DL.params['writesubtitles'] = True
299         self.DL.params['allsubtitles'] = True
300         subtitles = self.getSubtitles()
301         self.assertEqual(set(subtitles.keys()), set(['cs']))
302         self.assertTrue(len(subtitles['cs']) > 20000)
303
304     def test_nosubtitles(self):
305         self.DL.expect_warning('video doesn\'t have subtitles')
306         self.url = 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220'
307         self.DL.params['writesubtitles'] = True
308         self.DL.params['allsubtitles'] = True
309         subtitles = self.getSubtitles()
310         self.assertEqual(len(subtitles), 0)
311
312
313 class TestLyndaSubtitles(BaseTestSubtitles):
314     url = 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html'
315     IE = LyndaIE
316
317     def test_allsubtitles(self):
318         self.DL.params['writesubtitles'] = True
319         self.DL.params['allsubtitles'] = True
320         subtitles = self.getSubtitles()
321         self.assertEqual(set(subtitles.keys()), set(['en']))
322         self.assertEqual(md5(subtitles['en']), '09bbe67222259bed60deaa26997d73a7')
323
324
325 class TestNPOSubtitles(BaseTestSubtitles):
326     url = 'http://www.npo.nl/nos-journaal/28-08-2014/POW_00722860'
327     IE = NPOIE
328
329     def test_allsubtitles(self):
330         self.DL.params['writesubtitles'] = True
331         self.DL.params['allsubtitles'] = True
332         subtitles = self.getSubtitles()
333         self.assertEqual(set(subtitles.keys()), set(['nl']))
334         self.assertEqual(md5(subtitles['nl']), 'fc6435027572b63fb4ab143abd5ad3f4')
335
336
337 class TestMTVSubtitles(BaseTestSubtitles):
338     url = 'http://www.cc.com/video-clips/kllhuv/stand-up-greg-fitzsimmons--uncensored---too-good-of-a-mother'
339     IE = ComedyCentralIE
340
341     def getInfoDict(self):
342         return super(TestMTVSubtitles, self).getInfoDict()['entries'][0]
343
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()), set(['en']))
349         self.assertEqual(md5(subtitles['en']), 'b9f6ca22a6acf597ec76f61749765e65')
350
351
352 class TestNRKSubtitles(BaseTestSubtitles):
353     url = 'http://tv.nrk.no/serie/ikke-gjoer-dette-hjemme/DMPV73000411/sesong-2/episode-1'
354     IE = NRKTVIE
355
356     def test_allsubtitles(self):
357         self.DL.params['writesubtitles'] = True
358         self.DL.params['allsubtitles'] = True
359         subtitles = self.getSubtitles()
360         self.assertEqual(set(subtitles.keys()), set(['no']))
361         self.assertEqual(md5(subtitles['no']), '1d221e6458c95c5494dcd38e6a1f129a')
362
363
364 class TestRaiSubtitles(BaseTestSubtitles):
365     url = 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-cb27157f-9dd0-4aee-b788-b1f67643a391.html'
366     IE = RaiIE
367
368     def test_allsubtitles(self):
369         self.DL.params['writesubtitles'] = True
370         self.DL.params['allsubtitles'] = True
371         subtitles = self.getSubtitles()
372         self.assertEqual(set(subtitles.keys()), set(['it']))
373         self.assertEqual(md5(subtitles['it']), 'b1d90a98755126b61e667567a1f6680a')
374
375
376 class TestVikiSubtitles(BaseTestSubtitles):
377     url = 'http://www.viki.com/videos/1060846v-punch-episode-18'
378     IE = VikiIE
379
380     def test_allsubtitles(self):
381         self.DL.params['writesubtitles'] = True
382         self.DL.params['allsubtitles'] = True
383         subtitles = self.getSubtitles()
384         self.assertEqual(set(subtitles.keys()), set(['en']))
385         self.assertEqual(md5(subtitles['en']), 'b0b781eeb45efd3f6398a925b259150b')
386
387
388 if __name__ == '__main__':
389     unittest.main()