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.

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