#include #include #include #include #include #pragma comment(lib,"strmiids") int main(int argc, char **argv) { HRESULT res; IMediaDet *pMediaDet; CoInitialize(NULL); res = CoCreateInstance(CLSID_MediaDet, NULL, CLSCTX_INPROC, IID_IMediaDet, (void **)&pMediaDet); if(FAILED(res)) { printf("CoCreateInstance(): res=0x%08x\n", res); return 1; } OLECHAR bstrFilename[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, argv[1], -1, bstrFilename, sizeof(bstrFilename) / sizeof(OLECHAR)); res = pMediaDet->put_Filename(bstrFilename); if(FAILED(res)) { printf("put_Filename(): res=0x%08x\n", res); return 1; } long lStreams, lStreamSelected; res = pMediaDet->get_OutputStreams(&lStreams); if(FAILED(res)) { printf("get_OutputStreams(): res=0x%08x\n", res); return 1; } AM_MEDIA_TYPE amMediaType; BITMAPINFOHEADER *pbih; lStreamSelected = -1; for(long lStream = 0; lStream < lStreams; lStream++) { res = pMediaDet->put_CurrentStream(lStream); if(FAILED(res)) { printf("put_CurrentStream(): res=0x%08x\n", res); return 1; } res = pMediaDet->get_StreamMediaType(&amMediaType); if(FAILED(res)) { printf("get_StreamMediaType(): res=0x%08x\n", res); return 1; } char cSelected = ' '; if(IsEqualGUID(amMediaType.majortype, MEDIATYPE_Video)) { lStreamSelected = lStream; cSelected = '*'; } OLECHAR szGUID[256]; StringFromGUID2(amMediaType.majortype, szGUID, sizeof(szGUID)); printf("[%2d] majortype: %S %c\n", lStream, IsEqualGUID(amMediaType.majortype, MEDIATYPE_Video) ? L"MEDIATYPE_Video" : IsEqualGUID(amMediaType.majortype, MEDIATYPE_Audio) ? L"MEDIATYPE_Audio" : szGUID, cSelected); StringFromGUID2(amMediaType.formattype, szGUID, sizeof(szGUID)); printf(" formattype: %S\n", IsEqualGUID(amMediaType.formattype, FORMAT_VideoInfo) ? L"FORMAT_VideoInfo" : IsEqualGUID(amMediaType.formattype, FORMAT_WaveFormatEx) ? L"FORMAT_WaveFormatEx" : szGUID); if(IsEqualGUID(amMediaType.formattype, FORMAT_VideoInfo)) { pbih = &((VIDEOINFOHEADER *)amMediaType.pbFormat)->bmiHeader; printf(" (%dx%dx%d '%4.4s')\n", pbih->biWidth, pbih->biHeight, pbih->biBitCount, &pbih->biCompression); } } if(lStreamSelected == -1) { printf("No video stream found!\n"); return 1; } res = pMediaDet->put_CurrentStream(lStreamSelected); if(FAILED(res)) { printf("put_CurrentStream(): res=0x%08x\n", res); return 1; } double fps, length; if(FAILED(pMediaDet->get_FrameRate(&fps)) || FAILED(pMediaDet->get_StreamLength(&length))) { printf("get_FrameRate/StreamLength(): res=0x%08x\n", res); return 1; } UINT nFrames = (UINT)(length * fps); printf("Length: %gs @ %gfps (%d frames of %dx%dx%d)\n", length, fps, nFrames, pbih->biWidth, pbih->biHeight, pbih->biBitCount); WNDCLASSEX wcx = {sizeof(WNDCLASSEX), 0, DefWindowProc, 0, 0, GetModuleHandle(NULL), NULL, LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW+1), NULL, "FrameGrabberClass", NULL}; RegisterClassEx(&wcx); HWND hWnd = CreateWindow(wcx.lpszClassName, NULL, WS_POPUP | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, pbih->biWidth, pbih->biHeight, NULL, NULL, wcx.hInstance, NULL); HDC hDC = GetDC(hWnd); UpdateWindow(hWnd); MSG msg; while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } for(UINT nFrame = 0; nFrame < nFrames; nFrame++) { double position = (double)nFrame / fps; long size; res = pMediaDet->GetBitmapBits(position, &size, NULL, pbih->biWidth, pbih->biHeight); if(FAILED(res)) { printf("GetBitmapBits(size): res=0x%08x\n", res); return 1; } char *pBuffer = new char[size]; res = pMediaDet->GetBitmapBits(position, NULL, pBuffer, pbih->biWidth, pbih->biHeight); if(FAILED(res)) { printf("GetBitmapBits(buffer): res=0x%08x\n", res); return 1; } res = SetDIBitsToDevice(hDC, 0, 0, pbih->biWidth, pbih->biHeight, 0, 0, 0, pbih->biHeight, pBuffer + ((BITMAPINFO *)pBuffer)->bmiHeader.biSize, (BITMAPINFO *)pBuffer, DIB_RGB_COLORS); if(res != pbih->biHeight) { printf("SetDIBitsToDevice(): only set %d scanline (error %d)\n", res, GetLastError()); return 1; } /*Sleep(1000); RedrawWindow(hWnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW); Sleep(200);*/ delete pBuffer; } ReleaseDC(hWnd, hDC); DestroyWindow(hWnd); pMediaDet->Release(); CoUninitialize(); return 0; }