ffmpeg编译

环境

  1. 云主机 Centos 7 x86_64 bbr系统
  2. ffmpeg-3.0.11库
  3. android-ndk-r13b
  1. ndk安装
    1
    2
    3
    4
    //下载
    wget https://dl.google.com/android/repository/android-ndk-r13b-linux-x86_64.zip
    //解压
    unzip android-ndk-r13b-linux-x86_64.zip

ndk个版本下载地址选择:https://blog.csdn.net/shuzfan/article/details/52690554

2.配置环境变量并为ndk授权

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

//进入当前用户根目录
cd ~
//编辑
vim ~/.bashrc

//编辑 ndk为例
export NDKROOT=/usr/ndk/android-ndk-r13b
export PATH=$NDKROOT:$PATH


//更新环境变量
source ~/.bashrc

//授权
chmod 777 -R /usr/ndk

3.下载ffmpge,解压授权

1
2
3
4
5
6
//下载
wget http://ffmpeg.org/releases/ffmpeg-3.0.11.tar.gz
//解压
tar zxvf ffmpeg-3.0.11.tar.gz
//授权
chmod 777 -R /usr/zzw/ffmpeg-3.0.11

ffmpeg各版本下载地址:http://ffmpeg.org/download.html

  1. 根目录创建ffmpegtemp文件夹,配置脚本build_android.sh,config我是windows+xftp的方式
    4.1 在windows上新建build_android.sh,并书写
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    #!/bin/bash
    export TMPDIR=/usr/zzw/ffmpeg-3.0.11/ffmpegtemp
    NDK=/usr/ndk/android-ndk-r13b
    PLATFORM=$NDK/platforms/android-14/arch-arm/
    TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64

    function build_one
    {
    ./configure \
    --prefix=$PREFIX \
    --target-os=linux \
    --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
    --arch=arm \
    --sysroot=$PLATFORM \
    --extra-cflags="-I$PLATFORM/usr/include" \
    --cc=$TOOLCHAIN/bin/arm-linux-androideabi-gcc \
    --nm=$TOOLCHAIN/bin/arm-linux-androideabi-nm \
    --enable-shared \
    --enable-runtime-cpudetect \
    --enable-gpl \
    --enable-small \
    --enable-cross-compile \
    --disable-debug \
    --disable-static \
    --disable-doc \
    --disable-asm \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-ffserver \
    --disable-postproc \
    --disable-avdevice \
    --disable-symver \
    --disable-stripping \


    $ADDITIONAL_CONFIGURE_FLAG
    sed -i '' 's/HAVE_LRINT 0/HAVE_LRINT 1/g' config.h
    sed -i '' 's/HAVE_LRINTF 0/HAVE_LRINTF 1/g' config.h
    sed -i '' 's/HAVE_ROUND 0/HAVE_ROUND 1/g' config.h
    sed -i '' 's/HAVE_ROUNDF 0/HAVE_ROUNDF 1/g' config.h
    sed -i '' 's/HAVE_TRUNC 0/HAVE_TRUNC 1/g' config.h
    sed -i '' 's/HAVE_TRUNCF 0/HAVE_TRUNCF 1/g' config.h
    sed -i '' 's/HAVE_CBRT 0/HAVE_CBRT 1/g' config.h
    sed -i '' 's/HAVE_RINT 0/HAVE_RINT 1/g' config.h

    make clean
    make -j4
    make install
    }

    # arm v7vfp
    CPU=armv7-a
    OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
    PREFIX=./android/$CPU-vfp
    ADDITIONAL_CONFIGURE_FLAG=
    build_one

    # CPU=armv
    # PREFIX=$(pwd)/android/$CPU
    # ADDI_CFLAGS="-marm"
    # build_one

    #arm v6
    #CPU=armv6
    #OPTIMIZE_CFLAGS="-marm -march=$CPU"
    #PREFIX=./android/$CPU
    #ADDITIONAL_CONFIGURE_FLAG=
    #build_one

    #arm v7vfpv3
    # CPU=armv7-a
    # OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU "
    # PREFIX=./android/$CPU
    # ADDITIONAL_CONFIGURE_FLAG=
    # build_one

    #arm v7n
    #CPU=armv7-a
    #OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=neon -marm -march=$CPU -mtune=cortex-a8"
    #PREFIX=./android/$CPU
    #ADDITIONAL_CONFIGURE_FLAG=--enable-neon
    #build_one

    #arm v6+vfp
    #CPU=armv6
    #OPTIMIZE_CFLAGS="-DCMP_HAVE_VFP -mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU"
    #PREFIX=./android/${CPU}_vfp
    #ADDITIONAL_CONFIGURE_FLAG=
    #build_one

注意”\“后面不要放空格

4.2 使用xftp传输到云主机的ffmpeg解压根目录,并将该文件改为linux的编码

1
2
3
4
5
vim build_android.sh

#vim下执行
:set ff=unix
:wq

4.3 修改configure

1
2
3
4
5
6
7
8
9
10
11
12
13
#这段脚本的作用是使生成的静态库为.so为后缀,生成的库是这种格式:libavcodec-57.so ... 
#不加的话生成的库是这种格式:libavcodec.so.57 ...等等

#注释以下内容
#SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
#LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
#SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
#SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR) $(SLIBNAME)'
#增加以下内容
SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
SLIB_INSTALL_LINKS='$(SLIBNAME)'
  1. 执行脚本
    1
    ./build_android.sh

6.上面生成的是so动态库,生成.a静态库只需在build_android.sh修改如下:

