test_YoutubeDL.py 24 KB

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