FFMpeg在Android中的应用(一)

一、准备工作(基于Mac电脑开发)

1、下载ffmpeg3.4.6版本,http://ffmpeg.org/releases/ffmpeg-3.4.6.tar.gz,解压到/Users/xxx/Downloads/FFMpeg
2、下载NDK,版本r15c,解压放入/Users/xxx/Downloads/NDK/android-ndk-r15c
注意:版本最好一致,不然可能很多坑,高手随意,可自行填坑

二、编译ffmpeg,得到android上能使用的动态库so文件

1、打开终端,进入ffmpeg根目录
2、终端输入:vim configure,然后直接输入:?build setting,可以直接找到需要修改的地方,默认ffmpeg编译出来的库文件是给linux使用的,
我们需要修改为android能使用的so后缀名,点击键盘上的i键切换为输入模式,改好后,输入:wq,回车保存
替换前

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

3、编写脚本文件,可以自己新建一个文件,也可以使用命令行,终端进入ffmpeg根目录,输入
touch build_android_armv7.sh
然后
vim build_android_armv7.sh打开,
把下面的内容复制进去,保存,然后给可执行权限:
chmod 777 build_android_armv7.sh
最后执行./build_android_armv7.sh
回车等待最后编译完成
完成后会在ffmpeg根目录下生成android/armv7-a文件夹,include和lib就是我们所需要的

#!/bin/bash
rm -rf android/armv7-a
NDK=/Users/xxx/Downloads/NDK/android-ndk-r15c
NDK_VERSION=android-14
ARCH=arm
SYSROOT=$NDK/platforms/$NDK_VERSION/arch-$ARCH
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
CROSS_COMPILE=$TOOLCHAIN/bin/arm-linux-androideabi-
CPU=armv7-a
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-march=$CPU -mfloat-abi=softfp -mfpu=neon"
function build_one
{
./configure \
--prefix=$PREFIX \
--enable-shared \
--disable-static \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-symver \
--cross-prefix=$CROSS_COMPILE \
--target-os=android \
--arch=$ARCH \
--cpu=armv7-a \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make clean
make -j8
make install
}
build_one

4、Android中的使用
1)打开AS,新建项目,把创建C++选项勾上,然后next到完成
2)把自动生成的main下面的cpp目录删除,新建jni目录(也可不删,我比较喜欢使用jni目录),同时新建jniLibs目录
3)把上面生成的include整个复制到jni目录下,把lib目录下的so文件复制到jniLibs/armeabi-v7a下
目录结构如下图



4)CMakeLists.txt文件的编写

#CMake最低版本要求
cmake_minimum_required(VERSION 3.4.1)

add_library( # Sets the name of the library.
        ffmpegutil

        #共享库
        SHARED

        # Provides a relative path to your source file(s).
        src/main/jni/ffmpegutil.c)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

#引入ffmepg的头文件
include_directories(${CMAKE_SOURCE_DIR}/src/main/jni/include)

add_library(avcodec
        SHARED #这个表明是动态库
        IMPORTED)
set_target_properties( avcodec
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavcodec.so)

add_library(avdevice SHARED IMPORTED)
set_target_properties( avdevice
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavdevice.so)

add_library(avformat SHARED IMPORTED)
set_target_properties( avformat
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavformat.so)

add_library(avfilter SHARED IMPORTED)
set_target_properties( avfilter
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavfilter.so )

add_library(avutil SHARED IMPORTED)
set_target_properties( avutil
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libavutil.so )

add_library(swresample SHARED IMPORTED)
set_target_properties( swresample
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libswresample.so )

add_library(swscale SHARED IMPORTED)
set_target_properties( swscale
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a/libswscale.so )
#


# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

#链接
target_link_libraries( # Specifies the target library.
        ffmpegutil  #这里的名字要与add_library里面的名称一致
        avcodec
        avdevice
        avformat
        avfilter
        avutil
        swresample
        swscale
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        )

5、编写代码进行测试
1)新建一个FFMpegUtil类

public class FFMpegUtil {
    static {
        System.loadLibrary("avcodec");
        System.loadLibrary("avdevice");
        System.loadLibrary("avfilter");
        System.loadLibrary("avformat");
        System.loadLibrary("avutil");
        System.loadLibrary("swresample");
        System.loadLibrary("swscale");
        System.loadLibrary("ffmpegutil");
    }
  //获取视频文件的角度
    public static native int getRotation(String path);
}

2)这时候会看到方法getRotation为红色,鼠标点击该方法,按下快捷键alt+enter,AS会帮我们新建一个c文件,改名字为ffmpegutil.c,里面的内容如下:

JNIEXPORT jint JNICALL
Java_com_dh_ffmpegproject_utils_FFMpegUtil_getRotation(JNIEnv *env, jclass type, jstring path_) {
    const char *path = (*env)->GetStringUTFChars(env, path_, 0);

    // TODO

    (*env)->ReleaseStringUTFChars(env, path_, path);
}

3)然后我们加入获取视频角度的代码

JNIEXPORT jint JNICALL
Java_com_dh_ffmpegproject_utils_FFMpegUtil_getRotation(JNIEnv *env, jclass type, jstring path_) {
//    const char *path = (*env)->GetStringUTFChars(env, path_, 0);

//    avformat_network_init();
//    注册协议,确保所有的格式和编解码器都被注册到FFMpeg框架中
    av_register_all();
    int angle = -1;
//    jni-->c
    const char *filePath = (*env)->GetStringUTFChars(env, path_, NULL);
    //封装格式上下文
    AVFormatContext *avFormatContext = avformat_alloc_context();
    //打开
    if (avformat_open_input(&avFormatContext, filePath, NULL, NULL) != 0) {
        LOGE("%s", "无法打开视频文件");
        return -1;
    }
    //读取
    if (avformat_find_stream_info(avFormatContext, NULL) < 0) {
        LOGE("%s", "无法获取视频信息");
        return -1;
    }
    //寻找视频流
    //获取视频流的索引位置
    int v_stream_idx = -1;
    int a_stream_idx = -1;
    for (int i = 0; i < avFormatContext->nb_streams; ++i) {
        AVStream *stream = avFormatContext->streams[i];
        if (AVMEDIA_TYPE_VIDEO == stream->codecpar->codec_type) {
            //视频流
            v_stream_idx = i;
        } else if (AVMEDIA_TYPE_AUDIO == stream->codecpar->codec_type) {
            //音频流
            a_stream_idx = i;
        }
    }
    if (v_stream_idx == -1) {
        LOGE("%s", "没有找到视频流");
        return -1;
    }
    //获取视频的元数据
    const AVDictionaryEntry *tag = NULL;
    tag = av_dict_get(avFormatContext->streams[v_stream_idx]->metadata, "rotate", tag, 0);
    if (tag != NULL) {
        angle = atoi(tag->value);
    }
    return angle;
}

4)在MainActivity中调用

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        object : Thread() {
            override fun run() {
                val path = Environment.getExternalStorageDirectory().absolutePath + File.separator + "test.mp4"
                Log.d("dh", "path:$path")
                val rotation = FFMpegUtil.getRotation(path)
                Log.d("dh", "ratation:$rotation")
            }
        }.start()
    }
}

5)修改app目录下的build.gradle文件

android {
    defaultConfig {
        ...
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
            ndk {
                abiFilters "armeabi-v7a"//, "x86"//, "arm64-v8a"
            }
        }
    }
}

6)运行项目查看结果

你可能感兴趣的:(FFMpeg在Android中的应用(一))