前言:
编译部分网络上例子很多,这里用静态库的方式记录下编译后ffmpeg的使用
1.创建Natice C++工程
2: app/main/cpp下拷贝编译好的ffmpeg头文件以及库文件
注意目录层级结构,ffmpeg文件夹与CMakeList平级
3.编写CMakeList.txt编译ffmpeg
完整的CMakeList.txt内容,按需修改路径
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("ffmpegandroid")
#设置头文件
set(TFFMPEG_INCLUDE ${CMAKE_SOURCE_DIR}/ffmpeg/include)
INCLUDE_DIRECTORIES(${TFFMPEG_INCLUDE})
#设置库文件,支持armv7a和arm64
set(TFFMPEG_LIB ${CMAKE_SOURCE_DIR}/ffmpeg/lib/${CMAKE_ANDROID_ARCH_ABI})
#链接ffmpeg静态库
add_library(libavcodec STATIC IMPORTED)
set_target_properties(libavcodec PROPERTIES IMPORTED_LOCATION ${TFFMPEG_LIB}/libavcodec.a)
add_library(libavfilter STATIC IMPORTED)
set_target_properties(libavfilter PROPERTIES IMPORTED_LOCATION ${TFFMPEG_LIB}/libavfilter.a)
add_library(libavformat STATIC IMPORTED)
set_target_properties(libavformat PROPERTIES IMPORTED_LOCATION ${TFFMPEG_LIB}/libavformat.a)
add_library(libavutil STATIC IMPORTED)
set_target_properties(libavutil PROPERTIES IMPORTED_LOCATION ${TFFMPEG_LIB}/libavutil.a)
add_library(libswresample STATIC IMPORTED)
set_target_properties(libswresample PROPERTIES IMPORTED_LOCATION ${TFFMPEG_LIB}/libswresample.a)
add_library(libswscale STATIC IMPORTED)
set_target_properties(libswscale PROPERTIES IMPORTED_LOCATION ${TFFMPEG_LIB}/libswscale.a)
# 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.
add_library(# Sets the name of the library.
ffmpegandroid
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp)
# 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.
#链接ffmpeg静态库 按需链接
target_link_libraries(# Specifies the target library.
ffmpegandroid
libavcodec
libavfilter
libavformat
libavutil
libswresample
libswscale
z
# Links the target library to the log library
# included in the NDK.
${log-lib})
4.修改build.gradle文件,设置支持版本
5.写个例子测试一下
注意引入头文件时增加extern "C",调用一下ffmpeg内部方法,会输出内容