Click here to Skip to main content
15,890,512 members
Articles / Desktop Programming / MFC
Article

Extracting bitmaps from movies using DirectShow

Rate me:
Please Sign up or sign in to vote.
4.41/5 (25 votes)
4 Sep 20011 min read 634.7K   11.8K   114   153
An article showing how to extract a frame from a movie using DirectShow

Sample Image - FrameGrabberDemo.gif

Introduction

This article explains how to use the ISampleGrabber interface to grab a frame from a movie. We'll add the SampleGrabber filter to the graphbuilders filter list and use it to extract the bitmap. It'll show the necessary steps how to create the filter, create the graph, start it upp and grab the frame.

Setup the enviroment, we are using ATL smartpointers and DirectShow

#include "AtlBase.h"	// For atl smart pointers
#include "dShow.h"	// DirectShow header
#include "Qedit.h"	// SampleGrabber filter

The project has to be linked with Strmbase.lib

Since we're using COM we have to call CoInitialize() and CoUninitialize() in InitInstance, make sure the dialog destructor is called before CoUninitialize is called.

BOOL CFrameGrabberApp::InitInstance()
{
...
...
	CoInitialize(NULL);
	{
		CFrameGrabberDemoDlg dlg;
		m_pMainWnd = &dlg;
		int nResponse = dlg.DoModal();
	}
	CoUninitialize();
...
...
}

Step 1: Create the GraphBuilder

CComPtr<IGraphBuilder> pGraphBuilder;
HRESULT hr = ::CoCreateInstance(CLSID_FilterGraph, NULL, 
              CLSCTX_INPROC_SERVER,IID_IGraphBuilder,(void**)&pGraphBuilder);

Step 2: Create the Grabber filter and add it to the graph builder

CComPtr<IBaseFilter> pGrabberBaseFilter;
CComPtr<ISampleGrabber> pSampleGrabber;
AM_MEDIA_TYPE mt;
hr = ::CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
                        IID_IBaseFilter, (LPVOID *)&pGrabberBaseFilter);
if (FAILED(hr))
	return hr;
pGrabberBaseFilter->QueryInterface(IID_ISampleGrabber, (void**)&pSampleGrabber);
if (pSampleGrabber == NULL)
	return E_NOINTERFACE;
hr = pGraphBuilder->AddFilter(pGrabberBaseFilter,L"Grabber");
if (FAILED(hr))
	return hr;

Step 3: Setup the media type we're interrested in and render the file. The graph builder will now setup all the filters it needs to render the movie including the sample grabber we added.

ZeroMemory(&mt,sizeof(AM_MEDIA_TYPE));
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;
mt.formattype = FORMAT_VideoInfo; 
hr = pSampleGrabber->SetMediaType(&mt);
if (FAILED(hr)) 
	return hr;
hr = pGraphBuilder->RenderFile(wFile,NULL); 
if (FAILED(hr)) 
	return hr;

Now when the graph is created we need to tell the sample grabber to stop the graph after receiving one sample, we also tell it to copy the sample data into it's internal buffer.

hr = pSampleGrabber->SetBufferSamples(TRUE);
if (FAILED(hr)) 
	return hr; 
hr = pSampleGrabber->SetOneShot(TRUE); 
if (FAILED(hr)) 
return hr;

Step 4: Now we run the graph and collects the data from the sample grabber.

hr = pMediaControl->Run();
if (FAILED(hr)) 
	return hr; 
long evCode;
hr = pMediaEventEx->WaitForCompletion(INFINITE, &evCode); 
if (FAILED(hr)) 
	return hr; 
AM_MEDIA_TYPE MediaType; 
ZeroMemory(&MediaType,sizeof(MediaType)); 
hr = pSampleGrabber->GetConnectedMediaType(&MediaType); 
if (FAILED(hr)) 
	return hr; 
// Get a pointer to the video header. 
VIDEOINFOHEADER *pVideoHeader = (VIDEOINFOHEADER*)MediaType.pbFormat; 
if (pVideoHeader == NULL) 
	return E_FAIL; 
