- pyTivo
[pyTivo.git] / plugins / video / video.py
blobb76d6bd616084454d1691dfc44aaaae859967bb9
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 import Config
10 SCRIPTDIR = os.path.dirname(__file__)
13 class video(Plugin):
14 count = 0
16 content_type = 'x-container/tivo-videos'
19 # Used for 8.3's broken requests
20 request_history = {}
22 def SendFile(self, handler, container, name):
24 #No longer a 'cheep' hack :p
25 if handler.headers.getheader('Range') and not handler.headers.getheader('Range') == 'bytes=0-':
26 handler.send_response(206)
27 handler.send_header('Connection', 'close')
28 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
29 handler.send_header('Transfer-Encoding', 'chunked')
30 handler.send_header('Server', 'TiVo Server/1.4.257.475')
31 handler.end_headers()
32 handler.wfile.write("\x30\x0D\x0A")
33 return
35 tsn = handler.headers.getheader('tsn', '')
37 o = urlparse("http://fake.host" + handler.path)
38 path = unquote_plus(o[2])
39 handler.send_response(200)
40 handler.end_headers()
41 transcode.output_video(container['path'] + path[len(name)+1:], handler.wfile, tsn)
43 def hack(self, handler, query, subcname):
45 tsn = handler.headers.getheader('tsn', '')
47 #not a tivo
48 if not tsn:
49 return query
51 try:
52 path, state = self.request_history[tsn]
53 except KeyError:
54 path = []
55 state = {}
56 self.request_history[tsn] = (path, state)
57 state['query'] = query
58 state['redirected'] = False
61 current_folder = subcname.split('/')[-1]
63 #at the root
64 if len(subcname.split('/')) == 1:
65 path[:] = [current_folder]
66 state['query'] = query
67 state['redirected'] = 0
69 #entering a new folder
70 elif 'AnchorItem' not in query:
71 path.append(current_folder)
72 state['query'] = query
74 #went down a folder
75 elif len(path) > 1 and current_folder == path[-2]:
76 path.pop()
78 #this must be crap
79 else:
80 print 'BROKEN'
81 if not state['redirected']:
82 import time
83 time.sleep(.25)
84 state['redirected'] = True
85 return None
86 else:
87 state['redirected'] = False
89 print 'Hack says', path
90 return state['query']
92 def QueryContainer(self, handler, query):
94 subcname = query['Container'][0]
96 print '========================================================================='
97 print 'Tivo said' + subcname
99 query = self.hack(handler, query, subcname)
101 if not query:
102 handler.send_response(302)
103 handler.send_header('Location ', 'http://' + handler.headers.getheader('host') + handler.path)
104 handler.end_headers()
105 return
107 keys = query.keys()
108 keys.sort()
110 #for k in keys:
111 # print k, ':',query[k]
113 cname = subcname.split('/')[0]
115 if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query):
116 handler.send_response(404)
117 handler.end_headers()
118 return
120 path = self.get_local_path(handler, query)
121 def isdir(file):
122 return os.path.isdir(os.path.join(path, file))
124 def duration(file):
125 full_path = os.path.join(path, file)
126 return transcode.video_info(full_path)[4]
128 def est_size(file):
129 full_path = os.path.join(path, file)
130 #Size is estimated by taking audio and video bit rate adding 2%
132 if transcode.tivo_compatable(full_path): # Is TiVo compatible mpeg2
133 return int(os.stat(full_path).st_size)
134 else: # Must be re-encoded
135 audioBPS = strtod(Config.getAudioBR())
136 videoBPS = strtod(Config.getVideoBR())
137 bitrate = audioBPS + videoBPS
138 return int((duration(file)/1000)*(bitrate * 1.02 / 8))
140 def VideoFileFilter(file):
141 full_path = os.path.join(path, file)
143 if os.path.isdir(full_path):
144 return True
145 return transcode.suported_format(full_path)
147 handler.send_response(200)
148 handler.end_headers()
149 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
150 t.name = subcname
151 t.files, t.total, t.start = self.get_files(handler, query, VideoFileFilter)
152 t.duration = duration
153 t.est_size = est_size
154 t.isdir = isdir
155 t.quote = quote
156 t.escape = escape
157 handler.wfile.write(t)
160 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
161 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
162 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
163 def strtod(value):
164 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}
165 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
166 m = p.match(value)
167 if m is None:
168 raise SyntaxError('Invalid bit value syntax')
169 (coef, prefix, power, byte) = m.groups()
170 if prefix is None:
171 value = float(coef)
172 else:
173 exponent = float(prefixes[prefix])
174 if power == "i":
175 # Use powers of 2
176 value = float(coef) * pow(2.0, exponent/0.3)
177 else:
178 # Use powers of 10
179 value = float(coef) * pow(10.0, exponent)
180 if byte == "B": # B==Byte, b=bit
181 value *= 8;
182 return value