FFMPEG 和 Pythons 子进程 [英] FFMPEG and Pythons subprocess

查看:33
本文介绍了FFMPEG 和 Pythons 子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 FFMPEG 编写 gui.我正在使用 pythons 子进程为我想要的每个转换创建一个 ffmpeg 进程.这很好用,但我也想要一种方法来获取转换的进度,无论它是否失败等等.我想我可以通过像这样访问进程的标准输出来做到这一点:

I'm trying to write a gui for FFMPEG. I'm using pythons subprocess to create a ffmpeg process for every conversion I want. This works fine, but I'd also like a way to get the progress of the conversion, whether it failed or not etc. I figured I could do this by accessing the process's stdout like so:

调用subprocess.Popen()

# Convert - Calls FFMPEG with current settings. (in a seperate
# thread.)
def convert(self):
    # Check if options are valid
    if self.input == "" or self.output == "":
        return False

# Make the command string
ffmpegString = self.makeString()

# Try to open with these settings
try:
    self.ffmpeg = subprocess.Popen(ffmpegString, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except OSError:
    self.error.append("OSError: ")
except ValueError:
    self.error.append("ValueError: Couldn't call FFMPEG with these parameters")

# Convert process should be running now.

并阅读stdout:

convert = Convert()
convert.input = "test.ogv"
convert.output = "test.mp4"
convert.output_size = (0, 0)

convert.convert()

while 1:
    print convert.ffmpeg.stdout.readline()

这有效,但是 ffmpeg 的状态没有显示.我假设它与 ffmpeg 刷新它的方式有关.有没有办法访问它?

This works but, ffmpeg's status doesn't show. I'm assuming it has something to do with way ffmpeg refreshes it. Is there a way to access it?

推荐答案

我经常注意到由于难以解决的缓冲问题,在使用子进程读取标准输出(甚至标准错误!)时出现问题.我最喜欢的解决方案,当我确实需要从子进程中读取这样的 stdout/stderr 时,是切换到使用,而不是 subprocesspexpect(或者,在 Windows 上,wexpect).

I've often noticed problems reading standard output (or even standard error!) with subprocess, due to buffering issues that are hard to defeat. My favorite solution, when I do need to read such stdout/stderr from the subprocess, is to switch to using, instead of subprocess, pexpect (or, on Windows, wexpect).

这篇关于FFMPEG 和 Pythons 子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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