Increase the ffmpeg timeout from four seconds to ten.
[pyTivo.git] / config.py
blobafc8386c72ffaf8d8528cdb6c2df4ab6f6704cb8
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)))
46 for section in config.sections()
47 if not(section.startswith('_tivo_') or section == 'Server')]
49 for name, data in shares:
50 if not data.get('auto_subshares', 'False').lower() == 'true':
51 continue
53 base_path = data['path']
54 for item in os.listdir(base_path):
55 item_path = os.path.join(base_path, item)
56 if not os.path.isdir(item_path):
57 continue
59 new_name = name + '/' + item
60 new_data = dict(data)
61 new_data['path'] = item_path
63 shares.append((new_name, new_data))
65 return shares
67 def getDebug():
68 try:
69 return config.getboolean('Server', 'debug')
70 except NoOptionError, ValueError:
71 return False
73 def getOptres():
74 try:
75 return config.getboolean('Server', 'optres')
76 except NoOptionError, ValueError:
77 return False
79 def get(section, key):
80 return config.get(section, key)
82 def getFFMPEGTemplate(tsn):
83 if tsn and config.has_section('_tivo_' + tsn):
84 try:
85 return config.get('_tivo_' + tsn, 'ffmpeg_prams', raw=True)
86 except NoOptionError:
87 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 isHDtivo(tsn): # tsn's of High Definition Tivo's
94 return tsn != '' and tsn[:3] in ['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
126 try:
127 height = config.getint('Server', 'height')
128 return nearestTivoHeight(height)
129 except NoOptionError: #defaults for S3/S2 TiVo
130 if isHDtivo(tsn):
131 return 720
132 else:
133 return 480
135 def getTivoWidth(tsn):
136 if tsn and config.has_section('_tivo_' + tsn):
137 try:
138 width = config.getint('_tivo_' + tsn, 'width')
139 return nearestTivoWidth(width)
140 except NoOptionError:
141 pass
142 try:
143 width = config.getint('Server', 'width')
144 return nearestTivoWidth(width)
145 except NoOptionError: #defaults for S3/S2 TiVo
146 if isHDtivo(tsn):
147 return 1280
148 else:
149 return 544
151 def getAudioBR(tsn = None):
152 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
153 #compare audio_br to max_audio_br and return lowest
154 if tsn and config.has_section('_tivo_' + tsn):
155 try:
156 audiobr = int(max(int(strtod(config.get('_tivo_' + tsn, 'audio_br'))/1000), 64)/64)*64
157 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
158 except NoOptionError:
159 pass
160 try:
161 audiobr = int(max(int(strtod(config.get('Server', 'audio_br'))/1000), 64)/64)*64
162 return str(min(audiobr, getMaxAudioBR(tsn))) + 'k'
163 except NoOptionError: #defaults for S3/S2 TiVo
164 if isHDtivo(tsn):
165 return '384k'
166 else:
167 return '192k'
169 def getVideoBR(tsn = None):
170 if tsn and config.has_section('_tivo_' + tsn):
171 try:
172 return config.get('_tivo_' + tsn, 'video_br')
173 except NoOptionError:
174 pass
175 try:
176 return config.get('Server', 'video_br')
177 except NoOptionError: #defaults for S3/S2 TiVo
178 if isHDtivo(tsn):
179 return '8192k'
180 else:
181 return '4096K'
183 def getMaxVideoBR():
184 try:
185 return str(int(strtod(config.get('Server', 'max_video_br'))/1000)) + 'k'
186 except NoOptionError: #default to 17Mi
187 return '17408k'
189 def getBuffSize():
190 try:
191 return config.get('Server', 'bufsize')
192 except NoOptionError: #default 1024k
193 return '1024k'
195 def getMaxAudioBR(tsn = None):
196 #convert to non-zero multiple of 64 for ffmpeg compatibility
197 if tsn and config.has_section('_tivo_' + tsn):
198 try:
199 return int(int(strtod(config.get('_tivo_' + tsn, 'max_audio_br'))/1000)/64)*64
200 except NoOptionError:
201 pass
202 try:
203 return int(int(strtod(config.get('Server', 'max_audio_br'))/1000)/64)*64
204 except NoOptionError:
205 if isHDtivo(tsn):
206 return int(448) #default to 448, max supported by HD TiVo's
207 else:
208 return int(384) #default to 384, max supported by mp2 audio (S2 TiVo)
210 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
211 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
212 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
213 def strtod(value):
214 prefixes = {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
215 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
216 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
217 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
218 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
219 m = p.match(value)
220 if m is None:
221 raise SyntaxError('Invalid bit value syntax')
222 (coef, prefix, power, byte) = m.groups()
223 if prefix is None:
224 value = float(coef)
225 else:
226 exponent = float(prefixes[prefix])
227 if power == 'i':
228 # Use powers of 2
229 value = float(coef) * pow(2.0, exponent / 0.3)
230 else:
231 # Use powers of 10
232 value = float(coef) * pow(10.0, exponent)
233 if byte == 'B': # B == Byte, b == bit
234 value *= 8;
235 return value