美文网首页智能家居ndk
ubuntu 下编译FFmpeg以及Android开发环境的搭建

ubuntu 下编译FFmpeg以及Android开发环境的搭建

作者: ChineseBoy | 来源:发表于2017-05-23 18:14 被阅读1381次

    一,开发环境的搭建

    1.安装ubuntu系统,我这里演示的是ubuntu_16.04_LTS。

    注意:[ubuntu14.04交叉编译ffmpeg出现unable to create an executable file. C compiler test failed.]

    解决:我64位ubuntu是新装的,都还没安装32位的兼容库,于是:
    sudo apt-get install ia32-libs
    坑又来了!!!

    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    Package ia32-libs is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source
    However the following packages replace it:
      lib32ncurses5 lib32z1
    
    E: Package 'ia32-libs' has no installation candidate
    
    

    已经被其他的包给替代了。。。
    重新开刷:

    sudo apt-get update
    sudo apt-get -y upgrade
    apt-get -y install gcc g++ make cmake curl  libcurl3 libcurl3-dev bzip2 pkg-config
    

    OK!

    2.进入Home目录下面(图形界面非命令控制台),按Ctrl+H,就可以看到影藏的一些文件了。
    3.找到.bashrc文件,直接打开。
    4.修改.bashrc文件,在最末尾加入下面内容:(当然前提是你已经安装了相应的环境)

    export JAVA_HOME=/home/chinese-boy/Android/jdk1.8.0_121
    export JRE_HOME=${JAVA_HOME}/jre 
    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib 
    export ANDROID_NDK=/home/chinese-boy/Android/android-ndk-r10e
    export ANDROID_SDK=/home/chinese-boy/Android/Sdk
    export PATH=${JAVA_HOME}/bin:$ANDROID_NDK:$PATH
    

    5.这样一个android NDK开发环境基本上就配置完成了。

    二,开始编译ffmpeg

    1.到官网下载最新的ffmepg源码,解压到/home/Android/ffmpeg3.2/目录下面,最好chmod u+x 一下
    2.开始编写build_android.sh shell脚本:内容如下:

    #!/bin/bash  
    NDK=/home/chinese-boy/Android/android-ndk-r10e 
    SYSROOT=$NDK/platforms/android-8/arch-arm/  
    TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64  
    
    function build_one
    {  
        ./configure \
        --prefix=$PREFIX \
        --disable-shared \
        --enable-static \
        --disable-doc \
        --disable-ffmpeg \
        --disable-ffplay \
        --disable-ffprobe \
        --disable-ffserver \
        --disable-doc \
        --disable-symver \
        --enable-small \
        --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
        --target-os=linux \
        --arch=arm \
        --enable-cross-compile \
        --sysroot=$SYSROOT \
        --extra-cflags="-Os -fpic $ADDI_CFLAGS" \
        --extra-ldflags="$ADDI_LDFLAGS" \
        $ADDITIONAL_CONFIGURE_FLAG
        make clean
        make
        make install
    }  
    
    CPU=arm
    PREFIX=$(pwd)/android/$CPU
    ADDI_CFLAGS="-marm"
    build_one 
    

    注意:

    1../configure 后面的配置可以根据自己的需要来填写,其他基本都是固定格式了

    2.如果是直接copy上面的shell脚本,可能会是dos格式,请使用dos2unix build_android.sh 转换一下,删掉多余空格(这一点非常重要,我就掉坑里面了,报错各种命令找不到各种路径找不到之类的,哎!!!!!!!)

    dos2unix 是一个工具,如果没有安装的话请先安装一下:sudo apt-get install dos2unix ,很快就完事。
    下面贴一段报错信息:

    ./build_android.sh: line 18: --enable-shared: command not found
    ./build_android.sh: line 20: --disable-static: command not found
    ./build_android.sh: line 22: --disable-doc: command not found
    ./build_android.sh: line 24: --disable-ffmpeg: command not found
    ./build_android.sh: line 26: --disable-ffplay: command not found
    ./build_android.sh: line 28: --disable-ffprobe: command not found
    ./build_android.sh: line 30: --disable-ffserver: command not found
    ./build_android.sh: line 32: --disable-avdevice: command not found
    

    再来一段大神的shell脚本,基本够了:

    #!/bin/bash
    ######################################################
    # Usage:
    # put this script in top of FFmpeg source tree
    # ./build_android
    # It generates binary for following architectures:
    # ARMv6 
    # ARMv6+VFP 
    # ARMv7+VFPv3-d16 (Tegra2) 
    # ARMv7+Neon (Cortex-A8)
    # Customizing:
    # 1. Feel free to change ./configure parameters for more features
    # 2. To adapt other ARM variants
    # set $CPU and $OPTIMIZE_CFLAGS 
    # call build_one
    ######################################################
    NDK=../..
    PLATFORM=$NDK/platforms/android-8/arch-arm/
    PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
    function build_one
    {
    ./configure --target-os=linux \
        --prefix=$PREFIX \
        --enable-cross-compile \
        --extra-libs="-lgcc" \
        --arch=arm \
        --cc=$PREBUILT/bin/arm-linux-androideabi-gcc \
        --cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
        --nm=$PREBUILT/bin/arm-linux-androideabi-nm \
        --sysroot=$PLATFORM \
        --extra-cflags=" -O3 -fpic -DANDROID -DHAVE_SYS_UIO_H=1 -Dipv6mr_interface=ipv6mr_ifindex -fasm -Wno-psabi -fno-short-enums -fno-strict-aliasing -finline-limit=300 $OPTIMIZE_CFLAGS " \
        --disable-shared \
        --enable-static \
        --extra-ldflags="-Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib -lc -lm -ldl -llog" \
        --disable-everything \
        --enable-demuxer=mov \
        --enable-demuxer=h264 \
        --disable-ffplay \
        --enable-protocol=file \
        --enable-avformat \
        --enable-avcodec \
        --enable-decoder=rawvideo \
        --enable-decoder=mjpeg \
        --enable-decoder=h263 \
        --enable-decoder=mpeg4 \
        --enable-decoder=h264 \
        --enable-parser=h264 \
        --disable-network \
        --enable-zlib \
        --disable-avfilter \
        --disable-avdevice \
        $ADDITIONAL_CONFIGURE_FLAG
    
    make clean
    make  -j4 install
    $PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.o
    $PREBUILT/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib  -soname libffmpeg.so -shared -nostdlib  -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a -lc -lm -lz -ldl -llog  --warn-once  --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-linux-androideabi/4.8/libgcc.a
    }
    
    #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 v7vfp
    #CPU=armv7-a
    #OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
    #PREFIX=./android/$CPU-vfp
    #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
    

    3.增加执行权限,执行:
    $chmod a+x build_android.sh
    $./build_android.sh
    生成:[头文件]android/arm/include/.h 和  [静态库文件]android/arm/lib/.a

    三.接下来就是拿来主义了,开始Android应用之旅:

    1.copy后的目录结构如下:

    Paste_Image.png

    2.android.mk如下:(如果是动态库的话,PREBUILT_SHARED_LIBRARY,LOCAL_SHARED_LIBRARIES,.a -》.so)

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    LOCAL_MODULE    := libavcodec 
    LOCAL_SRC_FILES := prebuilt/lib/$(TARGET_ARCH_ABI)/libavcodec.a
    include $(PREBUILT_STATIC_LIBRARY)
    
    include $(CLEAR_VARS)
    LOCAL_MODULE    := libavformat 
    LOCAL_SRC_FILES := prebuilt/lib/$(TARGET_ARCH_ABI)/libavformat.a
    include $(PREBUILT_STATIC_LIBRARY)
    
    include $(CLEAR_VARS)
    LOCAL_MODULE    := libavutil 
    LOCAL_SRC_FILES := prebuilt/lib/$(TARGET_ARCH_ABI)/libavutil.a
    include $(PREBUILT_STATIC_LIBRARY)
    
    include $(CLEAR_VARS)
    LOCAL_MODULE    := libswresample 
    LOCAL_SRC_FILES := prebuilt/lib/$(TARGET_ARCH_ABI)/libswresample.a
    include $(PREBUILT_STATIC_LIBRARY)
    
    include $(CLEAR_VARS)
    LOCAL_MODULE    := libswscale 
    LOCAL_SRC_FILES := prebuilt/lib/$(TARGET_ARCH_ABI)/libswscale.a
    include $(PREBUILT_STATIC_LIBRARY)
    
    
    # build jni
    include $(CLEAR_VARS)
    LOCAL_MODULE := ffmpeg
    LOCAL_SRC_FILES := ffmpeg.cpp
    LOCAL_C_INCLUDES += $(LOCAL_PATH)/prebuilt/include
    LOCAL_LDLIBS := -llog -lz
    LOCAL_STATIC_LIBRARIES := libavformat libavcodec libswscale libswresample libavutil     
    include $(BUILD_SHARED_LIBRARY)
    

    3.android.mk如下:

    #APP_ABI := all
    #APP_ABI := armeabi armeabi-v7a arm64-v8a x86 x86_64
    APP_ABI := armeabi
    

    4.其中ffmpeg.cpp就是你自己要写的jni代码了。
    5.完毕总结:文章一定要仔细看,中间的坑还是很多的。
    http://blog.csdn.net/chengkaizone/article/details/52983495
    http://blog.csdn.net/u011485531/article/details/55804380
    http://www.cnblogs.com/wainiwann/p/6223342.html

    相关文章

      网友评论

        本文标题:ubuntu 下编译FFmpeg以及Android开发环境的搭建

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