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

asked 2015-05-07 11:17:38 -0600

Gabriel Barreras gravatar image

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!

edit retag flag offensive close merge delete

Comments

any chance, you are on win ? cv2.pyd needs access to opencv_ffmpeg249.dll there, so put it on the PATH or right into the site-packages folder

berak gravatar imageberak ( 2015-05-07 11:38:18 -0600 )edit

Yes, I'm in Windows 8.1. I've tried what you suggested and the result was the same. However, since I was running it 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?

Gabriel Barreras gravatar imageGabriel Barreras ( 2015-05-07 12:56:03 -0600 )edit