HRESULT CVideoSource: GetFameImageFromAVI (TCHAR * szAVIFileSource, TCHAR * szImageFileDest)
{
HRESULT hr;
// Define the IMediaDet interface instance
CComPtr <IMediaDet> pDet;
Hr = CoCreateInstance (CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER,
IID_IMediaDet, (void **) & pDet );
If (FAILED (hr ))
Return hr;
// Convert the video file name to the BSTR type
CComBSTR openBSTR (szAVIFileSource );
// Set the file association for the IMediaDet Interface
Hr = pDet-> put_Filename (openBSTR );
If (FAILED (hr ))
Return hr;
// Retrieve video streams and audio streams from a video
Long lStreams;
Hr = pDet-> get_OutputStreams (& lStreams );
If (FAILED (hr ))
Return hr;
// Extract the video stream of the video because the frame information is stored in the video stream.
Bool bFound = false;
For (int I = 0; I <lStreams; I ++)
{
GUID major_type;
Hr = pDet-> put_CurrentStream (I );
If (SUCCEEDED (hr ))
Hr = pDet-> get_StreamType (& major_type );
If (FAILED (hr ))
Break;
If (major_type = MEDIATYPE_Video)
{
BFound = true;
Break;
}
}
If (! BFound)
Return VFW_E_INVALIDMEDIATYPE;
Long width = 0, height = 0; // store the width and height of the bitmap (unit: pixels)
AM_MEDIA_TYPE mt;
Hr = pDet-> get_StreamMediaType (& mt );
If (SUCCEEDED (hr ))
{
If (mt. formattype = FORMAT_VideoInfo )&&
(Mt. cbFormat> = sizeof (VIDEOINFOHEADER )))
{
// Get the VIDEOINFOHEADER structure pointer. The VIDEOINFOHEADER structure includes some
// Related information, which contains the BITMAPINFORHEADER Structure
VIDEOINFOHEADER * pVih = (VIDEOINFOHEADER *) (mt. pbFormat );
Width = pVih-> bmiHeader. biWidth;
Height = pVih-> bmiHeader. biHeight;
}
Else
Hr = VFW_E_INVALIDMEDIATYPE;
MyFreeMediaType (mt); // release the AM_MEDIA_TYPE Structure
}
If (FAILED (hr ))
Return hr;
CComBSTR saveBSTR (szImageFileDest );
// Save 2nd frames as bitmap files in the specified path
Hr = pDet-> WriteBitmapBits (10, width, height, saveBSTR );
If (FAILED (hr ))
Return hr;
Return S_ OK;
}