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