directshow 抓取视频帧

#include "dshow.h"
#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
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老猿的春天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值