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
.read(os
.path
.join(p
, 'pyTivo.conf'))
12 if config
.has_option('Server', 'GUID'):
13 guid
= config
.get('Server', 'GUID')
18 def getBeaconAddresses():
19 if config
.has_option('Server', 'beacon'):
20 beacon_ips
= config
.get('Server', 'beacon')
22 beacon_ips
= '255.255.255.255'
26 return config
.get('Server', 'Port')
28 def get169Setting(tsn
):
32 if config
.has_section('_tivo_' + tsn
):
33 if config
.has_option('_tivo_' + tsn
, 'aspect169'):
35 return config
.getboolean('_tivo_' + tsn
, 'aspect169')
39 if tsn
[:3] in BLACKLIST_169
:
44 def getShares(tsn
=''):
45 shares
= [(section
, dict(config
.items(section
)))
46 for section
in config
.sections()
47 if not(section
.startswith('_tivo_') or section
== 'Server')]
49 if config
.has_section('_tivo_' + tsn
):
50 if config
.has_option('_tivo_' + tsn
, 'shares'):
51 # clean up leading and trailing spaces & make sure ref is valid
53 for x
in config
.get('_tivo_' + tsn
, 'shares').split(','):
54 y
= x
.lstrip().rstrip()
55 if config
.has_section(y
):
56 tsnshares
+= [(y
, dict(config
.items(y
)))]
60 for name
, data
in shares
:
61 if not data
.get('auto_subshares', 'False').lower() == 'true':
64 base_path
= data
['path']
65 for item
in os
.listdir(base_path
):
66 item_path
= os
.path
.join(base_path
, item
)
67 if not os
.path
.isdir(item_path
):
70 new_name
= name
+ '/' + item
72 new_data
['path'] = item_path
74 shares
.append((new_name
, new_data
))
80 return config
.getboolean('Server', 'debug')
81 except NoOptionError
, ValueError:
86 return config
.getboolean('Server', 'optres')
87 except NoOptionError
, ValueError:
90 def get(section
, key
):
91 return config
.get(section
, key
)
93 def getFFMPEGTemplate(tsn
):
94 if tsn
and config
.has_section('_tivo_' + tsn
):
96 return config
.get('_tivo_' + tsn
, 'ffmpeg_prams', raw
=True)
100 return config
.get('Server', 'ffmpeg_prams', raw
=True)
101 except NoOptionError
: #default
102 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 -'
104 def isHDtivo(tsn
): # tsn's of High Definition Tivo's
105 return tsn
!= '' and tsn
[:3] in ['648', '652']
107 def getValidWidths():
108 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
110 def getValidHeights():
111 return [1080, 720, 480] # Technically 240 is also supported
113 # Return the number in list that is nearest to x
114 # if two values are equidistant, return the larger
115 def nearest(x
, list):
116 return reduce(lambda a
, b
: closest(x
, a
, b
), list)
118 def closest(x
, a
, b
):
119 if abs(x
- a
) < abs(x
- b
) or (abs(x
- a
) == abs(x
- b
) and a
> b
):
124 def nearestTivoHeight(height
):
125 return nearest(height
, getValidHeights())
127 def nearestTivoWidth(width
):
128 return nearest(width
, getValidWidths())
130 def getTivoHeight(tsn
):
131 if tsn
and config
.has_section('_tivo_' + tsn
):
133 height
= config
.getint('_tivo_' + tsn
, 'height')
134 return nearestTivoHeight(height
)
135 except NoOptionError
:
138 height
= config
.getint('Server', 'height')
139 return nearestTivoHeight(height
)
140 except NoOptionError
: #defaults for S3/S2 TiVo
146 def getTivoWidth(tsn
):
147 if tsn
and config
.has_section('_tivo_' + tsn
):
149 width
= config
.getint('_tivo_' + tsn
, 'width')
150 return nearestTivoWidth(width
)
151 except NoOptionError
:
154 width
= config
.getint('Server', 'width')
155 return nearestTivoWidth(width
)
156 except NoOptionError
: #defaults for S3/S2 TiVo
162 def getAudioBR(tsn
= None):
163 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
164 #compare audio_br to max_audio_br and return lowest
165 if tsn
and config
.has_section('_tivo_' + tsn
):
167 audiobr
= int(max(int(strtod(config
.get('_tivo_' + tsn
, 'audio_br'))/1000), 64)/64)*64
168 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
169 except NoOptionError
:
172 audiobr
= int(max(int(strtod(config
.get('Server', 'audio_br'))/1000), 64)/64)*64
173 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
174 except NoOptionError
: #defaults for S3/S2 TiVo
180 def getVideoBR(tsn
= None):
181 if tsn
and config
.has_section('_tivo_' + tsn
):
183 return config
.get('_tivo_' + tsn
, 'video_br')
184 except NoOptionError
:
187 return config
.get('Server', 'video_br')
188 except NoOptionError
: #defaults for S3/S2 TiVo
196 return str(int(strtod(config
.get('Server', 'max_video_br'))/1000)) + 'k'
197 except NoOptionError
: #default to 17Mi
202 return config
.get('Server', 'bufsize')
203 except NoOptionError
: #default 1024k
206 def getMaxAudioBR(tsn
= None):
207 #convert to non-zero multiple of 64 for ffmpeg compatibility
208 if tsn
and config
.has_section('_tivo_' + tsn
):
210 return int(int(strtod(config
.get('_tivo_' + tsn
, 'max_audio_br'))/1000)/64)*64
211 except NoOptionError
:
214 return int(int(strtod(config
.get('Server', 'max_audio_br'))/1000)/64)*64
215 except NoOptionError
:
217 return int(448) #default to 448, max supported by HD TiVo's
219 return int(384) #default to 384, max supported by mp2 audio (S2 TiVo)
221 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
222 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
223 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
225 prefixes
= {'y': -24, 'z': -21, 'a': -18, 'f': -15, 'p': -12,
226 'n': -9, 'u': -6, 'm': -3, 'c': -2, 'd': -1,
227 'h': 2, 'k': 3, 'K': 3, 'M': 6, 'G': 9,
228 'T': 12, 'P': 15, 'E': 18, 'Z': 21, 'Y': 24}
229 p
= re
.compile(r
'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
232 raise SyntaxError('Invalid bit value syntax')
233 (coef
, prefix
, power
, byte
) = m
.groups()
237 exponent
= float(prefixes
[prefix
])
240 value
= float(coef
) * pow(2.0, exponent
/ 0.3)
243 value
= float(coef
) * pow(10.0, exponent
)
244 if byte
== 'B': # B == Byte, b == bit