import sys, threading, re
from PyQt4 import QtGui, QtCore
from subprocess import Popen, PIPE
input, output = sys.argv[1:3]
cmd = """mencoder -o %s -vf scale=160:128 -ovc xvid -xvidencopts bitrate=384 -oac mp3lame -lameopts cbr:br=128 -ofps 15 %s""" % (output, input)
print cmd
class StateKeeper(threading.Thread, QtCore.QObject):
def __init__(self):
threading.Thread.__init__(self)
QtCore.QObject.__init__(self)
self.daemon = True
self.stopped = False
def m_stop(self):
self.stopped = True
def run(self):
try:
command = cmd + """ 2>/dev/null | tr '\\r' '\\n'"""
# | perl -ne 'if (/\( ?(\d+)%\)/) {print "$1\\n";}'"""
r = Popen(command, shell=True, stdout=PIPE).stdout
state = r.readline()
while True:
if self.stopped:
return
try:
state = re.compile(".*\( ?(\d+)%\)").match(state).group(1)
self.emit(QtCore.SIGNAL("stateChanged(int)"), int(state))
if 100 == state:
return
except:
pass
try:
state = r.readline()
except EOFError:
return
except KeyboardInterrupt:
return
app = QtGui.QApplication(sys.argv)
bar = QtGui.QProgressBar()
bar.show()
bar.setMinimum(0)
bar.setMaximum(100)
state = StateKeeper()
QtCore.QObject.connect(state, QtCore.SIGNAL("stateChanged(int)"), bar, QtCore.SLOT("setValue(int)"))
QtCore.QObject.connect(app, QtCore.SIGNAL("aboutToQuit()"), state.m_stop)
state.start()
app.exec_()