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

ubuntu完整编译安装ffmpeg,成功通过opencv+ffmpeg编译

2016-12-14 22:23 1261 查看
ubuntu14下编译安装ffmpeg是件麻烦事,但是ffmpeg的成功编译又关系到后面使用opencv的成功与否,本文作者在3台ubuntu系统上折腾过ffmpeg,每台电脑因为ffmpeg都重装过2次以上系统,有的是因为莫名其妙的问题,有的是因为一开始版本过高后来想换低版本,结果链接库的时候又提示失败。今天给大家分享完整的编译ffmpeg的过程,下面的命令行都可以直接复制使用:

此次完全成功的版本是:ubuntu14.04+ffmpeg

本文作者安装的共享库路径一致指定为:/usr/local,你也可以指定自己的,比如/usr

1:安装MP3音频编码器 lame

wget https://sourceforge.net/projects/lame/files/lame/3.99/lame-3.99.5.tar.gz/download  "lame-3.99.5.tar.gz"
tar zxvf lame-3.99.5.tar.gz
cd lame-3.99.5
./configure --enable-shared --prefix=/usr/local  &&make -j8 && make install


2.安装汇编优化器yasm

wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz tar zxvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --enable-shared --prefix=/usr/local  &&make -j8 && make install


3.安装ogg库,libogg

wget http://downloads.xiph.org/releases/ogg/libogg-1.3.2.tar.gz tar zxvf libogg-1.3.2.tar.gz
cd libogg-1.3.2
./configure --enable-shared --prefix=/usr/local  &&make -j8 && make install


4.安装vorbis编解码器,libvorbis

wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz tar zxvf libvorbis-1.3.3.tar.gz
cd libvorbis-1.3.3
./configure --enable-shared --prefix=/usr/local  &&make -j8 && make install


这里有可能出现错误提示:

*** Could not run Ogg test program, checking why... *** The test program compiled, but did not run. This usually means *** that the run-time linker is not finding Ogg or finding the wrong *** version of Ogg. If it is not finding Ogg, you'll need to set
your *** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point *** to the installed location Also, make sure you have run ldconfig if that *** is required on your system *** *** If you have an old version installed, it is best to remove it,
although *** you may also be able to get things to work by modifying LD_LIBRARY_PATH configure: error: must have Ogg installed!


解决办法:

# vi /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf
/usr/local/lib
# ldconfig –v


5.安装x264编码器的库,x264-snapshot

git clone http://git.videolan.org/git/x264.git cd x264
./configure --enable-shared --prefix=/usr/local  &&make -j8 && make install


6.安装xvid编码器,xvidcore

wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz tar zxvf xvidcore-1.3.2.tar.gz
cd xvidcore/build/generic/
./configure --enable-shared --prefix=/usr/local  &&make -j8 && make install


7.安装dts库,libdca

wget http://download.videolan.org/pub/videolan/libdca/0.0.5/libdca-0.0.5.tar.bz2 tar xvf libdca-0.0.5.tar.bz2
cd libdca-0.0.5
./configure --enable-shared --prefix=/usr/local  &&make -j8 && make install


8.安装a52,a52dec

wget http://liba52.sourceforge.net/files/a52dec-0.7.4.tar.gz tar zxvf a52dec-0.7.4.tar.gz
cd a52dec-0.7.4
./configure --enable-shared --prefix=/usr/local   'CFLAGS=-fPIC' &&make -j8 && make install


注意这里configure 的选址里面多了’CFLAGS=-fPIC’,如果加这个选项的话,会出现这个错误:

/usr/bin/ld: .libs/imdct.o: relocation R_X86_64_32S against `a local symbol’ can not be used when making a shared object; recompile with -fPIC
.libs/imdct.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
make[1]: *** [liba52.la] Error 1
make[1]: Leaving directory `/home/buynintw/src/a52dec-0.7.4/liba52′
make: *** [install-recursive] Error 1


9.安装faad

wget http://downloads.sourceforge.net/faac/faad2-2.7.tar.gz tar xvf faad2-2.7.tar.gz
cd faad2-2.7
./configure --enable-shared --prefix=/usr/local  --with-mp4v2 &&make -j8 && make install


10.安装faac编码器

wget http://downloads.sourceforge.net/faac/faac-1.28.tar.gz tar xvf faac-1.28.tar.gz
cd faac-1.28
./configure --enable-shared --prefix=/usr/local --with-mp4v2  &&make -j8 && make install


