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']
72 for item
in os
.listdir(base_path
):
73 item_path
= os
.path
.join(base_path
, item
)
74 if not os
.path
.isdir(item_path
):
77 new_name
= name
+ '/' + item
79 new_data
['path'] = item_path
81 shares
.append((new_name
, new_data
))
87 return config
.getboolean('Server', 'debug')
88 except NoOptionError
, ValueError:
93 debug
= config
.get('Server', 'hack83')
94 if debug
.lower() == 'true':
103 return config
.getboolean('Server', 'optres')
104 except NoOptionError
, ValueError:
107 def get(section
, key
):
108 return config
.get(section
, key
)
110 def getFFMPEGTemplate(tsn
):
111 if tsn
and config
.has_section('_tivo_' + tsn
):
113 return config
.get('_tivo_' + tsn
, 'ffmpeg_prams', raw
=True)
114 except NoOptionError
:
117 return config
.get('Server', 'ffmpeg_prams', raw
=True)
118 except NoOptionError
: #default
119 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 -ab %(audio_br)s %(audio_fr)s %(audio_codec)s -f vob -'
121 def isHDtivo(tsn
): # tsn's of High Definition Tivo's
122 return tsn
!= '' and tsn
[:3] in ['648', '652']
124 def getValidWidths():
125 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
127 def getValidHeights():
128 return [1080, 720, 480] # Technically 240 is also supported
130 # Return the number in list that is nearest to x
131 # if two values are equidistant, return the larger
132 def nearest(x
, list):
133 return reduce(lambda a
, b
: closest(x
, a
, b
), list)
135 def closest(x
, a
, b
):
136 if abs(x
- a
) < abs(x
- b
) or (abs(x
- a
) == abs(x
- b
) and a
> b
):
141 def nearestTivoHeight(height
):
142 return nearest(height
, getValidHeights())
144 def nearestTivoWidth(width
):
145 return nearest(width
, getValidWidths())
147 def getTivoHeight(tsn
):
148 if tsn
and config
.has_section('_tivo_' + tsn
):
150 height
= config
.getint('_tivo_' + tsn
, 'height')
151 return nearestTivoHeight(height
)
152 except NoOptionError
:
155 height
= config
.getint('Server', 'height')
156 return nearestTivoHeight(height
)
157 except NoOptionError
: #defaults for S3/S2 TiVo
163 def getTivoWidth(tsn
):
164 if tsn
and config
.has_section('_tivo_' + tsn
):
166 width
= config
.getint('_tivo_' + tsn
, 'width')
167 return nearestTivoWidth(width
)
168 except NoOptionError
:
171 width
= config
.getint('Server', 'width')
172 return nearestTivoWidth(width
)
173 except NoOptionError
: #defaults for S3/S2 TiVo
179 def getAudioBR(tsn
= None):
180 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
181 #compare audio_br to max_audio_br and return lowest
182 if tsn
and config
.has_section('_tivo_' + tsn
):
184 audiobr
= int(max(int(strtod(config
.get('_tivo_' + tsn
, 'audio_br'))/1000), 64)/64)*64
185 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
186 except NoOptionError
:
189 audiobr
= int(max(int(strtod(config
.get('Server', 'audio_br'))/1000), 64)/64)*64
190 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
191 except NoOptionError
: #defaults for S3/S2 TiVo
197 def getVideoBR(tsn
= None):
198 if tsn
and config
.has_section('_tivo_' + tsn
):
200 return config
.get('_tivo_' + tsn
, 'video_br')
201 except NoOptionError
:
204 return config
.get('Server', 'video_br')
205 except NoOptionError
: #defaults for S3/S2 TiVo
213 return str(int(strtod(config
.get('Server', 'max_video_br'))/1000)) + 'k'
214 except NoOptionError
: #default to 17Mi
219 return config
.get('Server', 'bufsize')
220 except NoOptionError
: #default 1024k
223 def getMaxAudioBR(tsn
= None):
224 #convert to non-zero multiple of 64 for ffmpeg compatibility
225 if tsn
and config
.has_section('_tivo_' + tsn
):
227 return int(int(strtod(config
.get('_tivo_' + tsn
, 'max_audio_br'))/1000)/64)*64
228 except NoOptionError
:
231 return int(int(strtod(config
.get('Server', 'max_audio_br'))/1000)/64)*64
232 except NoOptionError
:
233 return int(448) #default to 448
235 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
236 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
237 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
239 prefixes
= {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
240 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
241 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
242 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
243 p
= re
.compile(r
'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
246 raise SyntaxError('Invalid bit value syntax')
247 (coef
, prefix
, power
, byte
) = m
.groups()
251 exponent
= float(prefixes
[prefix
])
254 value
= float(coef
) * pow(2.0, exponent
/ 0.3)
257 value
= float(coef
) * pow(10.0, exponent
)
258 if byte
== 'B': # B == Byte, b == bit