在linux下开发ffmpeg应用的CMake示例

CmakeLists.txt示例

 1 cmake_minimum_required(VERSION 2.8)
 2 
 3 PROJECT(testFFMpeg)
 4 SET(SRC_LIST main.cpp
 5 
 6 include_directories("/usr/include/ffmpeg")
 7 
 8 ADD_EXECUTABLE(testFFMpegDemo ${SRC_LIST})
 9 
10 target_link_libraries(testFFMpegDemo libavutil.so libavcodec.so libavformat.so libavdevice.so libavfilter.so libswscale.so libpostproc.so )

第六行加入ffmpeg的头文件所在目录,可以根据具体安装目录修改。

第十行为连接动态链接库,有些库是不必要的,但是为了避免缺少的时候添加的麻烦,这里全部都加进去。这些库应位于环境变量下,如果不是可以将动态链接库的路径加入到环境变量中,或者在脚本中写完全路径。

测试代码:

 1 #include <stdio.h>
 2 //注意这里的extern “C” 很重要,不然会出现所有的函数都是 unference的错误。
 3 extern "C" {
 4 #include "libavcodec/avcodec.h"
 5 #include "libavformat/avformat.h"
 6 #include "libavutil/avutil.h"
 7 }
 8 
 9   
10 int main(int argc,char* argv[])
11 {
12     if(argc<2)
13     {
14         printf("input a video file!\r\n");
15     }
16     av_register_all();
17     printf("the video file is %s\r\n",argv[1]);
18 
19     AVFormatContext *pFormatCtx;
20 
21     if(av_open_input_file(&pFormatCtx,argv[1],NULL,0,NULL)!=0)
22         return -1;
23     if(av_find_stream_info(pFormatCtx)<0)
24         return -1;
25     dump_format(pFormatCtx,0,argv[1],0);
26 
27     int i;
28     AVCodecContext *pCodecCtx;
29     int videoStream = -1;
30     for(i=0;i<pFormatCtx->nb_streams;i++)
31     {
32         if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
33         {
34             videoStream = i;
35             break;
36         }
37     } 
38     if(videoStream==-1)
39     {
40         return -1;
41     }
42     pCodecCtx = pFormatCtx->streams[videoStream]->codec;
43 
44     AVCodec *pCodec;
45     pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
46     if(pCodec==NULL){
47         printf("Unspported codec!\n");
48         return -1;
49     }
50     if(avcodec_open(pCodecCtx,pCodec)<0)
51         return -1;
52     printf("open file successed!\n");
53 
54 
55     return 0;
56 }

posted on 2012-11-09 19:28  xiaxia—博客园  阅读(3369)  评论(0编辑  收藏  举报

导航