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.

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