手制动器的Python监测进展

手制动器的Python监测进展,python,subprocess,progress-bar,handbrake,Python,Subprocess,Progress Bar,Handbrake,所以我使用handbrake和python根据时间表对视频进行编码。我需要监控进度,因为我使用它来估计编码时间。然后我可以把它安装到我的调度程序中 我在获取过程中的ETA和完成百分比时遇到问题。这是我到目前为止所拥有的 profile = ["HandBrakeCLI","-i",input,"-o","output","-e","x264"] cp = subprocess.Popen(profile, stderr=subprocess.PIPE, bufsize=1) for line i

所以我使用handbrake和python根据时间表对视频进行编码。我需要监控进度,因为我使用它来估计编码时间。然后我可以把它安装到我的调度程序中

我在获取过程中的ETA和完成百分比时遇到问题。这是我到目前为止所拥有的

profile = ["HandBrakeCLI","-i",input,"-o","output","-e","x264"]
cp = subprocess.Popen(profile, stderr=subprocess.PIPE, bufsize=1)
for line in iter(cp.stderr.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.wait()
这不匹配,事实上我不完全确定到底发生了什么。当我运行脚本时,我会看到ETA和%complete打印输出

Encoding: task 1 of 1, 1.19 % (45.57 fps, avg 62.74 fps, ETA 00h08m01s)

我尝试过使用stdout,但它也不起作用。

您需要从stdout读取,而不是从stderr读取

profile = ["HandBrakeCLI","-i",input,"-o","output","-e","x264"]
cp = subprocess.Popen(profile, stderr=subprocess.PIPE, strout=subprocess.PIPE, bufsize=1)
for line in iter(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()
使用进度包装器(使用clint.textui.progress.Bar)并逐字节读取(对我有用):

没有测试代码,重新编写代码以匹配线程初始post


希望有帮助。

这里有好骨头。然而,我不得不做一些修改,使其能够与Python3.7和PyQt5一起工作。ui行用于PyQt5 QProgressBar和QLineEdit

这段代码已经过测试。我感谢你们所有人的帮助

def HBCONVERTOMP4():


我很确定问题在于Handbrake CLI不会在每次进程进展一点时输出新的附加行,而是修改现有行。尝试在inter中的
for line之后立即打印该行。
并查看它实际为您提供了什么。这不会在for循环退出后为我提供输出吗?那么这个过程什么时候结束呢?我想在这个过程中得到信息。首先,你要迭代stderr,也许你需要迭代stdout。其次,我认为cp.stderr.readline将从子流程本身返回完整的行,而不是一个重复更新的行。我会在
handbreak
过程结束后打印最后一行,以查看它的外观。正如我告诉过你的,我会打印for循环中的每一行,以了解我实际接收到的内容。在stdout上迭代不会打印出编码信息。我不明白你的意思,你能提供一个例子吗?试着删除第5-13行,只输出
print(line)
,让我知道它实际打印的时间和内容。
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 = 0

while True:    
  nl = cp.stdout.read(1)
  if nl == '' and cp.poll() is not None:
     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
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()

while True:
    nl = cp.stdout.read(1)

    if nl == '' or cp.poll() is not None:
        break  # Aborted, no characters available, process died.

    elif nl.hex() == '0d' and len(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!' in str(error):
    ui.progressBar.setValue(0)
    ui.leProgress.setText("Encode Done!")
else:
    ui.leProgress.setText('Error during Endoding')