我了解到在Python中执行命令时,我应该使用子进程.我想要实现的是通过ffmpeg编码文件并观察程序输出直到文件完成.Ffmpeg将进度记录到stderr.
如果我尝试这样的事情:
child = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE)
complete = False
while not complete:
stderr = child.communicate()
# Get progress
print "Progress here later"
if child.poll() is not None:
complete = True
time.sleep(2)
Run Code Online (Sandbox Code Playgroud)
调用child.communicate()并等待命令完成后,程序不会继续.还有其他方法可以跟随输出吗?
所以我试图保存我的输出,subprocess.call但我不断收到以下错误:
AttributeError: 'int' object has no attribute 'communicate'
代码如下:
p2 = subprocess.call(['./test.out', 'new_file.mfj', 'delete1.out'], stdout = PIPE)
output = p2.communicate[0]
Run Code Online (Sandbox Code Playgroud)