Ubuntu环境编译FFmpeg3.2.2-for-android

前言

最近一直在弄FFmpeg如何在Android上集成开发使用。那么问题来了,作为一个Android应用程序员来说,想使用C算法库的某些算法就一脸懵逼了,因为我们只能调用别人编译好的xxx.so类库才能在应用层使用的如鱼得水。为此,本博客教大家如何编译FFmpeg,以实现得到在Android的调用所需的xx.so库文件(纯粹学习,大牛请慢走不送)。

准备环境

1、Ubuntu14.4的编译环境(可在windows下安装虚拟机VM)

2、android-ndk-r9d-linux-x86_64.tar.bz2

3、FFmpeg-3.2.2.tar.bz2

对于android-ndk-r9d-linux-x86_64.tar.bz2可在https://dl.google.com/android/ndk/android-ndk-r9d-linux-x86_64.tar.bz2自行下载

对于FFmpeg-3.2.2.tar.bz2可在http://ffmpeg.org/download.html#releases自行下载

步骤如下

1、首先把所需的安装包复制到ubuntu环境中并解压

ljw@ubuntu:~/ndk-lib/workspace/ffmpeg-demo$ ls
android-ndk-r9d-linux-x86_64.tar.bz2  ffmpeg-3.2.2.tar.bz2
ljw@ubuntu:~/ndk-lib/workspace/ffmpeg-demo$ pwd
/home/ljw/ndk-lib/workspace/ffmpeg-demo
ljw@ubuntu:~/ndk-lib/workspace/ffmpeg-demo$ tar jxvf android-ndk-r9d-linux-x86_64.tar.bz2 
#......编译信息省略
ljw@ubuntu:~/ndk-lib/workspace/ffmpeg-demo$ tar jxvf ffmpeg-3.2.2.tar.bz2 
#......省略
#结果如下
ljw@ubuntu:~/ndk-lib/workspace/ffmpeg-demo$ ls
android-ndk-r9d                       ffmpeg-3.2.2
android-ndk-r9d-linux-x86_64.tar.bz2  ffmpeg-3.2.2.tar.bz2
ljw@ubuntu:~/ndk-lib/workspace/ffmpeg-demo$ 

2、对文件重命名

将android-ndk-r9d重命名为ndk-r9d、ffmpeg-3.2.2重命名为ffmpeg(纯粹是为了方便编译!-_-||强迫症)
ljw@ubuntu:~/ndk-lib/workspace/ffmpeg-demo$ mv android-ndk-r9d ndk-r9d
ljw@ubuntu:~/ndk-lib/workspace/ffmpeg-demo$ mv ffmpeg-3.2.2 ffmpeg
ljw@ubuntu:~/ndk-lib/workspace/ffmpeg-demo$ ls
android-ndk-r9d-linux-x86_64.tar.bz2  ffmpeg  ffmpeg-3.2.2.tar.bz2  ndk-r9d

3、为了得到android所需xx.so类库,需要对ffmpeg目录下的configure进行一点改动

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

4、在ffmpeg目录下执行./configure命令

如出现以下问题,则执行./configure –disable-yasm命令
yasm/nasm not found or too old. Use --disable-yasm for a crippled build.

If you think configure made a mistake, make sure you are using the latest
version from Git.  If the latest version fails, report the problem to the
[email protected] mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "config.log" produced by configure as this will help
solve the problem.

5、在ffmpeg的同级目录建立simplefflib文件夹

6、在ffmpeg的统计目录建立build-ffmpeg-arm.sh脚步文件

#!/bin/bash  

cd /home/ljw/ndk-lib/workspace/ffmpeg-demo/ffmpeg

make clean

export NDK=/home/ljw/ndk-lib/workspace/ffmpeg-demo/ndk-r9d
export PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.8/prebuilt
export PLATFORM=$NDK/platforms/android-8/arch-arm
export PREFIX=/home/ljw/ndk-lib/workspace/ffmpeg-demo/simplefflib
build_one(){
./configure --target-os=linux --prefix=$PREFIX \
 --enable-cross-compile \
 --enable-runtime-cpudetect \
 --disable-asm \
 --arch=arm \
 --cc=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi-gcc \
 --cross-prefix=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi- \
 --disable-stripping \
 --nm=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi-nm \
 --sysroot=$PLATFORM \
 --enable-gpl --enable-shared --disable-static --enable-small \
 --disable-ffprobe --disable-ffplay --disable-ffmpeg --disable-ffserver --disable-debug \
 --extra-cflags="-fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -marm -march=armv7-a"
}

