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.

466 lines
18 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 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. import copy
  9. from test.helper import FakeYDL, assertRegexpMatches
  10. from youtube_dl import YoutubeDL
  11. from youtube_dl.extractor import YoutubeIE
  12. from youtube_dl.postprocessor.common import PostProcessor
  13. TEST_URL = 'http://localhost/sample.mp4'
  14. class YDL(FakeYDL):
  15. def __init__(self, *args, **kwargs):
  16. super(YDL, self).__init__(*args, **kwargs)
  17. self.downloaded_info_dicts = []
  18. self.msgs = []
  19. def process_info(self, info_dict):
  20. self.downloaded_info_dicts.append(info_dict)
  21. def to_screen(self, msg):
  22. self.msgs.append(msg)
  23. def _make_result(formats, **kwargs):
  24. res = {
  25. 'formats': formats,
  26. 'id': 'testid',
  27. 'title': 'testttitle',
  28. 'extractor': 'testex',
  29. }
  30. res.update(**kwargs)
  31. return res
  32. class TestFormatSelection(unittest.TestCase):
  33. def test_prefer_free_formats(self):
  34. # Same resolution => download webm
  35. ydl = YDL()
  36. ydl.params['prefer_free_formats'] = True
  37. formats = [
  38. {'ext': 'webm', 'height': 460, 'url': TEST_URL},
  39. {'ext': 'mp4', 'height': 460, 'url': TEST_URL},
  40. ]
  41. info_dict = _make_result(formats)
  42. yie = YoutubeIE(ydl)
  43. yie._sort_formats(info_dict['formats'])
  44. ydl.process_ie_result(info_dict)
  45. downloaded = ydl.downloaded_info_dicts[0]
  46. self.assertEqual(downloaded['ext'], 'webm')
  47. # Different resolution => download best quality (mp4)
  48. ydl = YDL()
  49. ydl.params['prefer_free_formats'] = True
  50. formats = [
  51. {'ext': 'webm', 'height': 720, 'url': TEST_URL},
  52. {'ext': 'mp4', 'height': 1080, 'url': TEST_URL},
  53. ]
  54. info_dict['formats'] = formats
  55. yie = YoutubeIE(ydl)
  56. yie._sort_formats(info_dict['formats'])
  57. ydl.process_ie_result(info_dict)
  58. downloaded = ydl.downloaded_info_dicts[0]
  59. self.assertEqual(downloaded['ext'], 'mp4')
  60. # No prefer_free_formats => prefer mp4 and flv for greater compatibility
  61. ydl = YDL()
  62. ydl.params['prefer_free_formats'] = False
  63. formats = [
  64. {'ext': 'webm', 'height': 720, 'url': TEST_URL},
  65. {'ext': 'mp4', 'height': 720, 'url': TEST_URL},
  66. {'ext': 'flv', 'height': 720, 'url': TEST_URL},
  67. ]
  68. info_dict['formats'] = formats
  69. yie = YoutubeIE(ydl)
  70. yie._sort_formats(info_dict['formats'])
  71. ydl.process_ie_result(info_dict)
  72. downloaded = ydl.downloaded_info_dicts[0]
  73. self.assertEqual(downloaded['ext'], 'mp4')
  74. ydl = YDL()
  75. ydl.params['prefer_free_formats'] = False
  76. formats = [
  77. {'ext': 'flv', 'height': 720, 'url': TEST_URL},
  78. {'ext': 'webm', 'height': 720, 'url': TEST_URL},
  79. ]
  80. info_dict['formats'] = formats
  81. yie = YoutubeIE(ydl)
  82. yie._sort_formats(info_dict['formats'])
  83. ydl.process_ie_result(info_dict)
  84. downloaded = ydl.downloaded_info_dicts[0]
  85. self.assertEqual(downloaded['ext'], 'flv')
  86. def test_format_limit(self):
  87. formats = [
  88. {'format_id': 'meh', 'url': 'http://example.com/meh', 'preference': 1},
  89. {'format_id': 'good', 'url': 'http://example.com/good', 'preference': 2},
  90. {'format_id': 'great', 'url': 'http://example.com/great', 'preference': 3},
  91. {'format_id': 'excellent', 'url': 'http://example.com/exc', 'preference': 4},
  92. ]
  93. info_dict = _make_result(formats)
  94. ydl = YDL()
  95. ydl.process_ie_result(info_dict)
  96. downloaded = ydl.downloaded_info_dicts[0]
  97. self.assertEqual(downloaded['format_id'], 'excellent')
  98. ydl = YDL({'format_limit': 'good'})
  99. assert ydl.params['format_limit'] == 'good'
  100. ydl.process_ie_result(info_dict.copy())
  101. downloaded = ydl.downloaded_info_dicts[0]
  102. self.assertEqual(downloaded['format_id'], 'good')
  103. ydl = YDL({'format_limit': 'great', 'format': 'all'})
  104. ydl.process_ie_result(info_dict.copy())
  105. self.assertEqual(ydl.downloaded_info_dicts[0]['format_id'], 'meh')
  106. self.assertEqual(ydl.downloaded_info_dicts[1]['format_id'], 'good')
  107. self.assertEqual(ydl.downloaded_info_dicts[2]['format_id'], 'great')
  108. self.assertTrue('3' in ydl.msgs[0])
  109. ydl = YDL()
  110. ydl.params['format_limit'] = 'excellent'
  111. ydl.process_ie_result(info_dict.copy())
  112. downloaded = ydl.downloaded_info_dicts[0]
  113. self.assertEqual(downloaded['format_id'], 'excellent')
  114. def test_format_selection(self):
  115. formats = [
  116. {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL},
  117. {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': TEST_URL},
  118. {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': TEST_URL},
  119. {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': TEST_URL},
  120. ]
  121. info_dict = _make_result(formats)
  122. ydl = YDL({'format': '20/47'})
  123. ydl.process_ie_result(info_dict.copy())
  124. downloaded = ydl.downloaded_info_dicts[0]
  125. self.assertEqual(downloaded['format_id'], '47')
  126. ydl = YDL({'format': '20/71/worst'})
  127. ydl.process_ie_result(info_dict.copy())
  128. downloaded = ydl.downloaded_info_dicts[0]
  129. self.assertEqual(downloaded['format_id'], '35')
  130. ydl = YDL()
  131. ydl.process_ie_result(info_dict.copy())
  132. downloaded = ydl.downloaded_info_dicts[0]
  133. self.assertEqual(downloaded['format_id'], '2')
  134. ydl = YDL({'format': 'webm/mp4'})
  135. ydl.process_ie_result(info_dict.copy())
  136. downloaded = ydl.downloaded_info_dicts[0]
  137. self.assertEqual(downloaded['format_id'], '47')
  138. ydl = YDL({'format': '3gp/40/mp4'})
  139. ydl.process_ie_result(info_dict.copy())
  140. downloaded = ydl.downloaded_info_dicts[0]
  141. self.assertEqual(downloaded['format_id'], '35')
  142. def test_format_selection_audio(self):
  143. formats = [
  144. {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL},
  145. {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL},
  146. {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': TEST_URL},
  147. {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': TEST_URL},
  148. ]
  149. info_dict = _make_result(formats)
  150. ydl = YDL({'format': 'bestaudio'})
  151. ydl.process_ie_result(info_dict.copy())
  152. downloaded = ydl.downloaded_info_dicts[0]
  153. self.assertEqual(downloaded['format_id'], 'audio-high')
  154. ydl = YDL({'format': 'worstaudio'})
  155. ydl.process_ie_result(info_dict.copy())
  156. downloaded = ydl.downloaded_info_dicts[0]
  157. self.assertEqual(downloaded['format_id'], 'audio-low')
  158. formats = [
  159. {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL},
  160. {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': TEST_URL},
  161. ]
  162. info_dict = _make_result(formats)
  163. ydl = YDL({'format': 'bestaudio/worstaudio/best'})
  164. ydl.process_ie_result(info_dict.copy())
  165. downloaded = ydl.downloaded_info_dicts[0]
  166. self.assertEqual(downloaded['format_id'], 'vid-high')
  167. def test_format_selection_audio_exts(self):
  168. formats = [
  169. {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
  170. {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
  171. {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
  172. {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
  173. {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
  174. ]
  175. info_dict = _make_result(formats)
  176. ydl = YDL({'format': 'best'})
  177. ie = YoutubeIE(ydl)
  178. ie._sort_formats(info_dict['formats'])
  179. ydl.process_ie_result(copy.deepcopy(info_dict))
  180. downloaded = ydl.downloaded_info_dicts[0]
  181. self.assertEqual(downloaded['format_id'], 'aac-64')
  182. ydl = YDL({'format': 'mp3'})
  183. ie = YoutubeIE(ydl)
  184. ie._sort_formats(info_dict['formats'])
  185. ydl.process_ie_result(copy.deepcopy(info_dict))
  186. downloaded = ydl.downloaded_info_dicts[0]
  187. self.assertEqual(downloaded['format_id'], 'mp3-64')
  188. ydl = YDL({'prefer_free_formats': True})
  189. ie = YoutubeIE(ydl)
  190. ie._sort_formats(info_dict['formats'])
  191. ydl.process_ie_result(copy.deepcopy(info_dict))
  192. downloaded = ydl.downloaded_info_dicts[0]
  193. self.assertEqual(downloaded['format_id'], 'ogg-64')
  194. def test_format_selection_video(self):
  195. formats = [
  196. {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': TEST_URL},
  197. {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': TEST_URL},
  198. {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': TEST_URL},
  199. ]
  200. info_dict = _make_result(formats)
  201. ydl = YDL({'format': 'bestvideo'})
  202. ydl.process_ie_result(info_dict.copy())
  203. downloaded = ydl.downloaded_info_dicts[0]
  204. self.assertEqual(downloaded['format_id'], 'dash-video-high')
  205. ydl = YDL({'format': 'worstvideo'})
  206. ydl.process_ie_result(info_dict.copy())
  207. downloaded = ydl.downloaded_info_dicts[0]
  208. self.assertEqual(downloaded['format_id'], 'dash-video-low')
  209. def test_youtube_format_selection(self):
  210. order = [
  211. '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
  212. # Apple HTTP Live Streaming
  213. '96', '95', '94', '93', '92', '132', '151',
  214. # 3D
  215. '85', '84', '102', '83', '101', '82', '100',
  216. # Dash video
  217. '137', '248', '136', '247', '135', '246',
  218. '245', '244', '134', '243', '133', '242', '160',
  219. # Dash audio
  220. '141', '172', '140', '171', '139',
  221. ]
  222. for f1id, f2id in zip(order, order[1:]):
  223. f1 = YoutubeIE._formats[f1id].copy()
  224. f1['format_id'] = f1id
  225. f1['url'] = 'url:' + f1id
  226. f2 = YoutubeIE._formats[f2id].copy()
  227. f2['format_id'] = f2id
  228. f2['url'] = 'url:' + f2id
  229. info_dict = _make_result([f1, f2], extractor='youtube')
  230. ydl = YDL()
  231. yie = YoutubeIE(ydl)
  232. yie._sort_formats(info_dict['formats'])
  233. ydl.process_ie_result(info_dict)
  234. downloaded = ydl.downloaded_info_dicts[0]
  235. self.assertEqual(downloaded['format_id'], f1id)
  236. info_dict = _make_result([f2, f1], extractor='youtube')
  237. ydl = YDL()
  238. yie = YoutubeIE(ydl)
  239. yie._sort_formats(info_dict['formats'])
  240. ydl.process_ie_result(info_dict)
  241. downloaded = ydl.downloaded_info_dicts[0]
  242. self.assertEqual(downloaded['format_id'], f1id)
  243. def test_format_filtering(self):
  244. formats = [
  245. {'format_id': 'A', 'filesize': 500, 'width': 1000},
  246. {'format_id': 'B', 'filesize': 1000, 'width': 500},
  247. {'format_id': 'C', 'filesize': 1000, 'width': 400},
  248. {'format_id': 'D', 'filesize': 2000, 'width': 600},
  249. {'format_id': 'E', 'filesize': 3000},
  250. {'format_id': 'F'},
  251. {'format_id': 'G', 'filesize': 1000000},
  252. ]
  253. for f in formats:
  254. f['url'] = 'http://_/'
  255. f['ext'] = 'unknown'
  256. info_dict = _make_result(formats)
  257. ydl = YDL({'format': 'best[filesize<3000]'})
  258. ydl.process_ie_result(info_dict)
  259. downloaded = ydl.downloaded_info_dicts[0]
  260. self.assertEqual(downloaded['format_id'], 'D')
  261. ydl = YDL({'format': 'best[filesize<=3000]'})
  262. ydl.process_ie_result(info_dict)
  263. downloaded = ydl.downloaded_info_dicts[0]
  264. self.assertEqual(downloaded['format_id'], 'E')
  265. ydl = YDL({'format': 'best[filesize <= ? 3000]'})
  266. ydl.process_ie_result(info_dict)
  267. downloaded = ydl.downloaded_info_dicts[0]
  268. self.assertEqual(downloaded['format_id'], 'F')
  269. ydl = YDL({'format': 'best [filesize = 1000] [width>450]'})
  270. ydl.process_ie_result(info_dict)
  271. downloaded = ydl.downloaded_info_dicts[0]
  272. self.assertEqual(downloaded['format_id'], 'B')
  273. ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'})
  274. ydl.process_ie_result(info_dict)
  275. downloaded = ydl.downloaded_info_dicts[0]
  276. self.assertEqual(downloaded['format_id'], 'C')
  277. ydl = YDL({'format': '[filesize>?1]'})
  278. ydl.process_ie_result(info_dict)
  279. downloaded = ydl.downloaded_info_dicts[0]
  280. self.assertEqual(downloaded['format_id'], 'G')
  281. ydl = YDL({'format': '[filesize<1M]'})
  282. ydl.process_ie_result(info_dict)
  283. downloaded = ydl.downloaded_info_dicts[0]
  284. self.assertEqual(downloaded['format_id'], 'E')
  285. ydl = YDL({'format': '[filesize<1MiB]'})
  286. ydl.process_ie_result(info_dict)
  287. downloaded = ydl.downloaded_info_dicts[0]
  288. self.assertEqual(downloaded['format_id'], 'G')
  289. def test_subtitles(self):
  290. def s_formats(lang, autocaption=False):
  291. return [{
  292. 'ext': ext,
  293. 'url': 'http://localhost/video.%s.%s' % (lang, ext),
  294. '_auto': autocaption,
  295. } for ext in ['vtt', 'srt', 'ass']]
  296. subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es'])
  297. auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es'])
  298. info_dict = {
  299. 'id': 'test',
  300. 'title': 'Test',
  301. 'url': 'http://localhost/video.mp4',
  302. 'subtitles': subtitles,
  303. 'automatic_captions': auto_captions,
  304. 'extractor': 'TEST',
  305. }
  306. def get_info(params={}):
  307. params.setdefault('simulate', True)
  308. ydl = YDL(params)
  309. ydl.report_warning = lambda *args, **kargs: None
  310. return ydl.process_video_result(info_dict, download=False)
  311. result = get_info()
  312. self.assertFalse(result.get('requested_subtitles'))
  313. self.assertEqual(result['subtitles'], subtitles)
  314. self.assertEqual(result['automatic_captions'], auto_captions)
  315. result = get_info({'writesubtitles': True})
  316. subs = result['requested_subtitles']
  317. self.assertTrue(subs)
  318. self.assertEqual(set(subs.keys()), set(['en']))
  319. self.assertTrue(subs['en'].get('data') is None)
  320. self.assertEqual(subs['en']['ext'], 'ass')
  321. result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'})
  322. subs = result['requested_subtitles']
  323. self.assertEqual(subs['en']['ext'], 'srt')
  324. result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']})
  325. subs = result['requested_subtitles']
  326. self.assertTrue(subs)
  327. self.assertEqual(set(subs.keys()), set(['es', 'fr']))
  328. result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
  329. subs = result['requested_subtitles']
  330. self.assertTrue(subs)
  331. self.assertEqual(set(subs.keys()), set(['es', 'pt']))
  332. self.assertFalse(subs['es']['_auto'])
  333. self.assertTrue(subs['pt']['_auto'])
  334. result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
  335. subs = result['requested_subtitles']
  336. self.assertTrue(subs)
  337. self.assertEqual(set(subs.keys()), set(['es', 'pt']))
  338. self.assertTrue(subs['es']['_auto'])
  339. self.assertTrue(subs['pt']['_auto'])
  340. def test_add_extra_info(self):
  341. test_dict = {
  342. 'extractor': 'Foo',
  343. }
  344. extra_info = {
  345. 'extractor': 'Bar',
  346. 'playlist': 'funny videos',
  347. }
  348. YDL.add_extra_info(test_dict, extra_info)
  349. self.assertEqual(test_dict['extractor'], 'Foo')
  350. self.assertEqual(test_dict['playlist'], 'funny videos')
  351. def test_prepare_filename(self):
  352. info = {
  353. 'id': '1234',
  354. 'ext': 'mp4',
  355. 'width': None,
  356. }
  357. def fname(templ):
  358. ydl = YoutubeDL({'outtmpl': templ})
  359. return ydl.prepare_filename(info)
  360. self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
  361. self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
  362. # Replace missing fields with 'NA'
  363. self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
  364. def test_format_note(self):
  365. ydl = YoutubeDL()
  366. self.assertEqual(ydl._format_note({}), '')
  367. assertRegexpMatches(self, ydl._format_note({
  368. 'vbr': 10,
  369. }), '^\s*10k$')
  370. def test_postprocessors(self):
  371. filename = 'post-processor-testfile.mp4'
  372. audiofile = filename + '.mp3'
  373. class SimplePP(PostProcessor):
  374. def run(self, info):
  375. with open(audiofile, 'wt') as f:
  376. f.write('EXAMPLE')
  377. info['filepath']
  378. return False, info
  379. def run_pp(params):
  380. with open(filename, 'wt') as f:
  381. f.write('EXAMPLE')
  382. ydl = YoutubeDL(params)
  383. ydl.add_post_processor(SimplePP())
  384. ydl.post_process(filename, {'filepath': filename})
  385. run_pp({'keepvideo': True})
  386. self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
  387. self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
  388. os.unlink(filename)
  389. os.unlink(audiofile)
  390. run_pp({'keepvideo': False})
  391. self.assertFalse(os.path.exists(filename), '%s exists' % filename)
  392. self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
  393. os.unlink(audiofile)
  394. if __name__ == '__main__':
  395. unittest.main()