<p>I would like to open a small video file and map every frames in memory (to apply some custom filter). I don't want to handle the video codec, I would rather let the library handle that for me.</p>

<p>I've tried to use Direct Show with the SampleGrabber filter (using this sample <a href="http://msdn.microsoft.com/en-us/library/ms787867(VS.85).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms787867(VS.85).aspx</a>), but I only managed to grab some frames (not every frames!). I'm quite new in video software programming, maybe I'm not using the best library, or I'm doing it wrong. </p>

<p>I've pasted a part of my code (mainly a modified copy/paste from the msdn example), unfortunately it doesn't grabb the 25 first frames as expected...</p>

<pre><code>[...]

hr = pGrabber-&gt;SetOneShot(TRUE);
hr = pGrabber-&gt;SetBufferSamples(TRUE);

pControl-&gt;Run(); // Run the graph.
pEvent-&gt;WaitForCompletion(INFINITE, &amp;evCode); // Wait till it's done.

// Find the required buffer size.
long cbBuffer = 0;
hr = pGrabber-&gt;GetCurrentBuffer(&amp;cbBuffer, NULL);

for( int i = 0 ; i &lt; 25 ; ++i )
{
    pControl-&gt;Run(); // Run the graph.
    pEvent-&gt;WaitForCompletion(INFINITE, &amp;evCode); // Wait till it's done.

    char *pBuffer = new char[cbBuffer];
    hr = pGrabber-&gt;GetCurrentBuffer(&amp;cbBuffer, (long*)pBuffer);

    AM_MEDIA_TYPE mt;
    hr = pGrabber-&gt;GetConnectedMediaType(&amp;mt);
    VIDEOINFOHEADER *pVih;
    pVih = (VIDEOINFOHEADER*)mt.pbFormat;

    [...]
}

[...]
</code></pre>

<p>Is there somebody, with video software experience, who can advise me about code or other simpler library?</p>

<p>Thanks</p>

<p>Edit:
Msdn links seems not to work (<a href="http://stackoverflow.uservoice.com/pages/general/suggestions/19963" rel="noreferrer">see the bug</a>)</p>

