Conversion code of AVI and BMP in VC

Source: Internet
Author: User

Post a code memo about mutual conversion between Avi and BMP

Code can be found on the Internet. I can run it on the console.

Pay attention to the order of header files, and it is easy to report errors.

This code is used to disassemble an AVI video into a BMP frame. It is applicable to 24-Bit Bitmap, 8-bit, or other bmp formats that are useful for color palette. You need to make some minor modifications.

# Pragma comment (Lib, "vfw32") # include <atlstr. h> # include <VFW. h> # include <string> # include <stdio. h> int main () {STD: String S1 = "C :\\ Users \ Asus \ Desktop \ task \ XXX. avi "; STD: String S2 =" C :\\ Users \ Asus \ Desktop \ task \ picture "; cstring stravifilename; cstring strbmp dir; stravifilename. format ("% s", s1.data (); strbmp dir. format ("% s", s2.data (); avifileinit (); pavifile Avi; int res = avifileopen (& Avi, S Travifilename, of_read, null); int n = getlasterror (); If (res! = Avierr_ OK) {// an error occures if (Avi! = NULL) iterate (AVI); Return 0;} avifileinfo avi_info; avifileinfo (AVI, & avi_info, sizeof (avifileinfo); pavistream pstream; Res = avifilegetstream (AVI, & pstream, streamtypevideo/* video stream */, 0/* first stream */); If (res! = Avierr_ OK) {If (pstream! = NULL) avistreamrelease (pstream); avifileexit (); Return 0;} // do some tasks with the stream int inumframes; int ifirstframe; ifirstframe = avistreamstart (pstream ); if (ifirstframe =-1) {// error getteing the frame inside the stream if (pstream! = NULL) avistreamrelease (pstream); avifileexit (); Return 0;} inumframes = avistreamlength (pstream); If (inumframes =-1) {// error getteing the number of frames inside the stream if (pstream! = NULL) avistreamrelease (pstream); avifileexit (); Return 0;} // getting bitmap from frame bitmapinfoheader BiH; zeromemory (& BiH, sizeof (bitmapinfoheader); BiH. bibitcount = 8; // 24 bit per pixel BiH. biclrimportant = 0; BiH. biclrused = 256; BiH. bicompression = bi_rgb; BiH. biplanes = 1; BiH. bisize = 40; // BiH. bisize = sizeof (BiH); // BiH. bixpelspermeter = 0; BiH. biypelspermeter = 0; // Calculate total size Rgbquad scanlines (DWORD aligned) BiH. bisizeimage = (BIH. biwidth * 3) + 3) & 0 xfffc) * BiH. biheight; // -- // BiH. bisizeimage = (BIH. biwidth * BiH. bibitcount + 31)/32*4 * BiH. biheight * 3; // ---- pgetframe pframe; pframe = avistreamgetframeopen (pstream, null); avistreaminfo streaminfo; avistreaminfo (pstream, & streaminfo, sizeof (avistreaminfo )); // get the first frame bitmapinfoheader bih2; long lsize = sizeof (Bih2); int Index = 0; // avistreamreadformat (pstream, index, & bih2, & lsize); For (INT I = ifirstframe; I <inumframes; I ++) {Index = I-ifirstframe; byte * pdib = (byte *) avistreamgetframe (pframe, index); // avistreamreadformat (pstream, index, & bih2, & lsize); bitmapfileheader stfilehdr; rgbtriple STP; int H = bih2.biheight; int W = bih2.biwidth; int BC = bih2.bibitcount; int CM = bih2.biclrimportant; int BCU = Bih2.biclrused; int COM = bih2.bicompression; int PL = bih2.biplanes; int S = bih2.bisize; int Si = bih2.bisizeimage; int x = bytes; int y = bytes; // printf ("h: % d w: % d bitcount: % d clri: % d clru: % d COMP: % d planes: % d size: % d sizeimage: % d xppm: % d yppm: % d \ n ", H, W, BC, CM, BCU, COM, PL, S, Si, x, y); byte * bits = new byte [bih2.bisizeimage]; avistreamread (pstream, index, 1, bits, bih2.bisiz Eimage, null, null); // rtlmovememory (bits, pdib + sizeof (bitmapinfoheader), bih2.bisizeimage); // --- original annotation bih2.biclrused = 256; stfilehdr. bfoffbits = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader); stfilehdr. bfsize = sizeof (bitmapfileheader); stfilehdr. bftype = 0x4d42; cstring filename; filename. format ("frame-000005dof0000d.bmp", index, inumframes); cstring strtemp = strbmp dir; strtemp ++ = "//"; strtemp + = filena Me; file * fp = _ tfopen (strtemp, _ T ("WB"); fwrite (& stfilehdr, 1, sizeof (bitmapfileheader), FP); fwrite (& bih2, 1, sizeof (bitmapinfoheader), FP); // fwrite (& STP, FP); // Note: Each color palette 4B, 8-bit images have 256 // -- int FF = fwrite (bits, 1, bih2.bisizeimage, FP); int e = getlasterror (); fclose (FP ); //// delete bits; // createfrompackeddibpointer (pdib, index);} avistreamgetframeclose (pframe); // close the stream after finishi Ng the task if (pstream! = NULL) avistreamrelease (pstream); avifileexit (); Return 0 ;}

