I need to parse a video stream (mpeg ts) from proprietary network protocol (which I already know how to do) and then I would like to use OpenCV to process the video stream into frames. I know how to use cv::VideoCapture from a file or from a standard URL, but I would like to setup OpenCV to read from a buffer(s) in memory where I can store the video stream data until it is needed. Is there a way to setup a call back method (or any other interfrace) so that I can still use the cv::VideoCapture object? Is there a better way to accomplish processing the video with out writing it out to a file and then re-reading it. I would also entertain using FFMPEG directly if that is a better choice. I think I can convert AVFrames to Mat if needed.
I need to parse a video stream (mpeg ts) from proprietary network protocol (which
Share
I had a similar need recently. I was looking for a way in OpenCV to play a video that was already in memory, but without ever having to write the video file to disk. I found out that the FFMPEG interface already supports this through
av_open_input_stream. There is just a little more prep work required compared to theav_open_input_filecall used in OpenCV to open a file.Between the following two websites I was able to piece together a working solution using the ffmpeg calls. Please refer to the information on these websites for more details:
http://ffmpeg.arrozcru.org/forum/viewtopic.php?f=8&t=1170
http://cdry.wordpress.com/2009/09/09/using-custom-io-callbacks-with-ffmpeg/
To get it working in OpenCV, I ended up adding a new function to the
CvCapture_FFMPEGclass:I provided access to it through a new API call in the highgui DLL, similar to
cvCreateFileCapture. The newopenBufferfunction is basically the same as theopen( const char* _filename )function with the following difference:is replaced by:
Also, make sure to call
av_close_input_streamin theCvCapture_FFMPEG::close()function instead ofav_close_input_filein this situation.Now the
read_buffercallback function that is passed in toavio_alloc_contextI defined as:This solution assumes the entire video is contained in a memory buffer and would probably have to be tweaked to work with streaming data.
So that’s it! Btw, I’m using OpenCV version 2.1 so YMMV.