Cross-compilation-Mac environment uses NDK to compile FFmpeg

overview

FFmpeg is a set of very powerful audio and video processing tools. It is definitely a veteran in the field of audio and video. Around FFmpeh, audio and video codec, cropping, splicing and other operations can be performed. Today's topic is to use NDK to teach cross-compilation and generate so files for use on Android

My compilation environment:

  • FFmpeg v3.0.11 (previously tested the latest version 3.3.4 and failed to compile)
  • macOS
  • NDK android-ndk-r14b
  • Android Studio 3.1

Download FFmpeg source code

FFmpeg official website download: www.ffmpeg.org/download.ht…

You can also download git clone  git.ffmpeg.org/ffmpeg.git  ffmpeg with Git

layout script

The version number of the so library compiled by default is after .so, and Android cannot recognize it, so the configure file needs to be modified

Use sublime to open configure, about 3305 lines

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)'

Change the above lines directly to

SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)$(LIBMAJOR)$(SLIBSUF)' 
LIB_INSTALL_EXTRA_CMD='?(RANLIB)"$(LIBDIR)/$(LIBNAME)"'  
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'  
SLIB_INSTALL_LINKS='$(SLIBNAME)'

Configure build_android.sh

This file needs to be manually created in the FFmpeg root directory, and a new .sh file is created directly in sublime. There are many scripts on the Internet, and most of them can be used directly, but pay attention to modifying the NDK directory. Below I provide a modified script for reference

# ndk环境   
export NDK=/Users/CH/Learn/android-ndk-r14b
export SYSROOT=$NDK/platforms/android-14/arch-arm
export TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
CPU=armv7-a

# 要保存动态库的目录,这里保存在源码根目录下的android/armv7-a
export PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
function build_android
{
./configure 
	--target-os=linux 
	--prefix=$PREFIX \
    --enable-cross-compile \
    --enable-shared \
    --disable-static \
    --disable-doc \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-ffserver \
    --enable-avdevice \
    --disable-doc \
    --disable-symver \
    --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
    --arch=arm \
    --sysroot=$SYSROOT \
    --extra-cflags="-Os -fpic $ADDI_CFLAGS" \
    --extra-ldflags="$ADDI_LDFLAGS" \
    $ADDITIONAL_CONFIGURE_FLAG
make clean
# 不确定自己上面的目录或者环境有没有错误时
# 可以先注释一下下面两个命令
# make
# make install
}
build_android

execute script

Enter the root directory of the script. If you are executing it for the first time, you may be prompted to have insufficient permissions, as follows:

Permission denied

Grant permissions to:

chmod 777 build_android.sh

and then re-execute

./build_android.sh

Here, if the script file environment configuration is normal, a WARNING will be prompted, ignore it and continue to execute

make

After about 15 minutes, the execution ends. Under the output directory you configured in the root directory, you can see two folders, the .so file and the header file, as shown below:

hint

Try to execute ./build_android first to confirm that there is no problem with the configuration, then execute make and makeinstall because it takes about ten minutes to compile once. So be sure to confirm the environment first, and execute make if there is no problem with the directory.

question

1. When executing ./build_android.sh, the following error is reported:
nasm/yasm not found or too old. Use --disable-x86asm for a crippled build,
which means that you have not installed the assembly tool yasm and
executed it directly on the terminal
 

brew install yasm

ready to install

2. The error reported in the output directory is roughly as follows

No such file or directory make: Error 127

This means that your output directory cannot be found, try not to build the output directory under the system. Another point is that it is best to manually set the output directory to read and write (although it may already be read and write~)

3. The new version of FFpmeg fails to compile

At present, the latest version 3.4.2 is not working during my compilation process. I am currently using 3.0.11

4. It is best not to use the ndk downloaded by androidstudio

Go to the official website to manually download ndk (I am using android-ndk-r14b )
developer.android.google.cn/ndk/downloa…Summary

You must be patient and patient during the compilation process~ especially for those like me who do not know C or Linux, they are simply looking at all kinds of Google in the sky.

Original  cross-compilation-Mac environment uses NDK to compile FFmpeg - Programmer Sought

★The business card at the end of the article can receive free audio and video development learning materials, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmaps, etc.

see below!

 

Guess you like

Origin blog.csdn.net/yinshipin007/article/details/131437715
Recommended