Directshow video file bitmap Extraction

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.