FFMPEG和Pythons子过程 [英] FFMPEG and Pythons subprocess
问题描述
我正在尝试为 FFMPEG
写一个gui。我正在使用pythons子进程来为每个我想要的转换创建一个ffmpeg进程。这样做很好,但是我也希望获得转换的进度,无论它是否失败等。我想我可以通过访问进程的stdout来执行此操作:
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时,是切换到使用,而不是子进程
, pexpect
(或在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屋!