From 2393bc1b52479317efc5baadc25a513cd7a23e25 Mon Sep 17 00:00:00 2001 From: Jason Michalski Date: Sat, 25 Nov 2006 04:20:46 +0000 Subject: [PATCH] pyTivo - Fixed AnchorItem when a folder - Should do a better job picking an aspect ratio --- httpserver.py | 25 ++++++++++++++++++------- templates/container.tmpl | 2 +- transcode.py | 20 ++++++++++++-------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/httpserver.py b/httpserver.py index 99129bd..ebb4a86 100644 --- a/httpserver.py +++ b/httpserver.py @@ -3,7 +3,7 @@ from urllib import unquote_plus from urlparse import urlparse from cgi import parse_qs from Cheetah.Template import Template -from transcode import output_video +import transcode class TivoHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): containers = {} @@ -62,19 +62,32 @@ class TivoHTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): folders = subcname.split('/') path = container['path'] for folder in folders[1:]: + if folder == '..': + return path = path + '/' + folder + def isdir(file): + path = container['path'] + '/' + file + return os.path.isdir(path) + files = os.listdir(path) - totalFiles = len(files) + + files = filter(lambda f: os.path.isdir(path+'/'+f) or transcode.suported_format(path+'/'+f), files) + totalFiles = len(files) + + index = 0 if query.has_key('ItemCount'): count = int(query['ItemCount'] [0]) - index = 0 if query.has_key('AnchorItem'): anchor = query['AnchorItem'] [0] for i in range(len(files)): - file_url = '/' + subcname + '/' + files[i] + + if isdir(files[i]): + file_url = '/TiVoConnect?Command=QueryContainer&Container=' + subcname + '/' + files[i] + else: + file_url = '/' + subcname + '/' + files[i] if file_url == anchor: index = i + 1 break @@ -106,9 +119,7 @@ class TivoHTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler): path = unquote_plus(o.path) self.send_response(200) self.end_headers() - #rfile = open(container['path'] + path[len(name)+1:], 'rb') - #shutil.copyfileobj(rfile, self.wfile) - output_video(container['path'] + path[len(name)+1:], self.wfile) + transcode.output_video(container['path'] + path[len(name)+1:], self.wfile) def infopage(self): self.send_response(200) diff --git a/templates/container.tmpl b/templates/container.tmpl index 10258af..435d6a4 100644 --- a/templates/container.tmpl +++ b/templates/container.tmpl @@ -26,7 +26,7 @@ #else
- $file + #echo '.'.join(file.split('.')[:-1]) # video/x-tivo-mpeg video/x-tivo-mpeg 5000000536 diff --git a/transcode.py b/transcode.py index e2de99d..9e39087 100644 --- a/transcode.py +++ b/transcode.py @@ -11,7 +11,6 @@ def output_video(inFile, outFile): def transcode(inFile, outFile): cmd = "ffmpeg_mp2.exe -i \"%s\" -vcodec mpeg2video -r 29.97 -b 4096 %s -ac 2 -ab 192 -f vob -" % (inFile, select_aspect(inFile)) - print cmd ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE) try: shutil.copyfileobj(ffmpeg.stdout, outFile) @@ -20,16 +19,14 @@ def transcode(inFile, outFile): def select_aspect(inFile): type, height, width, fps = video_info(inFile) - print type, height, width, fps d = gcd(height,width) rheight, rwidth = height/d, width/d - print height, width - if (rheight, rwidth) == (4, 3): + if (rheight, rwidth) in [(4, 3), (10, 11), (15, 11), (59, 54), (59, 72), (59, 36), (59, 54)]: return '-aspect 4:3 -s 720x480' - elif (rheight, rwidth) == (16, 9): + elif (rheight, rwidth) in [(16, 9), (20, 11), (40, 33), (118, 81), (59, 27)]: return '-aspect 16:9 -s 720x480' else: settings = [] @@ -38,7 +35,6 @@ def select_aspect(inFile): endHeight = (720*width)/height if endHeight % 2: endHeight -= 1 - print endHeight settings.append('-s 720x' + str(endHeight)) @@ -69,8 +65,10 @@ def tivo_compatable(inFile): def video_info(inFile): cmd = "ffmpeg_mp2.exe -i \"%s\"" % inFile - ffmpeg = subprocess.Popen(cmd, stderr=subprocess.PIPE) + print cmd + ffmpeg = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE) output = ffmpeg.stderr.read() + print ffmpeg.stdout.read() rezre = re.compile(r'.*Video: (.+), (\d+)x(\d+), (.+) fps.*') m = rezre.search(output) @@ -78,7 +76,13 @@ def video_info(inFile): return m.group(1), int(m.group(2)), int(m.group(3)), m.group(4) else: return None, None, None, None - + +def suported_format(inFile): + if video_info(inFile)[0]: + return True + else: + return False + def win32kill(pid): import ctypes handle = ctypes.windll.kernel32.OpenProcess(1, False, pid) -- 2.11.4.GIT