Call ffmpeg only when really, really needed: when getting the detailed info
[pyTivo/wgw.git] / plugins / video / video.py
blob04bff4758b5d16f068cf20a52cd2317844cbff00
1 import transcode, os, socket, re, urllib, zlib
2 from Cheetah.Template import Template
3 from plugin import Plugin, quote, unquote
4 from urlparse import urlparse
5 from xml.sax.saxutils import escape
6 from lrucache import LRUCache
7 from UserDict import DictMixin
8 from datetime import datetime, timedelta
9 import config
11 SCRIPTDIR = os.path.dirname(__file__)
13 CLASS_NAME = 'Video'
15 extfile = os.path.join(SCRIPTDIR, 'video.ext')
16 try:
17 extensions = file(extfile).read().split()
18 except:
19 extensions = None
21 class Video(Plugin):
23 CONTENT_TYPE = 'x-container/tivo-videos'
25 def video_file_filter(self, full_path, type=None):
26 if os.path.isdir(full_path):
27 return True
28 if extensions:
29 return os.path.splitext(full_path)[1].lower() in extensions
30 else:
31 return transcode.supported_format(full_path)
33 def send_file(self, handler, container, name):
34 if handler.headers.getheader('Range') and \
35 handler.headers.getheader('Range') != 'bytes=0-':
36 handler.send_response(206)
37 handler.send_header('Connection', 'close')
38 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
39 handler.send_header('Transfer-Encoding', 'chunked')
40 handler.end_headers()
41 handler.wfile.write("\x30\x0D\x0A")
42 return
44 tsn = handler.headers.getheader('tsn', '')
46 o = urlparse("http://fake.host" + handler.path)
47 path = unquote(o[2])
48 handler.send_response(200)
49 handler.end_headers()
50 transcode.output_video(container['path'] + path[len(name) + 1:],
51 handler.wfile, tsn)
53 def __isdir(self, full_path):
54 return os.path.isdir(full_path)
56 def __duration(self, full_path):
57 return transcode.video_info(full_path)[4]
59 def __est_size(self, full_path, tsn = ''):
60 # Size is estimated by taking audio and video bit rate adding 2%
62 if transcode.tivo_compatable(full_path, tsn):
63 # Is TiVo-compatible mpeg2
64 return int(os.stat(full_path).st_size)
65 else:
66 # Must be re-encoded
67 audioBPS = config.strtod(config.getAudioBR(tsn))
68 videoBPS = config.strtod(config.getVideoBR(tsn))
69 bitrate = audioBPS + videoBPS
70 return int((self.__duration(full_path) / 1000) *
71 (bitrate * 1.02 / 8))
73 def __getMetadataFromTxt(self, full_path):
74 metadata = {}
76 default_file = os.path.join(os.path.split(full_path)[0], 'default.txt')
77 description_file = full_path + '.txt'
79 metadata.update(self.__getMetadataFromFile(default_file))
80 metadata.update(self.__getMetadataFromFile(description_file))
82 return metadata
84 def __getMetadataFromFile(self, file):
85 metadata = {}
87 if os.path.exists(file):
88 for line in open(file):
89 if line.strip().startswith('#'):
90 continue
91 if not ':' in line:
92 continue
94 key, value = line.split(':', 1)
95 key = key.strip()
96 value = value.strip()
98 if key.startswith('v'):
99 if key in metadata:
100 metadata[key].append(value)
101 else:
102 metadata[key] = [value]
103 else:
104 metadata[key] = value
106 return metadata
108 def __metadata_basic(self, full_path):
109 metadata = {}
111 base_path, title = os.path.split(full_path)
112 originalAirDate = datetime.fromtimestamp(os.stat(full_path).st_ctime)
114 metadata['title'] = '.'.join(title.split('.')[:-1])
115 metadata['seriesTitle'] = metadata['title'] # default to the filename
116 metadata['originalAirDate'] = originalAirDate.isoformat()
118 metadata.update(self.__getMetadataFromTxt(full_path))
120 return metadata
122 def __metadata_full(self, full_path, tsn=''):
123 metadata = {}
124 metadata.update(self.__metadata_basic(full_path))
126 now = datetime.now()
128 duration = self.__duration(full_path)
129 duration_delta = timedelta(milliseconds = duration)
131 metadata['time'] = now.isoformat()
132 metadata['startTime'] = now.isoformat()
133 metadata['stopTime'] = (now + duration_delta).isoformat()
135 metadata.update( self.__getMetadataFromTxt(full_path) )
137 metadata['size'] = self.__est_size(full_path, tsn)
138 metadata['duration'] = duration
140 min = duration_delta.seconds / 60
141 sec = duration_delta.seconds % 60
142 hours = min / 60
143 min = min % 60
144 metadata['iso_duration'] = 'P' + str(duration_delta.days) + \
145 'DT' + str(hours) + 'H' + str(min) + \
146 'M' + str(sec) + 'S'
147 return metadata
149 def QueryContainer(self, handler, query):
150 tsn = handler.headers.getheader('tsn', '')
151 subcname = query['Container'][0]
152 cname = subcname.split('/')[0]
154 if not handler.server.containers.has_key(cname) or \
155 not self.get_local_path(handler, query):
156 handler.send_response(404)
157 handler.end_headers()
158 return
160 files, total, start = self.get_files(handler, query,
161 self.video_file_filter)
163 videos = []
164 local_base_path = self.get_local_base_path(handler, query)
165 for file in files:
166 video = VideoDetails()
167 video['name'] = os.path.split(file)[1]
168 video['path'] = file
169 video['part_path'] = file.replace(local_base_path, '', 1)
170 video['title'] = os.path.split(file)[1]
171 video['is_dir'] = self.__isdir(file)
172 if video['is_dir']:
173 video['small_path'] = subcname + '/' + video['name']
174 else:
175 if len(files) > 1:
176 video['valid'] = True
177 video.update(self.__metadata_basic(file))
178 else:
179 video['valid'] = transcode.supported_format(file)
180 if video['valid']:
181 video.update(self.__metadata_full(file, tsn))
183 videos.append(video)
185 handler.send_response(200)
186 handler.end_headers()
187 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
188 t.container = cname
189 t.name = subcname
190 t.total = total
191 t.start = start
192 t.videos = videos
193 t.quote = quote
194 t.escape = escape
195 t.crc = zlib.crc32
196 t.guid = config.getGUID()
197 handler.wfile.write(t)
199 def TVBusQuery(self, handler, query):
200 tsn = handler.headers.getheader('tsn', '')
201 file = query['File'][0]
202 path = self.get_local_path(handler, query)
203 file_path = path + file
205 file_info = VideoDetails()
206 file_info['valid'] = transcode.supported_format(file_path)
207 if file_info['valid']:
208 file_info.update(self.__metadata_full(file_path, tsn))
210 handler.send_response(200)
211 handler.end_headers()
212 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'TvBus.tmpl'))
213 t.video = file_info
214 t.escape = escape
215 handler.wfile.write(t)
217 class VideoDetails(DictMixin):
219 def __init__(self, d=None):
220 if d:
221 self.d = d
222 else:
223 self.d = {}
225 def __getitem__(self, key):
226 if key not in self.d:
227 self.d[key] = self.default(key)
228 return self.d[key]
230 def __contains__(self, key):
231 return True
233 def __setitem__(self, key, value):
234 self.d[key] = value
236 def __delitem__(self):
237 del self.d[key]
239 def keys(self):
240 return self.d.keys()
242 def __iter__(self):
243 return self.d.__iter__()
245 def iteritems(self):
246 return self.d.iteritems()
248 def default(self, key):
249 defaults = {
250 'showingBits' : '0',
251 'episodeNumber' : '0',
252 'displayMajorNumber' : '0',
253 'displayMinorNumber' : '0',
254 'isEpisode' : 'true',
255 'colorCode' : ('COLOR', '4'),
256 'showType' : ('SERIES', '5'),
257 'tvRating' : ('NR', '7')
259 if key in defaults:
260 return defaults[key]
261 elif key.startswith('v'):
262 return []
263 else:
264 return ''