讀AVI檔如何取得影格速率 |
尚未結案
|
dragonhippop
一般會員 發表:5 回覆:0 積分:1 註冊:2004-12-26 發送簡訊給我 |
各位大大好:
因最近需用讀AVI檔進來並對AVI檔的影格做影像處理,而所使用之讀檔程式如下: 目前是利用以下程式將avi檔讀入並顯示於image物件上,但遇到一個問題即是讀入的檔案 有些可能不一定都是一秒為30個影格的影片,請問該如何先取得此avi檔的撥放率呢 [code cpp] bool Frame2TImage(PAVISTREAM *gapavi,AVISTREAMINFO *avis,long miniSecond,TImage *Image1);// 從影像串流中取出相對應時間的Frame, 並秀在指定TImage的Canvas 上 bool GetVideoStream(char *szFile,PAVISTREAM *gapavi,AVISTREAMINFO *avis); // 由 AVI 檔中取出影像串流 PAVISTREAM gapavi; AVISTREAMINFO avis; static long Counter = 0; unsigned char *CBuffer=new unsigned char[320*240*3]; bool GetVideoStream(char *szFile,PAVISTREAM *gapavi,AVISTREAMINFO *avis) { AVIFileInit(); HRESULT hr; PAVIFILE pfile; hr = AVIFileOpen(&pfile,szFile , 0, 0L); if (hr != 0) { return false; } int gcpavi; AVIFileGetStream(pfile, gapavi, 0L, 0); // 把得到的 Stream interface放在gapavi中 AVICOMPRESSOPTIONS gaAVIOptions; LPAVICOMPRESSOPTIONS galpAVIOptions; AVIStreamInfo(*gapavi, avis, sizeof(*avis)); galpAVIOptions = &gaAVIOptions; _fmemset(galpAVIOptions, 0, sizeof(AVICOMPRESSOPTIONS)); galpAVIOptions->fccType = avis->fccType; switch(avis->fccType) { case streamtypeVIDEO: galpAVIOptions->dwFlags = AVICOMPRESSF_VALID | AVICOMPRESSF_KEYFRAMES | AVICOMPRESSF_DATARATE; galpAVIOptions->fccHandler = 0; galpAVIOptions->dwQuality = (DWORD)ICQUALITY_DEFAULT; galpAVIOptions->dwKeyFrameEvery = (DWORD)-1; // Default galpAVIOptions->dwBytesPerSecond = 0; galpAVIOptions->dwInterleaveEvery = 1; break; default: break; } pfile->Release(); AVIFileExit(); return true; } bool Frame2TImage (PAVISTREAM *gapavi,AVISTREAMINFO *avis,long miniSecond,TImage *Image1) { PGETFRAME gapgf; // GetFrame物件 if (avis->fccType == streamtypeVIDEO) { gapgf = AVIStreamGetFrameOpen(*gapavi, NULL); if (gapgf == NULL) return false; } LONG lFrame; lFrame = AVIStreamTimeToSample(*gapavi, miniSecond); if(!(lFrame>=AVIStreamStart(*gapavi) && lFrame Form1->Timer1->Enabled=false; Form1->Timer2->Enabled=false; return false; } BYTE abFormat[1024]; LPBITMAPINFOHEADER lpbi; // <----- the frame data lpbi = (LPBITMAPINFOHEADER)abFormat; // 指定 lpbi的位址 lpbi = (LPBITMAPINFOHEADER)AVIStreamGetFrame(gapgf,lFrame ); HBITMAP hBitmap; HDC dc = GetDC(NULL); hBitmap=CreateDIBitmap( dc,lpbi,CBM_INIT,(BYTE *)lpbi 40,(BITMAPINFO *)lpbi,DIB_RGB_COLORS); ReleaseDC(NULL, dc); HBITMAP2TImage(hBitmap,Image1); DeleteObject(hBitmap); AVIFileExit(); gapgf->Release(); return true; } bool HBITMAP2TImage(HBITMAP hBitmap,TImage *Image1) { BITMAP bitmap; Graphics::TBitmap *Bitmap1=new Graphics::TBitmap(); Bitmap1->Handle=hBitmap; GetObject(hBitmap,sizeof(BITMAP),&bitmap); Image1->Width=bitmap.bmWidth; Image1->Height=bitmap.bmHeight; Image1->Canvas->Draw(0,0,Bitmap1); delete Bitmap1; DeleteObject(hBitmap); return true; } void __fastcall TForm1::Timer1Timer(TObject *Sender) { Frame2TImage(&gapavi,&avis,Counter,Image1); Counter = (1000 / 30); //假設AVI一秒30Frames Graphics::TBitmap *bmp=new Graphics::TBitmap(); bmp->Assign(Image1->Picture->Bitmap); Byte *ptr2; bmp->PixelFormat=pf24bit; for (int y = 0; y < ImageHeight; y ) { ptr2 = (Byte *)bmp->ScanLine[y]; memcpy(&CBuffer[(239-y)*ImageWidth*3], ptr2, ImageWidth*3*sizeof(BYTE)); } delete bmp; } [/code] |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |