美文网首页
ffmpeg 编译

ffmpeg 编译

作者: 曾大稳丶 | 来源:发表于2018-05-02 21:18 被阅读0次

    环境

    1. 云主机 Centos 7 x86_64 bbr系统
    2. ffmpeg-3.0.11库
    3. android-ndk-r13b
    1. ndk安装
    //下载
    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授权

    
    //进入当前用户根目录
    cd ~ 
    //编辑
    vim ~/.bashrc
    
    //编辑 ndk为例
    export NDKROOT=/usr/ndk/android-ndk-r13b
    export PATH=$NDKROOT:$PATH
    
    
    //更新环境变量
    source ~/.bashrc
    
    //授权
    chmod 777 -R /usr/ndk
    
    

    3.下载ffmpge,解压授权

    //下载
    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,并书写
    #!/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的编码

    vim build_android.sh
    
    #vim下执行
    :set ff=unix
    :wq
    
    

    4.3 修改configure

    #这段脚本的作用是使生成的静态库为.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. 执行脚本
    ./build_android.sh
    

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

    --enable-static \
    --disable-shared \
    

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

    #!/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.具体要编译哪个库,自己设置。 通过

    ./configure --help
    

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

    依赖关系

    # 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库加载顺序

     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");
        }
    

    相关文章

      网友评论

          本文标题:ffmpeg 编译

          本文链接:https://www.haomeiwen.com/subject/bisktftx.html