这里肯定会出现一个错误,因为这是源码中的语法错误:

In file included from mp4common.h:29:0,from 3gp.cpp:28:
mpeg4ip.h:126:58: error: new declaration ‘char* strcasestr(const char*, const char*)’ /usr/include/string.h:369:28: error: ambiguates old declaration ‘const char* strcasestr(const char*, const char*)’
make[3]: *** [3gp.o] Error 1
make[3]: Leaving directory `/usr/local/pc/ffmpeg/faac-1.28/common/mp4v2'
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory `/usr/local/pc/ffmpeg/faac-1.28/common'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/pc/ffmpeg/faac-1.28'
make: *** [all] Error 2


解决办法很简单:

在faac-1.28当前目录下,

cd common/mp4v2
vi mpeg4ip.h


找到源代码的第126行和129行

如果vi没有打开行数显示,先:

vi ~/.vimrc


然后在里面写入set nu

然后保存退出
source ~/.vimrc
就能显示行号了

修改对应代码段为:

修改前:
#ifdef __cplusplus
extern "C" {
#endif
char *strcasestr(const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif

修改后:
#ifdef __cplusplus
extern "C++" {
#endif
const char *strcasestr(const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif


全部的改变在于两行:

1:extern “C” {变成extern “C++” {

2:char *strcasestr变成const char *strcasestr

然后再make clean,重新configure,make和make install就行

11.安装3gp的一些编解码文件,amrnb、amrwb

首先是amrnb:
wget http://ftp.penguin.cz/pub/users/utx/amr/amrnb-11.0.0.0.tar.bz2 tar xvf amrnb-11.0.0.0.tar.bz2
cd amrnb-11.0.0.0
./configure --enable-shared --prefix=/usr/local --with-mp4v2  &&make -j8 && make install


然后是amrwb:
wget http://ftp.penguin.cz/pub/users/utx/amr/amrwb-11.0.0.0.tar.bz2 tar xvf amrwb-11.0.0.0.tar.bz2
cd amrwb-11.0.0.0
./configure --enable-shared --prefix=/usr/local --with-mp4v2  &&make -j8 && make install


12.安装opencore-amr

wget https://sourceforge.net/projects/opencore-amr/files/opencore-amr/opencore-amr-0.1.3.tar.gz/download  "opencore-amr-0.1.3.tar.gz"
tar zxvf opencore-amr-0.1.3.tar.gz
cd opencore-amr-0.1.3
./configure --enable-shared --prefix=/usr/local --with-mp4v2  &&make -j8 && make install


13.最后终于可以安装ffmpeg了.可是这里的坑也多

首先是ffmpeg版本的选择,3以上的版本对opencv支持有的不兼容,因此推荐装2版本,2.8,2.7,2.4都行

然后就是这里的configure选项也是很重要的,网上很多版本,让人不知道该configure哪些,这里的终极大招还不会出错的configure如下(其实也是错了多次再回来重新configure得出的教训):

./configure --prefix=/usr/local --enable-libmp3lame --enable-libvorbis --enable-gpl --enable-version3 --enable-nonfree --enable-pthreads --enable-libfaac --enable-libopencore-amrnb \--enable-libopencore-amrwb --enable-libx264 --enable-libxvid --enable-postproc --disable-ffserver --disable-ffplay  --enable-pic --enable-shared

最后
make -j8 && make install


还没完,现在你在命令行直接输入ffmpeg会提示缺少某个库libXXXX, No such file or directoy

,因为我们还没把ffmpeg需要的库告诉系统,这里就取决于之前你–prefix=/usr/local的路径,将这个路径后面加一个lib加入到/etc/ld.so.conf文件中即可:

我们这里的路径当然是/usr/local/lib

sudo vi /etc/ld.so.conf


在文件里面另起一行

内容就是之前的路径:
/usr/local/lib


然后保存退出,

执行命令
ldconfig


此时在命令行输入ffmpeg就行了

[注意]:一定注意之前在不同的configure中加入的 ‘CFLAGS=-fPIC’之类的不同的选项不能少,不然后面你还是得回来重新安装,别在前面为了快捷直接./configure也不–enable-shared ,后面有你好受的,全程遵循我的步骤,不会错的骚年!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: