Mac 下编译FFmpeg4.3.1

需要使用NDK来编译FFmpeg,需要下载NDK和FFmpeg
我使用的NDK版本:android-ndk-r21
FFmpeg版本 FFemeg4.3.1
FFmpeg下载链接:
http://ffmpeg.org/releases/ffmpeg-4.3.1.tar.xz
NDK下载链接:
https://dl.google.com/android/repository/android-ndk-r21-darwin-x86_64.dmg
下载后解压ffmpeg和ndk(略)

配置NDK环境变量:
进入终端
pico 命令打开bash_profile
关于环境变量

pico ./.bash_profile
springtomdeMacBook-Pro:~ springtom$ pico ./.bash_profile

#Setting PATH for Python 2.7
#The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
#Setting PATH for Python 3.6
#The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
#export JAVA_HOME = "/Users/springtom/Library/Java/JavaVirtualMachines/jdk-11.0$
#export RED5_HOME = "/Users/springtom/Documents/red5/red5-server"
export ANDROID_HOME=/Users/springtom/Library/Android/sdk
export NDKROOT=/Users/springtom/Documents/android-ndk-r21
#export PATH = ”${PATH}:${ANDROID_HOME}/platform-tools“
#export PATH = ${PATH}:${ANDROID_HOME}/tools
export PATH=$PATH:$NDKROOT
#export PATH = ${PATH}:${NDKROOT}/toolchains
#export PATH = ${PATH}:${ANDROID_HOME}/build-tools
export PATH=/Users/springtom/Library/Android/sdk:$PATH
export PATH=/Users/springtom/Library/Android/sdk/build-tools:$PATH
export PATH=/Users/springtom/Library/Android/sdk/platform-tools:$PATH
export PATH=/Users/springtom/Library/Android/sdk/tools:$PATH
export PATH=/Users/springtom/Documents/android-ndk-r21:$PATH
export PATH=/Users/springtom/Documents/android-ndk-r21/toolchains:$PATH

control+x保持
选择y(yes)
然后按enter结束编辑

输入 source ~/.bash_profile使改动立即有效。

springtomdeMacBook-Pro:~ springtom$ source ~/.bash_profile
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
': not a valid identifier
': not a valid identifier
: command not found
springtomdeMacBook-Pro:~ springtom$ 

ndk-build -verison 检查环境变量是否生效

: command not found
springtomdeMacBook-Pro:~ springtom$ ndk-build -version
GNU Make 4.2.1
Built for x86_64-apple-darwin
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
springtomdeMacBook-Pro:~ springtom$ 

如果不生效去查找环境变量是否配置正确

在ffmpeg根目录下编写build_android.sh

#!/bin/bash
NDK=/Users/springtom/Documents/android-ndk-r21
ADDI_LDFLAGS="-fPIE -pie"
ADDI_CFLAGS="-fPIE -pie -march=armv7-a -mfloat-abi=softfp -mfpu=neon"
CPU=armeabi-v7a
ARCH=armeabi-v7a
HOST=arm-linux
SYSROOT=$NDK/toolchains/llvm/prebuilt/darwin-x86_64/sysroot
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin
PREFIX=$(pwd)/android/$CPU
#x264=$(pwd)/x264/android/$CPU
#export PATH=$x264/bin:$PATH
#export PATH=$x264/include:$PATH
#export PATH=$x264/lib:$PATH
#export PKG_CONFIG_PATH=$x264/lib/pkgconfig:$PKG_CONFIG_PATH
configure()
{
    ./configure \
    --prefix=$PREFIX \
    --toolchain=clang-usan \
    --enable-cross-compile \
    --target-os=android \
    --arch=$ARCH \
    --sysroot=$SYSROOT \
    --cc=$TOOLCHAIN/armv7a-linux-androideabi21-clang \
    --cxx=$TOOLCHAIN/armv7a-linux-androideabi21-clang++ \
    --strip=$TOOLCHAIN/arm-linux-androideabi-strip \
    --extra-cflags="$ADDI_CFLAGS" \
    --extra-ldflags="$ADDI_LDFLAGS" \
    --disable-encoders \
    --disable-decoders \
    --disable-avdevice \
    --disable-static \
    --disable-doc \
    --disable-ffplay \
    --disable-network \
    --disable-doc \
    --disable-symver \
    --disable-ffprobe \
    --enable-neon \
    --enable-shared \
    #--enable-libx264 \
    --enable-gpl \
    --enable-pic \
    --enable-jni \
    --enable-pthreads \
    --enable-mediacodec \
    --enable-encoder=aac \
    --enable-encoder=gif \
    --enable-encoder=libopenjpeg \
    --enable-encoder=libmp3lame \
    --enable-encoder=libwavpack \
    #--enable-encoder=libx264 \
    --enable-encoder=mpeg4 \
    --enable-encoder=pcm_s16le \
    --enable-encoder=png \
    --enable-encoder=mjpeg \
    --enable-encoder=srt \
    --enable-encoder=subrip \
    --enable-encoder=yuv4 \
    --enable-encoder=text \
    --enable-decoder=aac \
    --enable-decoder=aac_latm \
    --enable-decoder=libopenjpeg \
    --enable-decoder=mp3 \
    --enable-decoder=mpeg4_mediacodec \
    --enable-decoder=pcm_s16le \
    --enable-decoder=flac \
    --enable-decoder=flv \
    --enable-decoder=gif \
    --enable-decoder=png \
    --enable-decoder=srt \
    --enable-decoder=xsub \
    --enable-decoder=yuv4 \
    --enable-decoder=vp8_mediacodec \
    #--enable-decoder=h264_mediacodec \
    --enable-decoder=hevc_mediacodec \
    --enable-bsf=aac_adtstoasc \
    #--enable-bsf=h264_mp4toannexb \
    --enable-bsf=hevc_mp4toannexb \
    --enable-bsf=mpeg4_unpack_bframes

}

build_android()
{
    configure
    make clean
    make -j4
    make install
}

build_android

进入root权限

springtomdeMacBook-Pro:~ springtom$ su
Password:

问题:Sorry!
解决:
在 Linux 下,执行 su 命令后输入密码即可切换到 root 用户执行各类操作
但是 Mac 下,这样行不通,只会返回你一句 Sorry!
解决方法很简单,首先打开终端
依次输入命令:
sudo su
提示输入密码,输入当前用户登录密码后回车
左边变成了 sh-x.x(我这儿是 sh-3.2,可能不一样)
接着输入
passwd root
提示 Changing password for root.
New password: 输入新密码
Retype new password: 再次输入新密码
回车后完成 root 用户的密码修改
接下来再试试 su 命令,输入刚刚设置的密码后,成功……

修改权限:

sh-3.2# cd ffmpeg-4.3.1/
sh-3.2# chmod 777 build_android.sh

执行脚本:

sh-3.2# ./build_android.sh

问题:
1、Yasm版本过低

nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.

原因:
原因是ffmpeg使用了yasm编译指令,需要机器安装yasm插件。

解决:
解决方法非常简单,命令行输入:

brew install yasm

前提是本机已经安装了brew插件,如果没有安装参考命令:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2、权限问题

install: /usr/local/share/ffmpeg/libvpx-1080p.ffpreset: Permission denied
make: *** [install-data] Error 71

解决:
需要进入root权限

3.问题
prefix=/Users/springtom/Documents/ffmpeg/ffmpeg4.3.1/ffmpeg-4.3.1/android/armeabi-v7a: No such file or directory
./build_android.sh: line 115: --cc=/Users/springtom/Documents/android-ndk-r21/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi21-clang: No such file or directory

Enabled outdevs:

License: LGPL version 2.1 or later
config.h is unchanged
config.asm is unchanged
libavutil/avconfig.h is unchanged
libavfilter/filter_list.c is unchanged
libavcodec/codec_list.c is unchanged
libavcodec/parser_list.c is unchanged
libavcodec/bsf_list.c is unchanged
libavformat/demuxer_list.c is unchanged
libavformat/muxer_list.c is unchanged
libavdevice/indev_list.c is unchanged
libavdevice/outdev_list.c is unchanged
libavformat/protocol_list.c is unchanged
ffbuild/config.sh is unchanged
./build_android.sh: line 94: --prefix=/Users/springtom/Documents/ffmpeg/ffmpeg4.3.1/ffmpeg-4.3.1/android/armeabi-v7a: No such file or directory
./build_android.sh: line 96: --enable-shared: command not found
./build_android.sh: line 99: --enable-jni: command not found
./build_android.sh: line 101: --disable-doc: command not found
./build_android.sh: line 103: --disable-programs: command not found
./build_android.sh: line 105: --disable-symver: command not found
./build_android.sh: line 107: --disable-x86asm: command not found
./build_android.sh: line 109: --target-os=linux: command not found
./build_android.sh: line 111: --arch=arm: command not found
./build_android.sh: line 113: --cpu=armv7-a: command not found
./build_android.sh: line 115: --cc=/Users/springtom/Documents/android-ndk-r21/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi21-clang: No such file or directory
./build_android.sh: line 117: --cxx=/Users/springtom/Documents/android-ndk-r21/toolchains/llvm/prebuilt/darwin-x86_64/bin/armv7a-linux-androideabi21-clang++: No such file or directory
./build_android.sh: line 119: --enable-cross-compile: command not found
./build_android.sh: line 121: --sysroot=/Users/springtom/Documents/android-ndk-r21/toolchains/llvm/prebuilt/darwin-x86_64/sysroot: No such file or directory
./build_android.sh: line 123: --extra-cflags=-Os -fpic -march=armv7-a: command not found
./build_android.sh: line 125: --extra-ldflags=: command not found
./build_android.sh: line 127: --disable-asm: command not found

原因:NDK工具链路径不对:
SYSROOT=NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin
darwin-x86_64

armv7a-linux-androideabi21-clang 路径不对

4.权限问题:


image.png

解决:系统偏好设置->安全性与隐私->通用->最底下 勾选允许

最后编辑结果在ffmpeg根目录下android目录下:
/Users/springtom/Documents/ffmpeg/ffmpeg4.3.1/ffmpeg-4.3.1/android/armeabi-v7a/lib

image.png

ffmpge导入android studio

你可能感兴趣的:(Mac 下编译FFmpeg4.3.1)