S3 mpeg settings
[pyTivo.git] / plugins / video / video.py
blob2759835a6c3a335c5de4761b31143d55247e3343
1 import transcode, os, socket, re
2 from Cheetah.Template import Template
3 from plugin import Plugin
4 from urllib import unquote_plus, quote, unquote
5 from urlparse import urlparse
6 from xml.sax.saxutils import escape
7 from lrucache import LRUCache
8 from UserDict import DictMixin
9 from datetime import datetime, timedelta
10 import config
12 SCRIPTDIR = os.path.dirname(__file__)
14 CLASS_NAME = 'Video'
16 class Video(Plugin):
18 CONTENT_TYPE = 'x-container/tivo-videos'
20 def send_file(self, handler, container, name):
22 #No longer a 'cheep' hack :p
23 if handler.headers.getheader('Range') and not handler.headers.getheader('Range') == 'bytes=0-':
24 handler.send_response(206)
25 handler.send_header('Connection', 'close')
26 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
27 handler.send_header('Transfer-Encoding', 'chunked')
28 handler.send_header('Server', 'TiVo Server/1.4.257.475')
29 handler.end_headers()
30 handler.wfile.write("\x30\x0D\x0A")
31 return
33 tsn = handler.headers.getheader('tsn', '')
35 o = urlparse("http://fake.host" + handler.path)
36 path = unquote_plus(o[2])
37 handler.send_response(200)
38 handler.end_headers()
39 transcode.output_video(container['path'] + path[len(name)+1:], handler.wfile, tsn)
42 def __isdir(self, full_path):
43 return os.path.isdir(full_path)
45 def __duration(self, full_path):
46 return transcode.video_info(full_path)[4]
48 def __est_size(self, full_path, tsn = ''):
49 #Size is estimated by taking audio and video bit rate adding 2%
51 if transcode.tivo_compatable(full_path): # Is TiVo compatible mpeg2
52 return int(os.stat(full_path).st_size)
53 else: # Must be re-encoded
54 audioBPS = strtod(config.getAudioBR())
55 videoBPS = strtod(config.getVideoBR())
56 bitrate = audioBPS + videoBPS
57 return int((self.__duration(full_path)/1000)*(bitrate * 1.02 / 8))
59 def __getMetadataFromTxt(self, full_path):
60 metadata = {}
62 default_file = os.path.join(os.path.split(full_path)[0], 'default.txt')
63 description_file = full_path + '.txt'
65 metadata.update(self.__getMetadataFromFile(default_file))
66 metadata.update(self.__getMetadataFromFile(description_file))
68 return metadata
70 def __getMetadataFromFile(self, file):
71 metadata = {}
73 if os.path.exists(file):
74 for line in open(file):
75 if line.strip().startswith('#'):
76 continue
77 if not ':' in line:
78 continue
80 key, value = line.split(':', 1)
81 key = key.strip()
82 value = value.strip()
84 if key.startswith('v'):
85 if key in metadata:
86 metadata[key].append(value)
87 else:
88 metadata[key] = [value]
89 else:
90 metadata[key] = value
92 return metadata
94 def __metadata(self, full_path):
96 metadata = {}
98 base_path, title = os.path.split(full_path)
99 now = datetime.now()
100 originalAirDate = datetime.fromtimestamp(os.stat(full_path).st_ctime)
101 duration = self.__duration(full_path)
102 duration_delta = timedelta(milliseconds = duration)
104 metadata['title'] = '.'.join(title.split('.')[:-1])
105 metadata['seriesTitle'] = os.path.split(base_path)[1]
106 metadata['originalAirDate'] = originalAirDate.isoformat()
107 metadata['time'] = now.isoformat()
108 metadata['startTime'] = now.isoformat()
109 metadata['stopTime'] = (now + duration_delta).isoformat()
111 metadata.update( self.__getMetadataFromTxt(full_path) )
113 metadata['size'] = self.__est_size(full_path)
114 metadata['duration'] = duration
116 min = duration_delta.seconds / 60
117 sec = duration_delta.seconds % 60
118 hours = min / 60
119 min = min % 60
120 metadata['iso_durarion'] = 'P' + str(duration_delta.days) + 'DT' + str(hours) + 'H' + str(min) + 'M' + str(sec) + 'S'
122 return metadata
124 def QueryContainer(self, handler, query):
126 tsn = handler.headers.getheader('tsn', '')
127 subcname = query['Container'][0]
128 cname = subcname.split('/')[0]
130 if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query):
131 handler.send_response(404)
132 handler.end_headers()
133 return
135 def video_file_filter(file, type = None):
136 full_path = file
137 if os.path.isdir(full_path):
138 return True
139 return transcode.suported_format(full_path)
141 files, total, start = self.get_files(handler, query, video_file_filter)
143 videos = []
144 for file in files:
145 video = VideoDetails()
146 video['name'] = os.path.split(file)[1]
147 video['path'] = file
148 video['title'] = os.path.split(file)[1]
149 video['is_dir'] = self.__isdir(file)
150 if not video['is_dir']:
151 video.update(self.__metadata(file))
153 videos.append(video)
155 handler.send_response(200)
156 handler.end_headers()
157 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
158 t.name = subcname
159 t.total = total
160 t.start = start
161 t.videos = videos
162 t.quote = quote
163 t.escape = escape
164 handler.wfile.write(t)
166 def TVBusQuery(self, handler, query):
168 file = query['File'][0]
169 path = self.get_local_path(handler, query)
170 file_path = os.path.join(path, file)
172 file_info = VideoDetails()
173 file_info.update(self.__metadata(file_path))
175 handler.send_response(200)
176 handler.end_headers()
177 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'TvBus.tmpl'))
178 t.video = file_info
179 t.escape = escape
180 handler.wfile.write(t)
182 class VideoDetails(DictMixin):
184 def __init__(self, d = None):
185 if d:
186 self.d = d
187 else:
188 self.d = {}
190 def __getitem__(self, key):
191 if key not in self.d:
192 self.d[key] = self.default(key)
193 return self.d[key]
195 def __contains__(self, key):
196 return True
198 def __setitem__(self, key, value):
199 self.d[key] = value
201 def __delitem__(self):
202 del self.d[key]
204 def keys(self):
205 return self.d.keys()
207 def __iter__(self):
208 return self.d.__iter__()
210 def iteritems(self):
211 return self.d.iteritems()
213 def default(self, key):
214 defaults = {
215 'showingBits' : '0',
216 'episodeNumber' : '0',
217 'displayMajorNumber' : '0',
218 'displayMinorNumber' : '0',
219 'isEpisode' : 'true',
220 'colorCode' : ('COLOR', '4'),
221 'showType' : ('SERIES', '5'),
222 'tvRating' : ('NR', '7'),
224 if key in defaults:
225 return defaults[key]
226 elif key.startswith('v'):
227 return []
228 else:
229 return ''
232 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
233 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
234 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
235 def strtod(value):
236 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}
237 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
238 m = p.match(value)
239 if m is None:
240 raise SyntaxError('Invalid bit value syntax')
241 (coef, prefix, power, byte) = m.groups()
242 if prefix is None:
243 value = float(coef)
244 else:
245 exponent = float(prefixes[prefix])
246 if power == "i":
247 # Use powers of 2
248 value = float(coef) * pow(2.0, exponent/0.3)
249 else:
250 # Use powers of 10
251 value = float(coef) * pow(10.0, exponent)
252 if byte == "B": # B==Byte, b=bit
253 value *= 8;
254 return value