Python监控手刹的进度 [英] Python monitoring progress of Handbrake

查看:132
本文介绍了Python监控手刹的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用手刹和python根据时间表对视频进行编码.我需要监视进度,因为我用它来估计编码时间.然后,我可以将其安装到我的调度程序中.

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 scheduler.

我在从流程中获取ETA和%的费用时遇到了问题.这是我到目前为止所拥有的

I am having an issue getting the ETA and % complete from the process. Here is what I have so far

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和完整的打印输出

It does not match, in fact I'm not entirely sure what is going on. When I run my script, I see the ETA and % complete print out

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

我已经尝试过使用stdout,但是它也不起作用.

I've tried using stdout but it doesn't work either.

推荐答案

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

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

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 = 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

未测试代码,因此将其重写为与初始帖子的线程相匹配.

Did not test the code, rewrote it to match the threads initial post.

希望有帮助.

这篇关于Python监控手刹的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