#!/usr/bin/python2
import os, sys, pygame, signal, time
from subprocess import Popen, STDOUT

screen = pygame.display.set_mode((640, 480))

fmt = '%08d.jpg'

tempi = 2
def processimages():
    global tempi
    while os.path.exists(fmt % tempi):
        f = fmt % (tempi - 1)
        process(f)
        tempi += 1

foo = pygame.Surface((640, 480))
last = None
def process(f):
    global last
    print f,
    img = pygame.image.load(f)
    count = pygame.transform.threshold(foo, img, (255, 255, 255), (16, 16, 16))
    print count
    if count < 10:
        os.remove(f)
    else:
        screen.blit(img, (0, 0))
        pygame.display.update()

avi = sys.argv[1]
convert = Popen(['mplayer', '-quiet', '-vo', 'jpeg:quality=95', avi], stdin=open('/dev/null', 'r'), stdout=open('/dev/null', 'w'), stderr=STDOUT)
while convert.poll() is None:
    pygame.event.get()
    time.sleep(.05)
    convert.send_signal(signal.SIGSTOP)
    processimages()
    convert.send_signal(signal.SIGCONT)
processimages()
f = fmt % (tempi - 1)
if os.path.exists(f):
    process(f)

# vim: ts=4 et
