maxaudiobr compare missing
[pyTivo.git] / config.py
blob1384e5833aa52bbeb61073189814648295fad4f5
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_file = os.path.join(p, 'pyTivo.conf')
10 config.read(config_file)
12 def reset():
13 global config
14 del config
15 config = ConfigParser.ConfigParser()
16 config.read(config_file)
18 def getGUID():
19 if config.has_option('Server', 'GUID'):
20 guid = config.get('Server', 'GUID')
21 else:
22 guid = '123456'
23 return guid
25 def getBeaconAddresses():
26 if config.has_option('Server', 'beacon'):
27 beacon_ips = config.get('Server', 'beacon')
28 else:
29 beacon_ips = '255.255.255.255'
30 return beacon_ips
32 def getPort():
33 return config.get('Server', 'Port')
35 def get169Setting(tsn):
36 if not tsn:
37 return True
39 if config.has_section('_tivo_' + tsn):
40 if config.has_option('_tivo_' + tsn, 'aspect169'):
41 try:
42 return config.getboolean('_tivo_' + tsn, 'aspect169')
43 except ValueError:
44 pass
46 if tsn[:3] in BLACKLIST_169:
47 return False
49 return True
51 def getShares(tsn=''):
52 shares = [(section, dict(config.items(section)))
53 for section in config.sections()
54 if not(section.startswith('_tivo_') or section == 'Server')]
56 if config.has_section('_tivo_' + tsn):
57 if config.has_option('_tivo_' + tsn, 'shares'):
58 # clean up leading and trailing spaces & make sure ref is valid
59 tsnshares = []
60 for x in config.get('_tivo_' + tsn, 'shares').split(','):
61 y = x.lstrip().rstrip()
62 if config.has_section(y):
63 tsnshares += [(y, dict(config.items(y)))]
64 if tsnshares:
65 shares = tsnshares
67 for name, data in shares:
68 if not data.get('auto_subshares', 'False').lower() == 'true':
69 continue
71 base_path = data['path']
72 try:
73 for item in os.listdir(base_path):
74 item_path = os.path.join(base_path, item)
75 if not os.path.isdir(item_path):
76 continue
78 new_name = name + '/' + item
79 new_data = dict(data)
80 new_data['path'] = item_path
82 shares.append((new_name, new_data))
83 except:
84 pass
86 return shares
88 def getDebug(ref):
89 if config.has_option('Server', 'debug'):
90 try:
91 return str2tuple(config.get('Server', 'debug')+',,')[ref]
92 except NoOptionError:
93 pass
94 return str2tuple('False,,')[ref]
96 def getHack83():
97 try:
98 debug = config.get('Server', 'hack83')
99 if debug.lower() == 'true':
100 return True
101 else:
102 return False
103 except NoOptionError:
104 return False
106 def getOptres(tsn = None):
107 if tsn and config.has_section('_tivo_' + tsn):
108 try:
109 return config.getboolean('_tivo_' + tsn, 'optres')
110 except NoOptionError, ValueError:
111 pass
112 try:
113 return config.getboolean('Server', 'optres')
114 except NoOptionError, ValueError:
115 return False
117 def get(section, key):
118 return config.get(section, key)
120 def getFFmpegTemplate(tsn):
121 if tsn and config.has_section('_tivo_' + tsn):
122 try:
123 return config.get('_tivo_' + tsn, 'ffmpeg_tmpl', raw=True)
124 except NoOptionError:
125 pass
126 try:
127 return config.get('Server', 'ffmpeg_tmpl', raw=True)
128 except NoOptionError: #default
129 return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
130 %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
131 %(audio_fr)s %(audio_ch)s %(audio_codec)s %(ffmpeg_pram)s %(format)s'
133 def getFFmpegPrams(tsn):
134 if tsn and config.has_section('_tivo_' + tsn):
135 try:
136 return config.get('_tivo_' + tsn, 'ffmpeg_pram', raw=True)
137 except NoOptionError:
138 pass
139 try:
140 return config.get('Server', 'ffmpeg_pram', raw=True)
141 except NoOptionError:
142 return None
144 def isHDtivo(tsn): # tsn's of High Definition Tivo's
145 return tsn != '' and tsn[:3] in ['648', '652']
147 def getValidWidths():
148 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
150 def getValidHeights():
151 return [1080, 720, 480] # Technically 240 is also supported
153 # Return the number in list that is nearest to x
154 # if two values are equidistant, return the larger
155 def nearest(x, list):
156 return reduce(lambda a, b: closest(x, a, b), list)
158 def closest(x, a, b):
159 if abs(x - a) < abs(x - b) or (abs(x - a) == abs(x - b) and a > b):
160 return a
161 else:
162 return b
164 def nearestTivoHeight(height):
165 return nearest(height, getValidHeights())
167 def nearestTivoWidth(width):
168 return nearest(width, getValidWidths())
170 def getTivoHeight(tsn):
171 if tsn and config.has_section('_tivo_' + tsn):
172 try:
173 height = config.getint('_tivo_' + tsn, 'height')
174 return nearestTivoHeight(height)
175 except NoOptionError:
176 pass
177 try:
178 height = config.getint('Server', 'height')
179 return nearestTivoHeight(height)
180 except NoOptionError: #defaults for S3/S2 TiVo
181 if isHDtivo(tsn):
182 return 720
183 else:
184 return 480
186 def getTivoWidth(tsn):
187 if tsn and config.has_section('_tivo_' + tsn):
188 try:
189 width = config.getint('_tivo_' + tsn, 'width')
190 return nearestTivoWidth(width)
191 except NoOptionError:
192 pass
193 try:
194 width = config.getint('Server', 'width')
195 return nearestTivoWidth(width)
196 except NoOptionError: #defaults for S3/S2 TiVo
197 if isHDtivo(tsn):
198 return 1280
199 else:
200 return 544
202 def getAudioBR(tsn = None):
203 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
204 #compare audio_br to max_audio_br and return lowest
205 if tsn and config.has_section('_tivo_' + tsn):
206 try:
207 audiobr = int(max(int(strtod(config.get('_tivo_' + tsn, 'audio_br'))/1000), 64)/64)*64
208 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
209 except NoOptionError:
210 pass
211 try:
212 audiobr = int(max(int(strtod(config.get('Server', 'audio_br'))/1000), 64)/64)*64
213 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
214 except NoOptionError: #defaults for S3/S2 TiVo
215 audiobr = 192
216 if isHDtivo(tsn):
217 audiobr = 384
218 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
220 def getVideoBR(tsn = None):
221 if tsn and config.has_section('_tivo_' + tsn):
222 try:
223 return config.get('_tivo_' + tsn, 'video_br')
224 except NoOptionError:
225 pass
226 try:
227 return config.get('Server', 'video_br')
228 except NoOptionError: #defaults for S3/S2 TiVo
229 if isHDtivo(tsn):
230 return '8192k'
231 else:
232 return '4096K'
234 def getMaxVideoBR():
235 try:
236 return str(int(strtod(config.get('Server', 'max_video_br'))/1000)) + 'k'
237 except NoOptionError: #default to 17Mi
238 return '17408k'
240 def getBuffSize():
241 try:
242 return str(int(strtod(config.get('Server', 'bufsize'))))
243 except NoOptionError: #default 1024k
244 return '1024k'
246 def getMaxAudioBR(tsn = None):
247 #convert to non-zero multiple of 64 for ffmpeg compatibility
248 if tsn and config.has_section('_tivo_' + tsn):
249 try:
250 return int(int(strtod(config.get('_tivo_' + tsn, 'max_audio_br'))/1000)/64)*64
251 except NoOptionError:
252 pass
253 try:
254 return int(int(strtod(config.get('Server', 'max_audio_br'))/1000)/64)*64
255 except NoOptionError:
256 return int(448) #default to 448
258 def getAudioCodec(tsn = None):
259 if tsn and config.has_section('_tivo_' + tsn):
260 try:
261 return config.get('_tivo_' + tsn, 'audio_codec')
262 except NoOptionError:
263 pass
264 try:
265 return config.get('Server', 'audio_codec')
266 except NoOptionError:
267 return None
269 def getAudioCH(tsn = None):
270 if tsn and config.has_section('_tivo_' + tsn):
271 try:
272 return config.get('_tivo_' + tsn, 'audio_ch')
273 except NoOptionError:
274 pass
275 try:
276 return config.get('Server', 'audio_ch')
277 except NoOptionError:
278 return None
280 def getAudioFR(tsn = None):
281 if tsn and config.has_section('_tivo_' + tsn):
282 try:
283 return config.get('_tivo_' + tsn, 'audio_fr')
284 except NoOptionError:
285 pass
286 try:
287 return config.get('Server', 'audio_fr')
288 except NoOptionError:
289 return None
291 def getVideoFPS(tsn = None):
292 if tsn and config.has_section('_tivo_' + tsn):
293 try:
294 return config.get('_tivo_' + tsn, 'video_fps')
295 except NoOptionError:
296 pass
297 try:
298 return config.get('Server', 'video_fps')
299 except NoOptionError:
300 return None
302 def getVideoCodec(tsn = None):
303 if tsn and config.has_section('_tivo_' + tsn):
304 try:
305 return config.get('_tivo_' + tsn, 'video_codec')
306 except NoOptionError:
307 pass
308 try:
309 return config.get('Server', 'video_codec')
310 except NoOptionError:
311 return None
313 def getFormat(tsn = None):
314 if tsn and config.has_section('_tivo_' + tsn):
315 try:
316 return config.get('_tivo_' + tsn, 'force_format')
317 except NoOptionError:
318 pass
319 try:
320 return config.get('Server', 'force_format')
321 except NoOptionError:
322 return None
324 def str2tuple(s):
325 items = s.split(',')
326 L = [x.strip() for x in items]
327 return tuple(L)
329 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
330 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
331 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
332 def strtod(value):
333 prefixes = {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
334 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
335 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
336 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
337 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
338 m = p.match(value)
339 if m is None:
340 raise SyntaxError('Invalid bit value syntax')
341 (coef, prefix, power, byte) = m.groups()
342 if prefix is None:
343 value = float(coef)
344 else:
345 exponent = float(prefixes[prefix])
346 if power == 'i':
347 # Use powers of 2
348 value = float(coef) * pow(2.0, exponent / 0.3)
349 else:
350 # Use powers of 10
351 value = float(coef) * pow(10.0, exponent)
352 if byte == 'B': # B == Byte, b == bit
353 value *= 8;
354 return value