您的位置:首页 > 运维架构 > Linux

linux下ffmpeg原码下载编译封装调用

2016-09-01 10:13 316 查看
转载自:http://blog.sina.com.cn/s/blog_75992b660101mlfo.html

一、安装好Linux的虚拟机。

    我用的是Centos6.3

二、更新系统软件

    执行“sudo yum update”,可使得很多软件更新完毕,当然,耗时有点长,取决于机器性能和网络环境。

三、安装git,并且拉取最新ffmpeg源代码

    因为ffmpeg是用git来维护整个项目的,所以我们必须安装好git,然后用git去拉去最新的源代码。

    执行“sudo yum install git”,来安装git。

    执行“git clone git://source.ffmpeg.org/ffmpeg.git”,完毕后会发现当前目录下会有一个ffmpeg目录,这就是最新的源代码了。

四、编译ffmpeg

    进入ffmpeg文件夹,三部曲,”./configure“,”make“,”sudo make install“。

    在执行./configure的时候,可能会提示没有安装yasm,那么执行”sudo yum installyasm“即可。

    在执行make,就是编译整个ffmpeg项目了,会生成一些二进制文件,例如:ffplay、ffserver、ffmpeg、ffprobe。

    在执行sudo make install时候,会把编译生成的.a归档复制到/usr/local/lib下,下面看结果

[michael@localhost /]$ ll /usr/local/lib

total 69676

-rw-r--r--. 1 root root 51473968 May  9 16:48 libavcodec.a

-rw-r--r--. 1 root root   582964 May  9 16:48 libavdevice.a

-rw-r--r--. 1 root root  1855462 May  9 16:48 libavfilter.a

-rw-r--r--. 1 root root 15274554 May  9 16:48 libavformat.a

-rw-r--r--. 1 root root   219562 May  9 16:48 libavresample.a

-rw-r--r--. 1 root root   576360 May  9 16:48 libavutil.a

-rw-r--r--. 1 root root   189182 May  9 16:48 libswresample.a

-rw-r--r--. 1 root root  1157418 May  9 16:48 libswscale.a

drwxr-xr-x. 2 root root     4096 May  9 16:48 pkgconfig

[michael@localhost /]$ 

   还会把ffmpeg的一些.h的头文件复制到/usr/local/include下,看结果:

[michael@localhost /]$ ll /usr/local/include/

total 32

drwxr-xr-x. 2 root root 4096 May  9 16:48 libavcodec

drwxr-xr-x. 2 root root 4096 May  9 16:48 libavdevice

drwxr-xr-x. 2 root root 4096 May  9 16:48 libavfilter

drwxr-xr-x. 2 root root 4096 May  9 16:48 libavformat

drwxr-xr-x. 2 root root 4096 May  9 16:48 libavresample

drwxr-xr-x. 2 root root 4096 May  9 16:48 libavutil

drwxr-xr-x. 2 root root 4096 May  9 16:48 libswresample

drwxr-xr-x. 2 root root 4096 May  9 16:48 libswscale

[michael@localhost /]$ 

    注意,你可以看到上面这几个都是目录,不然进入libavformat看看,看结果:

[michael@localhost /]$ ll /usr/local/include/libavformat/

total 92

-rw-r--r--. 1 root root 71469 May  9 16:48 avformat.h

-rw-r--r--. 1 root root 16168 May  9 16:48 avio.h

-rw-r--r--. 1 root root  2858 May  9 16:48 version.h

[michael@localhost /]$ 

    可见真正的头文件都在对应名字的目录内,那么我们在程序中引用头文件的时候,应该这样写”#include libavformat/avformat.h“,而不是这样写”#include avformat.h“,为什么呢?因为你不指定路径的情况下,gcc会去/usr/local;/usr下找avformat.h,当然找不到(因为avformat.h在/usr/local/libavformat目录下,而不是在/usr/local下)。所以往往有人编译的时候报错,找不到头文件,就是这个原因。五、写一个调用了ffmpeg库的小程序
   既然ffmpeg安装完毕,那么就自己编一个小程序,在程序里面调用ffmpeg的API吧。

    源代码如下所示:(应该是非常简短的了)

#include "libavformat/avformat.h"

#include "libavcodec/avcodec.h"

#include "libavutil/avutil.h"

#include "stdio.h"

int main(int argc, char* argv[])

{

        printf("going to av_register_all\n");

        av_register_all();

        return 0;

}

    编译命令如下所示:

gcc -o test ./shortest.c -lavformat -lavcodec -lavutil -lz -lm -lpthread -lbz2

    说一下,为什么使用这个编译命令。

    首先,av_register_all()函数里面嵌套了更多的函数,他们的声明在avcodec.h、avformat.h、avutil.h中,所以需要包含这三个头文件,这一步做好了,可保证编译不出错;其次,链接阶段,需要去找这些被声明的函数的定义部分,所以需要链接这些库,libavformat.a、libavcodec.a、libavutil.a、z库、m库(数学库)、pthread库(线程库)、bz2库。所以必须-l链接这些归档库,最后生成test二进制文件,试着运行吧。

六、总结

    写起来很简单,就这五个步骤。但是我自己摸索着,一步一步走过来,理解了不少,可以看到,我之前做了很多准备工作(之前有几篇blog就是做这些准备工作的记录,比如编译,库的理解)。

    有句话叫一通百通,真的弄懂这个了,不管遇到什么库,什么API,只要真心搞懂了一种,其他的也是相似的。

    接下来,就是一个基于ffmpeg的转码工具的设计实现了,为什么不直接做播放器呢?因为播放器需要SDL的相关知识,暂时我不会,而且播放也只是把解码后的数据再进一步通过SDL库来展现,所以转码工具就是基础了,那么从基础做起总没坏处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: