通过下面的代码,我可以使用Python、Numpy和FFMPEG二进制文件将视频帧通过管道传输到FFMPEG:
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秒,这就很好了,但是如果我试图写更多的帧,代码就会挂起。这种行为的根本原因是什么?我怎样才能修好它?在
一个极有可能的罪魁祸首是恶心的
subprocess.Popen
线。不仅要忽略它的返回值-为了确保子进程在某个点完成和/或检查其退出代码,您还将stderr
作为一个管道,但从不读取它,因此当缓冲区填充时,进程必须挂起。在这应该可以修复它:
相关问题 更多 >
编程相关推荐