#include "qedit.h"
#include "atlbase.h"
//库文件 strmiids.lib
CComPtr< IMediaDet > pDet; //实例化接口
HRESULT hr;
hr = CoCreateInstance( CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER,
IID_IMediaDet, (void**) &pDet );
if(FAILED(hr))
{
}
CComBSTR openBSTR(L"E:\\电影\\一路有你.flv"); //要抓取的媒体文件
hr = pDet->put_Filename(openBSTR);
if (FAILED(hr))
{
}
long lStreams;
hr = pDet->get_OutputStreams(&lStreams);
if (FAILED(hr))
{
}
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 (major_type == MEDIATYPE_Video) //发现数据流中存在 视频流
{
bFound = true;
break;
}
}
long width = 0, height = 0;
AM_MEDIA_TYPE mt;
hr = pDet->get_StreamMediaType(&mt);
if (SUCCEEDED(hr))
{
if ((mt.formattype == FORMAT_VideoInfo) &&
(mt.cbFormat >= sizeof(VIDEOINFOHEADER)))
{
VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)(mt.pbFormat);
width = pVih->bmiHeader.biWidth; // 视频流的宽
height = pVih->bmiHeader.biHeight; // 视频流的高
}
else
hr = VFW_E_INVALIDMEDIATYPE;
MyFreeMediaType(mt); //释放结构体
if(FAILED(hr))
{
}
CComBSTR saveBSTR(L"E:\\yuyu.bmp"); // 抓取的视频保存图片
hr = pDet->WriteBitmapBits(2, width, height, saveBSTR); //保存图片 大小可任意调
if (FAILED(hr))
{
}
}
void MyFreeMediaType(AM_MEDIA_TYPE& mt)
{
if (mt.cbFormat != 0)
{
CoTaskMemFree((PVOID)mt.pbFormat);
mt.cbFormat = 0;
mt.pbFormat = NULL;
}
if (mt.pUnk != NULL)
{
// Unecessary because pUnk should not be used, but safest.
mt.pUnk->Release();
mt.pUnk = NULL;
}
}
The following code uses the GetBitmapBits method to create a device-independent bitmap.
long size;hr = pDet->GetBitmapBits(0, &size, 0, width, height);if (SUCCEEDED(hr)) {char *pBuffer = new char[size];if (!pBuffer)return E_OUTOFMEMORY;try { hr = pDet->GetBitmapBits(0, 0, pBuffer, width, height); }catch (...) { delete [] pBuffer; throw; }if (SUCCEEDED(hr)) {BITMAPINFOHEADER *bmih = (BITMAPINFOHEADER*)pBuffer; HDC hdcDest = GetDC(0); // Find the address of the start of the image data. void *pData = pBuffer + sizeof(BITMAPINFOHEADER); // Note: In general a BITMAPINFOHEADER can include extra color // information at the end, so calculating the offset to the image // data is not generally correct. However, the IMediaDet interface // always returns an RGB-24 image with no extra color information. BITMAPINFO bmi; ZeroMemory(&bmi, sizeof(BITMAPINFO)); CopyMemory(&(bmi.bmiHeader), bmih, sizeof(BITMAPINFOHEADER)); HBITMAP hBitmap = CreateDIBitmap(hdcDest, bmih, CBM_INIT, pData, &bmi, DIB_RGB_COLORS); //数据都保存在pData内了}delete[] pBuffer;}
fwrite file to save bmp file