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.

217 lines
8.0 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
  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_youtube_format_selection(self):
  131. order = [
  132. '38', '37', '46', '22', '45', '35', '44', '18', '34', '43', '6', '5', '36', '17', '13',
  133. # Apple HTTP Live Streaming
  134. '96', '95', '94', '93', '92', '132', '151',
  135. # 3D
  136. '85', '84', '102', '83', '101', '82', '100',
  137. # Dash video
  138. '138', '137', '248', '136', '247', '135', '246',
  139. '245', '244', '134', '243', '133', '242', '160',
  140. # Dash audio
  141. '141', '172', '140', '139', '171',
  142. ]
  143. for f1id, f2id in zip(order, order[1:]):
  144. f1 = YoutubeIE._formats[f1id].copy()
  145. f1['format_id'] = f1id
  146. f2 = YoutubeIE._formats[f2id].copy()
  147. f2['format_id'] = f2id
  148. info_dict = {'formats': [f1, f2], 'extractor': 'youtube'}
  149. ydl = YDL()
  150. yie = YoutubeIE(ydl)
  151. yie._sort_formats(info_dict['formats'])
  152. ydl.process_ie_result(info_dict)
  153. downloaded = ydl.downloaded_info_dicts[0]
  154. self.assertEqual(downloaded['format_id'], f1id)
  155. info_dict = {'formats': [f2, f1], 'extractor': 'youtube'}
  156. ydl = YDL()
  157. yie = YoutubeIE(ydl)
  158. yie._sort_formats(info_dict['formats'])
  159. ydl.process_ie_result(info_dict)
  160. downloaded = ydl.downloaded_info_dicts[0]
  161. self.assertEqual(downloaded['format_id'], f1id)
  162. def test_add_extra_info(self):
  163. test_dict = {
  164. 'extractor': 'Foo',
  165. }
  166. extra_info = {
  167. 'extractor': 'Bar',
  168. 'playlist': 'funny videos',
  169. }
  170. YDL.add_extra_info(test_dict, extra_info)
  171. self.assertEqual(test_dict['extractor'], 'Foo')
  172. self.assertEqual(test_dict['playlist'], 'funny videos')
  173. def test_prepare_filename(self):
  174. info = {
  175. u'id': u'1234',
  176. u'ext': u'mp4',
  177. u'width': None,
  178. }
  179. def fname(templ):
  180. ydl = YoutubeDL({'outtmpl': templ})
  181. return ydl.prepare_filename(info)
  182. self.assertEqual(fname(u'%(id)s.%(ext)s'), u'1234.mp4')
  183. self.assertEqual(fname(u'%(id)s-%(width)s.%(ext)s'), u'1234-NA.mp4')
  184. # Replace missing fields with 'NA'
  185. self.assertEqual(fname(u'%(uploader_date)s-%(id)s.%(ext)s'), u'NA-1234.mp4')
  186. if __name__ == '__main__':
  187. unittest.main()