test_YoutubeDL.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 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_format_filtering(self):
  261. formats = [
  262. {'format_id': 'A', 'filesize': 500, 'width': 1000},
  263. {'format_id': 'B', 'filesize': 1000, 'width': 500},
  264. {'format_id': 'C', 'filesize': 1000, 'width': 400},
  265. {'format_id': 'D', 'filesize': 2000, 'width': 600},
  266. {'format_id': 'E', 'filesize': 3000},
  267. {'format_id': 'F'},
  268. {'format_id': 'G', 'filesize': 1000000},
  269. ]
  270. for f in formats:
  271. f['url'] = 'http://_/'
  272. f['ext'] = 'unknown'
  273. info_dict = _make_result(formats)
  274. ydl = YDL({'format': 'best[filesize<3000]'})
  275. ydl.process_ie_result(info_dict)
  276. downloaded = ydl.downloaded_info_dicts[0]
  277. self.assertEqual(downloaded['format_id'], 'D')
  278. ydl = YDL({'format': 'best[filesize<=3000]'})
  279. ydl.process_ie_result(info_dict)
  280. downloaded = ydl.downloaded_info_dicts[0]
  281. self.assertEqual(downloaded['format_id'], 'E')
  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'], 'F')
  286. ydl = YDL({'format': 'best [filesize = 1000] [width>450]'})
  287. ydl.process_ie_result(info_dict)
  288. downloaded = ydl.downloaded_info_dicts[0]
  289. self.assertEqual(downloaded['format_id'], 'B')
  290. ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'})
  291. ydl.process_ie_result(info_dict)
  292. downloaded = ydl.downloaded_info_dicts[0]
  293. self.assertEqual(downloaded['format_id'], 'C')
  294. ydl = YDL({'format': '[filesize>?1]'})
  295. ydl.process_ie_result(info_dict)
  296. downloaded = ydl.downloaded_info_dicts[0]
  297. self.assertEqual(downloaded['format_id'], 'G')
  298. ydl = YDL({'format': '[filesize<1M]'})
  299. ydl.process_ie_result(info_dict)
  300. downloaded = ydl.downloaded_info_dicts[0]
  301. self.assertEqual(downloaded['format_id'], 'E')
  302. ydl = YDL({'format': '[filesize<1MiB]'})
  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': 'all[width>=400][width<=600]'})
  307. ydl.process_ie_result(info_dict)
  308. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  309. self.assertEqual(downloaded_ids, ['B', 'C', 'D'])
  310. class TestYoutubeDL(unittest.TestCase):
  311. def test_subtitles(self):
  312. def s_formats(lang, autocaption=False):
  313. return [{
  314. 'ext': ext,
  315. 'url': 'http://localhost/video.%s.%s' % (lang, ext),
  316. '_auto': autocaption,
  317. } for ext in ['vtt', 'srt', 'ass']]
  318. subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es'])
  319. auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es'])
  320. info_dict = {
  321. 'id': 'test',
  322. 'title': 'Test',
  323. 'url': 'http://localhost/video.mp4',
  324. 'subtitles': subtitles,
  325. 'automatic_captions': auto_captions,
  326. 'extractor': 'TEST',
  327. }
  328. def get_info(params={}):
  329. params.setdefault('simulate', True)
  330. ydl = YDL(params)
  331. ydl.report_warning = lambda *args, **kargs: None
  332. return ydl.process_video_result(info_dict, download=False)
  333. result = get_info()
  334. self.assertFalse(result.get('requested_subtitles'))
  335. self.assertEqual(result['subtitles'], subtitles)
  336. self.assertEqual(result['automatic_captions'], auto_captions)
  337. result = get_info({'writesubtitles': True})
  338. subs = result['requested_subtitles']
  339. self.assertTrue(subs)
  340. self.assertEqual(set(subs.keys()), set(['en']))
  341. self.assertTrue(subs['en'].get('data') is None)
  342. self.assertEqual(subs['en']['ext'], 'ass')
  343. result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'})
  344. subs = result['requested_subtitles']
  345. self.assertEqual(subs['en']['ext'], 'srt')
  346. result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']})
  347. subs = result['requested_subtitles']
  348. self.assertTrue(subs)
  349. self.assertEqual(set(subs.keys()), set(['es', 'fr']))
  350. result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
  351. subs = result['requested_subtitles']
  352. self.assertTrue(subs)
  353. self.assertEqual(set(subs.keys()), set(['es', 'pt']))
  354. self.assertFalse(subs['es']['_auto'])
  355. self.assertTrue(subs['pt']['_auto'])
  356. result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
  357. subs = result['requested_subtitles']
  358. self.assertTrue(subs)
  359. self.assertEqual(set(subs.keys()), set(['es', 'pt']))
  360. self.assertTrue(subs['es']['_auto'])
  361. self.assertTrue(subs['pt']['_auto'])
  362. def test_add_extra_info(self):
  363. test_dict = {
  364. 'extractor': 'Foo',
  365. }
  366. extra_info = {
  367. 'extractor': 'Bar',
  368. 'playlist': 'funny videos',
  369. }
  370. YDL.add_extra_info(test_dict, extra_info)
  371. self.assertEqual(test_dict['extractor'], 'Foo')
  372. self.assertEqual(test_dict['playlist'], 'funny videos')
  373. def test_prepare_filename(self):
  374. info = {
  375. 'id': '1234',
  376. 'ext': 'mp4',
  377. 'width': None,
  378. }
  379. def fname(templ):
  380. ydl = YoutubeDL({'outtmpl': templ})
  381. return ydl.prepare_filename(info)
  382. self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
  383. self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
  384. # Replace missing fields with 'NA'
  385. self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
  386. def test_format_note(self):
  387. ydl = YoutubeDL()
  388. self.assertEqual(ydl._format_note({}), '')
  389. assertRegexpMatches(self, ydl._format_note({
  390. 'vbr': 10,
  391. }), '^\s*10k$')
  392. def test_postprocessors(self):
  393. filename = 'post-processor-testfile.mp4'
  394. audiofile = filename + '.mp3'
  395. class SimplePP(PostProcessor):
  396. def run(self, info):
  397. with open(audiofile, 'wt') as f:
  398. f.write('EXAMPLE')
  399. return [info['filepath']], info
  400. def run_pp(params, PP):
  401. with open(filename, 'wt') as f:
  402. f.write('EXAMPLE')
  403. ydl = YoutubeDL(params)
  404. ydl.add_post_processor(PP())
  405. ydl.post_process(filename, {'filepath': filename})
  406. run_pp({'keepvideo': True}, SimplePP)
  407. self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
  408. self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
  409. os.unlink(filename)
  410. os.unlink(audiofile)
  411. run_pp({'keepvideo': False}, SimplePP)
  412. self.assertFalse(os.path.exists(filename), '%s exists' % filename)
  413. self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
  414. os.unlink(audiofile)
  415. class ModifierPP(PostProcessor):
  416. def run(self, info):
  417. with open(info['filepath'], 'wt') as f:
  418. f.write('MODIFIED')
  419. return [], info
  420. run_pp({'keepvideo': False}, ModifierPP)
  421. self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
  422. os.unlink(filename)
  423. def test_match_filter(self):
  424. class FilterYDL(YDL):
  425. def __init__(self, *args, **kwargs):
  426. super(FilterYDL, self).__init__(*args, **kwargs)
  427. self.params['simulate'] = True
  428. def process_info(self, info_dict):
  429. super(YDL, self).process_info(info_dict)
  430. def _match_entry(self, info_dict, incomplete):
  431. res = super(FilterYDL, self)._match_entry(info_dict, incomplete)
  432. if res is None:
  433. self.downloaded_info_dicts.append(info_dict)
  434. return res
  435. first = {
  436. 'id': '1',
  437. 'url': TEST_URL,
  438. 'title': 'one',
  439. 'extractor': 'TEST',
  440. 'duration': 30,
  441. 'filesize': 10 * 1024,
  442. }
  443. second = {
  444. 'id': '2',
  445. 'url': TEST_URL,
  446. 'title': 'two',
  447. 'extractor': 'TEST',
  448. 'duration': 10,
  449. 'description': 'foo',
  450. 'filesize': 5 * 1024,
  451. }
  452. videos = [first, second]
  453. def get_videos(filter_=None):
  454. ydl = FilterYDL({'match_filter': filter_})
  455. for v in videos:
  456. ydl.process_ie_result(v, download=True)
  457. return [v['id'] for v in ydl.downloaded_info_dicts]
  458. res = get_videos()
  459. self.assertEqual(res, ['1', '2'])
  460. def f(v):
  461. if v['id'] == '1':
  462. return None
  463. else:
  464. return 'Video id is not 1'
  465. res = get_videos(f)
  466. self.assertEqual(res, ['1'])
  467. f = match_filter_func('duration < 30')
  468. res = get_videos(f)
  469. self.assertEqual(res, ['2'])
  470. f = match_filter_func('description = foo')
  471. res = get_videos(f)
  472. self.assertEqual(res, ['2'])
  473. f = match_filter_func('description =? foo')
  474. res = get_videos(f)
  475. self.assertEqual(res, ['1', '2'])
  476. f = match_filter_func('filesize > 5KiB')
  477. res = get_videos(f)
  478. self.assertEqual(res, ['1'])
  479. def test_playlist_items_selection(self):
  480. entries = [{
  481. 'id': compat_str(i),
  482. 'title': compat_str(i),
  483. 'url': TEST_URL,
  484. } for i in range(1, 5)]
  485. playlist = {
  486. '_type': 'playlist',
  487. 'id': 'test',
  488. 'entries': entries,
  489. 'extractor': 'test:playlist',
  490. 'extractor_key': 'test:playlist',
  491. 'webpage_url': 'http://example.com',
  492. }
  493. def get_ids(params):
  494. ydl = YDL(params)
  495. # make a copy because the dictionary can be modified
  496. ydl.process_ie_result(playlist.copy())
  497. return [int(v['id']) for v in ydl.downloaded_info_dicts]
  498. result = get_ids({})
  499. self.assertEqual(result, [1, 2, 3, 4])
  500. result = get_ids({'playlistend': 10})
  501. self.assertEqual(result, [1, 2, 3, 4])
  502. result = get_ids({'playlistend': 2})
  503. self.assertEqual(result, [1, 2])
  504. result = get_ids({'playliststart': 10})
  505. self.assertEqual(result, [])
  506. result = get_ids({'playliststart': 2})
  507. self.assertEqual(result, [2, 3, 4])
  508. result = get_ids({'playlist_items': '2-4'})
  509. self.assertEqual(result, [2, 3, 4])
  510. result = get_ids({'playlist_items': '2,4'})
  511. self.assertEqual(result, [2, 4])
  512. result = get_ids({'playlist_items': '10'})
  513. self.assertEqual(result, [])
  514. if __name__ == '__main__':
  515. unittest.main()