]> jfr.im git - yt-dlp.git/blame - test/test_YoutubeDL.py
Add temporary _sort_formats helper function
[yt-dlp.git] / test / test_YoutubeDL.py
CommitLineData
e028d0d1
JMF
1#!/usr/bin/env python
2
5d254f77
PH
3# Allow direct execution
4import os
e028d0d1
JMF
5import sys
6import unittest
5d254f77 7sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
e028d0d1 8
5d254f77 9from test.helper import FakeYDL
26e63931 10from youtube_dl import YoutubeDL
e028d0d1 11
e028d0d1
JMF
12
13class YDL(FakeYDL):
f4d96df0
PH
14 def __init__(self, *args, **kwargs):
15 super(YDL, self).__init__(*args, **kwargs)
e028d0d1 16 self.downloaded_info_dicts = []
f4d96df0 17 self.msgs = []
5d254f77 18
e028d0d1
JMF
19 def process_info(self, info_dict):
20 self.downloaded_info_dicts.append(info_dict)
21
f4d96df0
PH
22 def to_screen(self, msg):
23 self.msgs.append(msg)
24
5d254f77 25
e028d0d1
JMF
26class TestFormatSelection(unittest.TestCase):
27 def test_prefer_free_formats(self):
28 # Same resolution => download webm
29 ydl = YDL()
30 ydl.params['prefer_free_formats'] = True
5d254f77
PH
31 formats = [
32 {u'ext': u'webm', u'height': 460},
33 {u'ext': u'mp4', u'height': 460},
34 ]
e028d0d1
JMF
35 info_dict = {u'formats': formats, u'extractor': u'test'}
36 ydl.process_ie_result(info_dict)
37 downloaded = ydl.downloaded_info_dicts[0]
38 self.assertEqual(downloaded[u'ext'], u'webm')
39
40 # Different resolution => download best quality (mp4)
41 ydl = YDL()
42 ydl.params['prefer_free_formats'] = True
5d254f77
PH
43 formats = [
44 {u'ext': u'webm', u'height': 720},
45 {u'ext': u'mp4', u'height': 1080},
46 ]
e028d0d1
JMF
47 info_dict[u'formats'] = formats
48 ydl.process_ie_result(info_dict)
49 downloaded = ydl.downloaded_info_dicts[0]
50 self.assertEqual(downloaded[u'ext'], u'mp4')
51
52 # No prefer_free_formats => keep original formats order
53 ydl = YDL()
54 ydl.params['prefer_free_formats'] = False
5d254f77
PH
55 formats = [
56 {u'ext': u'webm', u'height': 720},
57 {u'ext': u'flv', u'height': 720},
58 ]
e028d0d1
JMF
59 info_dict[u'formats'] = formats
60 ydl.process_ie_result(info_dict)
61 downloaded = ydl.downloaded_info_dicts[0]
62 self.assertEqual(downloaded[u'ext'], u'flv')
63
f4d96df0
PH
64 def test_format_limit(self):
65 formats = [
646e17a5
PH
66 {u'format_id': u'meh', u'url': u'http://example.com/meh'},
67 {u'format_id': u'good', u'url': u'http://example.com/good'},
68 {u'format_id': u'great', u'url': u'http://example.com/great'},
69 {u'format_id': u'excellent', u'url': u'http://example.com/exc'},
f4d96df0
PH
70 ]
71 info_dict = {
72 u'formats': formats, u'extractor': u'test', 'id': 'testvid'}
73
74 ydl = YDL()
75 ydl.process_ie_result(info_dict)
76 downloaded = ydl.downloaded_info_dicts[0]
77 self.assertEqual(downloaded[u'format_id'], u'excellent')
78
79 ydl = YDL({'format_limit': 'good'})
80 assert ydl.params['format_limit'] == 'good'
8e3e0322 81 ydl.process_ie_result(info_dict.copy())
f4d96df0
PH
82 downloaded = ydl.downloaded_info_dicts[0]
83 self.assertEqual(downloaded[u'format_id'], u'good')
84
85 ydl = YDL({'format_limit': 'great', 'format': 'all'})
8e3e0322 86 ydl.process_ie_result(info_dict.copy())
f4d96df0
PH
87 self.assertEqual(ydl.downloaded_info_dicts[0][u'format_id'], u'meh')
88 self.assertEqual(ydl.downloaded_info_dicts[1][u'format_id'], u'good')
89 self.assertEqual(ydl.downloaded_info_dicts[2][u'format_id'], u'great')
90 self.assertTrue('3' in ydl.msgs[0])
91
92 ydl = YDL()
93 ydl.params['format_limit'] = 'excellent'
8e3e0322 94 ydl.process_ie_result(info_dict.copy())
f4d96df0
PH
95 downloaded = ydl.downloaded_info_dicts[0]
96 self.assertEqual(downloaded[u'format_id'], u'excellent')
97
a9c58ad9
JMF
98 def test_format_selection(self):
99 formats = [
49e86983
JMF
100 {u'format_id': u'35', u'ext': u'mp4'},
101 {u'format_id': u'45', u'ext': u'webm'},
102 {u'format_id': u'47', u'ext': u'webm'},
103 {u'format_id': u'2', u'ext': u'flv'},
a9c58ad9
JMF
104 ]
105 info_dict = {u'formats': formats, u'extractor': u'test'}
106
107 ydl = YDL({'format': u'20/47'})
8e3e0322 108 ydl.process_ie_result(info_dict.copy())
a9c58ad9
JMF
109 downloaded = ydl.downloaded_info_dicts[0]
110 self.assertEqual(downloaded['format_id'], u'47')
111
112 ydl = YDL({'format': u'20/71/worst'})
8e3e0322 113 ydl.process_ie_result(info_dict.copy())
a9c58ad9
JMF
114 downloaded = ydl.downloaded_info_dicts[0]
115 self.assertEqual(downloaded['format_id'], u'35')
116
117 ydl = YDL()
8e3e0322 118 ydl.process_ie_result(info_dict.copy())
a9c58ad9
JMF
119 downloaded = ydl.downloaded_info_dicts[0]
120 self.assertEqual(downloaded['format_id'], u'2')
121
49e86983 122 ydl = YDL({'format': u'webm/mp4'})
8e3e0322 123 ydl.process_ie_result(info_dict.copy())
49e86983
JMF
124 downloaded = ydl.downloaded_info_dicts[0]
125 self.assertEqual(downloaded['format_id'], u'47')
126
127 ydl = YDL({'format': u'3gp/40/mp4'})
8e3e0322 128 ydl.process_ie_result(info_dict.copy())
49e86983
JMF
129 downloaded = ydl.downloaded_info_dicts[0]
130 self.assertEqual(downloaded['format_id'], u'35')
131
b6c45014
JMF
132 def test_add_extra_info(self):
133 test_dict = {
134 'extractor': 'Foo',
135 }
136 extra_info = {
137 'extractor': 'Bar',
138 'playlist': 'funny videos',
139 }
140 YDL.add_extra_info(test_dict, extra_info)
141 self.assertEqual(test_dict['extractor'], 'Foo')
142 self.assertEqual(test_dict['playlist'], 'funny videos')
143
26e63931
JMF
144 def test_prepare_filename(self):
145 info = {
146 u'id': u'1234',
147 u'ext': u'mp4',
148 u'width': None,
149 }
150 def fname(templ):
151 ydl = YoutubeDL({'outtmpl': templ})
152 return ydl.prepare_filename(info)
153 self.assertEqual(fname(u'%(id)s.%(ext)s'), u'1234.mp4')
154 self.assertEqual(fname(u'%(id)s-%(width)s.%(ext)s'), u'1234-NA.mp4')
155 # Replace missing fields with 'NA'
156 self.assertEqual(fname(u'%(uploader_date)s-%(id)s.%(ext)s'), u'NA-1234.mp4')
157
f4d96df0 158
e028d0d1
JMF
159if __name__ == '__main__':
160 unittest.main()