使用Python子进程冻结到FFMPEG的管道

2024-10-01 07:23:33 发布

您现在位置:Python中文网/ 问答频道 /正文

通过下面的代码,我可以使用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秒,这就很好了,但是如果我试图写更多的帧,代码就会挂起。这种行为的根本原因是什么?我怎样才能修好它?在


Tags: 代码fromimportcmdoutputifnpsys
1条回答
网友
1楼 · 发布于 2024-10-01 07:23:33

一个极有可能的罪魁祸首是恶心的subprocess.Popen线。不仅要忽略它的返回值-为了确保子进程在某个点完成和/或检查其退出代码,您还将stderr作为一个管道,但从不读取它,因此当缓冲区填充时,进程必须挂起。在

这应该可以修复它:

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)

相关问题 更多 >