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