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
:
45 shares
= [ (section
, dict(config
.items(section
))) for section
in config
.sections() if not(section
.startswith('_tivo_') or section
== 'Server') ]
47 for name
, data
in shares
:
48 if not data
.get('auto_subshares', 'False').lower() == 'true':
51 base_path
= data
['path']
52 for item
in os
.listdir(base_path
):
53 item_path
= os
.path
.join(base_path
, item
)
54 if not os
.path
.isdir(item_path
):
57 new_name
= name
+ '/' + item
59 new_data
['path'] = item_path
61 shares
.append( (new_name
, new_data
) )
68 return config
.getboolean('Server', 'debug')
69 except NoOptionError
, ValueError:
74 return config
.getboolean('Server', 'optres')
75 except NoOptionError
, ValueError:
78 def get(section
, key
):
79 return config
.get(section
, key
)
81 def getFFMPEGTemplate(tsn
):
82 if tsn
and config
.has_section('_tivo_' + tsn
):
84 return config
.get('_tivo_' + tsn
, 'ffmpeg_prams', raw
=True)
89 return config
.get('Server', 'ffmpeg_prams', raw
=True)
90 except NoOptionError
: #default
91 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 -'
93 def getHDtivos(): # tsn's of High Definition Tivo's
97 return [1920, 1440, 1280, 720, 704, 544, 480, 352]
99 def getValidHeights():
100 return [1080, 720, 480] # Technically 240 is also supported
102 # Return the number in list that is nearest to x
103 # if two values are equidistant, return the larger
104 def nearest(x
, list):
105 return reduce(lambda a
, b
: closest(x
,a
,b
), list)
108 if abs(x
-a
) < abs(x
-b
) or (abs(x
-a
) == abs(x
-b
)and a
>b
):
113 def nearestTivoHeight(height
):
114 return nearest(height
, getValidHeights())
116 def nearestTivoWidth(width
):
117 return nearest(width
, getValidWidths())
119 def getTivoHeight(tsn
):
120 if tsn
and config
.has_section('_tivo_' + tsn
):
122 height
= int(config
.get('_tivo_' + tsn
, 'height'))
123 return nearestTivoHeight(height
)
124 except NoOptionError
:
128 height
= int(config
.get('Server', 'height'))
129 return nearestTivoHeight(height
)
130 except NoOptionError
: #default
133 def getTivoWidth(tsn
):
134 if tsn
and config
.has_section('_tivo_' + tsn
):
136 width
= int(config
.get('_tivo_' + tsn
, 'width'))
137 return nearestTivoWidth(width
)
138 except NoOptionError
:
142 width
= int(config
.get('Server', 'width'))
143 return nearestTivoWidth(width
)
144 except NoOptionError
: #default
147 def getAudioBR(tsn
= None):
148 #convert to non-zero multiple of 64 to ensure ffmpeg compatibility
149 #compare audio_br to max_audio_br and return lowest
150 if tsn
and config
.has_section('_tivo_' + tsn
):
152 audiobr
= int(max(int(strtod(config
.get('_tivo_' + tsn
, 'audio_br'))/1000), 64)/64)*64
153 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
154 except NoOptionError
:
158 audiobr
= int(max(int(strtod(config
.get('Server', 'audio_br'))/1000), 64)/64)*64
159 return str(min(audiobr
, getMaxAudioBR(tsn
))) + 'k'
160 except NoOptionError
: #default to 192
163 def getAudioCodec(tsn
= None):
164 #check for HD tivo and return compatible audio parameters
165 if tsn
and tsn
[:3] in getHDtivos():
166 return '-acodec ac3 -ar 48000'
168 return '-acodec mp2 -ac 2 -ar 44100'
170 def getVideoBR(tsn
= None):
171 if tsn
and config
.has_section('_tivo_' + tsn
):
173 return config
.get('_tivo_' + tsn
, 'video_br')
174 except NoOptionError
:
178 return config
.get('Server', 'video_br')
179 except NoOptionError
: #default to 4096K
184 return str(int(strtod(config
.get('Server', 'max_video_br'))/1000)) + 'k'
185 except NoOptionError
: #default to 17Mi
190 return config
.get('Server', 'bufsize')
191 except NoOptionError
: #default 1024k
194 def getMaxAudioBR(tsn
= None):
195 #convert to non-zero multiple of 64 for ffmpeg compatibility
196 if tsn
and config
.has_section('_tivo_' + tsn
):
198 return int(int(strtod(config
.get('_tivo_' + tsn
, 'max_audio_br'))/1000)/64)*64
199 except NoOptionError
:
203 return int(int(strtod(config
.get('Server', 'max_audio_br'))/1000)/64)*64
204 except NoOptionError
:
205 if tsn
and tsn
[:3] in getHDtivos():
206 return int(448) #default to 448, max supported by HD TiVo's
208 return int(384) #default to 384, max supported by mp2 audio (S2 TiVo)
211 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
212 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
213 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
215 prefixes
= {"y":-24,"z":-21,"a":-18,"f":-15,"p":-12,"n":-9,"u":-6,"m":-3,"c":-2,"d":-1,"h":2,"k":3,"K":3,"M":6,"G":9,"T":12,"P":15,"E":18,"Z":21,"Y":24}
216 p
= re
.compile(r
'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
219 raise SyntaxError('Invalid bit value syntax')
220 (coef
, prefix
, power
, byte
) = m
.groups()
224 exponent
= float(prefixes
[prefix
])
227 value
= float(coef
) * pow(2.0, exponent
/0.3)
230 value
= float(coef
) * pow(10.0, exponent
)
231 if byte
== "B": # B==Byte, b=bit