动态(共享)链接库自是有诸多优于静态链接库的特点:如能共享内存空间; 节约整个系统的存储空间,从而使整个系统速度提高; 当只有某个动态链接库有更新时--便于维护--只用更新这个库。
但在有些情况下,如,服务器并不是你一个人独享;服务器上不止有一个版本的ffmpeg要用; 有一个集群的服务器需要你部署和维护,更新。这时能使用静态链接库就再好不过啦。
FFmpeg对第三方库通常是优先使用动态链接的。可以使用下面介绍的步骤实现这些第三方库的静态链接,从而你的ffmpeg只用copy一个程序就可以到处用,也方便了部署和维护。
第三方库:(3RDLIB_INSTALL_PATH为本机上你想将第三方库安装的路径)
》》》 为FFmpeg-4.0添加libopus静态库
1. opus-1.2.1
./configure --prefix=/data/PJT-ffmpeg-4.0/install/ --enable-static --disable-shared
make && make install
Libraries have been installed in:
/data/PJT-ffmpeg-4.0/install/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- use the '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
2. ffmpeg-4.0
$ cd ffmpeg-4.0
$ vim linux_configure.sh
#!/bin/sh
./configure \
--prefix=/data/PJT-ffmpeg-4.0/install/ \
--disable-ffplay \
--enable-debug=3 --extra-cflags=-Wall --extra-cflags=-O0 --enable-pic \
--enable-gpl --enable-nonfree --enable-version3 \
--disable-shared --enable-static \
--enable-pthreads --enable-runtime-cpudetect \
--disable-postproc \
--extra-cflags='-I/data/PJT-ffmpeg-4.0/install/include -DREDIRECT_DEBUG_LOG' \
--extra-ldflags='-L/data/PJT-ffmpeg-4.0/install/lib -gl' \
--enable-libopus --enable-encoder=libopus --enable-decoder=libopus \
--enable-libx264 --enable-encoder=libx264 --disable-opencl \
--enable-avfilter --enable-filter=movie
【保存并退出】
$ ./linux_configure.sh
ERROR: opus not found using pkg-config
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.
$ pkg-config --list-all
libpostproc libpostproc - FFmpeg postprocessing library
libswscale libswscale - FFmpeg image rescaling library
libavdevice libavdevice - FFmpeg device handling library
libssl OpenSSL - Secure Sockets Layer and cryptography libraries
libavfilter libavfilter - FFmpeg audio/video filtering library
libavutil libavutil - FFmpeg utility library
libswresample libswresample - FFmpeg audio resampling library
opus Opus - Opus IETF audio codec (floating-point build)
com_err com_err - Common error description library
openssl OpenSSL - Secure Sockets Layer and cryptography libraries and tools
libidn Libidn - IETF stringprep, nameprep, punycode, IDNA text processing.
zlib zlib - zlib compression library
fdk-aac Fraunhofer FDK AAC Codec Library - AAC codec library
libcrypto OpenSSL-libcrypto - OpenSSL cryptography library
uuid uuid - Universally unique id library
libsepol libsepol - SELinux policy library
libselinux libselinux - SELinux utility library
shared-mime-info shared-mime-info - Freedesktop common MIME database
libcurl libcurl - Library to transfer files with ftp, http, etc.
libavformat libavformat - FFmpeg container format library
x264 x264 - H.264 (MPEG4 AVC) encoder library
libavcodec libavcodec - FFmpeg codec library
$ export PKG_CONFIG_PATH=/data/PJT-ffmpeg-4.0/install/lib/pkgconfig
$ vim ffbuild/config.log
gcc -D_ISOC99_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -DPIC -Wall -O0 -I/data/PJT-ffmpeg-4.0/install/include -DREDIRECT_DEBUG_LOG -std=c99 -fomit-frame-pointer -fPIC -pthread -I/data/PJT-ffmpeg-4.0/install/include/opus -L/data/PJT-ffmpeg-4.0/install/lib -c -o /tmp/ffconf.S7eM3X5J/test.o /tmp/ffconf.S7eM3X5J/test.c
gcc -L/data/PJT-ffmpeg-4.0/install/lib -gl -Wl,--as-needed -Wl,-z,noexecstack -I/data/PJT-ffmpeg-4.0/install/include/opus -L/data/PJT-ffmpeg-4.0/install/lib -o /tmp/ffconf.S7eM3X5J/test /tmp/ffconf.S7eM3X5J/test.o -lopus
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(opus_decoder.o): In function `opus_decode_frame':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/src/opus_decoder.c:570: undefined reference to `exp'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(celt_decoder.o): In function `celt_decode_lost':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/celt_decoder.c:669: undefined reference to `sqrtf'
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/celt_decoder.c:743: undefined reference to `sqrtf'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(pitch.o): In function `compute_pitch_gain':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/pitch.c:433: undefined reference to `sqrtf'
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/pitch.c:433: undefined reference to `sqrtf'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(quant_bands.o): In function `amp2Log2':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/quant_bands.c:553: undefined reference to `log'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(quant_bands.o): In function `quant_fine_energy':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/quant_bands.c:379: undefined reference to `floorf'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(quant_bands.o): In function `quant_coarse_energy_impl':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/quant_bands.c:202: undefined reference to `floorf'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(vq.o): In function `renormalise_vector':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/vq.c:394: undefined reference to `sqrtf'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(vq.o): In function `fast_atan2f':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/vq.c:406: undefined reference to `floorf'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(vq.o): In function `stereo_itheta':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/vq.c:429: undefined reference to `sqrtf'
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/vq.c:428: undefined reference to `sqrtf'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(vq.o): In function `fast_atan2f':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/vq.c:406: undefined reference to `floorf'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(vq.o): In function `op_pvq_search_c':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/vq.c:224: undefined reference to `floorf'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(vq.o): In function `exp_rotation':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/vq.c:86: undefined reference to `cosf'
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/vq.c:87: undefined reference to `cosf'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(vq.o): In function `normalise_residual':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/vq.c:131: undefined reference to `sqrtf'
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/vq.c:131: undefined reference to `sqrtf'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(bands.o): In function `compute_band_energies':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/bands.c:169: undefined reference to `sqrtf'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(bands.o): In function `anti_collapse':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/bands.c:299: undefined reference to `exp'
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/bands.c:300: undefined reference to `sqrt'
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/bands.c:336: undefined reference to `exp'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(bands.o): In function `denormalise_bands':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/bands.c:230: undefined reference to `exp'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(bands.o): In function `quant_band':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/bands.c:1225: undefined reference to `sqrt'
/data/PJT-ffmpeg-4.0/install/lib/libopus.a(bands.o): In function `stereo_merge':
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/bands.c:458: undefined reference to `sqrtf'
/data/PJT-ffmpeg-4.0/3rdlib-source/opus-1.2.1/celt/bands.c:456: undefined reference to `sqrtf'
collect2: ld returned 1 exit status
ERROR: opus not found using pkg-config
【退出】
$ vim configure
将
enabled libopus && {
enabled libopus_decoder && {
require_pkg_config libopus opus opus_multistream.h opus_multistream_decoder_create
}
enabled libopus_encoder && {
require_pkg_config libopus opus opus_multistream.h opus_multistream_surround_encoder_create
}
}
改成:
enabled libopus && {
enabled libopus_decoder && {
require_pkg_config libopus opus opus_multistream.h opus_multistream_decoder_create -lm
}
enabled libopus_encoder && {
require_pkg_config libopus opus opus_multistream.h opus_multistream_surround_encoder_create -lm
}
}
【保存并退出】
$ ./linux_configure.sh && make && make install
3. 测试并验证
./ffmpeg -i opus_16k1c-old.aac -acodec libopus -b:a 64k -compression_level 10 -y output.opus
ffmpeg version 4.0 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-23)
configuration: --prefix=/data/PJT-ffmpeg-4.0/install/ --disable-ffplay --enable-debug=3 --extra-cflags=-Wall --extra-cflags=-O0 --enable-pic --enable-gpl --enable-nonfree --enable-version3 --disable-shared --enable-static --enable-pthreads --enable-runtime-cpudetect --disable-postproc --extra-cflags='-I/data/PJT-ffmpeg-4.0/install/include -DREDIRECT_DEBUG_LOG' --extra-ldflags='-L/data/PJT-ffmpeg-4.0/install/lib -gl' --enable-libopus --enable-encoder=libopus --enable-decoder=libopus --enable-libx264 --enable-encoder=libx264 --disable-opencl --enable-avfilter --enable-filter=movie
libavutil 56. 14.100 / 56. 14.100
libavcodec 58. 18.100 / 58. 18.100
libavformat 58. 12.100 / 58. 12.100
libavdevice 58. 3.100 / 58. 3.100
libavfilter 7. 16.100 / 7. 16.100
libswscale 5. 1.100 / 5. 1.100
libswresample 3. 1.100 / 3. 1.100
[aac @ 0x2e4a580] Estimating duration from bitrate, this may be inaccurate
Input #0, aac, from 'opus_16k1c-old.aac':
Duration: 00:02:56.28, bitrate: 56 kb/s
Stream #0:0: Audio: aac (LC), 16000 Hz, mono, fltp, 56 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (aac (native) -> opus (libopus))
Press [q] to stop, [?] for help
Output #0, opus, to 'output.opus':
Metadata:
encoder : Lavf58.12.100
Stream #0:0: Audio: opus (libopus), 16000 Hz, mono, flt, 64 kb/s
Metadata:
encoder : Lavc58.18.100 libopus
size= 1580kB time=00:02:39.95 bitrate= 80.9kbits/s speed= 142x
video:0kB audio:1567kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.864157%
》》》 libspeex 静态库的添加:
下载:https://www.speex.org/downloads/
tar -zxvf speex-1.2.0.tar.gz
cd speex-1.2.0
./configure --prefix=3RD_LIB_INSTALL_PATH --enable-static --disable-shared
make && make install
需要修复ffmpeg的configure文件
cd /data/ffmpeg
vim configure
将:
enabled libspeex && require_pkg_config speex speex/speex.h speex_decoder_init -lspeex
改成:
#enabled libspeex && require_pkg_config speex speex/speex.h speex_decoder_init -lspeex
enabled libspeex && require libspeex speex/speex.h speex_decoder_init -lspeex
【保存并退出】
配置选项添加如下:
#!/bin/sh
./configure \
--prefix=/data/ffmpeg-install/ \
--disable-ffplay --disable-ffserver \
--enable-debug=3 --extra-cflags=-Wall --extra-cflags=-O0 --disable-optimizations --disable-asm --enable-pic \
--enable-gpl --enable-nonfree --enable-version3 \
--disable-shared --enable-static \
--enable-pthreads --enable-runtime-cpudetect \
--enable-zlib --enable-bzlib \
--enable-postproc \
--extra-cflags='-I/data/ffmpeg-3rdlib/install/include -DREDIRECT_DEBUG_LOG' \
--extra-ldflags='-L/data/ffmpeg-3rdlib/install/lib -gl' \
--enable-libx264 --enable-encoder=libx264 --disable-opencl \
--enable-encoder=libfdk_aac --enable-decoder=libfdk_aac --enable-muxer=adts --enable-libfdk-aac \
--enable-libspeex --enable-encoder=libspeex --enable-decoder=libspeex \
--enable-avfilter --enable-filter=movie
》》》》
1. libfaac
#cd faac-1.28
#./configure --prefix=3RDLIB_INSTALL_PATH --enable-static --disable-shared
#make;make install
2. libmp3lame
#cd lame-3.98.4
#./configure --prefix=3RDLIB_INSTALL_PATH --enable-static --disable-shared
#make; make install
NOTE:
因为它的configure参数并不起作用,所以一定要到3RDLIB_INSTALL_PATH/lib下将libmp3lame的动态库都删除。
3. libx264
#cd x264-snapshot-VERSION
#./configure --prefix=3RDLIB_INSTALL_PATH --enable-static
#make; make install
FFmpeg的配置
#cd ffmpeg
#./configure
--enable-gpl --enable-nonfree --enable-version3
--disable-shared --enable-static
--enable-pthreads --enable-runtime-cpudetect
--enable-zlib --enable-bzlib \
--enable-postproc
--extra-cflags='-I3RDLIB_INSTALL_PATH/include -DREDIRECT_DEBUG_LOG'
--extra-ldflags='-L3RDLIB_INSTALL_PATH/lib -gl'
--enable-libxvid --enable-decoder=libxvid --enable-encoder=libxvid
--disable-encoder=aac
--enable-libfaac --enable-encoder=libfaac
--enable-libmp3lame --enable-encoder=libmp3lame
--enable-libx264 --enable-encoder=libx264
--enable-avfilter --enable-filter=movie
#make;
其实,静态链接的关键就是要保证在你的库路径“3RDLIB_INSTALL_PATH”下没有动态链接库(.so),这样ffmpeg在默认的动态库找不到的情况下就会链接静态库。
最新的ffmpeg v0.11编译与链接
1. libfaac
- $cd faac-1.28
- $./configure --prefix=3RDLIB_INSTALL_PATH --enable-static --disable-shard
- $make
- $make install
2. libmp3lame
- $tar -zxvf lame-3.98.4.tar.gz
- $./configure --prefix=3RDLIB_INSTALL_PATH--enable-static --disable-shared
- $make
- $make install
3.libx264
- $git clone git://git.videolan.org/x264.git
- $cd x264
- $./configure --prefix=3RDLIB_INSTALL_PATH --enable-static
- $make
- $make install
4.librtmp
- $git clone git://git.ffmpeg.org/rtmpdump
- $cd rtmpdump
- $cd librtmp
修改Makefile文件
$vim Makefile
- #prefix=/usr/local
- prefix=3RDLIB_INSTALL_PATH
- ...
- #CRYPTO=OPENSSL
- CRYPTO=
- ...
- #SO_LDFLAGS_posix=-shared -Wl,-soname,$@
- SO_LDFLAGS_posix=-Wl,-soname,$@
- ...
- #SHARED=yes
- SHARED=no
- 保存并退出
- $make
- $cd ..
- $make SYS=darwin XDEF=-DNO_SSL CRYPTO= SHARED=
- $make install
5. ffmpeg配置
- $cd ffmpeg
- 修改configure文件
- $vim configure
- ...
- #enabled librtmp && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket
- enabled librtmp && { check_lib librtmp/rtmp.h RTMP_Socket -lrtmp -lpolarssl -lwinmm -lws2_32 ||
- check_lib librtmp/rtmp.h RTMP_Socket -lrtmp -lssl -lcrypto -lz ||
- die "ERROR: librtmp not found or RTMP_Socket() missing, librtmp
- version must be >= 2.2.f"; }
- 保存并退出
-
- 也可以改成这样:
- $vim configure
- ...
- #enabled librtmp && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket
- enabled librtmp && { check_lib librtmp/rtmp.h -lrtmp||
- check_lib librtmp/rtmp.h -lrtmp -lz ||
- die "ERROR: librtmp not found or RTMP_Socket() missing, librtmp
- version must be >= 2.2.f"; }
- 保存并退出
- $./configure \
- --enable-gpl --enable-nonfree --enable-version3 \
- --disable-shared --enable-static \
- --enable-pthreads --enable-runtime-cpudetect \
- --enable-zlib --enable-bzlib \
- --enable-postproc \
- --extra-cflags='-I/3RDLIB_INSTALL_PATH/include -DREDIRECT_DEBUG_LOG' \
- --extra-ldflags='-L/3RDLIB_INSTALL_PATH/lib -gl' \
- --disable-encoder=aac \
- --enable-libfaac --enable-encoder=libfaac \
- --enable-libmp3lame --enable-encoder=libmp3lame \
- --enable-libx264 --enable-encoder=libx264 \
- --enable-avfilter --enable-filter=movie \
- --enable-librtmp
- $make
- $make install
编译ffmpeg以支持rtmp, librtmp not found问题
在mingw下编译ffmpeg,并加上对rtmp的支持,即要先编译librtmp. 编译过程出了个librtmp not found问题,
google了一圈,发现有很多类似的问题,但都和我的不完全一样。
我的根本问题是mingw下的pkg-config没有正确安装。
librtmp经过make和make install后,库文件和头文件都已经分别拷贝到/usr/local/lib和/usr/local/include目录下,
ffmpeg在configure时找不到librtmp,为什么找不到呢?
百思不得其解,无奈之下google,最终得到提示。
打开configure文件,发现下面一行
enabled librtmp && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket
原来ffmpeg通过pkg-config去找librtmp,难怪找不着了。
解决办法:
以#符号注释掉enabled librtmp一行
在configure时加上参数
./configure \
... ...
--extra-ldflags="-L/usr/local/lib" \
--extra-cflags="-I/usr/local/include" \
--extra-libs="-lrtmp" \
... ...