Changeset 047584cfad186d4dd2c6feee9e21e74f663b76fc
- Timestamp:
- 11/24/06 18:36:58 (2 years ago)
- git-parent:
- Files:
-
- httpserver.py (modified) (3 diffs)
- templates/container.tmpl (modified) (1 diff)
- transcode.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
httpserver.py
rf17b49b r047584c 3 3 from urlparse import urlparse 4 4 from cgi import parse_qs 5 6 5 from Cheetah.Template import Template 6 from transcode import output_video 7 7 8 8 class TivoHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): … … 91 91 t.files = files 92 92 t.total = totalFiles 93 t.start = index 93 94 t.isdir = isdir 94 95 self.wfile.write(t) … … 108 109 #rfile = open(container['path'] + path[len(name)+1:], 'rb') 109 110 #shutil.copyfileobj(rfile, self.wfile) 110 import transcode2 111 transcode2.transcode(container['path'] + path[len(name)+1:], self.wfile) 111 output_video(container['path'] + path[len(name)+1:], self.wfile) 112 112 113 113 def infopage(self): templates/container.tmpl
rf17b49b r047584c 1 1 <?xml version="1.0" encoding="ISO-8859-1" ?> 2 2 <TiVoContainer> 3 <ItemStart> 0</ItemStart>3 <ItemStart>$start</ItemStart> 4 4 <ItemCount>#echo len($files) #</ItemCount> 5 5 <Details> transcode.py
rf17b49b r047584c 1 import subprocess, shutil, os 1 import subprocess, shutil, os, re 2 3 def output_video(inFile, outFile): 4 if tivo_compatable(inFile): 5 f = file(inFile, 'rb') 6 shutil.copyfileobj(f, outFile) 7 f.close() 8 else: 9 transcode(inFile, outFile) 2 10 3 11 def transcode(inFile, outFile): 12 13 cmd = "ffmpeg_mp2.exe -i \"%s\" -vcodec mpeg2video -r 29.97 -b 4096 %s -ac 2 -ab 192 -f vob -" % (inFile, select_aspect(inFile)) 14 print cmd 15 ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE) 4 16 try: 5 cmd = "ffmpeg_mp2.exe -y -i \"%s\" -vcodec mpeg2video -s 720x480 -r 29.97 -b 4096 -aspect 4:3 -ac 2 -ab 192 -f vob -" % inFile6 ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE)7 17 shutil.copyfileobj(ffmpeg.stdout, outFile) 8 18 except: 19 win32kill(ffmpeg.pid) 20 21 def select_aspect(inFile): 22 type, height, width, fps = video_info(inFile) 23 print type, height, width, fps 24 25 d = gcd(height,width) 26 27 rheight, rwidth = height/d, width/d 28 print height, width 29 30 if (rheight, rwidth) == (4, 3): 31 return '-aspect 4:3 -s 720x480' 32 elif (rheight, rwidth) == (16, 9): 33 return '-aspect 16:9 -s 720x480' 34 else: 35 settings = [] 36 settings.apppend('-aspect 16:9') 37 38 endHeight = (720*width)/height 39 if endHeight % 2: 40 endHeight -= 1 41 print endHeight 42 43 settings.append('-s 720x' + str(endHeight)) 44 45 topPadding = ((480 - endHeight)/2) 46 if topPadding % 2: 47 topPadding -= 1 48 49 settings.append('-padtop ' + str(topPadding)) 50 bottomPadding = (480 - endHeight) - topPadding 51 settings.append('-padbottom ' + str(bottomPadding)) 52 53 return ' '.join(settings) 54 55 def tivo_compatable(inFile): 56 suportedModes = [[720, 480], [704, 480], [544, 480], [480, 480], [352, 480]] 57 type, height, width, fps = video_info(inFile) 58 59 if not type == 'mpeg2video': 60 return False 61 62 if not fps == '29.97': 63 return False 64 65 for mode in suportedModes: 66 if (mode[0], mode[1]) == (height, width): 67 return True 68 return False 69 70 def video_info(inFile): 71 cmd = "ffmpeg_mp2.exe -i \"%s\"" % inFile 72 ffmpeg = subprocess.Popen(cmd, stderr=subprocess.PIPE) 73 output = ffmpeg.stderr.read() 74 75 rezre = re.compile(r'.*Video: (.+), (\d+)x(\d+), (.+) fps.*') 76 m = rezre.search(output) 77 if m: 78 return m.group(1), int(m.group(2)), int(m.group(3)), m.group(4) 79 else: 80 return None, None, None, None 81 82 def win32kill(pid): 9 83 import ctypes 10 handle = ctypes.windll.kernel32.OpenProcess(1, False, ffmpeg.pid)84 handle = ctypes.windll.kernel32.OpenProcess(1, False, pid) 11 85 ctypes.windll.kernel32.TerminateProcess(handle, -1) 12 86 ctypes.windll.kernel32.CloseHandle(handle) 87 88 def gcd(a,b): 89 while b: 90 a, b = b, a % b 91 return a
