test_YoutubeDL.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from __future__ import unicode_literals
  4. # Allow direct execution
  5. import os
  6. import sys
  7. import unittest
  8. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  9. import copy
  10. from test.helper import FakeYDL, assertRegexpMatches
  11. from youtube_dl import YoutubeDL
  12. from youtube_dl.compat import compat_str, compat_urllib_error
  13. from youtube_dl.extractor import YoutubeIE
  14. from youtube_dl.extractor.common import InfoExtractor
  15. from youtube_dl.postprocessor.common import PostProcessor
  16. from youtube_dl.utils import ExtractorError, match_filter_func
  17. TEST_URL = 'http://localhost/sample.mp4'
  18. class YDL(FakeYDL):
  19. def __init__(self, *args, **kwargs):
  20. super(YDL, self).__init__(*args, **kwargs)
  21. self.downloaded_info_dicts = []
  22. self.msgs = []
  23. def process_info(self, info_dict):
  24. self.downloaded_info_dicts.append(info_dict)
  25. def to_screen(self, msg):
  26. self.msgs.append(msg)
  27. def _make_result(formats, **kwargs):
  28. res = {
  29. 'formats': formats,
  30. 'id': 'testid',
  31. 'title': 'testttitle',
  32. 'extractor': 'testex',
  33. 'extractor_key': 'TestEx',
  34. }
  35. res.update(**kwargs)
  36. return res
  37. class TestFormatSelection(unittest.TestCase):
  38. def test_prefer_free_formats(self):
  39. # Same resolution => download webm
  40. ydl = YDL()
  41. ydl.params['prefer_free_formats'] = True
  42. formats = [
  43. {'ext': 'webm', 'height': 460, 'url': TEST_URL},
  44. {'ext': 'mp4', 'height': 460, 'url': TEST_URL},
  45. ]
  46. info_dict = _make_result(formats)
  47. yie = YoutubeIE(ydl)
  48. yie._sort_formats(info_dict['formats'])
  49. ydl.process_ie_result(info_dict)
  50. downloaded = ydl.downloaded_info_dicts[0]
  51. self.assertEqual(downloaded['ext'], 'webm')
  52. # Different resolution => download best quality (mp4)
  53. ydl = YDL()
  54. ydl.params['prefer_free_formats'] = True
  55. formats = [
  56. {'ext': 'webm', 'height': 720, 'url': TEST_URL},
  57. {'ext': 'mp4', 'height': 1080, 'url': TEST_URL},
  58. ]
  59. info_dict['formats'] = formats
  60. yie = YoutubeIE(ydl)
  61. yie._sort_formats(info_dict['formats'])
  62. ydl.process_ie_result(info_dict)
  63. downloaded = ydl.downloaded_info_dicts[0]
  64. self.assertEqual(downloaded['ext'], 'mp4')
  65. # No prefer_free_formats => prefer mp4 and flv for greater compatibility
  66. ydl = YDL()
  67. ydl.params['prefer_free_formats'] = False
  68. formats = [
  69. {'ext': 'webm', 'height': 720, 'url': TEST_URL},
  70. {'ext': 'mp4', 'height': 720, 'url': TEST_URL},
  71. {'ext': 'flv', 'height': 720, 'url': TEST_URL},
  72. ]
  73. info_dict['formats'] = formats
  74. yie = YoutubeIE(ydl)
  75. yie._sort_formats(info_dict['formats'])
  76. ydl.process_ie_result(info_dict)
  77. downloaded = ydl.downloaded_info_dicts[0]
  78. self.assertEqual(downloaded['ext'], 'mp4')
  79. ydl = YDL()
  80. ydl.params['prefer_free_formats'] = False
  81. formats = [
  82. {'ext': 'flv', 'height': 720, 'url': TEST_URL},
  83. {'ext': 'webm', 'height': 720, 'url': TEST_URL},
  84. ]
  85. info_dict['formats'] = formats
  86. yie = YoutubeIE(ydl)
  87. yie._sort_formats(info_dict['formats'])
  88. ydl.process_ie_result(info_dict)
  89. downloaded = ydl.downloaded_info_dicts[0]
  90. self.assertEqual(downloaded['ext'], 'flv')
  91. def test_format_selection(self):
  92. formats = [
  93. {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL},
  94. {'format_id': 'example-with-dashes', 'ext': 'webm', 'preference': 1, 'url': TEST_URL},
  95. {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': TEST_URL},
  96. {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': TEST_URL},
  97. {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': TEST_URL},
  98. ]
  99. info_dict = _make_result(formats)
  100. ydl = YDL({'format': '20/47'})
  101. ydl.process_ie_result(info_dict.copy())
  102. downloaded = ydl.downloaded_info_dicts[0]
  103. self.assertEqual(downloaded['format_id'], '47')
  104. ydl = YDL({'format': '20/71/worst'})
  105. ydl.process_ie_result(info_dict.copy())
  106. downloaded = ydl.downloaded_info_dicts[0]
  107. self.assertEqual(downloaded['format_id'], '35')
  108. ydl = YDL()
  109. ydl.process_ie_result(info_dict.copy())
  110. downloaded = ydl.downloaded_info_dicts[0]
  111. self.assertEqual(downloaded['format_id'], '2')
  112. ydl = YDL({'format': 'webm/mp4'})
  113. ydl.process_ie_result(info_dict.copy())
  114. downloaded = ydl.downloaded_info_dicts[0]
  115. self.assertEqual(downloaded['format_id'], '47')
  116. ydl = YDL({'format': '3gp/40/mp4'})
  117. ydl.process_ie_result(info_dict.copy())
  118. downloaded = ydl.downloaded_info_dicts[0]
  119. self.assertEqual(downloaded['format_id'], '35')
  120. ydl = YDL({'format': 'example-with-dashes'})
  121. ydl.process_ie_result(info_dict.copy())
  122. downloaded = ydl.downloaded_info_dicts[0]
  123. self.assertEqual(downloaded['format_id'], 'example-with-dashes')
  124. def test_format_selection_audio(self):
  125. formats = [
  126. {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL},
  127. {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL},
  128. {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': TEST_URL},
  129. {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': TEST_URL},
  130. ]
  131. info_dict = _make_result(formats)
  132. ydl = YDL({'format': 'bestaudio'})
  133. ydl.process_ie_result(info_dict.copy())
  134. downloaded = ydl.downloaded_info_dicts[0]
  135. self.assertEqual(downloaded['format_id'], 'audio-high')
  136. ydl = YDL({'format': 'worstaudio'})
  137. ydl.process_ie_result(info_dict.copy())
  138. downloaded = ydl.downloaded_info_dicts[0]
  139. self.assertEqual(downloaded['format_id'], 'audio-low')
  140. formats = [
  141. {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL},
  142. {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': TEST_URL},
  143. ]
  144. info_dict = _make_result(formats)
  145. ydl = YDL({'format': 'bestaudio/worstaudio/best'})
  146. ydl.process_ie_result(info_dict.copy())
  147. downloaded = ydl.downloaded_info_dicts[0]
  148. self.assertEqual(downloaded['format_id'], 'vid-high')
  149. def test_format_selection_audio_exts(self):
  150. formats = [
  151. {'format_id': 'mp3-64', 'ext': 'mp3', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
  152. {'format_id': 'ogg-64', 'ext': 'ogg', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
  153. {'format_id': 'aac-64', 'ext': 'aac', 'abr': 64, 'url': 'http://_', 'vcodec': 'none'},
  154. {'format_id': 'mp3-32', 'ext': 'mp3', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
  155. {'format_id': 'aac-32', 'ext': 'aac', 'abr': 32, 'url': 'http://_', 'vcodec': 'none'},
  156. ]
  157. info_dict = _make_result(formats)
  158. ydl = YDL({'format': 'best'})
  159. ie = YoutubeIE(ydl)
  160. ie._sort_formats(info_dict['formats'])
  161. ydl.process_ie_result(copy.deepcopy(info_dict))
  162. downloaded = ydl.downloaded_info_dicts[0]
  163. self.assertEqual(downloaded['format_id'], 'aac-64')
  164. ydl = YDL({'format': 'mp3'})
  165. ie = YoutubeIE(ydl)
  166. ie._sort_formats(info_dict['formats'])
  167. ydl.process_ie_result(copy.deepcopy(info_dict))
  168. downloaded = ydl.downloaded_info_dicts[0]
  169. self.assertEqual(downloaded['format_id'], 'mp3-64')
  170. ydl = YDL({'prefer_free_formats': True})
  171. ie = YoutubeIE(ydl)
  172. ie._sort_formats(info_dict['formats'])
  173. ydl.process_ie_result(copy.deepcopy(info_dict))
  174. downloaded = ydl.downloaded_info_dicts[0]
  175. self.assertEqual(downloaded['format_id'], 'ogg-64')
  176. def test_format_selection_video(self):
  177. formats = [
  178. {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': TEST_URL},
  179. {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': TEST_URL},
  180. {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': TEST_URL},
  181. ]
  182. info_dict = _make_result(formats)
  183. ydl = YDL({'format': 'bestvideo'})
  184. ydl.process_ie_result(info_dict.copy())
  185. downloaded = ydl.downloaded_info_dicts[0]
  186. self.assertEqual(downloaded['format_id'], 'dash-video-high')
  187. ydl = YDL({'format': 'worstvideo'})
  188. ydl.process_ie_result(info_dict.copy())
  189. downloaded = ydl.downloaded_info_dicts[0]
  190. self.assertEqual(downloaded['format_id'], 'dash-video-low')
  191. ydl = YDL({'format': 'bestvideo[format_id^=dash][format_id$=low]'})
  192. ydl.process_ie_result(info_dict.copy())
  193. downloaded = ydl.downloaded_info_dicts[0]
  194. self.assertEqual(downloaded['format_id'], 'dash-video-low')
  195. formats = [
  196. {'format_id': 'vid-vcodec-dot', 'ext': 'mp4', 'preference': 1, 'vcodec': 'avc1.123456', 'acodec': 'none', 'url': TEST_URL},
  197. ]
  198. info_dict = _make_result(formats)
  199. ydl = YDL({'format': 'bestvideo[vcodec=avc1.123456]'})
  200. ydl.process_ie_result(info_dict.copy())
  201. downloaded = ydl.downloaded_info_dicts[0]
  202. self.assertEqual(downloaded['format_id'], 'vid-vcodec-dot')
  203. def test_youtube_format_selection(self):
  204. order = [
  205. '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '17', '36', '13',
  206. # Apple HTTP Live Streaming
  207. '96', '95', '94', '93', '92', '132', '151',
  208. # 3D
  209. '85', '84', '102', '83', '101', '82', '100',
  210. # Dash video
  211. '137', '248', '136', '247', '135', '246',
  212. '245', '244', '134', '243', '133', '242', '160',
  213. # Dash audio
  214. '141', '172', '140', '171', '139',
  215. ]
  216. def format_info(f_id):
  217. info = YoutubeIE._formats[f_id].copy()
  218. # XXX: In real cases InfoExtractor._parse_mpd_formats() fills up 'acodec'
  219. # and 'vcodec', while in tests such information is incomplete since
  220. # commit a6c2c24479e5f4827ceb06f64d855329c0a6f593
  221. # test_YoutubeDL.test_youtube_format_selection is broken without
  222. # this fix
  223. if 'acodec' in info and 'vcodec' not in info:
  224. info['vcodec'] = 'none'
  225. elif 'vcodec' in info and 'acodec' not in info:
  226. info['acodec'] = 'none'
  227. info['format_id'] = f_id
  228. info['url'] = 'url:' + f_id
  229. return info
  230. formats_order = [format_info(f_id) for f_id in order]
  231. info_dict = _make_result(list(formats_order), extractor='youtube')
  232. ydl = YDL({'format': 'bestvideo+bestaudio'})
  233. yie = YoutubeIE(ydl)
  234. yie._sort_formats(info_dict['formats'])
  235. ydl.process_ie_result(info_dict)
  236. downloaded = ydl.downloaded_info_dicts[0]
  237. self.assertEqual(downloaded['format_id'], '137+141')
  238. self.assertEqual(downloaded['ext'], 'mp4')
  239. info_dict = _make_result(list(formats_order), extractor='youtube')
  240. ydl = YDL({'format': 'bestvideo[height>=999999]+bestaudio/best'})
  241. yie = YoutubeIE(ydl)
  242. yie._sort_formats(info_dict['formats'])
  243. ydl.process_ie_result(info_dict)
  244. downloaded = ydl.downloaded_info_dicts[0]
  245. self.assertEqual(downloaded['format_id'], '38')
  246. info_dict = _make_result(list(formats_order), extractor='youtube')
  247. ydl = YDL({'format': 'bestvideo/best,bestaudio'})
  248. yie = YoutubeIE(ydl)
  249. yie._sort_formats(info_dict['formats'])
  250. ydl.process_ie_result(info_dict)
  251. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  252. self.assertEqual(downloaded_ids, ['137', '141'])
  253. info_dict = _make_result(list(formats_order), extractor='youtube')
  254. ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])+bestaudio'})
  255. yie = YoutubeIE(ydl)
  256. yie._sort_formats(info_dict['formats'])
  257. ydl.process_ie_result(info_dict)
  258. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  259. self.assertEqual(downloaded_ids, ['137+141', '248+141'])
  260. info_dict = _make_result(list(formats_order), extractor='youtube')
  261. ydl = YDL({'format': '(bestvideo[ext=mp4],bestvideo[ext=webm])[height<=720]+bestaudio'})
  262. yie = YoutubeIE(ydl)
  263. yie._sort_formats(info_dict['formats'])
  264. ydl.process_ie_result(info_dict)
  265. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  266. self.assertEqual(downloaded_ids, ['136+141', '247+141'])
  267. info_dict = _make_result(list(formats_order), extractor='youtube')
  268. ydl = YDL({'format': '(bestvideo[ext=none]/bestvideo[ext=webm])+bestaudio'})
  269. yie = YoutubeIE(ydl)
  270. yie._sort_formats(info_dict['formats'])
  271. ydl.process_ie_result(info_dict)
  272. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  273. self.assertEqual(downloaded_ids, ['248+141'])
  274. for f1, f2 in zip(formats_order, formats_order[1:]):
  275. info_dict = _make_result([f1, f2], extractor='youtube')
  276. ydl = YDL({'format': 'best/bestvideo'})
  277. yie = YoutubeIE(ydl)
  278. yie._sort_formats(info_dict['formats'])
  279. ydl.process_ie_result(info_dict)
  280. downloaded = ydl.downloaded_info_dicts[0]
  281. self.assertEqual(downloaded['format_id'], f1['format_id'])
  282. info_dict = _make_result([f2, f1], extractor='youtube')
  283. ydl = YDL({'format': 'best/bestvideo'})
  284. yie = YoutubeIE(ydl)
  285. yie._sort_formats(info_dict['formats'])
  286. ydl.process_ie_result(info_dict)
  287. downloaded = ydl.downloaded_info_dicts[0]
  288. self.assertEqual(downloaded['format_id'], f1['format_id'])
  289. def test_audio_only_extractor_format_selection(self):
  290. # For extractors with incomplete formats (all formats are audio-only or
  291. # video-only) best and worst should fallback to corresponding best/worst
  292. # video-only or audio-only formats (as per
  293. # https://github.com/rg3/youtube-dl/pull/5556)
  294. formats = [
  295. {'format_id': 'low', 'ext': 'mp3', 'preference': 1, 'vcodec': 'none', 'url': TEST_URL},
  296. {'format_id': 'high', 'ext': 'mp3', 'preference': 2, 'vcodec': 'none', 'url': TEST_URL},
  297. ]
  298. info_dict = _make_result(formats)
  299. ydl = YDL({'format': 'best'})
  300. ydl.process_ie_result(info_dict.copy())
  301. downloaded = ydl.downloaded_info_dicts[0]
  302. self.assertEqual(downloaded['format_id'], 'high')
  303. ydl = YDL({'format': 'worst'})
  304. ydl.process_ie_result(info_dict.copy())
  305. downloaded = ydl.downloaded_info_dicts[0]
  306. self.assertEqual(downloaded['format_id'], 'low')
  307. def test_format_not_available(self):
  308. formats = [
  309. {'format_id': 'regular', 'ext': 'mp4', 'height': 360, 'url': TEST_URL},
  310. {'format_id': 'video', 'ext': 'mp4', 'height': 720, 'acodec': 'none', 'url': TEST_URL},
  311. ]
  312. info_dict = _make_result(formats)
  313. # This must fail since complete video-audio format does not match filter
  314. # and extractor does not provide incomplete only formats (i.e. only
  315. # video-only or audio-only).
  316. ydl = YDL({'format': 'best[height>360]'})
  317. self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy())
  318. def test_invalid_format_specs(self):
  319. def assert_syntax_error(format_spec):
  320. ydl = YDL({'format': format_spec})
  321. info_dict = _make_result([{'format_id': 'foo', 'url': TEST_URL}])
  322. self.assertRaises(SyntaxError, ydl.process_ie_result, info_dict)
  323. assert_syntax_error('bestvideo,,best')
  324. assert_syntax_error('+bestaudio')
  325. assert_syntax_error('bestvideo+')
  326. assert_syntax_error('/')
  327. def test_format_filtering(self):
  328. formats = [
  329. {'format_id': 'A', 'filesize': 500, 'width': 1000},
  330. {'format_id': 'B', 'filesize': 1000, 'width': 500},
  331. {'format_id': 'C', 'filesize': 1000, 'width': 400},
  332. {'format_id': 'D', 'filesize': 2000, 'width': 600},
  333. {'format_id': 'E', 'filesize': 3000},
  334. {'format_id': 'F'},
  335. {'format_id': 'G', 'filesize': 1000000},
  336. ]
  337. for f in formats:
  338. f['url'] = 'http://_/'
  339. f['ext'] = 'unknown'
  340. info_dict = _make_result(formats)
  341. ydl = YDL({'format': 'best[filesize<3000]'})
  342. ydl.process_ie_result(info_dict)
  343. downloaded = ydl.downloaded_info_dicts[0]
  344. self.assertEqual(downloaded['format_id'], 'D')
  345. ydl = YDL({'format': 'best[filesize<=3000]'})
  346. ydl.process_ie_result(info_dict)
  347. downloaded = ydl.downloaded_info_dicts[0]
  348. self.assertEqual(downloaded['format_id'], 'E')
  349. ydl = YDL({'format': 'best[filesize <= ? 3000]'})
  350. ydl.process_ie_result(info_dict)
  351. downloaded = ydl.downloaded_info_dicts[0]
  352. self.assertEqual(downloaded['format_id'], 'F')
  353. ydl = YDL({'format': 'best [filesize = 1000] [width>450]'})
  354. ydl.process_ie_result(info_dict)
  355. downloaded = ydl.downloaded_info_dicts[0]
  356. self.assertEqual(downloaded['format_id'], 'B')
  357. ydl = YDL({'format': 'best [filesize = 1000] [width!=450]'})
  358. ydl.process_ie_result(info_dict)
  359. downloaded = ydl.downloaded_info_dicts[0]
  360. self.assertEqual(downloaded['format_id'], 'C')
  361. ydl = YDL({'format': '[filesize>?1]'})
  362. ydl.process_ie_result(info_dict)
  363. downloaded = ydl.downloaded_info_dicts[0]
  364. self.assertEqual(downloaded['format_id'], 'G')
  365. ydl = YDL({'format': '[filesize<1M]'})
  366. ydl.process_ie_result(info_dict)
  367. downloaded = ydl.downloaded_info_dicts[0]
  368. self.assertEqual(downloaded['format_id'], 'E')
  369. ydl = YDL({'format': '[filesize<1MiB]'})
  370. ydl.process_ie_result(info_dict)
  371. downloaded = ydl.downloaded_info_dicts[0]
  372. self.assertEqual(downloaded['format_id'], 'G')
  373. ydl = YDL({'format': 'all[width>=400][width<=600]'})
  374. ydl.process_ie_result(info_dict)
  375. downloaded_ids = [info['format_id'] for info in ydl.downloaded_info_dicts]
  376. self.assertEqual(downloaded_ids, ['B', 'C', 'D'])
  377. ydl = YDL({'format': 'best[height<40]'})
  378. try:
  379. ydl.process_ie_result(info_dict)
  380. except ExtractorError:
  381. pass
  382. self.assertEqual(ydl.downloaded_info_dicts, [])
  383. def test_default_format_spec(self):
  384. ydl = YDL({'simulate': True})
  385. self.assertEqual(ydl._default_format_spec({}), 'bestvideo+bestaudio/best')
  386. ydl = YDL({'outtmpl': '-'})
  387. self.assertEqual(ydl._default_format_spec({}), 'best')
  388. ydl = YDL({})
  389. self.assertEqual(ydl._default_format_spec({}, download=False), 'bestvideo+bestaudio/best')
  390. self.assertEqual(ydl._default_format_spec({'is_live': True}), 'best')
  391. class TestYoutubeDL(unittest.TestCase):
  392. def test_subtitles(self):
  393. def s_formats(lang, autocaption=False):
  394. return [{
  395. 'ext': ext,
  396. 'url': 'http://localhost/video.%s.%s' % (lang, ext),
  397. '_auto': autocaption,
  398. } for ext in ['vtt', 'srt', 'ass']]
  399. subtitles = dict((l, s_formats(l)) for l in ['en', 'fr', 'es'])
  400. auto_captions = dict((l, s_formats(l, True)) for l in ['it', 'pt', 'es'])
  401. info_dict = {
  402. 'id': 'test',
  403. 'title': 'Test',
  404. 'url': 'http://localhost/video.mp4',
  405. 'subtitles': subtitles,
  406. 'automatic_captions': auto_captions,
  407. 'extractor': 'TEST',
  408. }
  409. def get_info(params={}):
  410. params.setdefault('simulate', True)
  411. ydl = YDL(params)
  412. ydl.report_warning = lambda *args, **kargs: None
  413. return ydl.process_video_result(info_dict, download=False)
  414. result = get_info()
  415. self.assertFalse(result.get('requested_subtitles'))
  416. self.assertEqual(result['subtitles'], subtitles)
  417. self.assertEqual(result['automatic_captions'], auto_captions)
  418. result = get_info({'writesubtitles': True})
  419. subs = result['requested_subtitles']
  420. self.assertTrue(subs)
  421. self.assertEqual(set(subs.keys()), set(['en']))
  422. self.assertTrue(subs['en'].get('data') is None)
  423. self.assertEqual(subs['en']['ext'], 'ass')
  424. result = get_info({'writesubtitles': True, 'subtitlesformat': 'foo/srt'})
  425. subs = result['requested_subtitles']
  426. self.assertEqual(subs['en']['ext'], 'srt')
  427. result = get_info({'writesubtitles': True, 'subtitleslangs': ['es', 'fr', 'it']})
  428. subs = result['requested_subtitles']
  429. self.assertTrue(subs)
  430. self.assertEqual(set(subs.keys()), set(['es', 'fr']))
  431. result = get_info({'writesubtitles': True, 'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
  432. subs = result['requested_subtitles']
  433. self.assertTrue(subs)
  434. self.assertEqual(set(subs.keys()), set(['es', 'pt']))
  435. self.assertFalse(subs['es']['_auto'])
  436. self.assertTrue(subs['pt']['_auto'])
  437. result = get_info({'writeautomaticsub': True, 'subtitleslangs': ['es', 'pt']})
  438. subs = result['requested_subtitles']
  439. self.assertTrue(subs)
  440. self.assertEqual(set(subs.keys()), set(['es', 'pt']))
  441. self.assertTrue(subs['es']['_auto'])
  442. self.assertTrue(subs['pt']['_auto'])
  443. def test_add_extra_info(self):
  444. test_dict = {
  445. 'extractor': 'Foo',
  446. }
  447. extra_info = {
  448. 'extractor': 'Bar',
  449. 'playlist': 'funny videos',
  450. }
  451. YDL.add_extra_info(test_dict, extra_info)
  452. self.assertEqual(test_dict['extractor'], 'Foo')
  453. self.assertEqual(test_dict['playlist'], 'funny videos')
  454. def test_prepare_filename(self):
  455. info = {
  456. 'id': '1234',
  457. 'ext': 'mp4',
  458. 'width': None,
  459. 'height': 1080,
  460. 'title1': '$PATH',
  461. 'title2': '%PATH%',
  462. }
  463. def fname(templ):
  464. ydl = YoutubeDL({'outtmpl': templ})
  465. return ydl.prepare_filename(info)
  466. self.assertEqual(fname('%(id)s.%(ext)s'), '1234.mp4')
  467. self.assertEqual(fname('%(id)s-%(width)s.%(ext)s'), '1234-NA.mp4')
  468. # Replace missing fields with 'NA'
  469. self.assertEqual(fname('%(uploader_date)s-%(id)s.%(ext)s'), 'NA-1234.mp4')
  470. self.assertEqual(fname('%(height)d.%(ext)s'), '1080.mp4')
  471. self.assertEqual(fname('%(height)6d.%(ext)s'), ' 1080.mp4')
  472. self.assertEqual(fname('%(height)-6d.%(ext)s'), '1080 .mp4')
  473. self.assertEqual(fname('%(height)06d.%(ext)s'), '001080.mp4')
  474. self.assertEqual(fname('%(height) 06d.%(ext)s'), ' 01080.mp4')
  475. self.assertEqual(fname('%(height) 06d.%(ext)s'), ' 01080.mp4')
  476. self.assertEqual(fname('%(height)0 6d.%(ext)s'), ' 01080.mp4')
  477. self.assertEqual(fname('%(height)0 6d.%(ext)s'), ' 01080.mp4')
  478. self.assertEqual(fname('%(height) 0 6d.%(ext)s'), ' 01080.mp4')
  479. self.assertEqual(fname('%%'), '%')
  480. self.assertEqual(fname('%%%%'), '%%')
  481. self.assertEqual(fname('%%(height)06d.%(ext)s'), '%(height)06d.mp4')
  482. self.assertEqual(fname('%(width)06d.%(ext)s'), 'NA.mp4')
  483. self.assertEqual(fname('%(width)06d.%%(ext)s'), 'NA.%(ext)s')
  484. self.assertEqual(fname('%%(width)06d.%(ext)s'), '%(width)06d.mp4')
  485. self.assertEqual(fname('Hello %(title1)s'), 'Hello $PATH')
  486. self.assertEqual(fname('Hello %(title2)s'), 'Hello %PATH%')
  487. def test_format_note(self):
  488. ydl = YoutubeDL()
  489. self.assertEqual(ydl._format_note({}), '')
  490. assertRegexpMatches(self, ydl._format_note({
  491. 'vbr': 10,
  492. }), r'^\s*10k$')
  493. assertRegexpMatches(self, ydl._format_note({
  494. 'fps': 30,
  495. }), r'^30fps$')
  496. def test_postprocessors(self):
  497. filename = 'post-processor-testfile.mp4'
  498. audiofile = filename + '.mp3'
  499. class SimplePP(PostProcessor):
  500. def run(self, info):
  501. with open(audiofile, 'wt') as f:
  502. f.write('EXAMPLE')
  503. return [info['filepath']], info
  504. def run_pp(params, PP):
  505. with open(filename, 'wt') as f:
  506. f.write('EXAMPLE')
  507. ydl = YoutubeDL(params)
  508. ydl.add_post_processor(PP())
  509. ydl.post_process(filename, {'filepath': filename})
  510. run_pp({'keepvideo': True}, SimplePP)
  511. self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
  512. self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
  513. os.unlink(filename)
  514. os.unlink(audiofile)
  515. run_pp({'keepvideo': False}, SimplePP)
  516. self.assertFalse(os.path.exists(filename), '%s exists' % filename)
  517. self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)
  518. os.unlink(audiofile)
  519. class ModifierPP(PostProcessor):
  520. def run(self, info):
  521. with open(info['filepath'], 'wt') as f:
  522. f.write('MODIFIED')
  523. return [], info
  524. run_pp({'keepvideo': False}, ModifierPP)
  525. self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)
  526. os.unlink(filename)
  527. def test_match_filter(self):
  528. class FilterYDL(YDL):
  529. def __init__(self, *args, **kwargs):
  530. super(FilterYDL, self).__init__(*args, **kwargs)
  531. self.params['simulate'] = True
  532. def process_info(self, info_dict):
  533. super(YDL, self).process_info(info_dict)
  534. def _match_entry(self, info_dict, incomplete):
  535. res = super(FilterYDL, self)._match_entry(info_dict, incomplete)
  536. if res is None:
  537. self.downloaded_info_dicts.append(info_dict)
  538. return res
  539. first = {
  540. 'id': '1',
  541. 'url': TEST_URL,
  542. 'title': 'one',
  543. 'extractor': 'TEST',
  544. 'duration': 30,
  545. 'filesize': 10 * 1024,
  546. 'playlist_id': '42',
  547. 'uploader': "變態妍字幕版 太妍 тест",
  548. 'creator': "тест ' 123 ' тест--",
  549. }
  550. second = {
  551. 'id': '2',
  552. 'url': TEST_URL,
  553. 'title': 'two',
  554. 'extractor': 'TEST',
  555. 'duration': 10,
  556. 'description': 'foo',
  557. 'filesize': 5 * 1024,
  558. 'playlist_id': '43',
  559. 'uploader': "тест 123",
  560. }
  561. videos = [first, second]
  562. def get_videos(filter_=None):
  563. ydl = FilterYDL({'match_filter': filter_})
  564. for v in videos:
  565. ydl.process_ie_result(v, download=True)
  566. return [v['id'] for v in ydl.downloaded_info_dicts]
  567. res = get_videos()
  568. self.assertEqual(res, ['1', '2'])
  569. def f(v):
  570. if v['id'] == '1':
  571. return None
  572. else:
  573. return 'Video id is not 1'
  574. res = get_videos(f)
  575. self.assertEqual(res, ['1'])
  576. f = match_filter_func('duration < 30')
  577. res = get_videos(f)
  578. self.assertEqual(res, ['2'])
  579. f = match_filter_func('description = foo')
  580. res = get_videos(f)
  581. self.assertEqual(res, ['2'])
  582. f = match_filter_func('description =? foo')
  583. res = get_videos(f)
  584. self.assertEqual(res, ['1', '2'])
  585. f = match_filter_func('filesize > 5KiB')
  586. res = get_videos(f)
  587. self.assertEqual(res, ['1'])
  588. f = match_filter_func('playlist_id = 42')
  589. res = get_videos(f)
  590. self.assertEqual(res, ['1'])
  591. f = match_filter_func('uploader = "變態妍字幕版 太妍 тест"')
  592. res = get_videos(f)
  593. self.assertEqual(res, ['1'])
  594. f = match_filter_func('uploader != "變態妍字幕版 太妍 тест"')
  595. res = get_videos(f)
  596. self.assertEqual(res, ['2'])
  597. f = match_filter_func('creator = "тест \' 123 \' тест--"')
  598. res = get_videos(f)
  599. self.assertEqual(res, ['1'])
  600. f = match_filter_func("creator = 'тест \\' 123 \\' тест--'")
  601. res = get_videos(f)
  602. self.assertEqual(res, ['1'])
  603. f = match_filter_func(r"creator = 'тест \' 123 \' тест--' & duration > 30")
  604. res = get_videos(f)
  605. self.assertEqual(res, [])
  606. def test_playlist_items_selection(self):
  607. entries = [{
  608. 'id': compat_str(i),
  609. 'title': compat_str(i),
  610. 'url': TEST_URL,
  611. } for i in range(1, 5)]
  612. playlist = {
  613. '_type': 'playlist',
  614. 'id': 'test',
  615. 'entries': entries,
  616. 'extractor': 'test:playlist',
  617. 'extractor_key': 'test:playlist',
  618. 'webpage_url': 'http://example.com',
  619. }
  620. def get_ids(params):
  621. ydl = YDL(params)
  622. # make a copy because the dictionary can be modified
  623. ydl.process_ie_result(playlist.copy())
  624. return [int(v['id']) for v in ydl.downloaded_info_dicts]
  625. result = get_ids({})
  626. self.assertEqual(result, [1, 2, 3, 4])
  627. result = get_ids({'playlistend': 10})
  628. self.assertEqual(result, [1, 2, 3, 4])
  629. result = get_ids({'playlistend': 2})
  630. self.assertEqual(result, [1, 2])
  631. result = get_ids({'playliststart': 10})
  632. self.assertEqual(result, [])
  633. result = get_ids({'playliststart': 2})
  634. self.assertEqual(result, [2, 3, 4])
  635. result = get_ids({'playlist_items': '2-4'})
  636. self.assertEqual(result, [2, 3, 4])
  637. result = get_ids({'playlist_items': '2,4'})
  638. self.assertEqual(result, [2, 4])
  639. result = get_ids({'playlist_items': '10'})
  640. self.assertEqual(result, [])
  641. def test_urlopen_no_file_protocol(self):
  642. # see https://github.com/rg3/youtube-dl/issues/8227
  643. ydl = YDL()
  644. self.assertRaises(compat_urllib_error.URLError, ydl.urlopen, 'file:///etc/passwd')
  645. def test_do_not_override_ie_key_in_url_transparent(self):
  646. ydl = YDL()
  647. class Foo1IE(InfoExtractor):
  648. _VALID_URL = r'foo1:'
  649. def _real_extract(self, url):
  650. return {
  651. '_type': 'url_transparent',
  652. 'url': 'foo2:',
  653. 'ie_key': 'Foo2',
  654. 'title': 'foo1 title',
  655. 'id': 'foo1_id',
  656. }
  657. class Foo2IE(InfoExtractor):
  658. _VALID_URL = r'foo2:'
  659. def _real_extract(self, url):
  660. return {
  661. '_type': 'url',
  662. 'url': 'foo3:',
  663. 'ie_key': 'Foo3',
  664. }
  665. class Foo3IE(InfoExtractor):
  666. _VALID_URL = r'foo3:'
  667. def _real_extract(self, url):
  668. return _make_result([{'url': TEST_URL}], title='foo3 title')
  669. ydl.add_info_extractor(Foo1IE(ydl))
  670. ydl.add_info_extractor(Foo2IE(ydl))
  671. ydl.add_info_extractor(Foo3IE(ydl))
  672. ydl.extract_info('foo1:')
  673. downloaded = ydl.downloaded_info_dicts[0]
  674. self.assertEqual(downloaded['url'], TEST_URL)
  675. self.assertEqual(downloaded['title'], 'foo1 title')
  676. self.assertEqual(downloaded['id'], 'testid')
  677. self.assertEqual(downloaded['extractor'], 'testex')
  678. self.assertEqual(downloaded['extractor_key'], 'TestEx')
  679. if __name__ == '__main__':
  680. unittest.main()