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.

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