test_YoutubeDL.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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_add_extra_info(self):
  289. test_dict = {
  290. 'extractor': 'Foo',
  291. }
  292. extra_info = {
  293. 'extractor': 'Bar',
  294. 'playlist': 'funny videos',
  295. }
  296. YDL.add_extra_info(test_dict, extra_info)
  297. self.assertEqual(test_dict['extractor'], 'Foo')
  298. self.assertEqual(test_dict['playlist'], 'funny videos')
  299. def test_prepare_filename(self):
  300. info = {
  301. 'id': '1234',
  302. 'ext': 'mp4',
  303. 'width': None,
  304. }
  305. def fname(templ):
  306. ydl = YoutubeDL({'outtmpl': templ})
  307. return ydl.prepare_filename(info)
  308. self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
  309. self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
  310. # Replace missing fields with 'NA'
  311. self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
  312. def test_format_note(self):
  313. ydl = YoutubeDL()
  314. self.assertEqual(ydl._format_note({}), '')
  315. assertRegexpMatches(self, ydl._format_note({
  316. 'vbr': 10,
  317. }), '^\s*10k$')
  318. def test_postprocessors(self):
  319. filename = 'post-processor-testfile.mp4'
  320. audiofile = filename + '.mp3'
  321. class SimplePP(PostProcessor):
  322. def run(self, info):
  323. print(audiofile)
  324. with open(audiofile, 'wt') as f:
  325. f.write('EXAMPLE')
  326. info['filepath']
  327. return False, info
  328. def run_pp(params):
  329. with open(filename, 'wt') as f:
  330. f.write('EXAMPLE')
  331. ydl = YoutubeDL(params)
  332. ydl.add_post_processor(SimplePP())
  333. ydl.post_process(filename, {'filepath': filename})
  334. run_pp({'keepvideo': True})
  335. self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
  336. self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
  337. os.unlink(filename)
  338. os.unlink(audiofile)
  339. run_pp({'keepvideo': False})
  340. self.assertFalse(os.path.exists(filename), '%s exists' % filename)
  341. self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
  342. os.unlink(audiofile)
  343. if __name__ == '__main__':
  344. unittest.main()