Removed an extra print
[pyTivo/krkeegan.git] / plugins / video / video.py
blob8a832e3e4f3e128e2e11b2a9ff5faa557985aba6
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):
15 content_type = 'x-container/tivo-videos'
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 transcode.video_info(full_path)[4]
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%
62 if transcode.tivo_compatable(full_path): # Is TiVo compatible mpeg2
63 return int(os.stat(full_path).st_size)
64 else: # Must be re-encoded
65 audioBPS = strtod(Config.getAudioBR())
66 videoBPS = strtod(Config.getVideoBR())
67 bitrate = audioBPS + videoBPS
68 return int((duration(file)/1000)*(bitrate * 1.02 / 8))
70 def description(file):
71 full_path = os.path.join(path, file + '.txt')
72 if os.path.exists(full_path):
73 return open(full_path).read()
74 else:
75 return ''
77 def VideoFileFilter(file):
78 full_path = os.path.join(path, file)
80 if os.path.isdir(full_path):
81 return True
82 return transcode.suported_format(full_path)
85 files, total, start = self.get_files(handler, query, VideoFileFilter)
87 videos = []
88 for file in files:
89 video = {}
91 video['name'] = file
92 video['is_dir'] = isdir(file)
93 if not isdir(file):
94 video['size'] = est_size(file)
95 video['duration'] = duration(file)
96 video['description'] = description(file)
98 videos.append(video)
100 handler.send_response(200)
101 handler.end_headers()
102 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
103 t.name = subcname
104 t.total = total
105 t.start = start
106 t.videos = videos
107 t.quote = quote
108 t.escape = escape
109 handler.wfile.write(t)
112 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
113 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
114 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
115 def strtod(value):
116 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}
117 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
118 m = p.match(value)
119 if m is None:
120 raise SyntaxError('Invalid bit value syntax')
121 (coef, prefix, power, byte) = m.groups()
122 if prefix is None:
123 value = float(coef)
124 else:
125 exponent = float(prefixes[prefix])
126 if power == "i":
127 # Use powers of 2
128 value = float(coef) * pow(2.0, exponent/0.3)
129 else:
130 # Use powers of 10
131 value = float(coef) * pow(10.0, exponent)
132 if byte == "B": # B==Byte, b=bit
133 value *= 8;
134 return value