0

所以我使用手刹和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)

我试过使用标准输出,但它也不起作用。

4

2 回答 2

1

您需要从标准输出而不是标准错误中读取。

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)并逐字节读取(对我有用):

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

没有测试代码,重写它以匹配线程初始帖子。

希望有帮助。

于 2017-10-25T10:34:05.550 回答
-1

这里有好骨头。但是,我必须进行一些修改才能使其适用于 Python 3.7 和 PyQt5。ui 行用于 PyQt5 QProgressBar 和 QLineEdit

此代码已经过测试。我感谢大家的帮助。

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()

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')
于 2020-01-20T09:46:32.757 回答