C + + configuration FFmpeg

Source: Internet
Author: User

This tutorial is only configured for windows64/32+vs2013 environments
First step: With Environment
1. Open the compiled Windows version in FFmpeg official website http://ffmpeg.zeranoe.com/builds/
64-bit Windows systems and 32-bit systems each have three versions of the static version, share version, dev version;
It is recommended here that both 32-bit and 64-bit systems are directly configured with a 32-bit FFmpeg version, unless you vs2013 select the x64 compiler.

Download and unzip the 32-bit version share version and Dev.
In Dev the main is some header files and Lib, in the share version of the main DLL files.


2. Open vs2013 and create a new Win32 console program.
Select the project-"Properties-" C + + directory to open the include and Lib in the dev version in the included Directory and library directory (referring to the location where you unzipped dev save, where include and Lib are found)

3 Item-"Properties-" linker-"input-" the dependency is filled in

Avcodec.lib
Avformat.lib
Avutil.lib
Avdevice.lib
Avfilter.lib
Postproc.lib
Swresample.lib
Swscale.lib
Save it.

The second step: Video decoding, here is a complete video decoding project, from decoding to storage, can be run on vs2013, if the compilation is unsuccessful, you can consider whether the environment is wrong.

//ConsoleApplication9.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<windows.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#pragmaOnce#pragmaWarning (disable:4996)extern "C"{#include<libavcodec/avcodec.h>#include<libavdevice/avdevice.h>#include<libavfilter/avfilter.h>#include<libswscale/swscale.h>#include<libswresample/swresample.h>#include<libavformat/avformat.h>#include<libavutil/avutil.h>};//To define a BMP file header#ifndef _wingdi_#define_wingdi_typedefstructTagbitmapfileheader {WORD bftype;    DWORD bfsize;    WORD bfReserved1;    WORD BfReserved2; DWORD Bfoffbits;} Bitmapfileheader, Far*lpbitmapfileheader, *Pbitmapfileheader;typedefstructtagbitmapinfoheader{DWORD bisize;    LONG Biwidth;    LONG Biheight;    WORD biplanes;    WORD biBitCount;    DWORD bicompression;    DWORD biSizeImage;    LONG Bixpelspermeter;    LONG Biypelspermeter;    DWORD biclrused; DWORD Biclrimportant;} Bitmapinfoheader, Far*lpbitmapinfoheader, *Pbitmapinfoheader;#endif //functions to save BMP filesvoidSaveasbmp (Avframe *pframergb,intWidthintHeightintIndexintBPP) {    Charbuf[5] = {0 }; //BMP HeadBitmapfileheader Bmpheader;    Bitmapinfoheader Bmpinfo; FILE*FP; Char*filename =New Char[255]; //file storage path, based on your own modificationssprintf_s (filename,255,"%s_%d.bmp","d:\\", index); if(fp = fopen (filename,"wb+")) ==NULL) {printf ("Open File failed!\n"); return; } Bmpheader.bftype=0x4d42; Bmpheader.bfreserved1=0; Bmpheader.bfreserved2=0; Bmpheader.bfoffbits=sizeof(Bitmapfileheader) +sizeof(Bitmapinfoheader); Bmpheader.bfsize= Bmpheader.bfoffbits + width*height*bpp/8; Bmpinfo.bisize=sizeof(Bitmapinfoheader); Bmpinfo.biwidth=width; Bmpinfo.biheight=height; Bmpinfo.biplanes=1; Bmpinfo.bibitcount=BPP; Bmpinfo.bicompression=Bi_rgb; Bmpinfo.bisizeimage= (WIDTH*BPP + to) / +*4*height; Bmpinfo.bixpelspermeter= -; Bmpinfo.biypelspermeter= -; Bmpinfo.biclrused=0; Bmpinfo.biclrimportant=0; Fwrite (&bmpheader,sizeof(Bmpheader),1, FP); Fwrite (&bmpinfo,sizeof(Bmpinfo),1, FP); Fwrite (Pframergb->data[0], WIDTH*HEIGHT*BPP/8,1, FP); Fclose (FP);}//Main functionintMainvoid) {unsignedinti =0, Videostream =-1; Avcodeccontext*Pcodecctx; Avformatcontext*Pformatctx; Avcodec*Pcodec; Avframe*pframe, *Pframergb; structSwscontext *Pswsctx; Const Char*filename ="E:\\123.avi";    Avpacket packet; intframefinished; intpicturesize; uint8_t*buf; //Registering codecsAv_register_all ();    Avformat_network_init (); Pformatctx=Avformat_alloc_context (); //open a video file    if(Avformat_open_input (&pformatctx, filename, NULL, NULL)! =0) {printf ("av Open input file failed!\n"); Exit (1); }    //Get stream Information    if(Avformat_find_stream_info (Pformatctx, NULL) <0) {printf ("av Find stream info failed!\n"); Exit (1); }    //Get video stream     for(i =0; i<pformatctx->nb_streams; i++)        if(Pformatctx->streams[i]->codec->codec_type = =Avmedia_type_video) {Videostream=i;  Break; }    if(Videostream = =-1) {printf ("Find video stream failed!\n"); Exit (1); } pcodecctx= pformatctx->streams[videostream]->codec; Pcodec= Avcodec_find_decoder (pcodecctx->codec_id); if(Pcodec = =NULL) {printf ("Avcode Find Decoder failed!\n"); Exit (1); }    //Open Decoder    if(Avcodec_open2 (Pcodecctx, Pcodec, NULL) <0) {printf ("Avcode Open failed!\n"); Exit (1); }    //allocating memory for each frame imagePframe =Av_frame_alloc (); Pframergb=Av_frame_alloc (); if((pframe = = NULL) | | (Pframergb = =NULL)) {printf ("Avcodec alloc frame failed!\n"); Exit (1); }    //Get frame Graph sizePicturesize = Avpicture_get_size (Av_pix_fmt_bgr24, Pcodecctx->width, pcodecctx->height); BUF= (uint8_t*) Av_malloc (picturesize); if(BUF = =NULL) {printf ("av malloc failed!\n"); Exit (1); } avpicture_fill (Avpicture*) Pframergb, buf, Av_pix_fmt_bgr24, Pcodecctx->width, pcodecctx->height); //Setting the image conversion contextPswsctx = Sws_getcontext (pcodecctx->width, pcodecctx-height, pcodecctx-pix_fmt, Pcodecctx-width, pcodecctx-height, av_pix_fmt_bgr24, sws_bicubic, NULL, NULL, NULL); I=0;  while(Av_read_frame (Pformatctx, &packet) >=0)    {        if(Packet.stream_index = =videostream) {            //true decodingAvcodec_decode_video2 (Pcodecctx, Pframe, &framefinished, &packet); if(framefinished) {//invert the image, otherwise the resulting image is cut to thepframe->data[0] + = pframe->linesize[0] * (Pcodecctx->height-1); Pframe->linesize[0] *= -1; Pframe->data[1] + = pframe->linesize[1] * (Pcodecctx->height/2-1); Pframe->linesize[1] *= -1; Pframe->data[2] + = pframe->linesize[2] * (Pcodecctx->height/2-1); Pframe->linesize[2] *= -1; //Convert image formats to convert extracted yuv420p images to BRG24 imagesSws_scale (Pswsctx, Pframe->data, Pframe->linesize,0, Pcodecctx->height, Pframergb->data, pframergb->linesize); //Save as BMP diagramSaveasbmp (Pframergb, Pcodecctx->width, Pcodecctx->height, I, -); I++; } av_free_packet (&packet);    }} sws_freecontext (PSWSCTX);    Av_free (pframe);    Av_free (PFRAMERGB);    Avcodec_close (PCODECCTX); Avformat_close_input (&pformatctx); return 0;}

C + + configuration FFmpeg

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.