[{'answer_id': 37980, 'author': 'Chris de Vries', 'author_id': 3836, 'author_profile': 'https://Stackoverflow.com/users/3836', 'pm_score': 2, 'selected': False, 'text': '<p>I have used <a href="http://sourceforge.net/projects/opencvlibrary/" rel="nofollow noreferrer">OpenCV</a> to load video files and process them. It\'s also handy for many types of video processing including those useful for computer vision.</p>\n'}, {'answer_id': 37982, 'author': 'Lehane', 'author_id': 142, 'author_profile': 'https://Stackoverflow.com/users/142', 'pm_score': 1, 'selected': False, 'text': '<p>Try using the <a href="http://opencvlibrary.sourceforge.net/" rel="nofollow noreferrer">OpenCV</a> library. It definitely has the capabilities you require. </p>\n\n<p><a href="http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/index.html" rel="nofollow noreferrer">This guide</a> has a section about accessing frames from a video file.</p>\n'}, {'answer_id': 38017, 'author': 'martjno', 'author_id': 3373, 'author_profile': 'https://Stackoverflow.com/users/3373', 'pm_score': 6, 'selected': True, 'text': '<p>Currently these are the most popular video frameworks available on Win32 platforms:</p>\n\n<ol>\n<li><p>Video for Windows: old windows framework coming from the age of Win95 but still widely used because it is very simple to use. Unfortunately it supports only AVI files for which the proper VFW codec has been installed.</p></li>\n<li><p>DirectShow: standard WinXP framework, it can basically load all formats you can play with Windows Media Player. Rather difficult to use.</p></li>\n<li><p><a href="http://ffmpeg.mplayerhq.hu/" rel="noreferrer">Ffmpeg</a>: more precisely libavcodec and libavformat that comes with Ffmpeg open- source multimedia utility. It is extremely powerful and can read a lot of formats (almost everything you can play with <a href="http://www.videolan.org/vlc/" rel="noreferrer">VLC</a>) even if you don\'t have the codec installed on the system. It\'s quite complicated to use but you can always get inspired by the code of ffplay that comes shipped with it or by other implementations in open-source software. Anyway I think it\'s still much easier to use than DS (and much faster). It needs to be comipled by MinGW on Windows, but all the steps are explained very well <a href="http://arrozcru.no-ip.org/ffmpeg/" rel="noreferrer">here</a> (in this moment the link is down, hope not dead).</p></li>\n<li><p><a href="http://developer.apple.com/quicktime/download/" rel="noreferrer">QuickTime</a>: the Apple framework is not the best solution for Windows platform, since it needs QuickTime app to be installed and also the proper QuickTime codec for every format; it does not support many formats, but its quite common in professional field (so some codec are actually only for QuickTime). Shouldn\'t be too difficult to implement.</p></li>\n<li><p><a href="http://www.gstreamer.net/" rel="noreferrer">Gstreamer</a>: latest open source framework. I don\'t know much about it, I guess it wraps over some of the other systems (but I\'m not sure).</p></li>\n</ol>\n\n<p>All of this frameworks have been implemented as backend in OpenCv Highgui, except for DirectShow. The default framework for Win32 OpenCV is using VFW (and thus able only to open some AVI files), if you want to use the others you must download the CVS instead of the official release and still do some hacking on the code and it\'s anyway not too complete, for example FFMPEG backend doesn\'t allow to seek in the stream.\nIf you want to use QuickTime with OpenCV <a href="http://path.berkeley.edu/~zuwhan/QTforOpenCVWin/" rel="noreferrer">this</a> can help you.</p>\n'}, {'answer_id': 38047, 'author': 'Adam Mitz', 'author_id': 2574, 'author_profile': 'https://Stackoverflow.com/users/2574', 'pm_score': 2, 'selected': False, 'text': '<p>Using the "Callback" model of SampleGrabber may give you better results.  See the example in Samples\\C++\\DirectShow\\Editing\\GrabBitmaps.</p>\n\n<p>There\'s also a lot of info in Samples\\C++\\DirectShow\\Filters\\Grabber2\\grabber_text.txt and readme.txt.</p>\n'}, {'answer_id': 872954, 'author': 'Jimmy J', 'author_id': 73869, 'author_profile': 'https://Stackoverflow.com/users/73869', 'pm_score': 1, 'selected': False, 'text': '<p>If it\'s for AVI files I\'d read the data from the AVI file myself and extract the frames. Now use the video compression manager to decompress it.</p>\n\n<p>The AVI file format is very simple, see: <a href="http://msdn.microsoft.com/en-us/library/dd318187(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/dd318187(VS.85).aspx</a> (and use google).</p>\n\n<p>Once you have the file open you just extract each frame and pass it to ICDecompress() to decompress it.</p>\n\n<p>It seems like a lot of work but it\'s the most reliable way.</p>\n\n<p>If that\'s too much work, or if you want more than AVI files then use ffmpeg.</p>\n'}, {'answer_id': 3755176, 'author': 'DJKylix', 'author_id': 453167, 'author_profile': 'https://Stackoverflow.com/users/453167', 'pm_score': 1, 'selected': False, 'text': '<p>OpenCV is the best solution if video in your case only needs to lead to a sequence of pictures. If you\'re willing to do real video processing, so ViDeo equals "Visual Audio", you need to keep up track with the ones offered by "martjno". New windows solutions also for Win7 include 3 new possibilities additionally:</p>\n\n<ol>\n<li>Windows Media Foundation: Successor of DirectShow; cleaned-up interface</li>\n<li>Windows Media Encoder 9: It does not only include the programm, it also ships libraries for coding</li>\n<li>Windows Expression 4: Successor of 2.</li>\n</ol>\n\n<p>Last 2 are commercial-only solutions, but the first one is free. To code WMF, you need to install the Windows SDK.</p>\n'}, {'answer_id': 21258871, 'author': 'Bob', 'author_id': 3154041, 'author_profile': 'https://Stackoverflow.com/users/3154041', 'pm_score': 2, 'selected': False, 'text': '<p>I know it is very tempting in C++ to get a proper breakdown of the video files and just do it yourself. But although the information is out there, it is such a long winded process building classes to hand each file format, and make it easily alterable to take future structure changes into account, that frankly it just is not worth the effort.</p>\n\n<p>Instead I recommend ffmpeg. It got a mention above, but says it is difficult, it isn\'t difficult. There are a lot more options than most people would need which makes it look more difficult than it is. For the majority of operations you can just let ffmpeg work it out for itself.</p>\n\n<p>For example a file conversion \nffmpeg -i inputFile.mp4 outputFile.avi</p>\n\n<p>Decide right from the start that you will have ffmpeg operations run in a thread, or more precisely a thread library. But have your own thread class wrap it so that you can have your own EventAgs and methods of checking the thread is finished. Something like :-</p>\n\n<pre><code>ThreadLibManager()\n{\n  List&lt;MyThreads&gt; listOfActiveThreads;\n  public AddThread(MyThreads);\n}\nYour thread class is something like:-\nclass MyThread\n{\n public Thread threadForThisInstance { get; set; }\n public MyFFMpegTools mpegTools { get; set; }\n}\nMyFFMpegTools performs many different video operations, so you want your own event  \nargs to tell your parent code precisely what type of operation has just raised and \nevent.\nenum MyFmpegArgs\n{\npublic int thisThreadID { get; set; } //Set as a new MyThread is added to the List&lt;&gt;\npublic MyFfmpegType operationType {get; set;}\n//output paths etc that the parent handler will need to find output files\n}\nenum MyFfmpegType\n{\n  FF_CONVERTFILE = 0, FF_CREATETHUMBNAIL, FF_EXTRACTFRAMES ...\n}\n</code></pre>\n\n<p>Here is a small snippet of my ffmpeg tool class, this part collecting information about a video.\nI put FFmpeg in a particular location, and at the start of the software running it makes sure that it is there. For this version I have moved it to the Desktop, I am fairly sure I have written the path correctly for you (I really hate MS\'s special folders system, so I ignore it as much as I can).</p>\n\n<p>Anyway, it is an example of using windowless ffmpeg.</p>\n\n<pre><code>        public string GetVideoInfo(FileInfo fi)\n    {\n        outputBuilder.Clear();\n        string strCommand = string.Concat(" -i \\"", fi.FullName, "\\"");\n        string ffPath =   \n  System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\\\ffmpeg.exe";\n        string oStr = "";\n\n        try\n        {\n            Process build = new Process();\n            //build.StartInfo.WorkingDirectory = @"dir";\n            build.StartInfo.Arguments = strCommand;\n            build.StartInfo.FileName = ffPath;\n\n            build.StartInfo.UseShellExecute = false;\n            build.StartInfo.RedirectStandardOutput = true;\n            build.StartInfo.RedirectStandardError = true;\n            build.StartInfo.CreateNoWindow = true;\n            build.ErrorDataReceived += build_ErrorDataReceived;\n            build.OutputDataReceived += build_ErrorDataReceived;\n            build.EnableRaisingEvents = true;\n            build.Start();\n            build.BeginOutputReadLine();\n            build.BeginErrorReadLine();\n            build.WaitForExit();\n\n\n            string findThis = "start";\n            int offset = 0;\n            foreach (string str in outputBuilder)\n            {\n                if (str.Contains("Duration"))\n                {\n                    offset = str.IndexOf(findThis);\n                    oStr = str.Substring(0, offset);\n                }\n            }\n        }\n        catch\n        {\n            oStr = "Error collecting file information";\n        }\n\n        return oStr;\n    }\n    private void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)\n    {\n        string strMessage = e.Data;\n        if (outputBuilder != null &amp;&amp; strMessage != null)\n        {\n            outputBuilder.Add(string.Concat(strMessage, "\\n"));\n        }\n    } \n</code></pre>\n'}, {'answer_id': 73831637, 'author': 'aaries', 'author_id': 19002577, 'author_profile': 'https://Stackoverflow.com/users/19002577', 'pm_score': 0, 'selected': False, 'text': '<p>I would recommend FFMPEG or GStreamer. Try and stay away from openCV unless you plan to utilize some other functionality than just streaming video. The library is a beefy build and a pain to install from source to configure FFMPEG/+GStreamer options.</p>\n'}]
['https://Stackoverflow.com/questions/37956', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1578/']