#!/usr/bin/python
# Wrapper to crop videos to 16:10 widescreen using MPlayer
# Thomas Perl <thpinfo.com>; 2009-09-10

import re
import sys
import subprocess

if len(sys.argv) != 2:
    print >>sys.stderr, """
    Usage: %s /path/to/mediafile.ext
    """ % sys.argv[0]
    sys.exit(1)

filename = sys.argv[1]

target_ratio = 16./10.

p = subprocess.Popen(['mplayer', '-identify', '-vo', 'null', '-ao', 'null', \
        '-frames', '10', filename], stdout=subprocess.PIPE)

data = p.stdout.read()

width, height = -1, -1
for type, value in re.findall(r'ID_VIDEO_(WIDTH|HEIGHT)=(\d+)', data):
    if type == 'WIDTH':
        width = int(value)
    elif type == 'HEIGHT':
        height = int(value)

ratio = float(width)/float(height)

args = ['mplayer']
if ratio < target_ratio and width != -1 and height != -1:
    new_height = int(width/target_ratio)
    crop_top = int((height-new_height)/2)
    args += ['-vf', 'crop=%d:%d:%d:%d' % (width, new_height, 0, crop_top)]
args += [filename]

subprocess.Popen(args)

