test_YoutubeDL.py 19 KB

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