1 import ConfigParser
, os
, sys
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 if not os
.path
.exists(config_file
):
9 print 'ERROR: pyTivo.conf does not exist.\n' + \
10 'You must create this file before running pyTivo.'
12 config
.read(config_file
)
17 config
= ConfigParser
.ConfigParser()
18 config
.read(config_file
)
21 if config
.has_option('Server', 'GUID'):
22 guid
= config
.get('Server', 'GUID')
27 def getBeaconAddresses():
28 if config
.has_option('Server', 'beacon'):
29 beacon_ips
= config
.get('Server', 'beacon')
31 beacon_ips
= '255.255.255.255'
35 return config
.get('Server', 'Port')
37 def get169Blacklist(tsn
): # tivo does not pad 16:9 video
38 return tsn
!= '' and tsn
[:3] in ('540')
40 def get169Letterbox(tsn
): # tivo pads 16:9 video for 4:3 display
41 return tsn
!= '' and tsn
[:3] in ('649')
43 def get169Setting(tsn
):
47 if config
.has_section('_tivo_' + tsn
):
48 if config
.has_option('_tivo_' + tsn
, 'aspect169'):
50 return config
.getboolean('_tivo_' + tsn
, 'aspect169')
54 if get169Blacklist(tsn
) or get169Letterbox(tsn
):
59 def getShares(tsn
=''):
60 shares
= [(section
, dict(config
.items(section
)))
61 for section
in config
.sections()
62 if not(section
.startswith('_tivo_') or section
== 'Server')]
64 if config
.has_section('_tivo_' + tsn
):
65 if config
.has_option('_tivo_' + tsn
, 'shares'):
66 # clean up leading and trailing spaces & make sure ref is valid
68 for x
in config
.get('_tivo_' + tsn
, 'shares').split(','):
69 y
= x
.lstrip().rstrip()
70 if config
.has_section(y
):
71 tsnshares
+= [(y
, dict(config
.items(y
)))]
75 for name
, data
in shares
:
76 if not data
.get('auto_subshares', 'False').lower() == 'true':
79 base_path
= data
['path']
81 for item
in os
.listdir(base_path
):
82 item_path
= os
.path
.join(base_path
, item
)
83 if not os
.path
.isdir(item_path
) or item
.startswith('.'):
86 new_name
= name
+ '/' + item
88 new_data
['path'] = item_path
90 shares
.append((new_name
, new_data
))
97 if config
.has_option('Server', 'debug'):
99 return str2tuple(config
.get('Server', 'debug')+',,')[ref
]
100 except NoOptionError
:
102 return str2tuple('False,,')[ref
]
106 debug
= config
.get('Server', 'hack83')
107 if debug
.lower() == 'true':
111 except NoOptionError
:
114 def getOptres(tsn
= None):
115 if tsn
and config
.has_section('_tivo_' + tsn
):
117 return config
.getboolean('_tivo_' + tsn
, 'optres')
118 except NoOptionError
, ValueError:
121 return config
.getboolean('Server', 'optres')
122 except NoOptionError
, ValueError:
126 if config
.has_option('Server', 'par'):
128 return (True, config
.getfloat('Server', 'par'))[ref
]
129 except NoOptionError
, ValueError:
131 return (False, 1.0)[ref
]
133 def get(section
, key
):
134 return config
.get(section
, key
)
136 def getFFmpegTemplate(tsn
):
137 if tsn
and config
.has_section('_tivo_' + tsn
):
139 return config
.get('_tivo_' + tsn
, 'ffmpeg_tmpl', raw
=True)
140 except NoOptionError
:
143 return config
.get('Server', 'ffmpeg_tmpl', raw
=True)
144 except NoOptionError
: #default
145 return '%(video_codec)s %(video_fps)s %(video_br)s %(max_video_br)s \
146 %(buff_size)s %(aspect_ratio)s -comment pyTivo.py %(audio_br)s \
147 %(audio_fr)s %(audio_ch)s %(audio_codec)s %(ffmpeg_pram)s %(format)s'
149 def getFFmpegPrams(tsn
):
150 if tsn
and config
.has_section('_tivo_' + tsn
):
152 return config
.get('_tivo_' + tsn
, 'ffmpeg_pram', raw
=True)
153 except NoOptionError
:
156 return config
.get('Server', 'ffmpeg_pram', raw
=True)
157 except NoOptionError
:
160 def isHDtivo(tsn
): # tsn's of High Definition Tivo's
161 return tsn
!= '' and tsn
[:3] in ['648', '652']
163 def getValidWidths():
164 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
166 def getValidHeights():
167 return [1080, 720, 480] # Technically 240 is also supported
169 # Return the number in list that is nearest to x
170 # if two values are equidistant, return the larger
171 def nearest(x
, list):
172 return reduce(lambda a
, b
: closest(x
, a
, b
), list)
174 def closest(x
, a
, b
):
175 if abs(x
- a
) < abs(x
- b
) or (abs(x
- a
) == abs(x
- b
) and a
> b
):
180 def nearestTivoHeight(height
):
181 return nearest(height
, getValidHeights())
183 def nearestTivoWidth(width
):
184 return nearest(width
, getValidWidths())
186 def getTivoHeight(tsn
):
187 if tsn
and config
.has_section('_tivo_' + tsn
):
189 height
= config
.getint('_tivo_' + tsn
, 'height')
190 return nearestTivoHeight(height
)
191 except NoOptionError
:
194 height
= config
.getint('Server', 'height')
195 return nearestTivoHeight(height
)
196 except NoOptionError
: #defaults for S3/S2 TiVo
202 def getTivoWidth(tsn
):
203 if tsn
and config
.has_section('_tivo_' + tsn
):
205 width
= config
.getint('_tivo_' + tsn
, 'width')
206 return nearestTivoWidth(width
)
207 except NoOptionError
:
210 width
= config
.getint('Server', 'width')
211 return nearestTivoWidth(width
)
212 except NoOptionError
: #defaults for S3/S2 TiVo
218 def getAudioBR(tsn
= None):
219 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
220 #compare audio_br to max_audio_br and return lowest
221 if tsn
and config
.has_section('_tivo_' + tsn
):
223 audiobr
= int(max(int(strtod(config
.get('_tivo_' + tsn
, 'audio_br'))/1000), 64)/64)*64
224 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
225 except NoOptionError
:
228 audiobr
= int(max(int(strtod(config
.get('Server', 'audio_br'))/1000), 64)/64)*64
229 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
230 except NoOptionError
:
231 return str(min(384, getMaxAudioBR(tsn
))) + 'k'
233 def getVideoBR(tsn
= None):
234 if tsn
and config
.has_section('_tivo_' + tsn
):
236 return config
.get('_tivo_' + tsn
, 'video_br')
237 except NoOptionError
:
240 return config
.get('Server', 'video_br')
241 except NoOptionError
: #defaults for S3/S2 TiVo
249 return str(int(strtod(config
.get('Server', 'max_video_br'))/1000)) + 'k'
250 except NoOptionError
: #default to 30000k
255 return config
.getfloat('Server', 'video_pct')
256 except NoOptionError
:
261 return str(int(strtod(config
.get('Server', 'bufsize'))))
262 except NoOptionError
: #default 1024k
265 def getMaxAudioBR(tsn
= None):
266 #convert to non-zero multiple of 64 for ffmpeg compatibility
267 if tsn
and config
.has_section('_tivo_' + tsn
):
269 return int(int(strtod(config
.get('_tivo_' + tsn
, 'max_audio_br'))/1000)/64)*64
270 except NoOptionError
:
273 return int(int(strtod(config
.get('Server', 'max_audio_br'))/1000)/64)*64
274 except NoOptionError
:
275 return int(448) #default to 448
277 def getAudioCodec(tsn
= None):
278 if tsn
and config
.has_section('_tivo_' + tsn
):
280 return config
.get('_tivo_' + tsn
, 'audio_codec')
281 except NoOptionError
:
284 return config
.get('Server', 'audio_codec')
285 except NoOptionError
:
288 def getAudioCH(tsn
= None):
289 if tsn
and config
.has_section('_tivo_' + tsn
):
291 return config
.get('_tivo_' + tsn
, 'audio_ch')
292 except NoOptionError
:
295 return config
.get('Server', 'audio_ch')
296 except NoOptionError
:
299 def getAudioFR(tsn
= None):
300 if tsn
and config
.has_section('_tivo_' + tsn
):
302 return config
.get('_tivo_' + tsn
, 'audio_fr')
303 except NoOptionError
:
306 return config
.get('Server', 'audio_fr')
307 except NoOptionError
:
310 def getCopyTS(tsn
= None):
311 if tsn
and config
.has_section('_tivo_' + tsn
):
312 if config
.has_option('_tivo_' + tsn
, 'copy_ts'):
314 return config
.get('_tivo_' + tsn
, 'copy_ts')
315 except NoOptionError
, ValueError:
317 if config
.has_option('Server', 'copy_ts'):
319 return config
.get('Server', 'copy_ts')
320 except NoOptionError
, ValueError:
324 def getVideoFPS(tsn
= None):
325 if tsn
and config
.has_section('_tivo_' + tsn
):
327 return config
.get('_tivo_' + tsn
, 'video_fps')
328 except NoOptionError
:
331 return config
.get('Server', 'video_fps')
332 except NoOptionError
:
335 def getVideoCodec(tsn
= None):
336 if tsn
and config
.has_section('_tivo_' + tsn
):
338 return config
.get('_tivo_' + tsn
, 'video_codec')
339 except NoOptionError
:
342 return config
.get('Server', 'video_codec')
343 except NoOptionError
:
346 def getFormat(tsn
= None):
347 if tsn
and config
.has_section('_tivo_' + tsn
):
349 return config
.get('_tivo_' + tsn
, 'force_format')
350 except NoOptionError
:
353 return config
.get('Server', 'force_format')
354 except NoOptionError
:
359 L
= [x
.strip() for x
in items
]
362 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
363 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
364 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
366 prefixes
= {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
367 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
368 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
369 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
370 p
= re
.compile(r
'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
373 raise SyntaxError('Invalid bit value syntax')
374 (coef
, prefix
, power
, byte
) = m
.groups()
378 exponent
= float(prefixes
[prefix
])
381 value
= float(coef
) * pow(2.0, exponent
/ 0.3)
384 value
= float(coef
) * pow(10.0, exponent
)
385 if byte
== 'B': # B == Byte, b == bit