- pyTivo
[pyTivo.git] / plugins / video / video.py
blob8d08fc4e1a8f05d281cb4e8bbfc988cf77676b04
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
9 SCRIPTDIR = os.path.dirname(__file__)
12 class video(Plugin):
14 content_type = 'x-container/tivo-videos'
15 playable_cache = LRUCache(1000)
17 def SendFile(self, handler, container, name):
19 #No longer a 'cheep' hack :p
20 if handler.headers.getheader('Range') and not handler.headers.getheader('Range') == 'bytes=0-':
21 handler.send_response(206)
22 handler.send_header('Connection', 'close')
23 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
24 handler.send_header('Transfer-Encoding', 'chunked')
25 handler.send_header('Server', 'TiVo Server/1.4.257.475')
26 handler.end_headers()
27 handler.wfile.write("\x30\x0D\x0A")
28 return
30 tsn = handler.headers.getheader('tsn', '')
32 o = urlparse("http://fake.host" + handler.path)
33 path = unquote_plus(o[2])
34 handler.send_response(200)
35 handler.end_headers()
36 transcode.output_video(container['path'] + path[len(name)+1:], handler.wfile, tsn)
40 def QueryContainer(self, handler, query):
42 subcname = query['Container'][0]
43 cname = subcname.split('/')[0]
45 if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query):
46 handler.send_response(404)
47 handler.end_headers()
48 return
50 path = self.get_local_path(handler, query)
51 def isdir(file):
52 return os.path.isdir(os.path.join(path, file))
54 def duration(file):
55 full_path = os.path.join(path, file)
56 return self.playable_cache[full_path]
58 def est_size(file):
59 full_path = os.path.join(path, file)
60 #Size is estimated by taking audio and video bit rate adding 2%
61 if transcode.tivo_compatable(full_path): # Is TiVo compatible mpeg2
62 return int(os.stat(full_path).st_size)
63 else: # Must be re-encoded
64 return int((duration(file)/1000)*((4288 * 1.02 * 1000)/8))
66 def VideoFileFilter(file):
67 full_path = os.path.join(path, file)
69 if full_path in self.playable_cache:
70 return self.playable_cache[full_path]
71 if os.path.isdir(full_path):
72 self.playable_cache[full_path] = True
73 return True
74 millisecs = transcode.suported_format(full_path)
75 if millisecs:
76 self.playable_cache[full_path] = millisecs
77 return millisecs
78 else:
79 self.playable_cache[full_path] = False
80 return False
82 handler.send_response(200)
83 handler.end_headers()
84 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
85 t.name = subcname
86 t.files, t.total, t.start = self.get_files(handler, query, VideoFileFilter)
87 t.duration = duration
88 t.est_size = est_size
89 t.isdir = isdir
90 t.quote = quote
91 t.escape = escape
92 handler.wfile.write(t)