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.

145 lines
5.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
  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. class YDL(FakeYDL):
  9. def __init__(self, *args, **kwargs):
  10. super(YDL, self).__init__(*args, **kwargs)
  11. self.downloaded_info_dicts = []
  12. self.msgs = []
  13. def process_info(self, info_dict):
  14. self.downloaded_info_dicts.append(info_dict)
  15. def to_screen(self, msg):
  16. self.msgs.append(msg)
  17. class TestFormatSelection(unittest.TestCase):
  18. def test_prefer_free_formats(self):
  19. # Same resolution => download webm
  20. ydl = YDL()
  21. ydl.params['prefer_free_formats'] = True
  22. formats = [
  23. {u'ext': u'webm', u'height': 460},
  24. {u'ext': u'mp4', u'height': 460},
  25. ]
  26. info_dict = {u'formats': formats, u'extractor': u'test'}
  27. ydl.process_ie_result(info_dict)
  28. downloaded = ydl.downloaded_info_dicts[0]
  29. self.assertEqual(downloaded[u'ext'], u'webm')
  30. # Different resolution => download best quality (mp4)
  31. ydl = YDL()
  32. ydl.params['prefer_free_formats'] = True
  33. formats = [
  34. {u'ext': u'webm', u'height': 720},
  35. {u'ext': u'mp4', u'height': 1080},
  36. ]
  37. info_dict[u'formats'] = formats
  38. ydl.process_ie_result(info_dict)
  39. downloaded = ydl.downloaded_info_dicts[0]
  40. self.assertEqual(downloaded[u'ext'], u'mp4')
  41. # No prefer_free_formats => keep original formats order
  42. ydl = YDL()
  43. ydl.params['prefer_free_formats'] = False
  44. formats = [
  45. {u'ext': u'webm', u'height': 720},
  46. {u'ext': u'flv', u'height': 720},
  47. ]
  48. info_dict[u'formats'] = formats
  49. ydl.process_ie_result(info_dict)
  50. downloaded = ydl.downloaded_info_dicts[0]
  51. self.assertEqual(downloaded[u'ext'], u'flv')
  52. def test_format_limit(self):
  53. formats = [
  54. {u'format_id': u'meh', u'url': u'http://example.com/meh'},
  55. {u'format_id': u'good', u'url': u'http://example.com/good'},
  56. {u'format_id': u'great', u'url': u'http://example.com/great'},
  57. {u'format_id': u'excellent', u'url': u'http://example.com/exc'},
  58. ]
  59. info_dict = {
  60. u'formats': formats, u'extractor': u'test', 'id': 'testvid'}
  61. ydl = YDL()
  62. ydl.process_ie_result(info_dict)
  63. downloaded = ydl.downloaded_info_dicts[0]
  64. self.assertEqual(downloaded[u'format_id'], u'excellent')
  65. ydl = YDL({'format_limit': 'good'})
  66. assert ydl.params['format_limit'] == 'good'
  67. ydl.process_ie_result(info_dict)
  68. downloaded = ydl.downloaded_info_dicts[0]
  69. self.assertEqual(downloaded[u'format_id'], u'good')
  70. ydl = YDL({'format_limit': 'great', 'format': 'all'})
  71. ydl.process_ie_result(info_dict)
  72. self.assertEqual(ydl.downloaded_info_dicts[0][u'format_id'], u'meh')
  73. self.assertEqual(ydl.downloaded_info_dicts[1][u'format_id'], u'good')
  74. self.assertEqual(ydl.downloaded_info_dicts[2][u'format_id'], u'great')
  75. self.assertTrue('3' in ydl.msgs[0])
  76. ydl = YDL()
  77. ydl.params['format_limit'] = 'excellent'
  78. ydl.process_ie_result(info_dict)
  79. downloaded = ydl.downloaded_info_dicts[0]
  80. self.assertEqual(downloaded[u'format_id'], u'excellent')
  81. def test_format_selection(self):
  82. formats = [
  83. {u'format_id': u'35', u'ext': u'mp4'},
  84. {u'format_id': u'45', u'ext': u'webm'},
  85. {u'format_id': u'47', u'ext': u'webm'},
  86. {u'format_id': u'2', u'ext': u'flv'},
  87. ]
  88. info_dict = {u'formats': formats, u'extractor': u'test'}
  89. ydl = YDL({'format': u'20/47'})
  90. ydl.process_ie_result(info_dict)
  91. downloaded = ydl.downloaded_info_dicts[0]
  92. self.assertEqual(downloaded['format_id'], u'47')
  93. ydl = YDL({'format': u'20/71/worst'})
  94. ydl.process_ie_result(info_dict)
  95. downloaded = ydl.downloaded_info_dicts[0]
  96. self.assertEqual(downloaded['format_id'], u'35')
  97. ydl = YDL()
  98. ydl.process_ie_result(info_dict)
  99. downloaded = ydl.downloaded_info_dicts[0]
  100. self.assertEqual(downloaded['format_id'], u'2')
  101. ydl = YDL({'format': u'webm/mp4'})
  102. ydl.process_ie_result(info_dict)
  103. downloaded = ydl.downloaded_info_dicts[0]
  104. self.assertEqual(downloaded['format_id'], u'47')
  105. ydl = YDL({'format': u'3gp/40/mp4'})
  106. ydl.process_ie_result(info_dict)
  107. downloaded = ydl.downloaded_info_dicts[0]
  108. self.assertEqual(downloaded['format_id'], u'35')
  109. def test_add_extra_info(self):
  110. test_dict = {
  111. 'extractor': 'Foo',
  112. }
  113. extra_info = {
  114. 'extractor': 'Bar',
  115. 'playlist': 'funny videos',
  116. }
  117. YDL.add_extra_info(test_dict, extra_info)
  118. self.assertEqual(test_dict['extractor'], 'Foo')
  119. self.assertEqual(test_dict['playlist'], 'funny videos')
  120. if __name__ == '__main__':
  121. unittest.main()