test_YoutubeDL.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #!/usr/bin/env python
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. from test.helper import FakeYDL
  8. from youtube_dl import YoutubeDL
  9. from youtube_dl.extractor import YoutubeIE
  10. class YDL(FakeYDL):
  11. def __init__(self, *args, **kwargs):
  12. super(YDL, self).__init__(*args, **kwargs)
  13. self.downloaded_info_dicts = []
  14. self.msgs = []
  15. def process_info(self, info_dict):
  16. self.downloaded_info_dicts.append(info_dict)
  17. def to_screen(self, msg):
  18. self.msgs.append(msg)
  19. class TestFormatSelection(unittest.TestCase):
  20. def test_prefer_free_formats(self):
  21. # Same resolution => download webm
  22. ydl = YDL()
  23. ydl.params['prefer_free_formats'] = True
  24. formats = [
  25. {u'ext': u'webm', u'height': 460},
  26. {u'ext': u'mp4', u'height': 460},
  27. ]
  28. info_dict = {u'formats': formats, u'extractor': u'test'}
  29. yie = YoutubeIE(ydl)
  30. yie._sort_formats(info_dict['formats'])
  31. ydl.process_ie_result(info_dict)
  32. downloaded = ydl.downloaded_info_dicts[0]
  33. self.assertEqual(downloaded[u'ext'], u'webm')
  34. # Different resolution => download best quality (mp4)
  35. ydl = YDL()
  36. ydl.params['prefer_free_formats'] = True
  37. formats = [
  38. {u'ext': u'webm', u'height': 720},
  39. {u'ext': u'mp4', u'height': 1080},
  40. ]
  41. info_dict[u'formats'] = formats
  42. yie = YoutubeIE(ydl)
  43. yie._sort_formats(info_dict['formats'])
  44. ydl.process_ie_result(info_dict)
  45. downloaded = ydl.downloaded_info_dicts[0]
  46. self.assertEqual(downloaded[u'ext'], u'mp4')
  47. # No prefer_free_formats => prefer mp4 and flv for greater compatibilty
  48. ydl = YDL()
  49. ydl.params['prefer_free_formats'] = False
  50. formats = [
  51. {u'ext': u'webm', u'height': 720},
  52. {u'ext': u'mp4', u'height': 720},
  53. {u'ext': u'flv', u'height': 720},
  54. ]
  55. info_dict[u'formats'] = formats
  56. yie = YoutubeIE(ydl)
  57. yie._sort_formats(info_dict['formats'])
  58. ydl.process_ie_result(info_dict)
  59. downloaded = ydl.downloaded_info_dicts[0]
  60. self.assertEqual(downloaded[u'ext'], u'mp4')
  61. ydl = YDL()
  62. ydl.params['prefer_free_formats'] = False
  63. formats = [
  64. {u'ext': u'flv', u'height': 720},
  65. {u'ext': u'webm', u'height': 720},
  66. ]
  67. info_dict[u'formats'] = formats
  68. yie = YoutubeIE(ydl)
  69. yie._sort_formats(info_dict['formats'])
  70. ydl.process_ie_result(info_dict)
  71. downloaded = ydl.downloaded_info_dicts[0]
  72. self.assertEqual(downloaded[u'ext'], u'flv')
  73. def test_format_limit(self):
  74. formats = [
  75. {u'format_id': u'meh', u'url': u'http://example.com/meh', 'preference': 1},
  76. {u'format_id': u'good', u'url': u'http://example.com/good', 'preference': 2},
  77. {u'format_id': u'great', u'url': u'http://example.com/great', 'preference': 3},
  78. {u'format_id': u'excellent', u'url': u'http://example.com/exc', 'preference': 4},
  79. ]
  80. info_dict = {
  81. u'formats': formats, u'extractor': u'test', 'id': 'testvid'}
  82. ydl = YDL()
  83. ydl.process_ie_result(info_dict)
  84. downloaded = ydl.downloaded_info_dicts[0]
  85. self.assertEqual(downloaded[u'format_id'], u'excellent')
  86. ydl = YDL({'format_limit': 'good'})
  87. assert ydl.params['format_limit'] == 'good'
  88. ydl.process_ie_result(info_dict.copy())
  89. downloaded = ydl.downloaded_info_dicts[0]
  90. self.assertEqual(downloaded[u'format_id'], u'good')
  91. ydl = YDL({'format_limit': 'great', 'format': 'all'})
  92. ydl.process_ie_result(info_dict.copy())
  93. self.assertEqual(ydl.downloaded_info_dicts[0][u'format_id'], u'meh')
  94. self.assertEqual(ydl.downloaded_info_dicts[1][u'format_id'], u'good')
  95. self.assertEqual(ydl.downloaded_info_dicts[2][u'format_id'], u'great')
  96. self.assertTrue('3' in ydl.msgs[0])
  97. ydl = YDL()
  98. ydl.params['format_limit'] = 'excellent'
  99. ydl.process_ie_result(info_dict.copy())
  100. downloaded = ydl.downloaded_info_dicts[0]
  101. self.assertEqual(downloaded[u'format_id'], u'excellent')
  102. def test_format_selection(self):
  103. formats = [
  104. {u'format_id': u'35', u'ext': u'mp4', 'preference': 1},
  105. {u'format_id': u'45', u'ext': u'webm', 'preference': 2},
  106. {u'format_id': u'47', u'ext': u'webm', 'preference': 3},
  107. {u'format_id': u'2', u'ext': u'flv', 'preference': 4},
  108. ]
  109. info_dict = {u'formats': formats, u'extractor': u'test'}
  110. ydl = YDL({'format': u'20/47'})
  111. ydl.process_ie_result(info_dict.copy())
  112. downloaded = ydl.downloaded_info_dicts[0]
  113. self.assertEqual(downloaded['format_id'], u'47')
  114. ydl = YDL({'format': u'20/71/worst'})
  115. ydl.process_ie_result(info_dict.copy())
  116. downloaded = ydl.downloaded_info_dicts[0]
  117. self.assertEqual(downloaded['format_id'], u'35')
  118. ydl = YDL()
  119. ydl.process_ie_result(info_dict.copy())
  120. downloaded = ydl.downloaded_info_dicts[0]
  121. self.assertEqual(downloaded['format_id'], u'2')
  122. ydl = YDL({'format': u'webm/mp4'})
  123. ydl.process_ie_result(info_dict.copy())
  124. downloaded = ydl.downloaded_info_dicts[0]
  125. self.assertEqual(downloaded['format_id'], u'47')
  126. ydl = YDL({'format': u'3gp/40/mp4'})
  127. ydl.process_ie_result(info_dict.copy())
  128. downloaded = ydl.downloaded_info_dicts[0]
  129. self.assertEqual(downloaded['format_id'], u'35')
  130. def test_format_selection_audio(self):
  131. formats = [
  132. {u'format_id': u'audio-low', u'ext': u'webm', 'preference': 1, 'vcodec': 'none'},
  133. {u'format_id': u'audio-mid', u'ext': u'webm', 'preference': 2, 'vcodec': 'none'},
  134. {u'format_id': u'audio-high', u'ext': u'flv', 'preference': 3, 'vcodec': 'none'},
  135. {u'format_id': u'vid', u'ext': u'mp4', 'preference': 4},
  136. ]
  137. info_dict = {u'formats': formats, u'extractor': u'test'}
  138. ydl = YDL({'format': u'bestaudio'})
  139. ydl.process_ie_result(info_dict.copy())
  140. downloaded = ydl.downloaded_info_dicts[0]
  141. self.assertEqual(downloaded['format_id'], u'audio-high')
  142. ydl = YDL({'format': u'worstaudio'})
  143. ydl.process_ie_result(info_dict.copy())
  144. downloaded = ydl.downloaded_info_dicts[0]
  145. self.assertEqual(downloaded['format_id'], u'audio-low')
  146. formats = [
  147. {u'format_id': u'vid-low', u'ext': u'mp4', 'preference': 1},
  148. {u'format_id': u'vid-high', u'ext': u'mp4', 'preference': 2},
  149. ]
  150. info_dict = {u'formats': formats, u'extractor': u'test'}
  151. ydl = YDL({'format': u'bestaudio/worstaudio/best'})
  152. ydl.process_ie_result(info_dict.copy())
  153. downloaded = ydl.downloaded_info_dicts[0]
  154. self.assertEqual(downloaded['format_id'], u'vid-high')
  155. def test_youtube_format_selection(self):
  156. order = [
  157. '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
  158. # Apple HTTP Live Streaming
  159. '96', '95', '94', '93', '92', '132', '151',
  160. # 3D
  161. '85', '84', '102', '83', '101', '82', '100',
  162. # Dash video
  163. '138', '137', '248', '136', '247', '135', '246',
  164. '245', '244', '134', '243', '133', '242', '160',
  165. # Dash audio
  166. '141', '172', '140', '139', '171',
  167. ]
  168. for f1id, f2id in zip(order, order[1:]):
  169. f1 = YoutubeIE._formats[f1id].copy()
  170. f1['format_id'] = f1id
  171. f2 = YoutubeIE._formats[f2id].copy()
  172. f2['format_id'] = f2id
  173. info_dict = {'formats': [f1, f2], 'extractor': 'youtube'}
  174. ydl = YDL()
  175. yie = YoutubeIE(ydl)
  176. yie._sort_formats(info_dict['formats'])
  177. ydl.process_ie_result(info_dict)
  178. downloaded = ydl.downloaded_info_dicts[0]
  179. self.assertEqual(downloaded['format_id'], f1id)
  180. info_dict = {'formats': [f2, f1], 'extractor': 'youtube'}
  181. ydl = YDL()
  182. yie = YoutubeIE(ydl)
  183. yie._sort_formats(info_dict['formats'])
  184. ydl.process_ie_result(info_dict)
  185. downloaded = ydl.downloaded_info_dicts[0]
  186. self.assertEqual(downloaded['format_id'], f1id)
  187. def test_add_extra_info(self):
  188. test_dict = {
  189. 'extractor': 'Foo',
  190. }
  191. extra_info = {
  192. 'extractor': 'Bar',
  193. 'playlist': 'funny videos',
  194. }
  195. YDL.add_extra_info(test_dict, extra_info)
  196. self.assertEqual(test_dict['extractor'], 'Foo')
  197. self.assertEqual(test_dict['playlist'], 'funny videos')
  198. def test_prepare_filename(self):
  199. info = {
  200. u'id': u'1234',
  201. u'ext': u'mp4',
  202. u'width': None,
  203. }
  204. def fname(templ):
  205. ydl = YoutubeDL({'outtmpl': templ})
  206. return ydl.prepare_filename(info)
  207. self.assertEqual(fname(u'%(id)s.%(ext)s'), u'1234.mp4')
  208. self.assertEqual(fname(u'%(id)s-%(width)s.%(ext)s'), u'1234-NA.mp4')
  209. # Replace missing fields with 'NA'
  210. self.assertEqual(fname(u'%(uploader_date)s-%(id)s.%(ext)s'), u'NA-1234.mp4')
  211. if __name__ == '__main__':
  212. unittest.main()