Changeset 047584cfad186d4dd2c6feee9e21e74f663b76fc

Show
Ignore:
Timestamp:
11/24/06 18:36:58 (2 years ago)
Author:
Jason Michalski <armooo@armooo.net>
git-committer:
Jason Michalski <armooo@armooo.net> 1164415018 +0000
git-parent:

[4170f5970d1565e433b99279c8ef30d0efc05614]

git-author:
Jason Michalski <armooo@armooo.net> 1164415018 +0000
Message:

pyTivo
- Added support to pass correct mpegs past
- Added support to pick a aspect.
- Added support to pad a video

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • httpserver.py

    rf17b49b r047584c  
    33from urlparse import urlparse 
    44from cgi import parse_qs 
    5  
    65from Cheetah.Template import Template 
     6from transcode import output_video 
    77 
    88class TivoHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer): 
     
    9191            t.files = files 
    9292            t.total = totalFiles 
     93            t.start = index 
    9394            t.isdir = isdir 
    9495            self.wfile.write(t) 
     
    108109        #rfile = open(container['path'] + path[len(name)+1:], 'rb') 
    109110        #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) 
    112112     
    113113    def infopage(self): 
  • templates/container.tmpl

    rf17b49b r047584c  
    11<?xml version="1.0" encoding="ISO-8859-1" ?> 
    22<TiVoContainer> 
    3     <ItemStart>0</ItemStart> 
     3    <ItemStart>$start</ItemStart> 
    44    <ItemCount>#echo len($files) #</ItemCount> 
    55    <Details> 
  • transcode.py

    rf17b49b r047584c  
    1 import subprocess, shutil, os 
     1import subprocess, shutil, os, re 
     2 
     3def 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) 
    210 
    311def 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) 
    416    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 -" % inFile 
    6         ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE) 
    717        shutil.copyfileobj(ffmpeg.stdout, outFile) 
    818    except: 
     19        win32kill(ffmpeg.pid) 
     20 
     21def 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 
     55def 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 
     70def 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         
     82def win32kill(pid): 
    983        import ctypes 
    10         handle = ctypes.windll.kernel32.OpenProcess(1, False, ffmpeg.pid) 
     84        handle = ctypes.windll.kernel32.OpenProcess(1, False, pid) 
    1185        ctypes.windll.kernel32.TerminateProcess(handle, -1) 
    1286        ctypes.windll.kernel32.CloseHandle(handle) 
     87 
     88def gcd(a,b): 
     89    while b: 
     90        a, b = b, a % b 
     91    return a