本文转载自:http://www.voidcn.com/article/p-vxdntdgc-bkq.html

由于个人是从事音视频开发相关的工作,所以也把自己的一些过程写下来,方便大家以及自己查看,时间关系就不多瞎说了,具体过程,都是图文教程

在搭建开发环境之前,首先需要下载ffmpeg源码 (dev版本+share版本)
提醒下 ffmpeg 源码应该是 32bit, 因为当前新建工程也是32位
注意头文件的引用

 
main函数代码如下:
int main (int argc, char **argv)
{
    AVFormatContext *fmt_ctx = NULL;
    AVDictionaryEntry *tag = NULL;
    int ret;
av_register_all();
    if ((ret = avformat_open_input(&fmt_ctx, "love.mp4", NULL, NULL)))
        return ret;
    while ((tag = av_dict_get(fmt_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
{
printf("%s=%s\n", tag->key, tag->value);
}
//运行结果为打印音视频媒体文件基本信息
    avformat_close_input(&fmt_ctx);
    return 0;
}
 
运行结果截图:
 
提醒:VS2008安装目录: E:\software_setup\VS2008\VC\include  需要添加两个头文件 stdint.h 和 inttypes.h

遇到问题:

1.编译失败,

Error 1 error LNK2019: unresolved external symbol _av_register_all referenced in function _wmain C:\Users\Andres\Documents\Visual Studio 2013\Projects\PruebaFFMPEG\PruebaFFMPEG\PruebaFFMPEG.obj PruebaFFMPEG

除了要查明c++引用C文件的文件名有变化失败,还要检查

down vote accepted

 

It seems that you are trying to link a 32-bit application with 64-bit libraries. Download 32-bit libraries from http://ffmpeg.zeranoe.com/builds/ or create x64 configuration for your solution

Open Project properties -> click Configuration Manager... -> Active Solution Platform -> New.. -> Select x64 (see screenshot goo.gl/54ND5t) –  pogorskiy

2.缺少的头文件

Ffmpeg使用的是C99,但是vs2008不支持c99。在VS2008编译ffmpeg时会用到的两个C99标准的头文件。,所以需要你自己下载。并放至相应目录。对于VS2010来说通常是:C:\ProgramFiles (x86)\Microsoft Visual Studio10.0\VC\include。下载地址:http://download.csdn.net/detail/mfcai_blog/9540995

在cpp文件里调用ffmpeg函数要注意.extern“C”是使C++能够调用C写的库文件的一种方式。如果在c++代码中要使用C库中的函数的话,那么就要使用extern”C”来说明。

一个用C写成的库如果想被C/C++同时可以使用,那在头文件应该加上

#ifdef__cplusplus

extern "C"{

#endif

#ifdef__cplusplus

} // endof extern"C"

#endif

3.

VS2008下配置SDL开发环境:

1.下载SDL-devel-1.2.15-VC.zip(在http://www.libsdl.org/download-1.2.php可见)

2.解压缩SDL-devel-1.2.13-VC8.zip,docs里面包含了官方文档,这将是你学习SDL的主要参考资料。找到你在硬盘上安装VC的位置,类似:C:\Program Files\Microsoft Visual Studio 9.0\VC,打开include文件夹,在里面建立一个新文件夹,取名为SDL,打开这个新的文件夹:C:\Program Files\Microsoft Visual Studio 9.0\VC\include\SDL,然后,将SDL Development Libraries中include文件夹里面的文件全部拷贝到刚才建立起来的那个新文件夹中。
  然后,回到VC的.\VC文件夹下,打开lib文件夹:C:\Program Files\Microsoft Visual Studio 9.0\VC\lib,将SDL Development Libraries中lib文件夹下的SDL.lib和SDLmain.lib两个文件拷贝到刚才的那个VC的lib文件夹下

(注意库可以这么取,但是位置不要这么放,要像上面放置ffmpeg库那样去配置)

4.代码示例

#include <stdafx.h> #include <stdio.h>     #define __STDC_CONSTANT_MACROS extern "C"   {       #include "libavcodec/avcodec.h"       #include "libavformat/avformat.h"       #include "libswscale/swscale.h"       #include "libavutil/imgutils.h"   };   #include "SDL_include/SDL.h"   //Refresh Event   #define SFM_REFRESH_EVENT  (SDL_USEREVENT + 1)     #define SFM_BREAK_EVENT  (SDL_USEREVENT + 2)     int thread_exit=0;   int thread_pause=0;     int sfp_refresh_thread(void *opaque){       thread_exit=0;       thread_pause=0;         while (!thread_exit) {           if(!thread_pause){               SDL_Event event;               event.type = SFM_REFRESH_EVENT;               SDL_PushEvent(&event);           }           SDL_Delay(40);       }       thread_exit=0;       thread_pause=0;       //Break       SDL_Event event;       event.type = SFM_BREAK_EVENT;       SDL_PushEvent(&event);         return 0;   }       int main(int argc, char* argv[])   {         AVFormatContext *pFormatCtx;       int             i, videoindex;       AVCodecContext  *pCodecCtx;       AVCodec         *pCodec;       AVFrame *pFrame,*pFrameYUV;       unsigned char *out_buffer;       AVPacket *packet;       int ret, got_picture;         //------------SDL----------------       int screen_w,screen_h;       SDL_Window *screen;        SDL_Renderer* sdlRenderer;       SDL_Texture* sdlTexture;       SDL_Rect sdlRect;       SDL_Thread *video_tid;       SDL_Event event;         struct SwsContext *img_convert_ctx;         //char filepath[]="bigbuckbunny_480x272.h265";       char filepath[]="bigbuckbunny_480x272.h264";         av_register_all();       avformat_network_init();       pFormatCtx = avformat_alloc_context();         if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=0){           printf("Couldn't open input stream.\n");           return -1;       }       if(avformat_find_stream_info(pFormatCtx,NULL)<0){           printf("Couldn't find stream information.\n");           return -1;       }       videoindex=-1;       for(i=0; i<pFormatCtx->nb_streams; i++)            if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){               videoindex=i;               break;           }       if(videoindex==-1){           printf("Didn't find a video stream.\n");           return -1;       }       pCodecCtx=pFormatCtx->streams[videoindex]->codec;       pCodec=avcodec_find_decoder(pCodecCtx->codec_id);       if(pCodec==NULL){           printf("Codec not found.\n");           return -1;       }       if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){           printf("Could not open codec.\n");           return -1;       }       pFrame=av_frame_alloc();       pFrameYUV=av_frame_alloc();         out_buffer=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P,  pCodecCtx->width, pCodecCtx->height,1));       av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize,out_buffer,           AV_PIX_FMT_YUV420P,pCodecCtx->width, pCodecCtx->height,1);         //Output Info-----------------------------       printf("---------------- File Information ---------------\n");       av_dump_format(pFormatCtx,0,filepath,0);       printf("-------------------------------------------------\n");              img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,            pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);                 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {             printf( "Could not initialize SDL - %s\n", SDL_GetError());            return -1;       }        //SDL 2.0 Support for multiple windows       screen_w = pCodecCtx->width;       screen_h = pCodecCtx->height;       screen = SDL_CreateWindow("Simplest ffmpeg player's Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,           screen_w, screen_h,SDL_WINDOW_OPENGL);         if(!screen) {             printf("SDL: could not create window - exiting:%s\n",SDL_GetError());             return -1;       }       sdlRenderer = SDL_CreateRenderer(screen, -1, 0);         //IYUV: Y + U + V  (3 planes)       //YV12: Y + V + U  (3 planes)       sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,pCodecCtx->width,pCodecCtx->height);           sdlRect.x=0;       sdlRect.y=0;       sdlRect.w=screen_w;       sdlRect.h=screen_h;         packet=(AVPacket *)av_malloc(sizeof(AVPacket));         video_tid = SDL_CreateThread(sfp_refresh_thread,NULL,NULL);       //------------SDL End------------       //Event Loop              for (;;) {           //Wait           SDL_WaitEvent(&event);           if(event.type==SFM_REFRESH_EVENT){               while(1){                   if(av_read_frame(pFormatCtx, packet)<0)                       thread_exit=1;                     if(packet->stream_index==videoindex)                       break;               }               ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);               if(ret < 0){                   printf("Decode Error.\n");                   return -1;               }               if(got_picture){                   sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);                   //SDL---------------------------                   SDL_UpdateTexture( sdlTexture, NULL, pFrameYUV->data[0], pFrameYUV->linesize[0] );                     SDL_RenderClear( sdlRenderer );                     //SDL_RenderCopy( sdlRenderer, sdlTexture, &sdlRect, &sdlRect );                     SDL_RenderCopy( sdlRenderer, sdlTexture, NULL, NULL);                     SDL_RenderPresent( sdlRenderer );                     //SDL End-----------------------               }               av_free_packet(packet);           }else if(event.type==SDL_KEYDOWN){               //Pause               if(event.key.keysym.sym==SDLK_SPACE)                   thread_pause=!thread_pause;           }else if(event.type==SDL_QUIT){               thread_exit=1;           }else if(event.type==SFM_BREAK_EVENT){               break;           }         }         sws_freeContext(img_convert_ctx);         SDL_Quit();       //--------------       av_frame_free(&pFrameYUV);       av_frame_free(&pFrame);       avcodec_close(pCodecCtx);       avformat_close_input(&pFormatCtx);         return 0;   }

Windows下搭建ffmpeg+VS2008开发环境详细教程【转】的更多相关文章

  1. Windows下搭建objective C开发环境

    摘自:http://blog.csdn.net/zhanghefu/article/details/18320827 最近打算针对iPhone.iPod touch和iPad开发一些应用,所以,需要开 ...

  2. Windows下搭建Spark+Hadoop开发环境

    Windows下搭建Spark+Hadoop开发环境需要一些工具支持. 只需要确保您的电脑已装好Java环境,那么就可以开始了. 一. 准备工作 1. 下载Hadoop2.7.1版本(写Spark和H ...

  3. windows下搭建nginx+php开发环境

    windows下搭建nginx+php开发环境 1.前言 windows下大多我们都是下载使用集成环境,但是本地已经存在一个集成环境,但不适合项目的需求.因此准备再自己搭建一个环境. 2.准备 工具: ...

  4. 【RN - 基础】之Windows下搭建React Native开发环境

    前言 React Native由Facebook公司于2015年F8大会上开源,其主张“Learn once, write everywhere”.React Native的核心设计理念是:既拥有Na ...

  5. Windows下搭建Android NDK开发环境及命令行编译

    首先说明本文内的相关安装操作参考<Pro Android C++ with the NDK>一书. 安装 Windows搭建Android NDK开发环境需要安装如下部分(同时需要配置对应 ...

  6. WINDOWS 下搭建 OC 集成开发环境

    Objective-C是苹果软件的编程语言,想要上机学习.调试,有一个集成开发环境(IDE)方便很多.有三类方法搭建Objective-C的集成开发环境: 1)   使用苹果的平台,集成开发环境使用X ...

  7. C++开发安卓、windows下搭建Android NDK开发环境

    1. NDK(Native Development Kit) 1.1 NDK简介 Android NDK是一套允许开发人员使用本地代码(如C/C++)进行Android APP功能开发的工具,通过这个 ...

  8. windows下搭建spark+python 开发环境

    有时候我们会在windows 下开发spark程序,测试程序运行情况,再部署到真实服务器中运行. 那么本文介绍如何在windows 环境中搭建简单的基于hadoop 的spark 环境. 我的wind ...

  9. Windows下搭建go语言开发环境 以及 开发IDE (附下载链接)

    1.下载 并且 安装 Go安装包   =========================================================== 在CSDN上传了我的版本,供大家下载: = ...

随机推荐

  1. 关于EasyUI datagrid editor combogrid搜索框的实现

    首先需要datagrid editor对combogrid的扩展,这个是别人实现的: $.extend($.fn.datagrid.defaults.editors, { combogrid: { i ...

  2. Struts/Hibernate/Spring源码下载

    Struts: https://olex.openlogic.com/packages/struts Hibernate: https://olex.openlogic.com/packages/hi ...

  3. Asp.net MVC4 Step By Step(5)-使用Web API

    Web API是ASP.net MVC4新增的一个特色, 应用于处理Ajax请求, 他同时使用了Web标准规范, 比如Http, Json,和XML,以及一系列构建REST数据服务的参考原则, 和AS ...

  4. Linux的那点事

    1.重启nginx服务器 注意,修改了nginx配置文件后最好先检查一下修改过的配置文件是否正确,以免重启后Nginx出现错误影响服务器稳定运行. 判断Nginx配置是否正确命令如下: nginx - ...

  5. 分布式机器学习框架:CXXNet

    caffe是很优秀的dl平台.影响了后面很多相关框架.        cxxnet借鉴了很多caffe的思想.相比之下,cxxnet在实现上更加干净,例如依赖很少,通过mshadow的模板化使得gpu ...

  6. PCL:描述三维离散点的ROPS特征(Code)

    前言: 三维点云为三维欧式空间点的集合.对点云的形状描述若使用局部特征,则可分为两种:固定世界坐标系的局部描述和寻找局部主方向的局部描述,ROPS特征为寻找局部主方向的特征描述. 1.寻找主方向(对X ...

  7. 使用CImage类 显示图片

    在不适用openCv的一种时候,使用CImage显示图片数据,并且直接嵌入DC框中. 使用CImage 在pic控件里显示图片 void CMyCalLawsDlg::MyShowImage( CIm ...

  8. [Advanced Algorithm] - Exact Change

    题目 设计一个收银程序 checkCashRegister(),其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. ci ...

  9. springboot测试类

    Controller测试类 /** * Created by zhiqi.shao on 2017/5/12. */ @RunWith(SpringJUnit4ClassRunner.class) @ ...

  10. Node.js标准的回调函数

    Node.js标准的回调函数:第一个参数代表错误信息,第二个参数代表结果. function (err, data) 当正常读取时,err参数为null,data参数为读取到的String.当读取发生 ...