1 import ConfigParser
, os
3 from ConfigParser
import NoOptionError
5 config
= ConfigParser
.ConfigParser()
6 p
= os
.path
.dirname(__file__
)
7 config_file
= os
.path
.join(p
, 'pyTivo.conf')
8 config
.read(config_file
)
13 config
= ConfigParser
.ConfigParser()
14 config
.read(config_file
)
17 if config
.has_option('Server', 'GUID'):
18 guid
= config
.get('Server', 'GUID')
23 def getBeaconAddresses():
24 if config
.has_option('Server', 'beacon'):
25 beacon_ips
= config
.get('Server', 'beacon')
27 beacon_ips
= '255.255.255.255'
31 return config
.get('Server', 'Port')
33 def get169Blacklist(tsn
): # tivo does not pad 16:9 video
34 return tsn
!= '' and tsn
[:3] in ('540')
36 def get169Letterbox(tsn
): # tivo pads 16:9 video for 4:3 display
37 return tsn
!= '' and tsn
[:3] in ('649')
39 def get169Setting(tsn
):
43 if config
.has_section('_tivo_' + tsn
):
44 if config
.has_option('_tivo_' + tsn
, 'aspect169'):
46 return config
.getboolean('_tivo_' + tsn
, 'aspect169')
50 if get169Blacklist(tsn
) or get169Letterbox(tsn
):
55 def getShares(tsn
=''):
56 shares
= [(section
, dict(config
.items(section
)))
57 for section
in config
.sections()
58 if not(section
.startswith('_tivo_') or section
== 'Server')]
60 if config
.has_section('_tivo_' + tsn
):
61 if config
.has_option('_tivo_' + tsn
, 'shares'):
62 # clean up leading and trailing spaces & make sure ref is valid
64 for x
in config
.get('_tivo_' + tsn
, 'shares').split(','):
65 y
= x
.lstrip().rstrip()
66 if config
.has_section(y
):
67 tsnshares
+= [(y
, dict(config
.items(y
)))]
71 for name
, data
in shares
:
72 if not data
.get('auto_subshares', 'False').lower() == 'true':
75 base_path
= data
['path']
77 for item
in os
.listdir(base_path
):
78 item_path
= os
.path
.join(base_path
, item
)
79 if not os
.path
.isdir(item_path
) or item
.startswith('.'):
82 new_name
= name
+ '/' + item
84 new_data
['path'] = item_path
86 shares
.append((new_name
, new_data
))
93 if config
.has_option('Server', 'debug'):
95 return str2tuple(config
.get('Server', 'debug')+',,')[ref
]
98 return str2tuple('False,,')[ref
]
102 debug
= config
.get('Server', 'hack83')
103 if debug
.lower() == 'true':
107 except NoOptionError
:
110 def getOptres(tsn
= None):
111 if tsn
and config
.has_section('_tivo_' + tsn
):
113 return config
.getboolean('_tivo_' + tsn
, 'optres')
114 except NoOptionError
, ValueError:
117 return config
.getboolean('Server', 'optres')
118 except NoOptionError
, ValueError:
122 if config
.has_option('Server', 'par'):
124 return (True, config
.getfloat('Server', 'par'))[ref
]
125 except NoOptionError
, ValueError:
127 return (False, 1.0)[ref
]
129 def get(section
, key
):
130 return config
.get(section
, key
)
132 def getFFmpegTemplate(tsn
):
133 if tsn
and config
.has_section('_tivo_' + tsn
):
135 return config
.get('_tivo_' + tsn
, 'ffmpeg_tmpl', raw
=True)
136 except NoOptionError
:
139 return config
.get('Server', 'ffmpeg_tmpl', raw
=True)
140 except NoOptionError
: #default
141 return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
142 %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
143 %(audio_fr)s %(audio_ch)s %(audio_codec)s %(ffmpeg_pram)s %(format)s'
145 def getFFmpegPrams(tsn
):
146 if tsn
and config
.has_section('_tivo_' + tsn
):
148 return config
.get('_tivo_' + tsn
, 'ffmpeg_pram', raw
=True)
149 except NoOptionError
:
152 return config
.get('Server', 'ffmpeg_pram', raw
=True)
153 except NoOptionError
:
156 def isHDtivo(tsn
): # tsn's of High Definition Tivo's
157 return tsn
!= '' and tsn
[:3] in ['648', '652']
159 def getValidWidths():
160 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
162 def getValidHeights():
163 return [1080, 720, 480] # Technically 240 is also supported
165 # Return the number in list that is nearest to x
166 # if two values are equidistant, return the larger
167 def nearest(x
, list):
168 return reduce(lambda a
, b
: closest(x
, a
, b
), list)
170 def closest(x
, a
, b
):
171 if abs(x
- a
) < abs(x
- b
) or (abs(x
- a
) == abs(x
- b
) and a
> b
):
176 def nearestTivoHeight(height
):
177 return nearest(height
, getValidHeights())
179 def nearestTivoWidth(width
):
180 return nearest(width
, getValidWidths())
182 def getTivoHeight(tsn
):
183 if tsn
and config
.has_section('_tivo_' + tsn
):
185 height
= config
.getint('_tivo_' + tsn
, 'height')
186 return nearestTivoHeight(height
)
187 except NoOptionError
:
190 height
= config
.getint('Server', 'height')
191 return nearestTivoHeight(height
)
192 except NoOptionError
: #defaults for S3/S2 TiVo
198 def getTivoWidth(tsn
):
199 if tsn
and config
.has_section('_tivo_' + tsn
):
201 width
= config
.getint('_tivo_' + tsn
, 'width')
202 return nearestTivoWidth(width
)
203 except NoOptionError
:
206 width
= config
.getint('Server', 'width')
207 return nearestTivoWidth(width
)
208 except NoOptionError
: #defaults for S3/S2 TiVo
214 def getAudioBR(tsn
= None):
215 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
216 #compare audio_br to max_audio_br and return lowest
217 if tsn
and config
.has_section('_tivo_' + tsn
):
219 audiobr
= int(max(int(strtod(config
.get('_tivo_' + tsn
, 'audio_br'))/1000), 64)/64)*64
220 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
221 except NoOptionError
:
224 audiobr
= int(max(int(strtod(config
.get('Server', 'audio_br'))/1000), 64)/64)*64
225 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
226 except NoOptionError
:
227 return str(min(384, getMaxAudioBR(tsn
))) + 'k'
229 def getVideoBR(tsn
= None):
230 if tsn
and config
.has_section('_tivo_' + tsn
):
232 return config
.get('_tivo_' + tsn
, 'video_br')
233 except NoOptionError
:
236 return config
.get('Server', 'video_br')
237 except NoOptionError
: #defaults for S3/S2 TiVo
245 return str(int(strtod(config
.get('Server', 'max_video_br'))/1000)) + 'k'
246 except NoOptionError
: #default to 30000k
251 return config
.getfloat('Server', 'video_pct')
252 except NoOptionError
:
257 return str(int(strtod(config
.get('Server', 'bufsize'))))
258 except NoOptionError
: #default 1024k
261 def getMaxAudioBR(tsn
= None):
262 #convert to non-zero multiple of 64 for ffmpeg compatibility
263 if tsn
and config
.has_section('_tivo_' + tsn
):
265 return int(int(strtod(config
.get('_tivo_' + tsn
, 'max_audio_br'))/1000)/64)*64
266 except NoOptionError
:
269 return int(int(strtod(config
.get('Server', 'max_audio_br'))/1000)/64)*64
270 except NoOptionError
:
271 return int(448) #default to 448
273 def getAudioCodec(tsn
= None):
274 if tsn
and config
.has_section('_tivo_' + tsn
):
276 return config
.get('_tivo_' + tsn
, 'audio_codec')
277 except NoOptionError
:
280 return config
.get('Server', 'audio_codec')
281 except NoOptionError
:
284 def getAudioCH(tsn
= None):
285 if tsn
and config
.has_section('_tivo_' + tsn
):
287 return config
.get('_tivo_' + tsn
, 'audio_ch')
288 except NoOptionError
:
291 return config
.get('Server', 'audio_ch')
292 except NoOptionError
:
295 def getAudioFR(tsn
= None):
296 if tsn
and config
.has_section('_tivo_' + tsn
):
298 return config
.get('_tivo_' + tsn
, 'audio_fr')
299 except NoOptionError
:
302 return config
.get('Server', 'audio_fr')
303 except NoOptionError
:
306 def getCopyTS(tsn
= None):
307 if tsn
and config
.has_section('_tivo_' + tsn
):
308 if config
.has_option('_tivo_' + tsn
, 'copy_ts'):
310 return config
.get('_tivo_' + tsn
, 'copy_ts')
311 except NoOptionError
, ValueError:
313 if config
.has_option('Server', 'copy_ts'):
315 return config
.get('Server', 'copy_ts')
316 except NoOptionError
, ValueError:
320 def getVideoFPS(tsn
= None):
321 if tsn
and config
.has_section('_tivo_' + tsn
):
323 return config
.get('_tivo_' + tsn
, 'video_fps')
324 except NoOptionError
:
327 return config
.get('Server', 'video_fps')
328 except NoOptionError
:
331 def getVideoCodec(tsn
= None):
332 if tsn
and config
.has_section('_tivo_' + tsn
):
334 return config
.get('_tivo_' + tsn
, 'video_codec')
335 except NoOptionError
:
338 return config
.get('Server', 'video_codec')
339 except NoOptionError
:
342 def getFormat(tsn
= None):
343 if tsn
and config
.has_section('_tivo_' + tsn
):
345 return config
.get('_tivo_' + tsn
, 'force_format')
346 except NoOptionError
:
349 return config
.get('Server', 'force_format')
350 except NoOptionError
:
355 L
= [x
.strip() for x
in items
]
358 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
359 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
360 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
362 prefixes
= {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
363 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
364 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
365 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
366 p
= re
.compile(r
'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
369 raise SyntaxError('Invalid bit value syntax')
370 (coef
, prefix
, power
, byte
) = m
.groups()
374 exponent
= float(prefixes
[prefix
])
377 value
= float(coef
) * pow(2.0, exponent
/ 0.3)
380 value
= float(coef
) * pow(10.0, exponent
)
381 if byte
== 'B': # B == Byte, b == bit