1 import ConfigParser
, os
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
)
15 config
= ConfigParser
.ConfigParser()
16 config
.read(config_file
)
19 if config
.has_option('Server', 'GUID'):
20 guid
= config
.get('Server', 'GUID')
25 def getBeaconAddresses():
26 if config
.has_option('Server', 'beacon'):
27 beacon_ips
= config
.get('Server', 'beacon')
29 beacon_ips
= '255.255.255.255'
33 return config
.get('Server', 'Port')
35 def get169Setting(tsn
):
39 if config
.has_section('_tivo_' + tsn
):
40 if config
.has_option('_tivo_' + tsn
, 'aspect169'):
42 return config
.getboolean('_tivo_' + tsn
, 'aspect169')
46 if tsn
[:3] in BLACKLIST_169
:
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
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
)))]
67 for name
, data
in shares
:
68 if not data
.get('auto_subshares', 'False').lower() == 'true':
71 base_path
= data
['path']
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
):
78 new_name
= name
+ '/' + item
80 new_data
['path'] = item_path
82 shares
.append((new_name
, new_data
))
89 if config
.has_option('Server', 'debug'):
91 return str2tuple(config
.get('Server', 'debug')+',,')[ref
]
94 return str2tuple('False,,')[ref
]
98 debug
= config
.get('Server', 'hack83')
99 if debug
.lower() == 'true':
103 except NoOptionError
:
106 def getOptres(tsn
= None):
107 if tsn
and config
.has_section('_tivo_' + tsn
):
109 return config
.getboolean('_tivo_' + tsn
, 'optres')
110 except NoOptionError
, ValueError:
113 return config
.getboolean('Server', 'optres')
114 except NoOptionError
, ValueError:
118 if config
.has_option('Server', 'par'):
120 return (True, config
.getfloat('Server', 'par'))[ref
]
121 except NoOptionError
, ValueError:
123 return (False, 1.0)[ref
]
125 def get(section
, key
):
126 return config
.get(section
, key
)
128 def getFFmpegTemplate(tsn
):
129 if tsn
and config
.has_section('_tivo_' + tsn
):
131 return config
.get('_tivo_' + tsn
, 'ffmpeg_tmpl', raw
=True)
132 except NoOptionError
:
135 return config
.get('Server', 'ffmpeg_tmpl', raw
=True)
136 except NoOptionError
: #default
137 return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
138 %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
139 %(audio_fr)s %(audio_ch)s %(audio_codec)s %(ffmpeg_pram)s %(format)s'
141 def getFFmpegPrams(tsn
):
142 if tsn
and config
.has_section('_tivo_' + tsn
):
144 return config
.get('_tivo_' + tsn
, 'ffmpeg_pram', raw
=True)
145 except NoOptionError
:
148 return config
.get('Server', 'ffmpeg_pram', raw
=True)
149 except NoOptionError
:
152 def isHDtivo(tsn
): # tsn's of High Definition Tivo's
153 return tsn
!= '' and tsn
[:3] in ['648', '652']
155 def getValidWidths():
156 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
158 def getValidHeights():
159 return [1080, 720, 480] # Technically 240 is also supported
161 # Return the number in list that is nearest to x
162 # if two values are equidistant, return the larger
163 def nearest(x
, list):
164 return reduce(lambda a
, b
: closest(x
, a
, b
), list)
166 def closest(x
, a
, b
):
167 if abs(x
- a
) < abs(x
- b
) or (abs(x
- a
) == abs(x
- b
) and a
> b
):
172 def nearestTivoHeight(height
):
173 return nearest(height
, getValidHeights())
175 def nearestTivoWidth(width
):
176 return nearest(width
, getValidWidths())
178 def getTivoHeight(tsn
):
179 if tsn
and config
.has_section('_tivo_' + tsn
):
181 height
= config
.getint('_tivo_' + tsn
, 'height')
182 return nearestTivoHeight(height
)
183 except NoOptionError
:
186 height
= config
.getint('Server', 'height')
187 return nearestTivoHeight(height
)
188 except NoOptionError
: #defaults for S3/S2 TiVo
194 def getTivoWidth(tsn
):
195 if tsn
and config
.has_section('_tivo_' + tsn
):
197 width
= config
.getint('_tivo_' + tsn
, 'width')
198 return nearestTivoWidth(width
)
199 except NoOptionError
:
202 width
= config
.getint('Server', 'width')
203 return nearestTivoWidth(width
)
204 except NoOptionError
: #defaults for S3/S2 TiVo
210 def getAudioBR(tsn
= None):
211 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
212 #compare audio_br to max_audio_br and return lowest
213 if tsn
and config
.has_section('_tivo_' + tsn
):
215 audiobr
= int(max(int(strtod(config
.get('_tivo_' + tsn
, 'audio_br'))/1000), 64)/64)*64
216 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
217 except NoOptionError
:
220 audiobr
= int(max(int(strtod(config
.get('Server', 'audio_br'))/1000), 64)/64)*64
221 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
222 except NoOptionError
:
223 return str(min(384, getMaxAudioBR(tsn
))) + 'k'
225 def getVideoBR(tsn
= None):
226 if tsn
and config
.has_section('_tivo_' + tsn
):
228 return config
.get('_tivo_' + tsn
, 'video_br')
229 except NoOptionError
:
232 return config
.get('Server', 'video_br')
233 except NoOptionError
: #defaults for S3/S2 TiVo
241 return str(int(strtod(config
.get('Server', 'max_video_br'))/1000)) + 'k'
242 except NoOptionError
: #default to 17Mi
247 return str(int(strtod(config
.get('Server', 'bufsize'))))
248 except NoOptionError
: #default 1024k
251 def getMaxAudioBR(tsn
= None):
252 #convert to non-zero multiple of 64 for ffmpeg compatibility
253 if tsn
and config
.has_section('_tivo_' + tsn
):
255 return int(int(strtod(config
.get('_tivo_' + tsn
, 'max_audio_br'))/1000)/64)*64
256 except NoOptionError
:
259 return int(int(strtod(config
.get('Server', 'max_audio_br'))/1000)/64)*64
260 except NoOptionError
:
261 return int(448) #default to 448
263 def getAudioCodec(tsn
= None):
264 if tsn
and config
.has_section('_tivo_' + tsn
):
266 return config
.get('_tivo_' + tsn
, 'audio_codec')
267 except NoOptionError
:
270 return config
.get('Server', 'audio_codec')
271 except NoOptionError
:
274 def getAudioCH(tsn
= None):
275 if tsn
and config
.has_section('_tivo_' + tsn
):
277 return config
.get('_tivo_' + tsn
, 'audio_ch')
278 except NoOptionError
:
281 return config
.get('Server', 'audio_ch')
282 except NoOptionError
:
285 def getAudioFR(tsn
= None):
286 if tsn
and config
.has_section('_tivo_' + tsn
):
288 return config
.get('_tivo_' + tsn
, 'audio_fr')
289 except NoOptionError
:
292 return config
.get('Server', 'audio_fr')
293 except NoOptionError
:
296 def getVideoFPS(tsn
= None):
297 if tsn
and config
.has_section('_tivo_' + tsn
):
299 return config
.get('_tivo_' + tsn
, 'video_fps')
300 except NoOptionError
:
303 return config
.get('Server', 'video_fps')
304 except NoOptionError
:
307 def getVideoCodec(tsn
= None):
308 if tsn
and config
.has_section('_tivo_' + tsn
):
310 return config
.get('_tivo_' + tsn
, 'video_codec')
311 except NoOptionError
:
314 return config
.get('Server', 'video_codec')
315 except NoOptionError
:
318 def getFormat(tsn
= None):
319 if tsn
and config
.has_section('_tivo_' + tsn
):
321 return config
.get('_tivo_' + tsn
, 'force_format')
322 except NoOptionError
:
325 return config
.get('Server', 'force_format')
326 except NoOptionError
:
331 L
= [x
.strip() for x
in items
]
334 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
335 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
336 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
338 prefixes
= {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
339 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
340 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
341 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
342 p
= re
.compile(r
'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
345 raise SyntaxError('Invalid bit value syntax')
346 (coef
, prefix
, power
, byte
) = m
.groups()
350 exponent
= float(prefixes
[prefix
])
353 value
= float(coef
) * pow(2.0, exponent
/ 0.3)
356 value
= float(coef
) * pow(10.0, exponent
)
357 if byte
== 'B': # B == Byte, b == bit