5

I'm looking for a simple c/c++ lib that would allow to extract the first frame of a video as a uchar array. And have a simple function to access the next one.

I know of FFMPEG but it requiered to play with packets and things like that, and I'm surprised that nowhere on the net I can find a lib that allow something like :

Video v = openVideo("path"); uchar* data = v.getFrame(); v.nextFrame(); 

I just need to extract frames of a video to use it as a texture...no need for reencoding after or anything...

Of course something that would read the most format than possible would be great, something built upon libavcodec for example ;p

And I'm using Windows 7

1
  • libavcodec is part of the ffmpeg project. Commented Sep 22, 2010 at 19:28

3 Answers 3

4

Here's an example with OpenCV:

#include <cv.h> #include <highgui.h> int main(int argc, char **argv) { cv::VideoCapture capture(argv[1]); if (capture.grab()) { cv::Mat_<char> frame; capture.retrieve(frame); // // Convert to your byte array here // } return 0; } 

It's untested, but I cannibalized it from some existing working code, so it shouldn't take you long to get it working.

The cv::Mat_<unsigned char> is essentially a byte array. If you really need something that's explicitly of type unsigned char *, then you can malloc space of the appropriate size and iterate over the matrix using

You can convert a cv::Mat to a byte array using pixel positions(cv::Mat_::at()) or iterators (cv::Mat_::begin() and friends).

There are many reasons why libraries rarely expose image data as a simple pointer, such as:

  • It implies the entire image must occupy a contiguous space in memory. This is a big deal when dealing with large images
  • It requires committing a certain ordering of the data (pixel vs non-planar -- are the RGB planes stored interspersed or separately?) and reduces flexibility
  • Dereferencing pointers is a cause for bugs (buffer overruns, etc).

So if you want your pointer, you have to do a bit of work for it.

Sign up to request clarification or add additional context in comments.

1 Comment

Note this hides various things from you: codec selection is the big one. This typically falls back to some OS specific decoding mechanism (it can do encoding too!). So what it can actually decode will depend on your OS and its configuration...
-1

You can use OpenCV it is very simple and they have a useful manual on their site http://opencv.willowgarage.com/wiki/

Comments

-2

Use DirectShow. Here's an article: http://www.codeproject.com/KB/audio-video/framegrabber.aspx There are DirectShow 'add-ons' to decode different video formats. Here's one of the sites where you can grab free DirectShow filter packs to decode some common formats that are not directly supported by DirectShow: http://www.free-codecs.com/download/DirectShow_FilterPack.htm

1 Comment

I googled this question precisely because I am looking for a replacement for DirectShow. Is it cross-platform? Can it play strange formats out of the box? Uhm, no? I'll check the free-codecs.com website, to see if I can stop getting "pSampleGrabber->GetConnectedMediaType failed"... If it helps, I may even upvote this answer. Because what I need must not be OpenCV.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.