]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_subtitles.py
1e4e62faae69da625867d3cf5d2047e397808727
[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 YoutubeIE,
14 DailymotionIE,
15 TEDIE,
16 )
17
18
19 class BaseTestSubtitles(unittest.TestCase):
20 url = None
21 IE = None
22 def setUp(self):
23 self.DL = FakeYDL()
24 self.ie = self.IE(self.DL)
25
26 def getInfoDict(self):
27 info_dict = self.ie.extract(self.url)
28 return info_dict
29
30 def getSubtitles(self):
31 info_dict = self.getInfoDict()
32 return info_dict['subtitles']
33
34
35 class TestYoutubeSubtitles(BaseTestSubtitles):
36 url = 'QRS8MkLhQmM'
37 IE = YoutubeIE
38
39 def test_youtube_no_writesubtitles(self):
40 self.DL.params['writesubtitles'] = False
41 subtitles = self.getSubtitles()
42 self.assertEqual(subtitles, None)
43
44 def test_youtube_subtitles(self):
45 self.DL.params['writesubtitles'] = True
46 subtitles = self.getSubtitles()
47 self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
48
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
55 def test_youtube_allsubtitles(self):
56 self.DL.params['writesubtitles'] = True
57 self.DL.params['allsubtitles'] = True
58 subtitles = self.getSubtitles()
59 self.assertEqual(len(subtitles.keys()), 13)
60
61 def test_youtube_subtitles_sbv_format(self):
62 self.DL.params['writesubtitles'] = True
63 self.DL.params['subtitlesformat'] = 'sbv'
64 subtitles = self.getSubtitles()
65 self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
66
67 def test_youtube_subtitles_vtt_format(self):
68 self.DL.params['writesubtitles'] = True
69 self.DL.params['subtitlesformat'] = 'vtt'
70 subtitles = self.getSubtitles()
71 self.assertEqual(md5(subtitles['en']), '3cb210999d3e021bd6c7f0ea751eab06')
72
73 def test_youtube_list_subtitles(self):
74 self.DL.expect_warning(u'Video doesn\'t have automatic captions')
75 self.DL.params['listsubtitles'] = True
76 info_dict = self.getInfoDict()
77 self.assertEqual(info_dict, None)
78
79 def test_youtube_automatic_captions(self):
80 self.url = '8YoUxe5ncPo'
81 self.DL.params['writeautomaticsub'] = True
82 self.DL.params['subtitleslangs'] = ['it']
83 subtitles = self.getSubtitles()
84 self.assertTrue(subtitles['it'] is not None)
85
86 def test_youtube_nosubtitles(self):
87 self.DL.expect_warning(u'video doesn\'t have subtitles')
88 self.url = 'sAjKT8FhjI8'
89 self.DL.params['writesubtitles'] = True
90 self.DL.params['allsubtitles'] = True
91 subtitles = self.getSubtitles()
92 self.assertEqual(len(subtitles), 0)
93
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, u'Subtitles for \'%s\' not extracted' % lang)
102
103
104 class TestDailymotionSubtitles(BaseTestSubtitles):
105 url = 'http://www.dailymotion.com/video/xczg00'
106 IE = DailymotionIE
107
108 def test_no_writesubtitles(self):
109 subtitles = self.getSubtitles()
110 self.assertEqual(subtitles, None)
111
112 def test_subtitles(self):
113 self.DL.params['writesubtitles'] = True
114 subtitles = self.getSubtitles()
115 self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
116
117 def test_subtitles_lang(self):
118 self.DL.params['writesubtitles'] = True
119 self.DL.params['subtitleslangs'] = ['fr']
120 subtitles = self.getSubtitles()
121 self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
122
123 def test_allsubtitles(self):
124 self.DL.params['writesubtitles'] = True
125 self.DL.params['allsubtitles'] = True
126 subtitles = self.getSubtitles()
127 self.assertEqual(len(subtitles.keys()), 5)
128
129 def test_list_subtitles(self):
130 self.DL.expect_warning(u'Automatic Captions not supported by this server')
131 self.DL.params['listsubtitles'] = True
132 info_dict = self.getInfoDict()
133 self.assertEqual(info_dict, None)
134
135 def test_automatic_captions(self):
136 self.DL.expect_warning(u'Automatic Captions not supported by this server')
137 self.DL.params['writeautomaticsub'] = True
138 self.DL.params['subtitleslang'] = ['en']
139 subtitles = self.getSubtitles()
140 self.assertTrue(len(subtitles.keys()) == 0)
141
142 def test_nosubtitles(self):
143 self.DL.expect_warning(u'video doesn\'t have subtitles')
144 self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
145 self.DL.params['writesubtitles'] = True
146 self.DL.params['allsubtitles'] = True
147 subtitles = self.getSubtitles()
148 self.assertEqual(len(subtitles), 0)
149
150 def test_multiple_langs(self):
151 self.DL.params['writesubtitles'] = True
152 langs = ['es', 'fr', 'de']
153 self.DL.params['subtitleslangs'] = langs
154 subtitles = self.getSubtitles()
155 for lang in langs:
156 self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
157
158
159 class TestTedSubtitles(BaseTestSubtitles):
160 url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
161 IE = TEDIE
162
163 def test_no_writesubtitles(self):
164 subtitles = self.getSubtitles()
165 self.assertEqual(subtitles, None)
166
167 def test_subtitles(self):
168 self.DL.params['writesubtitles'] = True
169 subtitles = self.getSubtitles()
170 self.assertEqual(md5(subtitles['en']), '4262c1665ff928a2dada178f62cb8d14')
171
172 def test_subtitles_lang(self):
173 self.DL.params['writesubtitles'] = True
174 self.DL.params['subtitleslangs'] = ['fr']
175 subtitles = self.getSubtitles()
176 self.assertEqual(md5(subtitles['fr']), '66a63f7f42c97a50f8c0e90bc7797bb5')
177
178 def test_allsubtitles(self):
179 self.DL.params['writesubtitles'] = True
180 self.DL.params['allsubtitles'] = True
181 subtitles = self.getSubtitles()
182 self.assertEqual(len(subtitles.keys()), 28)
183
184 def test_list_subtitles(self):
185 self.DL.expect_warning(u'Automatic Captions not supported by this server')
186 self.DL.params['listsubtitles'] = True
187 info_dict = self.getInfoDict()
188 self.assertEqual(info_dict, None)
189
190 def test_automatic_captions(self):
191 self.DL.expect_warning(u'Automatic Captions not supported by this server')
192 self.DL.params['writeautomaticsub'] = True
193 self.DL.params['subtitleslang'] = ['en']
194 subtitles = self.getSubtitles()
195 self.assertTrue(len(subtitles.keys()) == 0)
196
197 def test_multiple_langs(self):
198 self.DL.params['writesubtitles'] = True
199 langs = ['es', 'fr', 'de']
200 self.DL.params['subtitleslangs'] = langs
201 subtitles = self.getSubtitles()
202 for lang in langs:
203 self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
204
205 if __name__ == '__main__':
206 unittest.main()