Cleened up content types
[pyTivo.git] / plugins / video / video.py
blob2a05b9ab8e44a6dc5c1f7b19a2e5799b7b840b9c
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__)
12 CLASS_NAME = 'Video'
14 class Video(Plugin):
16 CONTENT_TYPE = 'x-container/tivo-videos'
18 def send_file(self, handler, container, name):
20 #No longer a 'cheep' hack :p
21 if handler.headers.getheader('Range') and not handler.headers.getheader('Range') == 'bytes=0-':
22 handler.send_response(206)
23 handler.send_header('Connection', 'close')
24 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
25 handler.send_header('Transfer-Encoding', 'chunked')
26 handler.send_header('Server', 'TiVo Server/1.4.257.475')
27 handler.end_headers()
28 handler.wfile.write("\x30\x0D\x0A")
29 return
31 tsn = handler.headers.getheader('tsn', '')
33 o = urlparse("http://fake.host" + handler.path)
34 path = unquote_plus(o[2])
35 handler.send_response(200)
36 handler.end_headers()
37 transcode.output_video(container['path'] + path[len(name)+1:], handler.wfile, tsn)
41 def QueryContainer(self, handler, query):
43 subcname = query['Container'][0]
44 cname = subcname.split('/')[0]
46 if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query):
47 handler.send_response(404)
48 handler.end_headers()
49 return
51 path = self.get_local_path(handler, query)
52 def isdir(file):
53 return os.path.isdir(os.path.join(path, file))
55 def duration(file):
56 full_path = os.path.join(path, file)
57 return transcode.video_info(full_path)[4]
59 def est_size(file):
60 full_path = os.path.join(path, file)
61 #Size is estimated by taking audio and video bit rate adding 2%
63 if transcode.tivo_compatable(full_path): # Is TiVo compatible mpeg2
64 return int(os.stat(full_path).st_size)
65 else: # Must be re-encoded
66 audioBPS = strtod(config.getAudioBR())
67 videoBPS = strtod(config.getVideoBR())
68 bitrate = audioBPS + videoBPS
69 return int((duration(file)/1000)*(bitrate * 1.02 / 8))
71 def description(file):
72 full_path = os.path.join(path, file + '.txt')
73 if os.path.exists(full_path):
74 return open(full_path).read()
75 else:
76 return ''
78 def VideoFileFilter(file):
79 full_path = os.path.join(path, file)
81 if os.path.isdir(full_path):
82 return True
83 return transcode.suported_format(full_path)
86 files, total, start = self.get_files(handler, query, VideoFileFilter)
88 videos = []
89 for file in files:
90 video = {}
92 video['name'] = file
93 video['is_dir'] = isdir(file)
94 if not isdir(file):
95 video['size'] = est_size(file)
96 video['duration'] = duration(file)
97 video['description'] = description(file)
99 videos.append(video)
101 handler.send_response(200)
102 handler.end_headers()
103 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
104 t.name = subcname
105 t.total = total
106 t.start = start
107 t.videos = videos
108 t.quote = quote
109 t.escape = escape
110 handler.wfile.write(t)
113 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
114 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
115 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
116 def strtod(value):
117 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}
118 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
119 m = p.match(value)
120 if m is None:
121 raise SyntaxError('Invalid bit value syntax')
122 (coef, prefix, power, byte) = m.groups()
123 if prefix is None:
124 value = float(coef)
125 else:
126 exponent = float(prefixes[prefix])
127 if power == "i":
128 # Use powers of 2
129 value = float(coef) * pow(2.0, exponent/0.3)
130 else:
131 # Use powers of 10
132 value = float(coef) * pow(10.0, exponent)
133 if byte == "B": # B==Byte, b=bit
134 value *= 8;
135 return value