OpenCV不会从RTMP源捕获帧,而FFmpeg会捕获 [英] OpenCV won't capture frames from a RTMP source, while FFmpeg does

查看:918
本文介绍了OpenCV不会从RTMP源捕获帧,而FFmpeg会捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是每秒从rtmp流中捕获一个帧,并使用OpenCV处理它.我将FFmpeg版本N-71899-g6ef3426和OpenCV 2.4.9与Java接口一起使用(但我首先尝试使用Python). 目前,我只能采用简单且肮脏的解决方案,即使用FFmpeg捕获图像,将其存储在磁盘中,然后从我的OpenCV程序中读取这些图像.这是我正在使用的FFmpeg命令:

my goal is to capture a frame from a rtmp stream every second, and process it using OpenCV. I'm using FFmpeg version N-71899-g6ef3426 and OpenCV 2.4.9 with the Java interface (but I'm first experimenting with Python). For the moment, I can only take the simple and dirty solution, which is to capture images using FFmpeg, store them in disk, and then read those images from my OpenCV program. This is the FFmpeg command I'm using:

ffmpeg -i "rtmp://antena3fms35livefs.fplive.net:1935/antena3fms35live-live/stream-lasexta_1 live=1" -r 1 capImage%03d.jpg

这目前对我有用,至少在这个具体的rtmp来源中.然后,我需要以适当的方式从我的OpenCV程序中读取这些图像.我实际上尚未实现这一部分,因为我正在尝试寻找更好的解决方案.

This is currently working for me, at least with this concrete rtmp source. Then I would need to read those images from my OpenCV program in a proper way. I have not actually implemented this part, because I'm trying to find a better solution.

我认为理想的方法是直接从OpenCV捕获rtmp帧,但是我找不到实现此目的的方法.这是我正在使用的Python代码:

I think the ideal way would be to capture the rtmp frames directly from OpenCV, but I cannot find the way to do it. This is the code in Python I'm using:

cv2.namedWindow("camCapture", cv2.CV_WINDOW_AUTOSIZE)
cap = cv2.VideoCapture()
cap.open('"rtmp://antena3fms35livefs.fplive.net:1935/antena3fms35live-live/stream-lasexta_1 live=1"')
if not cap.open:
    print "Not open"
while (True):
    err,img = cap.read()
    if img and img.shape != (0,0):
        cv2.imwrite("img1", img)
        cv2.imshow("camCapture", img)
    if err:
        print err
        break
    cv2.waitKey(30)

我也没有尝试用read()函数,而是尝试了grab()和retrieve()函数,但没有任何好的结果.每次都执行read()函数,但是没有收到"img"或"err". 还有其他方法吗?还是无法从像这样的流中直接从OpenCV 2.4.9获取帧?

Instead of read() function, I'm also trying with grab() and retrieve() functions without any good result. The read() function is being executed every time, but no "img" or "err" is received. Is there any other way to do it? or maybe there is no way to get frames directly from OpenCV 2.4.9 from a stream like this?

我已经阅读过OpenCV使用FFmpeg来执行此类任务,但是如您所见,就我而言,FFmpeg能够从流中获取帧,而OpenCV却不能.

I've read OpenCV uses FFmpeg to do this kind of tasks, but as you can see, in my case FFmpeg is able to get frames from the stream while OpenCV is not.

在找不到直接从OpenCV获取帧的方法的情况下,我的下一个想法是以某种方式将FFmpeg输出传递给OpenCV,这似乎很难实现.

In the case I could not find the way to get the frames directly from OpenCV, my next idea is to pipe somehow, FFmpeg output to OpenCV, which seems harder to implement.

任何想法, 谢谢!

更新1: 我在Windows 8.1中.由于我是从Eclipse PyDev运行python脚本的,因此这次我从cmd运行它,并且收到以下警告:

UPDATE 1: I'm in Windows 8.1. Since I was running the python script from Eclipse PyDev, this time I run it from cmd instead, and I'm getting the following warning:

warning: Error opening file (../../modules/highgui/src/cap_ffmpeg_impl.hpp:545)

据我所知,此警告表示文件路径错误,或不支持编解码器.现在,问题是一样的. OpenCV无法从该来源获取帧吗?

This warning means, as far as I could read, that either the file-path is wrong, or either the codec is not supported. Now, the question is the same. Is OpenCV not capable of getting the frames from this source?

推荐答案

实际上,我花了一天多的时间来弄清楚如何解决此问题.终于,我在此链接的帮助下解决了这个问题. 这是客户端代码.

Actually I have spent more that one day to figure out how to solve this issue. Finally I have solved this problem with the help of this link. Here is client side code.

    #include <opencv2/core/core.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/opencv.hpp>

    using namespace cv;

    int main(int, char**) {
        cv::VideoCapture vcap;
        cv::Mat image;
        const std::string videoStreamAddress = "rtmp://192.168.173.1:1935/live/test.flv";
        if(!vcap.open(videoStreamAddress)) {
            std::cout << "Error opening video stream or file" << std::endl;
            return -1;
        }

        cv::namedWindow("Output Window");
        cv::Mat edges;
        for(;;) {
            if(!vcap.read(image)) {
                std::cout << "No frame" << std::endl;
                cv::waitKey();
            }
            cv::imshow("Output Window", image);
            if(cv::waitKey(1) >= 0) break;
        }
    }

注意:在这种情况下,我创建了一个Android应用程序来获取实时视频并将其发送到rtmp服务器 wowza 部署在PC中.因此,我在这里创建了用于实时视频处理的c ++实现.

Note: In this case I have created a android application to get real time video and send it to rtmp server wowza which is deployed in PC.So that is where I created this c++ implementation for real time video processing.

这篇关于OpenCV不会从RTMP源捕获帧,而FFmpeg会捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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