#!/usr/bin/python
import os
from os.path import *
import sys
import shutil
import warnings
warnings.filterwarnings("ignore", "tempnam")


if len(sys.argv) < 2:
  print "usage: %s video frame.png [position]" % sys.argv[0]
  print ""
  print "position defaults to 00:01:00 (one minute into the video)"
  print ""
  sys.exit(1)

input_file = abspath(sys.argv[1])
output_file = abspath(sys.argv[2])
if len(sys.argv) > 3:
  position = sys.argv[3]
else:
  position = "00:01:00"
width = 128

framedir = os.tempnam()
os.mkdir(framedir)
os.chdir(framedir)
cmd = "mplayer -vo png:z=6 -vf scale -zoom -xy %d -ao null -ss '%s' -frames 2 \"%s\" >/dev/null 2>&1" % (width, position, input_file)
os.system (cmd)
files = os.listdir(framedir)
if files:
  shutil.move(os.path.join(framedir,files[-1]), output_file)
shutil.rmtree(framedir)

