Merge branch 'master' into subfolders-8.3
[pyTivo.git] / config.py
blob6551bcb826f1970c6db3bd262b6c163435c78f7d
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 try:
35 return config.getboolean('_tivo_' + tsn, 'aspect169')
36 except ValueError:
37 pass
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 return config.getboolean('Server', 'debug')
69 except NoOptionError, ValueError:
70 return False
72 def getHack83():
73 try:
74 debug = config.get('Server', 'hack83')
75 if debug.lower() == 'true':
76 return True
77 else:
78 return False
79 except NoOptionError:
80 return False
82 def getOptres():
83 try:
84 return config.getboolean('Server', 'optres')
85 except NoOptionError, ValueError:
86 return False
88 def get(section, key):
89 return config.get(section, key)
91 def getFFMPEGTemplate(tsn):
92 if tsn and config.has_section('_tivo_' + tsn):
93 try:
94 return config.get('_tivo_' + tsn, 'ffmpeg_prams', raw=True)
95 except NoOptionError:
96 pass
98 try:
99 return config.get('Server', 'ffmpeg_prams', raw=True)
100 except NoOptionError: #default
101 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 -'
103 def getHDtivos(): # tsn's of High Definition Tivo's
104 return ['648', '652']
106 def getValidWidths():
107 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
109 def getValidHeights():
110 return [1080, 720, 480] # Technically 240 is also supported
112 # Return the number in list that is nearest to x
113 # if two values are equidistant, return the larger
114 def nearest(x, list):
115 return reduce(lambda a, b: closest(x,a,b), list)
117 def closest(x,a, b):
118 if abs(x-a) < abs(x-b) or (abs(x-a) == abs(x-b)and a>b):
119 return a
120 else:
121 return b
123 def nearestTivoHeight(height):
124 return nearest(height, getValidHeights())
126 def nearestTivoWidth(width):
127 return nearest(width, getValidWidths())
129 def getTivoHeight(tsn):
130 if tsn and config.has_section('_tivo_' + tsn):
131 try:
132 height = config.getint('_tivo_' + tsn, 'height')
133 return nearestTivoHeight(height)
134 except NoOptionError:
135 pass
137 try:
138 height = config.getint('Server', 'height')
139 return nearestTivoHeight(height)
140 except NoOptionError: #default
141 return 480
143 def getTivoWidth(tsn):
144 if tsn and config.has_section('_tivo_' + tsn):
145 try:
146 width = config.getint('_tivo_' + tsn, 'width')
147 return nearestTivoWidth(width)
148 except NoOptionError:
149 pass
151 try:
152 width = config.getint('Server', 'width')
153 return nearestTivoWidth(width)
154 except NoOptionError: #default
155 return 544
157 def getAudioBR(tsn = None):
158 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
159 #compare audio_br to max_audio_br and return lowest
160 if tsn and config.has_section('_tivo_' + tsn):
161 try:
162 audiobr = int(max(int(strtod(config.get('_tivo_' + tsn, 'audio_br'))/1000), 64)/64)*64
163 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
164 except NoOptionError:
165 pass
167 try:
168 audiobr = int(max(int(strtod(config.get('Server', 'audio_br'))/1000), 64)/64)*64
169 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
170 except NoOptionError: #default to 192
171 return '192k'
173 def getAudioCodec(tsn = None):
174 #check for HD tivo and return compatible audio parameters
175 if tsn and tsn[:3] in getHDtivos():
176 return '-acodec ac3 -ar 48000'
177 else:
178 return '-acodec mp2 -ac 2 -ar 44100'
180 def getVideoBR(tsn = None):
181 if tsn and config.has_section('_tivo_' + tsn):
182 try:
183 return config.get('_tivo_' + tsn, 'video_br')
184 except NoOptionError:
185 pass
187 try:
188 return config.get('Server', 'video_br')
189 except NoOptionError: #default to 4096K
190 return '4096K'
192 def getMaxVideoBR():
193 try:
194 return str(int(strtod(config.get('Server', 'max_video_br'))/1000)) + 'k'
195 except NoOptionError: #default to 17Mi
196 return '17408k'
198 def getBuffSize():
199 try:
200 return config.get('Server', 'bufsize')
201 except NoOptionError: #default 1024k
202 return '1024k'
204 def getMaxAudioBR(tsn = None):
205 #convert to non-zero multiple of 64 for ffmpeg compatibility
206 if tsn and config.has_section('_tivo_' + tsn):
207 try:
208 return int(int(strtod(config.get('_tivo_' + tsn, 'max_audio_br'))/1000)/64)*64
209 except NoOptionError:
210 pass
212 try:
213 return int(int(strtod(config.get('Server', 'max_audio_br'))/1000)/64)*64
214 except NoOptionError:
215 if tsn and tsn[:3] in getHDtivos():
216 return int(448) #default to 448, max supported by HD TiVo's
217 else:
218 return int(384) #default to 384, max supported by mp2 audio (S2 TiVo)
221 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
222 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
223 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
224 def strtod(value):
225 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}
226 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
227 m = p.match(value)
228 if m is None:
229 raise SyntaxError('Invalid bit value syntax')
230 (coef, prefix, power, byte) = m.groups()
231 if prefix is None:
232 value = float(coef)
233 else:
234 exponent = float(prefixes[prefix])
235 if power == "i":
236 # Use powers of 2
237 value = float(coef) * pow(2.0, exponent/0.3)
238 else:
239 # Use powers of 10
240 value = float(coef) * pow(10.0, exponent)
241 if byte == "B": # B==Byte, b=bit
242 value *= 8;
243 return value