test_YoutubeDL.py 5.6 KB

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