Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>av_open_input_stream</code>. There is just a little more prep work required compared to the <code>av_open_input_file</code> call used in OpenCV to open a file.</p> <p>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:</p> <p><a href="http://ffmpeg.arrozcru.org/forum/viewtopic.php?f=8&amp;t=1170">http://ffmpeg.arrozcru.org/forum/viewtopic.php?f=8&amp;t=1170</a></p> <p><a href="http://cdry.wordpress.com/2009/09/09/using-custom-io-callbacks-with-ffmpeg/">http://cdry.wordpress.com/2009/09/09/using-custom-io-callbacks-with-ffmpeg/</a></p> <p>To get it working in OpenCV, I ended up adding a new function to the <code>CvCapture_FFMPEG</code> class:</p> <pre><code>virtual bool openBuffer( unsigned char* pBuffer, unsigned int bufLen ); </code></pre> <p>I provided access to it through a new API call in the highgui DLL, similar to <code>cvCreateFileCapture</code>. The new <code>openBuffer</code> function is basically the same as the <code>open( const char* _filename )</code> function with the following difference:</p> <pre><code>err = av_open_input_file(&amp;ic, _filename, NULL, 0, NULL); </code></pre> <p>is replaced by:</p> <pre><code>ic = avformat_alloc_context(); ic-&gt;pb = av_alloc_put_byte(pBuffer, bufLen, 0, pBuffer, read_buffer, NULL, NULL); if(!ic-&gt;pb) { // handle error } // Need to probe buffer for input format unless you already know it AVProbeData probe_data; probe_data.buf_size = (bufLen &lt; 4096) ? bufLen : 4096; probe_data.filename = "stream"; probe_data.buf = (unsigned char *) malloc(probe_data.buf_size); memcpy(probe_data.buf, pBuffer, 4096); AVInputFormat *pAVInputFormat = av_probe_input_format(&amp;probe_data, 1); if(!pAVInputFormat) pAVInputFormat = av_probe_input_format(&amp;probe_data, 0); // cleanup free(probe_data.buf); probe_data.buf = NULL; if(!pAVInputFormat) { // handle error } pAVInputFormat-&gt;flags |= AVFMT_NOFILE; err = av_open_input_stream(&amp;ic , ic-&gt;pb, "stream", pAVInputFormat, NULL); </code></pre> <p>Also, make sure to call <code>av_close_input_stream</code> in the <code>CvCapture_FFMPEG::close()</code> function instead of <code>av_close_input_file</code> in this situation.</p> <p>Now the <code>read_buffer</code> callback function that is passed in to <code>av_alloc_put_byte</code> I defined as:</p> <pre><code>static int read_buffer(void *opaque, uint8_t *buf, int buf_size) { // This function must fill the buffer with data and return number of bytes copied. // opaque is the pointer to private_data in the call to av_alloc_put_byte (4th param) memcpy(buf, opaque, buf_size); return buf_size; } </code></pre> <p>This solution assumes the entire video is contained in a memory buffer and would probably have to be tweaked to work with streaming data.</p> <p>So that's it! Btw, I'm using OpenCV version 2.1 so YMMV.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload