Mac上编译FFmpeg For Android

詹高畅
2023-12-01

准备工作:

  1. 下载 FFMPEG
  2. 下载 android-ndk-r14b-MAC 

配置configure

  1. 解压下载到文件
  2. 找到configure文件,以文本方式打开,我这里用到是Xcode打开,找到以下代码,位置大概在3415行,或者使用control+F快速查找
  3. 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)'
    
  4. 替换为:
  5. SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
    LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
    SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
    SLIB_INSTALL_LINKS='$(SLIBNAME)'

    新建文件build_android.sh

  6. 配置build_android.sh,当前我只编译arm64-v8a,如需编译其他类型,直接去掉相应“#”即可
  7. #!/bin/sh
    
    #  build_android_arm64_v8a.sh
    #  
    #
    #  Created by Charlie on 2020/4/10.
    #  
    #!/bin/bash
    #Change NDK to your Android NDK location
    #aarch64-linux-android-gcc is unable to create an executable file.
    #!/bin/bash
    MY_LIBS_NAME=ffmpeg-3.4.7
    MY_DIR=ffmpeg-3.4.7
    
    # cd ./${MY_DIR}
    
    #编译的过程中产生的中间件的存放目录,为了区分编译目录,源码目录,install目录
    MY_BUILD_DIR=binary
    
    
    NDK_PATH=/Users/xxx(这是我的电脑文件名)/NDK/android-ndk-r14b
    BUILD_PLATFORM=darwin-x86_64
    TOOLCHAIN_VERSION=4.9
    ANDROID_VERSION=21
    
    ANDROID_ARMV5_CFLAGS="-march=armv5te"
    ANDROID_ARMV7_CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=neon"  #-mfloat-abi=hard -mfpu=vfpv3-d16 #-mfloat-abi=hard -mfpu=vfp
    ANDROID_ARMV8_CFLAGS="-march=armv8-a"
    ANDROID_X86_CFLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"
    ANDROID_X86_64_CFLAGS="-march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel"
    
    
    # params($1:arch,$2:arch_abi,$3:host,$4:cross_prefix,$5:cflags)
    build_bin() {
    
        echo "-------------------star build $2-------------------------"
    
        ARCH=$1         # arm arm64 x86 x86_64
        ANDROID_ARCH_ABI=$2     # armeabi armeabi-v7a x86 mips
    
        PREFIX=$(pwd)/dist/${MY_LIBS_NAME}/${ANDROID_ARCH_ABI}/
    
        HOST=$3
        SYSROOT=${NDK_PATH}/platforms/android-${ANDROID_VERSION}/arch-${ARCH}
    
        CFALGS=$5
    
    
        TOOLCHAIN=${NDK_PATH}/toolchains/${HOST}-${TOOLCHAIN_VERSION}/prebuilt/${BUILD_PLATFORM}
        CROSS_PREFIX=${TOOLCHAIN}/bin/$4-
    
        # build 中间件
        BUILD_DIR=./${MY_BUILD_DIR}/${ANDROID_ARCH_ABI}
    
        echo "pwd==$(pwd)"
        echo "ARCH==${ARCH}"
        echo "PREFIX==${PREFIX}"
        echo "HOST==${HOST}"
        echo "SYSROOT=${SYSROOT}"
        echo "CFALGS=$5"
        echo "CFALGS=${CFALGS}"
        echo "TOOLCHAIN==${TOOLCHAIN}"
        echo "CROSS_PREFIX=${CROSS_PREFIX}"
    
        #echo "-------------------------按任意键继续---------------------"
        #read -n 1
        #echo "-------------------------继续执行-------------------------"
    
        # mkdir -p ${BUILD_DIR}   #创建当前arch_abi的编译目录,比如:binary/armeabi-v7a
        # cd ${BUILD_DIR}         #此处 进了当前arch_abi的2级编译目录
    
    
        bash ./configure \
            --prefix=${PREFIX} \
            --target-os=android \
            --arch=${ARCH} \
            --sysroot=$SYSROOT \
            --enable-cross-compile \
            --cross-prefix=${CROSS_PREFIX} \
            --extra-cflags="$CFALGS -Os -fPIC -DANDROID -Wfatal-errors -Wno-deprecated" \
            --extra-cxxflags="-D__thumb__ -fexceptions -frtti" \
            --extra-ldflags="-L${SYSROOT}/usr/lib" \
            --enable-shared \
            --enable-runtime-cpudetect \
            --enable-gpl \
            --enable-small \
            --enable-cross-compile \
            --disable-debug \
            --disable-static \
            --disable-doc \
            --disable-ffmpeg \
            --disable-ffplay \
            --disable-ffprobe \
            --disable-postproc \
            --disable-avdevice \
            --disable-symver \
            --disable-stripping
    
    
        make clean
        make
        make install
    
        #从当前arch_abi编译目录跳出,对应上面的cd ${BUILD_DIR},以便function多次执行
            cd ../../
    
        echo "-------------------$2 build end-------------------------"
    }
    
    
    # build armeabi
    # build_bin arm armeabi arm-linux-androideabi arm-linux-androideabi "$ANDROID_ARMV5_CFLAGS"
    
    #build armeabi-v7a
    # build_bin arm armeabi-v7a arm-linux-androideabi arm-linux-androideabi "$ANDROID_ARMV7_CFLAGS"
    
    #build arm64-v8a
    build_bin arm64 arm64-v8a aarch64-linux-android aarch64-linux-android "$ANDROID_ARMV8_CFLAGS"
    
    #build x86
    #build_bin x86 x86 x86 i686-linux-android "$ANDROID_X86_CFLAGS"
    
    #build x86_64
    # build_bin x86_64 x86_64 x86_64 x86_64-linux-android "$ANDROID_X86_64_CFLAGS"
    
  8. 编译FFMPEG:
  9. 打开终端:进入到ffmpeg对应目录输入:chmod 777 -R ndk
  10. 再输入:./build_android.sh  等待一段时间看到ffmpeg目录下有dist/ffmpeg-3.4.7/arm64-v8a 就说明编译成功了,如果没有就仔细查看命令行中到错误信息

错误记录:

  1. 提示:无法打开xxx ,因为无法验证开发者的问题  输入:
    sudo spctl --master-disable

     

 类似资料: