本文将介绍如何在ubuntu16环境下搭建ffmpeg的开发环境,根本目标是编译安装ffmpeg并获得lib动态链接库与include头文件。
安装:
1、如果需要用ffplay播放音视频,请先安装依赖sdl2:
下载sdl2源码:http://libsdl.org/release/
tar -xvf xxx.tar.gz
cd xxx
./configure --enable-shared --prefix=/opt/sdl2
make && sudo make install
// --prefix可指定bin、lib、include等文件夹的安装目录
2、下载ffmpeg源码:http://www.ffmpeg.org/download.html
./configure --enable-shared --prefix=/opt/ffmpeg
make //时间较长
sudo make install
3、将/opt/ffmpeg/bin下的ffmpeg, ffplay, ffprobe三个可执行文件,建立 软链接到/usr/local/bin,如sudo ln -s /opt/ffmpeg/bin/ffplay /usr/local/bin/ffplay
4、最后vim /etc/ld.so.config/ffmpeg.conf,写入/opt/ffmpeg/lib
,添加配置动态库搜索路径。
最后使用命令行测试ffplay, ffprobe, ffmpeg是否安装成功。
ffplay播放视频或音频,ffprobe查看视频信息,ffmpeg进行格式转换,视频剪切等操作。
测试:
将/opt/ffmpeg下的include文件夹和lib下的.so文件提取出来,放到我们的工作目录
手动写一个helloworld测试开发环境
#include <stdio.h>
#include "libavcodec/avcodec.h"
int main(int argc, char **argv) {
printf("printf ffmpeg configuration: ");
printf("%s\n", avcodec_configuration());
exit(0);
}
编译命令:gcc helloworld.c -I include/ -L lib/ -lavcodec -lavutil
△gcc 编译出现 “ /usr/bin/ld: cannot open output file a.out: Permission denied ” 的解决办法:
提示权限不够,先查看下(ls -all)c文件的权限,后修改文件权限。使用命令:chmod 777 tmp.c修改权限后发现仍然提示权限不够。于是想到可能要修改目录或文件的所有者和属组
其它补充:
/opt/ffmpeg/lib下除了.so文件,还有一个pkgconfig目录,里面有不少.pc文件,这些文件有什么作用呢?
在bash配置(~/.bashrc)中添加下行,将该子文件夹加入pkg-config环境变量PKG_CONFIG_PATH作为搜索路径:
export PKG_CONFIG_PATH=/opt/ffmpeg/lib/pkgconfig:/lib/pkgconfig:$PKG_CONFIG_PATH
libavcodec.pc文件告诉系统,可以在/opt/ffmpeg/lib中找到libavcodec.so,可以在/opt/ffmpeg/include中找到xxx头文件。
gcc helloworld.c $(pkg-config --libs --cflags libavcodec libavutil)