Calling FFmpeg in Visual Studio 2010

Source: Internet
Author: User
Tags visual studio 2010

This article from: http://blog.sina.com.cn/s/blog_4178f4bf01018wqh.html

In recent days have been tossing ffmpeg, on the internet also looked up a lot of information, a lot of effort, now here and everyone to share.

First, the preparation of work      originally wanted to compile ffmpeg in Windows to generate LIB, DLL and other library files, but toss a long time always wrong, so decisively give up. Fortunately, there is a compiled version of the online, can be brought directly to use, the URL is http://ffmpeg.zeranoe.com/builds/. What we need is 32-bit Builds (Shared) and 32-bit Builds (Dev), which corresponds to the version number. 32-bit Builds (Shared) mainly contains the DLL files to be used, 32-bit Builds (Dev) mainly contains the header files and Lib files to be used. Actually these lib is not the traditional static library file (the real static library file is the *.a file in the Lib directory), they are the DLL's export file. DLL file in 32-bit Builds (Shared) Bin directory, lib file under 32-bit Builds (Dev) in the Lib directory, the header file in the 32-bit Builds (dev) include directory.      In addition, several new header files have been added to C99, not in Visual studio, so you need to download them yourself. and put it into the appropriate directory. For VS2010, it is usually: C:\Program Files (x86) \microsoft Visual Studio 10.0\vc\include. Second, Visual Studio configuration      after the library file is ready, you can do a simple compilation of Visual Studio, the following is a copy of the code that is currently ready to run, as an example. Ffmpeg-example.cpp:defines the entry point for the console application.////#include "stdafx.h"   #define Inline _i Nline#ifndef int64_c#define Int64_c (c) (C # LL) #define UINT64_C (c) (C # # ULL) #endif   #ifdef __cplusplusextern "C" {#e ndif    #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> #ifdef __cplusplus} #endif  # Include <stdio.h>  static void Saveframe (avframe *pframe, int width, int height, int iFrame);  int ma In (int argc, const char * argv[]) {    avformatcontext *pformatctx;    int             i, videostream;     Avcodeccontext  *pcodecctx;    avcodec          *pcodec;    avframe         *pFrame;      avframe         *pFrameRGB;     Avpacket        packet;    int              framefinished;    int              numbytes;    uint8_t          *buffer;     //Register all formats and codecs     av_register_all ();      //Open Video file    //if ( Avformat_open_input (&pformatctx, argv[1], NULL, NULL)!=0) if (Avformat_open_input (null, argv[1], NULL, NULL)!=0)         return-1; Couldn ' t open file     //Retrieve stream information    if (av_find_ Stream_info (PFORMATCTX) <0)         return-1; Couldn ' t find stream information     //Dump information about file onto standard error     av_dump_format (pformatctx, 0, argv[1], false)      //Find the first video Stream    videostream=-1;&nbsP;   for (i=0; i<pformatctx->nb_streams; i++)          if (Pformatctx->streams[i]->codec->codec_type==avmedia_type_video)          {            videoStream=i;             break;         }        if (videostream==-1)              return-1; Didn ' t find a video stream         //Get a pointer to the codec context F or the video stream        pcodecctx=pformatctx->streams[videostream]- >codec;         //Find The decoder for the video stream  & Nbsp;     pcodec=avcodec_find_decoder (pcodecctx->codec_id);         if (PCodec==NULL)              return-1; Codec not found         //Open codec         if (Avcodec_open (Pcodecctx, Pcodec) <0)              return-1; Could not open codec         //Hack to correct wrong frame rates this see M to is generated by some codecs        if (pcodecctx->time_base.num> && pcodecctx->time_base.den==1)              pcodecctx->time_base.den=1000;         //Allocate Video Frame        pframe=avcodec_alloc_frame ();           //Allocate an avframe structure         pframergb=avcodec_alloc_frame ();         if (PFrameRGB==NULL)             return-1;          //determine required buffer size and allocate buffer         numbytes=avpicture_get_size (Pix_fmt_rgb24, pcodecctx->width,             pcodecctx->height);          //buffer=malloc (numbytes);         buffer= (uint8_t *) av_ malloc (numbytes*sizeof (uint8_t));          //Assign appropriate Parts of buffer to image planes in Pframergb        avpicture_fill ((avpicture *) Pframergb, bUffer, pix_fmt_rgb24,            pcodecctx-> width, pcodecctx->height);          //Read frames and save first five Frames to disk        i=0;         while (Av_read_frame (Pformatctx, &packet) >=0)         {             //is this a packet from the video stream?             if (Packet.stream_index==videostream)             {                 //Decode Video frame                 avcodec_decode_video2 (PCodecCtx, PFrame, & Framefinished, &packet);                  //did we get a video frame?                 if (framefinished)                  {                     static struct Swscontext *img_convert_ctx;  #if 0                     //Older Removed code                     //Convert the image from its native format to RGB swscale      & Nbsp;             img_convert ((AVPicture *) PFRamergb, pix_fmt_rgb24,                           (avpicture*) pframe, PCODECCTX-&GT;PIX_FMT, pcodecctx->width,                          pcodecctx->height);                      // function template, for reference                     int Sws_scale (struct swscontext *context, uint8_t* src[], int srcStride[], int srcslicey,                         int Srcsliceh, uint8_t* dst[], int dststride[]); #endif                      //Convert the Image into YUV format that SDL uses                     if (Img_convert_ctx = = NULL) {                         int W = pcodecctx->width;                         int h = pcodecctx->height;                           img_convert_ctx = Sws_getcontext (W, h,                             & NBsp;pcodecctx->pix_fmt,                              w, H, PIX_FMT_RGB24, sws_bicubic,                             null, NULL, NULL);                          if (Img_convert_ctx = = NULL) {                             fprintf ( STDERR, "Cannot initialize the conversion context!\n");                              Exit (1); &NBsp;                        }                     }                     int ret = Sws_scale (Img_convert_ctx, PFrame->data, Pframe->linesize, 0,                          pcodecctx->height, PFrameRGB->data, pframergb->linesize); #if 0                      //this with be true, as of 1/2009, but apparently it is no longer true In 3/2009                   &nBsp; if (ret) {                         fprintf (stderr, "Sws_scale failed [%d]!\n", ret);                          exit ( -1);            &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP, #endif                      //Save the frame to disk                     if (i++ <=5)                          saveframe (Pframergb, Pcodecctx->width, pCodecCtx->height, i);                 }             }              //Free the packet, is allocated by av_read_frame             av_free_packet (&packet);        }          //Free the RGB image         //free (buffer);         av_free (buffer);         av_free (PFRAMERGB);          //Free the YUV Frame        av_free (pframe);          //Close the Codec        avcodec_close (PCODECCTX);          //Close the video file        av_ Close_input_file (pformatctx);          return 0;}  static void Saveframe (avframe *pframe, int width, int height, int iFrame) {    file *pfile;& Nbsp;   char Szfilename[32];    int  y;     // Open file    sprintf (szFileName, "frame%d.ppm", IFrame);     pfile=fopen ( szFileName, "WB");     if (Pfile==null)         return ;      //Write header    fprintf (pFile, "p6\n%d%d\n255\n", Width, height);      //Write pixel data    for (y=0; y


