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.

99 lines
3.3 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'},
  55. {u'format_id': u'good'},
  56. {u'format_id': u'great'},
  57. {u'format_id': u'excellent'},
  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. if __name__ == '__main__':
  82. unittest.main()