使用Python子进程管理FFMPEG冻结 [英] Piping to FFMPEG with Python subprocess freezes
问题描述
使用以下代码,我可以使用Python,Numpy和FFMPEG二进制文件将视频框架管理到FFMPEG:
With the following code, I am able to pipe frames of a video to FFMPEG using Python, Numpy and the FFMPEG binaries:
from __future__ import print_function
import subprocess
import numpy as np
import sys
npshape = [480, 480]
cmd_out = ['ffmpeg',
'-y', # (optional) overwrite output file if it exists
'-f', 'rawvideo',
'-vcodec','rawvideo',
'-s', '%dx%d'%(npshape[1], npshape[0]), # size of one frame
'-pix_fmt', 'rgb24',
'-r', '24', # frames per second
'-i', '-', # The input comes from a pipe
'-an', # Tells FFMPEG not to expect any audio
'-vcodec', 'mpeg4',
'output.mp4']
fout = subprocess.Popen(cmd_out, stdin=subprocess.PIPE, stderr=subprocess.PIPE).stdin
for i in range(24*40):
if i%(24)==0:
print('%d'%(i/24), end=' ')
sys.stdout.flush()
fout.write((np.random.random(npshape[0]*npshape[1]*3)*128).astype('uint8').tostring())
fout.close()
如果我写了不到37秒的框架,但如果我尝试写更多的东西,代码就挂起来了。这个行为的根本原因是什么?
This works fine if I write anything less than 37 seconds worth of frames, but if I try to write anything more, the code just hangs. What is the underlying cause for this behaviour? How can I fix it?
推荐答案
一个非常可能的罪魁祸首是令人厌恶的恶臭 subprocess.Popen
行。不仅你不理会它的返回值 - 你绝对不会这样做,以确保子流程完成某一点和/或检查其退出代码 - 你也使
stderr
一个管道,但从来没有读过 - 所以当缓冲区填满时,进程必须挂起。
A highly probable culprit is the disgustingly stinky subprocess.Popen
line. Not only you ignore its return value - which you must never do, in order to ensure the subprocess' completion by certain point and/or check its exit code - you also make stderr
a pipe but never read it - so the process must be hanging when its buffer fills.
这应该修复它:
p = subprocess.Popen(cmd_out, stdin=subprocess.PIPE)
fout = p.stdin
<...>
fout.close()
p.wait()
if p.returncode !=0: raise subprocess.CalledProcessError(p.returncode,cmd_out)
这篇关于使用Python子进程管理FFMPEG冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!