// The video header contains the bitmap information. 
// Copy it into a BITMAPINFO structure. 
BITMAPINFO BitmapInfo; 
ZeroMemory(&BitmapInfo, sizeof(BitmapInfo)); 
CopyMemory(&BitmapInfo.bmiHeader, &(pVideoHeader->bmiHeader), 
           sizeof(BITMAPINFOHEADER)); 

// Create a DIB from the bitmap header, and get a pointer to the buffer. 
void *buffer = NULL; 
HBITMAP hBitmap = ::CreateDIBSection(0, &BitmapInfo, DIB_RGB_COLORS, &buffer, 
                                     NULL, 0); 
GdiFlush(); 
// Copy the image into the buffer. 
long size = 0; 
hr = pSampleGrabber->GetCurrentBuffer(&size,(long *)buffer);   
if (FAILED(hr)) 
	return  hr;

Now we have the bitmap handle, the demo program takes the sample one second in the movie and displays it to the user using an picture box.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: How to control the sample number Pin
MarkusA4-Apr-02 21:27
MarkusA4-Apr-02 21:27 
AnswerRe: How to control the sample number Pin
MarkusA4-Apr-02 22:15
MarkusA4-Apr-02 22:15 
Questionhow to capture vidéo using buffers Pin
4-Mar-02 23:03
suss4-Mar-02 23:03 
AnswerRe: how to capture vidéo using buffers Pin
skwind15-May-02 19:44
skwind15-May-02 19:44 
Questionhow to synchronize Audio and Video Pin
15-Feb-02 0:19
suss15-Feb-02 0:19 
QuestionHow to convert from bitmap to avi ? Pin
12-Feb-02 18:50
suss12-Feb-02 18:50 
QuestionAM_MEDIA_TYPE to string? Pin
22-Jan-02 8:05
suss22-Jan-02 8:05 
General'Filter is in the wrong state' Pin
26-Nov-01 9:24
suss26-Nov-01 9:24 
GeneralRe: 'Filter is in the wrong state' Pin
14-Jun-02 7:07
suss14-Jun-02 7:07 
GeneralCrashes under win2k, directX8.1 Pin
23-Nov-01 2:14
suss23-Nov-01 2:14 
GeneralRe: Crashes under win2k, directX8.1 Pin
MarkusA24-Jan-02 23:52
MarkusA24-Jan-02 23:52 
Generaleats memory like cheese Pin
15-Oct-01 22:25
suss15-Oct-01 22:25 
GeneralRe: eats memory like cheese Pin
Markus Axelsson15-Oct-01 23:04
Markus Axelsson15-Oct-01 23:04 
GeneralRe: eats memory like cheese Pin
18-Oct-01 4:54
suss18-Oct-01 4:54 
QuestionWhere can i download Direct Media 6.0 SDK? Pin
15-Oct-01 21:57
suss15-Oct-01 21:57 
AnswerRe: Where can i download Direct Media 6.0 SDK? Pin
Paul A. Howes16-Oct-01 3:37
Paul A. Howes16-Oct-01 3:37 
AnswerRe: Where can i download Direct Media 6.0 SDK? Pin
10-Nov-01 17:36
suss10-Nov-01 17:36 
Questioncan someone help me? Pin
si1versand14-Oct-01 17:50
si1versand14-Oct-01 17:50 
GeneralI got problem and solved ! Pin
jinhoy28-Sep-01 1:55
jinhoy28-Sep-01 1:55 
GeneralRe: I got problem and solved ! Pin
Markus Axelsson28-Sep-01 3:31
Markus Axelsson28-Sep-01 3:31 
GeneralForgett StrmBase.lib Pin
Markus Axelsson11-Sep-01 5:28
Markus Axelsson11-Sep-01 5:28 
GeneralLink problem with this DShow project Pin
codeprj10-Sep-01 8:47
codeprj10-Sep-01 8:47 
GeneralRe: Link problem with this DShow project Pin
Markus Axelsson10-Sep-01 10:49
Markus Axelsson10-Sep-01 10:49 
GeneralRe: Link problem with this DShow project Pin
22-Oct-01 4:37
suss22-Oct-01 4:37 
GeneralRe: Link problem with this DShow project Pin
Markus Axelsson22-Oct-01 4:50
Markus Axelsson22-Oct-01 4:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.