MovieMaker::MovieMaker() { snprintf( fname, sizeof(fname), "movie.avi" ); /* Flawfinder: ignore */ width = -1; height = -1; bOK = true; nFrames = 0; pfile = NULL; ps = NULL; psCompressed = NULL; psText = NULL; aopts[0] = &opts; // Check VFW version. WORD wVer = HIWORD( VideoForWindowsVersion() ); if ( wVer < 0x010A ) { fprintf( stderr, "VFW version is too old.\n" ); exit( -1 ); } else { AVIFileInit(); } }
void MovieMaker::EndCapture() { fprintf( stderr, "\n" ); if (ps) { AVIStreamClose(ps); ps = NULL; } if (psCompressed) { AVIStreamClose(psCompressed); psCompressed = NULL; } if (psText) { AVIStreamClose(psText); psText = NULL; } if (pfile) { AVIFileClose(pfile); pfile = NULL; } WORD wVer = HIWORD(VideoForWindowsVersion()); if (wVer >= 0x010A) { AVIFileExit(); } }
VideoGlobalState() { /* first let's make sure we are running on 1.1 */ WORD wVer = HIWORD(VideoForWindowsVersion()); if (wVer < 0x010a) { throw openrave_exception("can't init avi library"); } AVIFileInit(); }
CAVIFile::CAVIFile() : bOK(true), nFrames(0), sFrames(0) { soundAdded = false; pfile = NULL; ps = NULL; psCompressed = NULL; psound = NULL; aopts[0] = &opts; WORD wVer = HIWORD(VideoForWindowsVersion()); if (wVer < 0x010A) { // oops, we are too old, blow out of here bOK = false; } else { AVIFileInit(); } }
CAVIFile::~CAVIFile() { if (ps) AVIStreamClose(ps); if (psCompressed) AVIStreamClose(psCompressed); if(psound) AVIStreamClose(psound); if (pfile) AVIFileClose(pfile); WORD wVer = HIWORD(VideoForWindowsVersion()); if (wVer >= 0x010A) { AVIFileExit(); } }
CAVIFile::CAVIFile(LPCTSTR lpszFileName, int xdim, int ydim) : FName(lpszFileName), xDim(xdim), yDim(ydim), bOK(true), nFrames(0) { pfile = NULL; ps = NULL; psCompressed = NULL; psText = NULL; aopts[0] = &opts; WORD wVer = HIWORD(VideoForWindowsVersion()); if (wVer < 0x010A) { // oops, we are too old, blow out of here bOK = false; } else { AVIFileInit(); } }
// --[ Method ]--------------------------------------------------------------- // // - Class : CAviRenderer // - prototype : bool StopRender() // // - Purpose : Stops capturing and closes the file. // // ----------------------------------------------------------------------------- bool CAviRenderer::StopRender() { // Close/free stuff if(m_pAviStream) { AVIStreamClose(m_pAviStream); m_pAviStream = NULL; } if(m_pAviStreamCompressed) { AVIStreamClose(m_pAviStreamCompressed); m_pAviStreamCompressed = NULL; } if(m_pAviText) { AVIStreamClose(m_pAviText); m_pAviText = NULL; } if(m_pAviFile) { AVIFileClose(m_pAviFile); m_pAviFile = NULL; } WORD wVer = HIWORD(VideoForWindowsVersion()); if(wVer >= 0x010A) { AVIFileExit(); } m_bIsWorking = false; m_pWindowGL = NULL; return true; }
MovieMaker::~MovieMaker() { if (ps) AVIStreamClose(ps); if (psCompressed) AVIStreamClose(psCompressed); if (psText) AVIStreamClose(psText); if (pfile) { AVIFileClose(pfile); } WORD wVer = HIWORD(VideoForWindowsVersion()); if (wVer >= 0x010A) { AVIFileExit(); } }
HRESULT CAVIGenerator::InitEngine() { AVISTREAMINFO strHdr; // information for a single stream static AVICOMPRESSOPTIONS opts; static AVICOMPRESSOPTIONS FAR * aopts[1] = {&opts}; static bool first = true; TCHAR szBuffer[1024]; HRESULT hr; m_sError=_T("Ok"); // Step 0 : Let's make sure we are running on 1.1 DWORD wVer = HIWORD(VideoForWindowsVersion()); if (wVer < 0x010a) { // oops, we are too old, blow out of here m_sError=_T("Version of Video for Windows too old. Come on, join the 21th century!"); return S_FALSE; } // Step 1 : initialize AVI engine AVIFileInit(); // Step 2 : Open the movie file for writing.... hr = AVIFileOpen(&m_pAVIFile, // Address to contain the new file interface pointer (LPCSTR)m_sFile, // Null-terminated string containing the name of the file to open OF_WRITE | OF_CREATE, // Access mode to use when opening the file. NULL); // use handler determined from file extension. // Name your file .avi -> very important if (hr != AVIERR_OK) { // sprintf(szBuffer,_T("AVI Engine failed to initialize. Check filename %s."),m_sFile); m_sError=szBuffer; // Check it succeded. switch(hr) { case AVIERR_BADFORMAT: m_sError+=_T("The file couldn't be read, indicating a corrupt file or an unrecognized format."); break; case AVIERR_MEMORY: m_sError+=_T("The file could not be opened because of insufficient memory."); break; case AVIERR_FILEREAD: m_sError+=_T("A disk error occurred while reading the file."); break; case AVIERR_FILEOPEN: m_sError+=_T("A disk error occurred while opening the file."); break; case REGDB_E_CLASSNOTREG: m_sError+=_T("According to the registry, the type of file specified in AVIFileOpen does not have a handler to process it"); break; } return hr; } // Fill in the header for the video stream.... memset(&strHdr, 0, sizeof(strHdr)); strHdr.fccType = streamtypeVIDEO; // video stream type strHdr.fccHandler = 0; strHdr.dwScale = 1; // should be one for video strHdr.dwRate = m_dwRate; // fps strHdr.dwSuggestedBufferSize = m_bih.biSizeImage; // Recommended buffer size, in bytes, for the stream. SetRect(&strHdr.rcFrame, 0, 0, // rectangle for stream (int) m_bih.biWidth, (int) m_bih.biHeight); // Step 3 : Create the stream; hr = AVIFileCreateStream(m_pAVIFile, // file pointer &m_pStream, // returned stream pointer &strHdr); // stream header // Check it succeded. if (hr != AVIERR_OK) { m_sError=_T("AVI Stream creation failed. Check Bitmap info."); if (hr==AVIERR_READONLY) { m_sError+=_T(" Read only file."); } return hr; } // if (first) { // Step 4: Get codec and infos about codec memset(&opts, 0, sizeof(opts)); // Poping codec dialog if (!AVISaveOptions(NULL, 0, 1, &m_pStream, (LPAVICOMPRESSOPTIONS FAR *) &aopts)) { AVISaveOptionsFree(1,(LPAVICOMPRESSOPTIONS FAR *) &aopts); return S_FALSE; } // first = false; } // Step 5: Create a compressed stream using codec options. hr = AVIMakeCompressedStream(&m_pStreamCompressed, m_pStream, &opts, NULL); if (hr != AVIERR_OK) { m_sError=_T("AVI Compressed Stream creation failed."); switch(hr) { case AVIERR_NOCOMPRESSOR: m_sError+=_T(" A suitable compressor cannot be found."); break; case AVIERR_MEMORY: m_sError+=_T(" There is not enough memory to complete the operation."); break; case AVIERR_UNSUPPORTED: m_sError+=_T("Compression is not supported for this type of data. This error might be returned if you try to compress data that is not audio or video."); break; } return hr; } // releasing memory allocated by AVISaveOptionFree hr = AVISaveOptionsFree(1,(LPAVICOMPRESSOPTIONS FAR *) &aopts); if (hr!=AVIERR_OK) { m_sError=_T("Error releasing memory"); return hr; } // Step 6 : sets the format of a stream at the specified position hr = AVIStreamSetFormat(m_pStreamCompressed, 0, // position &m_bih, // stream format m_bih.biSize + // format size m_bih.biClrUsed * sizeof(RGBQUAD)); if (hr != AVIERR_OK) { m_sError=_T("AVI Compressed Stream format setting failed."); return hr; } // Step 6 : Initialize step counter m_lFrame=0; return hr; }
// --[ Method ]--------------------------------------------------------------- // // - Class : CAviRenderer // - prototype : bool StartRender(CWindowGL *pWindowGL, std::string strAviFile, int nFramerate) // // - Purpose : Prepares the object for capturing the opengl window to an AVI. // // ----------------------------------------------------------------------------- bool CAviRenderer::StartRender(CWindowGL *pWindowGL, std::string strAviFile, int nFramerate) { assert(pWindowGL); m_pWindowGL = NULL; m_bIsWorking = false; // Check VFW version. WORD wVer = HIWORD(VideoForWindowsVersion()); if(wVer < 0x010A) { CLogger::ErrorWindow("Video For Windows outdated version"); return false; } // Init library AVIFileInit(); // Get an image and stuff it into a bitmap. HRESULT hr; HBITMAP bmp; if((bmp = LoadBMPFromFB(pWindowGL)) == NULL) { return false; } LPBITMAPINFOHEADER lpInfoHeader = (LPBITMAPINFOHEADER)GlobalLock(MakeDib(bmp, 32)); DeleteObject(bmp); if(lpInfoHeader == NULL) { LOG.Write("\nERROR - CAviRenderer::StartRender(): GlobalLock() failed."); return false; } m_nWidth = (int)lpInfoHeader->biWidth; m_nHeight = (int)lpInfoHeader->biHeight; // Open an avi file for writing hr = AVIFileOpen( &m_pAviFile, // returned file pointer strAviFile.data(), // file name OF_WRITE | OF_CREATE, // mode to open file with NULL); // use handler determined // from file extension.... if (hr != AVIERR_OK) { LOG.Write("\nERROR - CAviRenderer::StartRender(): AVIFileOpen() failed."); GlobalFreePtr(lpInfoHeader); return false; } // Configure the stream _fmemset(&m_aviStreamInfo, 0, sizeof(m_aviStreamInfo)); m_aviStreamInfo.fccType = streamtypeVIDEO; // stream type m_aviStreamInfo.fccHandler = 0; m_aviStreamInfo.dwScale = 1; m_aviStreamInfo.dwRate = nFramerate; m_aviStreamInfo.dwSuggestedBufferSize = lpInfoHeader->biSizeImage; SetRect(&m_aviStreamInfo.rcFrame, 0, 0, // rectangle for stream (int)lpInfoHeader->biWidth, (int)lpInfoHeader->biHeight); // And create the stream hr = AVIFileCreateStream(m_pAviFile, // file pointer &m_pAviStream, // returned stream pointer &m_aviStreamInfo); // stream header if(hr != AVIERR_OK) { LOG.Write("\nERROR - CAviRenderer::StartRender(): AVIFileCreateStream() failed."); GlobalFreePtr(lpInfoHeader); return false; } // Get save options (prompt dialog) _fmemset(&m_aviOptions, 0, sizeof(m_aviOptions)); if(!AVISaveOptions(NULL, 0, 1, &m_pAviStream, (LPAVICOMPRESSOPTIONS FAR *) &m_pAviOptions)) { LOG.Write("\nERROR - CAviRenderer::StartRender(): AVISaveOptions() failed."); GlobalFreePtr(lpInfoHeader); return false; } // Create compressed stream hr = AVIMakeCompressedStream(&m_pAviStreamCompressed, m_pAviStream, &m_aviOptions, NULL); if(hr != AVIERR_OK) { LOG.Write("\nERROR - CAviRenderer::StartRender(): AVIMakeCompressedStream() failed."); GlobalFreePtr(lpInfoHeader); return false; } // Set it's format hr = AVIStreamSetFormat(m_pAviStreamCompressed, 0, lpInfoHeader, // stream format lpInfoHeader->biSize + // format size lpInfoHeader->biClrUsed * sizeof(RGBQUAD)); if(hr != AVIERR_OK) { LOG.Write("\nERROR - CAviRenderer::StartRender(): AVIStreamSetFormat() failed."); GlobalFreePtr(lpInfoHeader); return false; } m_pWindowGL = pWindowGL; m_bIsWorking = true; UpdateWindow(pWindowGL->GetHWND()); return true; }
bool MovieMaker::Init(const char *newFileName, uint newWidth, uint newHeight, uint frameRate, const vector<string> &codecs) { //Test if init if(initSuccess) { LOGERR(("Init - Already init")); return false; } // Check VFW version. WORD wVer = HIWORD(VideoForWindowsVersion()); if (wVer < 0x010A) { LOGERR(("Init - VFW version 0x%x is too old.\n",wVer)); return false; } //Open the AVI system AVIFileInit(); aviSysOpen = true; //Open the file HRESULT result = AVIFileOpen(&pfile, // Returned file pointer newFileName, // File name OF_WRITE | OF_CREATE,// Mode to open file with NULL); // Use handler determined if(result != AVIERR_OK) { LOGERR(("Init - Error creating file %s",newFileName)); return false; } //Create the avi info structure AVISTREAMINFO aviInfo; memset(&aviInfo, 0, sizeof(aviInfo)); aviInfo.fccType = streamtypeVIDEO; aviInfo.fccHandler = 0; aviInfo.dwQuality = -1; aviInfo.dwLength = 0; aviInfo.dwScale = 1; aviInfo.dwRate = frameRate; aviInfo.dwSuggestedBufferSize = newWidth * newHeight * 4; SetRect(&aviInfo.rcFrame, 0, 0, (int) newWidth, (int) newHeight); // And create the stream; result = AVIFileCreateStream(pfile, // File pointer &ps, // Returned stream pointer &aviInfo); // Stream header if (result != AVIERR_OK) { LOGERR(("Init - Error creating stream for file %s",newFileName)); return false; } //Loop for all codecs bool codecSuccess = false; for(uint i=0; i<codecs.size(); i++) { //Attempt to create the codec and stop on the first successful codec if(CreateCompressedStream(codecs[i], frameRate)) { codecSuccess = true; break; } } if(!codecSuccess) { LOGERR(("Init - No valid compression codecs for %s",newFileName)); return false; } //Initialize bitmapInfo BITMAPINFOHEADER bitmapInfo; memset(&bitmapInfo,0, sizeof(BITMAPINFOHEADER)); //Fill the bitmap info structure. bitmapInfo.biSize=sizeof(BITMAPINFOHEADER); bitmapInfo.biWidth =newWidth; bitmapInfo.biHeight=newHeight; bitmapInfo.biPlanes=1; bitmapInfo.biBitCount=32; bitmapInfo.biSizeImage=newWidth*newHeight*4; bitmapInfo.biCompression=BI_RGB; //BI_RGB means BGR in reality //Set the format of a stream at the specified position result = AVIStreamSetFormat(psCompressed, 0, // position &bitmapInfo, // stream format bitmapInfo.biSize + // format size bitmapInfo.biClrUsed * sizeof(RGBQUAD)); if (result != AVIERR_OK) { LOGERR(("Init - AVI Compressed Stream format setting failed.")); return false; } //Assign init success width = newWidth; height = newHeight; initSuccess = true; return true; }
//在錄製OpengL動畫之前,首先需要設置AVI文件名稱、錄製幀的大小、錄製幀率、AVI文件壓縮方式等信息,具體的源代碼如下: bool CAVICapture::start(CString filename,int w, int h,float fps) { if (capturing) return false; width = w; height = h; frameRate = fps; if (HIWORD(VideoForWindowsVersion()) < 0x010a) { // 版本號必須大於1.1 return false; } int rowBytes = (width * 3 + 3) & ~0x3; image = new unsigned char[rowBytes * height]; // 創建AVI文件 HRESULT hr = AVIFileOpen(&aviFile, filename, OF_WRITE | OF_CREATE, NULL); if (hr != AVIERR_OK) { MessageBox(NULL,"創建AVI文件失敗","錯誤",MB_OK); return false; } // AVI文件的頭信息 AVISTREAMINFO info; ZeroMemory(&info, sizeof info); info.fccType = streamtypeVIDEO; info.fccHandler = 0; info.dwScale = 1; info.dwRate = (DWORD) frameRate; info.dwSuggestedBufferSize = rowBytes * height; SetRect(&info.rcFrame, 0, 0, width, height); hr = AVIFileCreateStream(aviFile, &aviStream, &info);//創建AVI文件流 if (hr != AVIERR_OK) { MessageBox(NULL,"創建AVI文件流失敗","錯誤",MB_OK); cleanup(); //清空內存 return false; } // 允許用戶選擇壓縮方式 AVICOMPRESSOPTIONS options; AVICOMPRESSOPTIONS* arrOptions[1] = { &options }; ZeroMemory(&options, sizeof options); if (!AVISaveOptions(NULL, 0, 1, &aviStream, (LPAVICOMPRESSOPTIONS*) &arrOptions)) { cleanup();//清空內存 return false; } //設置AVI壓縮方式 hr = AVIMakeCompressedStream(&compAviStream, aviStream, &options, NULL); if (hr != AVIERR_OK) { MessageBox(NULL,"設置AVI壓縮方式失敗", "錯誤",MB_OK); cleanup();//清空內存 return false; } BITMAPINFOHEADER bi; ZeroMemory(&bi, sizeof bi); bi.biSize = sizeof bi; bi.biWidth = width; bi.biHeight = height; bi.biPlanes = 1; bi.biBitCount = 24; bi.biCompression = BI_RGB; bi.biSizeImage = rowBytes * height; bi.biXPelsPerMeter = 0; bi.biYPelsPerMeter = 0; bi.biClrUsed = 0; bi.biClrImportant = 0; // 設置數據格式 hr = AVIStreamSetFormat(compAviStream, 0, &bi, sizeof bi); if (hr != AVIERR_OK) { MessageBox(NULL,"設置AVI數據格式","錯誤",MB_OK); cleanup();//清空內存 return false; } capturing = true; frameCounter = 0; return true; }