我正在与其他库一起编写一个脚本,它需要RGB24格式的帧或图像。为了提高兼容性,我决定允许外部管道将帧流式传输到这个程序中。每次在代码中使用更改设备或源代码可能会变得乏味,使用解析器简单地指定源代码会导致语法错误。示例:ffmpeg -f dshow -i video="OEM Device" a.mpg
完全符合你的想法。但是在python的子进程中
^{pr2}$
编辑我已尝试手动拆分“python”设备内部也不工作。
导致“无效参数”OEM将OEM和设备分离为两个不同的变量/参数。我也试过另一个名字。在
这让我相信
“
是问题所在。
这让我通过终端将视频流导入python。在ffmpeg -i a.mpg -f image2pipe -vcode rawvideo -pix_fmt rgb24 - |python myscript.py
这是我在剧本里写的。在import subprocess as sp
import numpy
import sys
import os
pipe = sp.Popen('ffmpeg -f rawvideo -pix_fmt rgb24 -an -vcodec rawvideo -i - -f image2pipe -pix_fmt rgb24 -an -vcodec rawvideo -'.split(), stdin=sys.stdin, stderr=sp.PIPE, stdout=sp.PIPE)
#Assumeing 720x576 resolution
raw_img = pipe.stdout.read(720*576*3)
image = numpy.fromstring(raw_img, dtype='uint8')
img_load = image.reshape(576, 720, 3)
我知道上面的管是不需要的,可能可以用(我试过的)代替raw_img = sys.stdin.read(720*576*3)
不管这两种情况,它通常都会产生输出,结果image.reshape(576,720,3)
接收不规则尺寸,且不符合规定的720x576。我不得不承认这是第一次在python中使用管道。据我所知,当我指定image2pipe时,stderr被抑制。在
如何让ffmpeg为python提供所需的维度,或者为子进程提供语法,这样就可以在给定的命令中使用“,而不会拆分值或导致语法错误?