moved bitrate info to cache
[pyTivo.git] / config.py
blob3997b00ef5dde20d10dd672d1922ef7e6f27c937
1 import ConfigParser, os
2 import re
3 from ConfigParser import NoOptionError
5 BLACKLIST_169 = ('540', '649')
7 config = ConfigParser.ConfigParser()
8 p = os.path.dirname(__file__)
9 config.read(os.path.join(p, 'pyTivo.conf'))
11 def getGUID():
12 if config.has_option('Server', 'GUID'):
13 guid = config.get('Server', 'GUID')
14 else:
15 guid = '123456'
16 return guid
18 def getBeaconAddresses():
19 if config.has_option('Server', 'beacon'):
20 beacon_ips = config.get('Server', 'beacon')
21 else:
22 beacon_ips = '255.255.255.255'
23 return beacon_ips
25 def getPort():
26 return config.get('Server', 'Port')
28 def get169Setting(tsn):
29 if not tsn:
30 return True
32 if config.has_section('_tivo_' + tsn):
33 if config.has_option('_tivo_' + tsn, 'aspect169'):
34 if config.get('_tivo_' + tsn, 'aspect169').lower() == 'true':
35 return True
36 else:
37 return False
39 if tsn[:3] in BLACKLIST_169:
40 return False
42 return True
44 def getShares():
45 shares = [ (section, dict(config.items(section))) for section in config.sections() if not(section.startswith('_tivo_') or section == 'Server') ]
47 for name, data in shares:
48 if not data.get('auto_subshares', 'False').lower() == 'true':
49 continue
51 base_path = data['path']
52 for item in os.listdir(base_path):
53 item_path = os.path.join(base_path, item)
54 if not os.path.isdir(item_path):
55 continue
57 new_name = name + '/' + item
58 new_data = dict(data)
59 new_data['path'] = item_path
61 shares.append( (new_name, new_data) )
63 return shares
66 def getDebug():
67 try:
68 debug = config.get('Server', 'debug')
69 if debug.lower() == 'true':
70 return True
71 else:
72 return False
73 except NoOptionError:
74 return False
76 def getOptres():
77 try:
78 optres = config.get('Server', 'optres')
79 if optres.lower() == 'true':
80 return True
81 else:
82 return False
83 except NoOptionError:
84 return False
86 def get(section, key):
87 return config.get(section, key)
89 def getFFMPEGTemplate(tsn):
90 if tsn and config.has_section('_tivo_' + tsn):
91 try:
92 return config.get('_tivo_' + tsn, 'ffmpeg_prams', raw = True)
93 except NoOptionError:
94 pass
96 try:
97 return config.get('Server', 'ffmpeg_prams', raw = True)
98 except NoOptionError: #default
99 return '-vcodec mpeg2video -r 29.97 -b %(video_br)s -maxrate %(max_video_br)s -bufsize %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_codec)s -ab %(audio_br)s -f vob -'
101 def getHDtivos(): # tsn's of High Definition Tivo's
102 return ['648', '652']
104 def getValidWidths():
105 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
107 def getValidHeights():
108 return [1080, 720, 480] # Technically 240 is also supported
110 # Return the number in list that is nearest to x
111 # if two values are equidistant, return the larger
112 def nearest(x, list):
113 return reduce(lambda a, b: closest(x,a,b), list)
115 def closest(x,a, b):
116 if abs(x-a) < abs(x-b) or (abs(x-a) == abs(x-b)and a>b):
117 return a
118 else:
119 return b
121 def nearestTivoHeight(height):
122 return nearest(height, getValidHeights())
124 def nearestTivoWidth(width):
125 return nearest(width, getValidWidths())
127 def getTivoHeight(tsn):
128 if tsn and config.has_section('_tivo_' + tsn):
129 try:
130 height = int(config.get('_tivo_' + tsn, 'height'))
131 return nearestTivoHeight(height)
132 except NoOptionError:
133 pass
135 try:
136 height = int(config.get('Server', 'height'))
137 return nearestTivoHeight(height)
138 except NoOptionError: #default
139 return 480
141 def getTivoWidth(tsn):
142 if tsn and config.has_section('_tivo_' + tsn):
143 try:
144 width = int(config.get('_tivo_' + tsn, 'width'))
145 return nearestTivoWidth(width)
146 except NoOptionError:
147 pass
149 try:
150 width = int(config.get('Server', 'width'))
151 return nearestTivoWidth(width)
152 except NoOptionError: #default
153 return 544
155 def getAudioBR(tsn = None):
156 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
157 #compare audio_br to max_audio_br and return lowest
158 if tsn and config.has_section('_tivo_' + tsn):
159 try:
160 audiobr = int(max(int(strtod(config.get('_tivo_' + tsn, 'audio_br'))/1000), 64)/64)*64
161 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
162 except NoOptionError:
163 pass
165 try:
166 audiobr = int(max(int(strtod(config.get('Server', 'audio_br'))/1000), 64)/64)*64
167 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
168 except NoOptionError: #default to 192
169 return '192k'
171 def getAudioCodec(tsn = None):
172 #check for HD tivo and return compatible audio parameters
173 if tsn and tsn[:3] in getHDtivos():
174 return '-acodec ac3 -ar 48000'
175 else:
176 return '-acodec mp2 -ac 2 -ar 44100'
178 def getVideoBR(tsn = None):
179 if tsn and config.has_section('_tivo_' + tsn):
180 try:
181 return config.get('_tivo_' + tsn, 'video_br')
182 except NoOptionError:
183 pass
185 try:
186 return config.get('Server', 'video_br')
187 except NoOptionError: #default to 4096K
188 return '4096K'
190 def getMaxVideoBR():
191 try:
192 return str(int(strtod(config.get('Server', 'max_video_br'))/1000)) + 'k'
193 except NoOptionError: #default to 17Mi
194 return '17408k'
196 def getBuffSize():
197 try:
198 return config.get('Server', 'bufsize')
199 except NoOptionError: #default 1024k
200 return '1024k'
202 def getMaxAudioBR(tsn = None):
203 #convert to non-zero multiple of 64 for ffmpeg compatibility
204 if tsn and config.has_section('_tivo_' + tsn):
205 try:
206 return int(int(strtod(config.get('_tivo_' + tsn, 'max_audio_br'))/1000)/64)*64
207 except NoOptionError:
208 pass
210 try:
211 return int(int(strtod(config.get('Server', 'max_audio_br'))/1000)/64)*64
212 except NoOptionError:
213 if tsn and tsn[:3] in getHDtivos():
214 return int(448) #default to 448, max supported by HD TiVo's
215 else:
216 return int(384) #default to 384, max supported by mp2 audio (S2 TiVo)
219 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
220 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
221 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
222 def strtod(value):
223 prefixes = {"y":-24,"z":-21,"a":-18,"f":-15,"p":-12,"n":-9,"u":-6,"m":-3,"c":-2,"d":-1,"h":2,"k":3,"K":3,"M":6,"G":9,"T":12,"P":15,"E":18,"Z":21,"Y":24}
224 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
225 m = p.match(value)
226 if m is None:
227 raise SyntaxError('Invalid bit value syntax')
228 (coef, prefix, power, byte) = m.groups()
229 if prefix is None:
230 value = float(coef)
231 else:
232 exponent = float(prefixes[prefix])
233 if power == "i":
234 # Use powers of 2
235 value = float(coef) * pow(2.0, exponent/0.3)
236 else:
237 # Use powers of 10
238 value = float(coef) * pow(10.0, exponent)
239 if byte == "B": # B==Byte, b=bit
240 value *= 8;
241 return value