1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#!/usr/bin/python
# Wrapper to crop videos to 16:10 widescreen using MPlayer
# Thomas Perl <thp@gpodder.org>; 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)
|