从 ffmpeg 流式传输视频并使用 OpenCV 捕获 [英] Stream video from ffmpeg and capture with OpenCV

查看:113
本文介绍了从 ffmpeg 流式传输视频并使用 OpenCV 捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 rtp 传输到 ffmpeg 的视频流,我想将其通过管道传输到我的 OpenCV 工具以进行实时流处理.rtp 链接正在工作,因为我能够将传入的数据发送到文件并播放它(或者如果通过 ffplay 播放).我的 OpenCV 实现也能正常工作,因为我能够从文件和网络摄像头捕获视频.

I have a video stream coming in on rtp to ffmpeg and I want to pipe this to my OpenCV tools for live streaming processing. The rtp linkage is working because I am able to send the incoming data to a file and play it (or play if via ffplay). My OpenCV implementation is functional as well because I am able to capture video from a file and also a webcam.

问题是流向 OpenCV.我听说这可以使用命名管道来完成.首先,我可以将 ffmpeg 输出流式传输到管道,然后让 OpenCV 打开此管道并开始处理.

The problem is the streaming to OpenCV. I have heard that this may be done using a named pipe. First I could stream the ffmpeg output to the pipe and then have OpenCV open this pipe and begin processing.

我尝试过的:

我在我的 cygwin bash 中创建了一个命名管道:

I make a named-pipe in my cygwin bash by:

    $ mkfifo stream_pipe

接下来我使用我的 ffmpeg 命令从 rtp 中提取流并将其发送到管道:

Next I use my ffmpeg command to pull the stream from rtp and send it to the pipe:

    $ ffmpeg -f avi -i rtp://xxx.xxx.xxx.xxx:1234 -f avi -y out.avi > stream_pipe

我不确定这是否是将流发送到命名管道的正确方法,但它似乎正在接受命令并工作,因为 ffmpeg 的输出为我提供了比特率、fps 等.

I am not sure if this is the right way to go about sending the stream to the named pipe but it seems to be accepting the command and work because of the output from ffmpeg gives me bitrates, fps, and such.

接下来我在我的 OpenCV 捕获函数中使用命名管道:

Next I use the named pipe in my OpenCV capture function:

    $ ./cvcap.exe stream_pipe

cvcap.cpp 的代码归结为:

where the code for cvcap.cpp boils down to this:

    cv::VideoCapture *pIns = new cv::VideoCapture(argv[1]);

程序在到达这一行时似乎挂起,所以,我想知道这是否是解决此问题的正确方法.我以前从未使用过命名管道,我不确定这是否正确使用.另外,我不知道我是否需要在 OpenCV 中以不同的方式处理命名管道——更改代码以接受这种输入.就像我说的,我的代码已经接受文件和相机输入,我只是挂断了一个进来的流.我只听说命名管道可以用于 OpenCV——我还没有看到任何实际的代码或命令!

The program seems to hang when reaching this one line, so, I am wondering if this is the right way of going about this. I have never used named pipes before and I am not sure if this is the correct usage. In addition, I don't know if I need to handle the named pipe differently in OpenCV--change code around to accept this kind of input. Like I said, my code already accepts files and camera inputs, I am just hung up on a stream coming in. I have only heard that named pipes can be used for OpenCV--I haven't seen any actual code or commands!

感谢任何帮助或见解!

更新:

我相信命名管道可能无法按我预期的方式工作.如 cygwin 论坛帖子所示:

I believe named pipes may not be working in the way I intended. As seen on this cygwin forum post:

问题是 Cygwin 的 fifos 实现非常有问题.除了最简单的应用程序,我不建议将 fifos 用于任何其他应用程序.

The problem is that Cygwin's implementation of fifos is very buggy. I wouldn't recommend using fifos for anything but the simplest of applications.

我可能需要找到另一种方法来做到这一点.我试图将 ffmpeg 输出通过管道传输到一个普通文件中,然后让 OpenCV 同时读取它.这在某种程度上是有效的,但我想同时从文件中读取和写入可能很危险——谁知道会发生什么!

I may need to find another way to do this. I have tried to pipe the ffmpeg output into a normal file and then have OpenCV read it at the same time. This works to some extent, but I imagine in can be dangerous to read and write from a file concurrently--who knows what would happen!

推荐答案

希望现在回答还为时不晚,但我前段时间尝试过同样的事情,以下是我的做法.

hope it's not too late to answer, but I have tried the same thing some time ago, and here is how I did it.

OpenCV 的视频解码后端实际上是 ffmpeg,所以它的所有工具也可以在 OpenCV 中使用.不是所有的接口都暴露出来,这增加了一些困难,但你可以将rtp流地址发送给OpenCV.

The video-decoding backend for OpenCV is actually ffmpeg, so all its facitilites are available in OpenCV as well. Not all the interface is exposed, and that adds some difficulties, but you can send the rtp stream address to OpenCV.

cap.open("rtp://xxx.xxx.xxx.xxx:1234");

重要:OpenCV 无法访问受密码保护的 rtp 流.为此,您需要提供用户名和密码,没有公开的 API.

Important: OpenCV is not able to access password-protected rtp streams. To do that, you would need to provide the username and the password, there is no API exposed for it.

这篇关于从 ffmpeg 流式传输视频并使用 OpenCV 捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