Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV won't capture frames from a RTMP source, while FFmpeg does

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

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.

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)

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?

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.

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.

Any idea, thank you!

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)

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?

like image 546
user2957378 Avatar asked May 07 '15 08:05

user2957378


People also ask

How do I use FFmpeg with OpenCV?

OpenCV can use the FFmpeg library (http://ffmpeg.org/) as backend to record, convert and stream audio and video. FFmpeg is a complete, cross-reference solution. If you enable FFmpeg while configuring OpenCV than CMake will download and install the binaries in OPENCV_SOURCE_CODE/3rdparty/ffmpeg/ .

How to increase fps in cv2?

Use threading to obtain higher FPS The “secret” to obtaining higher FPS when processing video streams with OpenCV is to move the I/O (i.e., the reading of frames from the camera sensor) to a separate thread. You see, accessing your webcam/USB camera using the cv2.

What is cv2 VideoCapture?

cv2. VideoCapture – Creates a video capture object, which would help stream or display the video.

What is the difference between OpenCV and FFmpeg?

OpenCV won't capture frames from a RTMP source, while FFmpeg does Ask Question Asked6 years, 9 months ago Active4 years, 8 months ago Viewed10k times

Is it possible to read OpenCV data from a pipe?

This is my attempt so far. If you can get OpenCV to read from a pipe rather than trying to write to a file or read frame by frame, you'll avoid a lot of complexity + overhead.

Why won't OpenCV handle my frames?

From there you can handle your frames like when you get it from your cam. if it still doesn't work, check if you have a valid version of ffmpeg linked with your opencv. You can check it with print (cv2.getBuildInformation ())

How many frames does G_capture read before Ret becomes false?

However, using the following loop: <pre> g_capture = cv2.VideoCapture ('fence.wmv') ret = True while ret: ret,im = g_capture.read () cv2.imshow ("video", im) cv2.waitKey (2) </pre> results in only ~178 or so frames read before ret becomes False and things start failing.


2 Answers

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;
        }
    }

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.

like image 143
GPrathap Avatar answered Jan 02 '23 16:01

GPrathap


python -c "import cv2; print(cv2.getBuildInformation())"   

check build opencv with ffmpeg。If it is correct, your code should be fine。

enter image description here

If not, rebuild opencv with ffmpeg。 Under osx

brew install opencv --with-ffmpeg
like image 35
alex hunter Avatar answered Jan 02 '23 17:01

alex hunter