build_one

make
make install

cd ..

7、读者可修改相应的信息:NDK、PREBUILT、PLATFORM、PREFIX。这几处信息。

勿忘记为脚步文件添加权限:chmod 777 build-ffmpeg-arm.sh

8、执行脚步文件:./build-ffmpeg.arm.sh

9、编译成功之后,会在simplefflib中生成相应的include、lib文件(这就是我们想要的)。

10、在Android中集成

鉴于公司加密策略,以至于无法上传截图,只能手写,见谅
  • 1、在Android工程中新建jni目录
  • 2、在jni中新建FFmpeg-arm文件夹、Android.mk、Application.mk、xx.c(随便命名一个.c文件)
  • 3、在FFmpeg-arm文件夹中放入上面的include文件夹
  • 4、在FFmpeg-arm文件夹中一次放入libavcodec-57.so、libavdevice-57.so、libavfilter-6.so、libavformat-57.so、libavutil-55.so、libpostproc-54.so、libswresample-2.so、libswscale-4.so
  • 5、在Android.mk中写入
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)

# FFmpeg library  
include $(CLEAR_VARS)  
LOCAL_MODULE := avcodec  
LOCAL_SRC_FILES := FFmpeg-arm/libavcodec-57.so  
include $(PREBUILT_SHARED_LIBRARY)  

include $(CLEAR_VARS)  
LOCAL_MODULE := avdevice  
LOCAL_SRC_FILES := FFmpeg-arm/libavdevice-57.so  
include $(PREBUILT_SHARED_LIBRARY)  

include $(CLEAR_VARS)  
LOCAL_MODULE := avfilter  
LOCAL_SRC_FILES := FFmpeg-arm/libavfilter-6.so  
include $(PREBUILT_SHARED_LIBRARY)  

include $(CLEAR_VARS)  
LOCAL_MODULE := avformat  
LOCAL_SRC_FILES := FFmpeg-arm/libavformat-57.so  
include $(PREBUILT_SHARED_LIBRARY)  

include $(CLEAR_VARS)  
LOCAL_MODULE := avutil  
LOCAL_SRC_FILES := FFmpeg-arm/libavutil-55.so  
include $(PREBUILT_SHARED_LIBRARY)  

include $(CLEAR_VARS)  
LOCAL_MODULE := postproc  
LOCAL_SRC_FILES := FFmpeg-arm/libpostproc-54.so  
include $(PREBUILT_SHARED_LIBRARY)  

include $(CLEAR_VARS)  
LOCAL_MODULE := swresample  
LOCAL_SRC_FILES := FFmpeg-arm/libswresample-2.so  
include $(PREBUILT_SHARED_LIBRARY)  

include $(CLEAR_VARS)  
LOCAL_MODULE := swscale  
LOCAL_SRC_FILES := FFmpeg-arm/libswscale-4.so  
include $(PREBUILT_SHARED_LIBRARY)

# Program
include $(CLEAR_VARS)

LOCAL_MODULE    := xx
LOCAL_SRC_FILES := xx.c
LOCAL_LDLIBS += -llog
LOCAL_C_INCLUDES += $(LOCAL_PATH)/FFmpeg-arm/include
LOCAL_SHARED_LIBRARIES := avcodec avdevice avfilter avformat avutil postproc swresample swscale  

include $(BUILD_SHARED_LIBRARY)
  • 6、在Application.mk中写入
若要适配x86,则需要改写相应的脚步文件
APP_ABI := armeabi

结尾

到此为止,我们已经得到了可在Android调用的FFmpeg的库文件了,我在研究编译的时候花费了几天的时间,分析了很多博客的操作方法,为此总结了一下,希望能帮助到一群和我一样的菜鸟,本文的分析流程借鉴了雷大神的博客http://blog.csdn.net/leixiaohua1020/article/details/47008825/并将其中的小部分流程(主要是编译步骤)细化。学习之路,你我共勉!

你可能感兴趣的:(算法库)