Then select the configuration Properties, C/C + +, general-and add-on include directories, adding directories for the header files in the 32-bit Builds (Dev) directory you downloaded.

2. Set the Lib file location of the FFmpeg right-click on the project name, select Properties, and select the configuration Properties------general----Add library directory, adding directories to the Lib file directory in 32-bit Builds (Dev) you downloaded.

3. Set the referenced Lib file for the FFmpeg
Right-click the project name, select Properties, and then select the additional dependency, input, configuration properties, linker, and add a file for the Lib file that you downloaded in 32-bit Builds (Dev).

If everything works, then you can compile successfully.
Iii. issues that may arise 1. Although the compilation passes, it does not mean that it can be run, and the following error occurs when you run the code

The reason is, although you reference the Lib file, but this is not a real static library file, but a reference to the DLL, so when you call the FFmpeg library function, the DLL file needs to be present. You can use DUMPBIN (vs self-contained tools) to see which DLL files are referenced in your generated EXE. You enter in the command line: >dumpbin d:\test\test.exe/imports then copy the DLL file under the Bin folder of the downloaded 32-bit Builds (Shared) to the source file directory of your new project, based on the file name displayed.

2. The variable argv[1 in the previous section of the code], if you do not specify the parameters, you will get an error. So if you do not specify it, you need to change it to a const char * string variable, with the string content being the full path of the video you want to read, such as const char* filename = "d:\\work\\code\\videoconference\\ test\\imgp1816. AVI "; The above are configured to run, for the time being written so much, I will continue to summarize later.

Calling FFmpeg in Visual Studio 2010

Related Article

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.