1、方法1
CFileDialog dlg(TRUE, _T("*.mp4"), NULL,OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,_T("video files (*.avi) |*.avi|All Files (*.*)|*.*||"),NULL);
CString path ;
dlg.m_ofn.lpstrTitle= _T("OpenVedio");
if (dlg.DoModal() == IDOK) {
path = dlg.GetPathName();
}else
return;
IplImage* m_pFrame = NULL; // Declare IPL/OpenCV image pointer
HRESULT hr;
// 定义IMediaDet接口实例
CComPtr< IMediaDet > pDet;
hr = pDet.CoCreateInstance(__uuidof(MediaDet));
if (FAILED(hr))
return;
// 将影片文件名转换成BSTR类型
CComBSTR openBSTR(path);
// 设置IMediaDet接口的文件关联
hr = pDet->put_Filename(openBSTR);
if (FAILED(hr))
return ;
// 从影片中检索视频流和音频流
long lStreams;
hr = pDet->get_OutputStreams(&lStreams);
if (FAILED(hr))
return ;
// 取出影片的视频流,因为帧的信息是保存在视频流中的
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 ;
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结构指针,VIDEOINFOHEADER结构包含一些与视频
// 有关的信息,其中含有BITMAPINFORHEADER结构
VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)(mt.pbFormat);
width = pVih->bmiHeader.biWidth;
height = pVih->bmiHeader.biHeight;
if(height < 0 ) height *= -1;
}
else
hr = VFW_E_INVALIDMEDIATYPE;
if(mt.cbFormat != 0)
{
CoTaskMemFree((PVOID)mt.pbFormat);
mt.cbFormat = 0;
}
if (mt.pUnk != NULL)
{
mt.pUnk->Release();
mt.pUnk = NULL;
}
}
if (FAILED(hr))
return ;
long size;
double time = 0.0;
double totaltime;
double rate ;
// 获取整个视频的时间长度
pDet->get_StreamLength(&totaltime);
//每秒多少帧
pDet->get_FrameRate(&rate);
cvNamedWindow( "result", 1 );
//totaltime = totaltime*rate;
m_pFrame = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
//每秒读一帧
for(time=0.0; time <totaltime; time+= 1.0)
{
/*
hr = pDet->EnterBitmapGrabMode(time);
if(FAILED(hr))
{
printf( "Failed in EnterBitmapGrabMode! hr=0x%x\r\n", hr );
return ;
}
CComPtr < ISampleGrabber > pGrabber = NULL;
hr = pDet->GetSampleGrabber(&pGrabber);
pGrabber->GetCurrentBuffer(&size, NULL);
long m_nBufferSize = size;
pGrabber->GetCurrentBuffer(&m_nBufferSize, (long*)m_pFrame->imageData);
*/
///*
hr = pDet->GetBitmapBits(time, &size, 0, width, height);
if (FAILED(hr))
return ;
char *pBuffer = new char[size];
if (!pBuffer)
return ;
hr = pDet->GetBitmapBits(time, 0, pBuffer, width, height);
if (FAILED(hr))
return ;
m_pFrame->imageData = pBuffer + sizeof(BITMAPINFOHEADER);
//*/
cvFlip(m_pFrame);
//detect_and_draw(m_pFrame);
cvShowImage( "result", m_pFrame );
cvWaitKey( 10 );
delete[] pBuffer;
//AfxMessageBox("t");
}
cvReleaseImage(&m_pFrame);
cvDestroyWindow("result");
2 方法:
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "ffopencv.h"
int main(int argc, char* argv[])
{
IplImage *frame=NULL;
CvCapture* capture;
capture=cvCaptureFromFile_FFMPEG("lake.mpg");//D:\\Downloads\\Guns and roses\\Guns N'Roses-Welcome To The Jungle.mpg");
// capture=cvCreateFileCapture("lake.mpg");
// CvSize imgSize;
cvNamedWindow("videoS",CV_WINDOW_AUTOSIZE);
// cvResizeWindow("video1",110,1000);
cvMoveWindow( "videoS", 10, 50 ); //原始图像窗口定位
frame=cvQueryFrame(capture);
int numTotal=100000;
for(int num=1;num<numTotal;num++)
{
frame=cvQueryFrame(capture);
cvShowImage("videoS", frame);
cvWaitKey(100);
}
return 0;
}