Python Monitoring Progress Of Handbrake
So I am using handbrake and python to encode videos based on a schedule. I need to monitor the progress because I use it to estimate the encoding time. Then I can fit it to my sche
Solution 1:
You need to read from stdout, not stderr.
profile = ["HandBrakeCLI","-i",input,"-o","output","-e","x264"]
cp = subprocess.Popen(profile, stderr=subprocess.PIPE, strout=subprocess.PIPE, bufsize=1)
for line initer(cp.stdout.readline, b''):
# regex match for % complete and ETA
matches = re.match( r'.*(\d+\.\d+)\s%.*ETA\s(\d+)h(\d+)m(\d+)s', line.decode('utf-8') )
if matches:
print( matches.group() )
print(line),
cp.stderr.close()
cp.stdout.close()
cp.wait()
Using a progress wrapper (using clint.textui.progress.Bar) and read byte by byte (works for me):
profile = ["HandBrakeCLI","-i",input,"-o","output","-e","x264"]
cp = subprocess.Popen(profile, stderr=subprocess.PIPE, strout=subprocess.PIPE, close_fds=True)
bar = Bar(label="Encoding %s" % input, width=30, expected_size=10000, every=1)
bar.show(0)
line = ""
c = 0whileTrue:
nl = cp.stdout.read(1)
if nl == ''and cp.poll() isnotNone:
break# Aborted, no characters available, process died.if nl == "\n":
line = ""elif nl == "\r":
# regex match for % complete and ETA, assuming the regex is ok.
matches = re.match( r'.*(\d+\.\d+)\s%.*ETA\s(\d+)h(\d+)m(\d+)s', line.decode('utf-8') )
if matches:
print( matches.group() )
# do something
line = ""else:
line += nl
error = cp.stderr.read()
success = "Encode done!"in error
Did not test the code, rewrote it to match the threads initial post.
Hope that helps.
Solution 2:
There are good bones here. However, I had to make a few modification to make it work with Python 3.7 and PyQt5. The ui lines are for a PyQt5 QProgressBar and a QLineEdit
This code has been tested. I thank all of you for your help.
def hbConvertISOtoMP4():
line = ""
inFile = #place your input file here!
oFile = #place your output file here!
ui.progressBar.setValue(0)
profile = ["HandBrakeCLI", "-t", "1", "-i", inFile, "-o", oFile, "-e", "x264"]
cp = Popen(profile, stderr=PIPE, stdout=PIPE, close_fds=True)
ui.leProgress.setText('Loading data... Please Wait.')
ui.centralwidget.repaint()
whileTrue:
nl = cp.stdout.read(1)
if nl == ''or cp.poll() isnotNone:
break# Aborted, no characters available, process died.elif nl.hex() == '0d'andlen(line) > 30:
# regex match for % complete and ETA, assuming the regex is ok.
matches = re.match(r'.*(\d+\.\d+)\s%.*ETA\s(\d+)h(\d+)m(\d+)s\)', line)
if matches:
# do something here# I inserted this code for a PyQt5 Progress Bar UI
ui.leProgress.setText(line)
ui.centralwidget.repaint()
pBar = matches.group().split(' ')
ui.progressBar.setValue(int(float(pBar[5])))
line = ""else:
line += nl.decode('utf-8')
error = cp.stderr.read()
if'Encode done!'instr(error):
ui.progressBar.setValue(0)
ui.leProgress.setText("Encode Done!")
else:
ui.leProgress.setText('Error during Endoding')
Post a Comment for "Python Monitoring Progress Of Handbrake"