This is an AVI video assembled with BMP. You can set the Frame Rate on your own.

#pragma comment(lib, "vfw32")#include <afx.h>#include <atlstr.h>#include<string>#include<stdio.h>#include<string.h>#include <Vfw.h>void main (){  CString szAVIName;  CString strBmpDir;  szAVIName = "C:\\Users\\asus\\Desktop\\Task\\pic\\tt.avi";  strBmpDir = "C:\\Users\\asus\\Desktop\\Task\\picture";       CFileFind finder;       strBmpDir += _T("\\*.*");        AVIFileInit();          AVISTREAMINFO strhdr;       PAVIFILE pfile;       PAVISTREAM ps;        int nFrames =0;        HRESULT hr;         BOOL bFind = finder.FindFile(strBmpDir);       while(bFind)       {              bFind = finder.FindNextFile();              if(!finder.IsDots() && !finder.IsDirectory())              {                     CString str = finder.GetFilePath();                     FILE *fp = fopen(str,"rb");                     BITMAPFILEHEADER bmpFileHdr;                     BITMAPINFOHEADER bmpInfoHdr;                     fseek( fp,0,SEEK_SET);                     fread(&bmpFileHdr,sizeof(BITMAPFILEHEADER),1, fp);                     fread(&bmpInfoHdr,sizeof(BITMAPINFOHEADER),1, fp);                      BYTE *tmp_buf = NULL;                     if(nFrames ==0 )                     {                           AVIFileOpen(&pfile,szAVIName,OF_WRITE | OF_CREATE,NULL);                            memset(&strhdr, 0, sizeof(strhdr));                            strhdr.fccType                = streamtypeVIDEO;// stream type                            strhdr.fccHandler             = 0;                            strhdr.dwScale                = 1;                            strhdr.dwRate                 = 15;                 // 15 fps                            strhdr.dwSuggestedBufferSize = bmpInfoHdr.biSizeImage ;                            SetRect(&strhdr.rcFrame, 0, 0, bmpInfoHdr.biWidth, bmpInfoHdr.biHeight);                             // And create the stream;                            hr = AVIFileCreateStream(pfile,&ps,&strhdr);                        // hr = AVIStreamSetFormat(ps,nFrames,&bmpInfoHdr,sizeof(bmpInfoHdr));                    }                     tmp_buf = new BYTE[bmpInfoHdr.biWidth * bmpInfoHdr.biHeight * 3];                   fread(tmp_buf, 1, bmpInfoHdr.biWidth * bmpInfoHdr.biHeight * 3, fp);                     hr = AVIStreamSetFormat(ps,nFrames,&bmpInfoHdr,sizeof(bmpInfoHdr));                     hr = AVIStreamWrite(ps,       // stream pointer                              nFrames ,                          // time of this frame                                   1,                         // number to write                                   (LPBYTE) tmp_buf,                                   bmpInfoHdr.biSizeImage , // size of this frame                                   AVIIF_KEYFRAME,                    // flags....                                   NULL,                                   NULL);                      nFrames ++;                      fclose(fp);               }       }        AVIStreamClose(ps);       if(pfile != NULL)      AVIFileRelease(pfile);         AVIFileExit();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.