1
2
--enable-static \
--disable-shared \

7.生成一个库,在第6步生成静态库.a文件下,在build_android.sh的函数build_one添加如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
export TMPDIR=/usr/zzw/ffmpeg-3.0.11/ffmpegtemp
NDK=/usr/ndk/android-ndk-r13b
PLATFORM=$NDK/platforms/android-14/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64

function build_one
{
./configure \
--prefix=$PREFIX \
--target-os=linux \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--arch=arm \
--sysroot=$PLATFORM \
--extra-cflags="-I$PLATFORM/usr/include" \
--cc=$TOOLCHAIN/bin/arm-linux-androideabi-gcc \
--nm=$TOOLCHAIN/bin/arm-linux-androideabi-nm \
--enable-static \
--enable-runtime-cpudetect \
--enable-gpl \
--enable-small \
--enable-cross-compile \
--disable-debug \
--disable-shared \
--disable-doc \
--disable-asm \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-postproc \
--disable-avdevice \
--disable-symver \
--disable-stripping \


$ADDITIONAL_CONFIGURE_FLAG
sed -i '' 's/HAVE_LRINT 0/HAVE_LRINT 1/g' config.h
sed -i '' 's/HAVE_LRINTF 0/HAVE_LRINTF 1/g' config.h
sed -i '' 's/HAVE_ROUND 0/HAVE_ROUND 1/g' config.h
sed -i '' 's/HAVE_ROUNDF 0/HAVE_ROUNDF 1/g' config.h
sed -i '' 's/HAVE_TRUNC 0/HAVE_TRUNC 1/g' config.h
sed -i '' 's/HAVE_TRUNCF 0/HAVE_TRUNCF 1/g' config.h
sed -i '' 's/HAVE_CBRT 0/HAVE_CBRT 1/g' config.h
sed -i '' 's/HAVE_RINT 0/HAVE_RINT 1/g' config.h

make clean
make -j4
make install

# 增加的内容
$TOOLCHAIN/bin/arm-linux-androideabi-ld \
-rpath-link=$PLATFORM/usr/lib \
-L$PLATFORM/usr/lib \
-L$PREFIX/lib \
-soname libffmpeg.so -shared -nostdlib -Bsymbolic --whole-archive --no-undefined -o \
$PREFIX/libffmpeg.so \
libavcodec/libavcodec.a \
libavfilter/libavfilter.a \
libswresample/libswresample.a \
libavformat/libavformat.a \
libavutil/libavutil.a \
libswscale/libswscale.a \
-lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker \
$TOOLCHAIN/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a \

}

# arm v7vfp
CPU=armv7-a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
PREFIX=./android/$CPU-vfp
ADDITIONAL_CONFIGURE_FLAG=
build_one

# CPU=armv
# PREFIX=$(pwd)/android/$CPU
# ADDI_CFLAGS="-marm"
# build_one

#arm v6
#CPU=armv6
#OPTIMIZE_CFLAGS="-marm -march=$CPU"
#PREFIX=./android/$CPU
#ADDITIONAL_CONFIGURE_FLAG=
#build_one

#arm v7vfpv3
# CPU=armv7-a
# OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU "
# PREFIX=./android/$CPU
# ADDITIONAL_CONFIGURE_FLAG=
# build_one

#arm v7n
#CPU=armv7-a
#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=neon -marm -march=$CPU -mtune=cortex-a8"
#PREFIX=./android/$CPU
#ADDITIONAL_CONFIGURE_FLAG=--enable-neon
#build_one

#arm v6+vfp
#CPU=armv6
#OPTIMIZE_CFLAGS="-DCMP_HAVE_VFP -mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU"
#PREFIX=./android/${CPU}_vfp
#ADDITIONAL_CONFIGURE_FLAG=
#build_one

8.具体要编译哪个库,自己设置。 通过

1
./configure --help

命令查看怎么设置,通过打开configure文件查看所对应的库之间的依赖等。

依赖关系

1
2
3
4
5
6
7
8
9
10
11
# libraries, in linking order
avcodec_deps="avutil"
avcodec_select="null_bsf"
avdevice_deps="avformat avcodec avutil"
avfilter_deps="avutil"
avformat_deps="avcodec avutil"
avformat_suggest="network"
avresample_deps="avutil"
postproc_deps="avutil gpl"
swresample_deps="avutil"
swscale_deps="avutil"

通过这个就可以知道在android里面的so库加载顺序

1
2
3
4
5
6
7
8
9
10
static {
System.loadLibrary("avutil-55");
System.loadLibrary("swresample-2");
System.loadLibrary("avcodec-57");
System.loadLibrary("avformat-57");
System.loadLibrary("swscale-4");
System.loadLibrary("avfilter-6");
//自己实现逻辑编译的库
System.loadLibrary("native-lib");
}

也可以通过查看生成的lib/pkgconfig里面对应的pc文件查看具体的依赖关系

比如libavfilter.pc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
prefix=./android/armv7-a-vfp
exec_prefix=${prefix}
libdir=${prefix}/lib
includedir=${prefix}/include

Name: libavfilter
Description: FFmpeg audio/video filtering library
Version: 6.31.100
Requires:
Requires.private: libswscale >= 4.0.100, libavformat >= 57.25.100, libavcodec >= 57.24.102, libswresample >= 2.0.101, libavutil >= 55.17.103
Conflicts:
Libs: -L${libdir} -lavfilter
Libs.private: -lm -lz -pthread
Cflags: -I${includedir}

-------------The End-------------