ffmpeg学习开篇-编译

ffmpeg学习开篇-编译

ffmpeg学习的话不得不提的一个神奇的人物是雷神了,这是雷神的博客
https://me.csdn.net/leixiaohua1020

还有另外一个小码哥的博客,写的ffmpeg文章也很好,也可以关注下,我是按照小码哥发的ffmpeg原创文章时间顺序去学习的,这是他的博客
https://me.csdn.net/king1425

编译环境

我的编译环境是
mac 10.13.6
android studio 3.0
cmake 3.6.1
ndk 15.2

编译脚本

#!/bin/bash
#ndk 环境 
NDK=/Users/tony/Library/Android/sdk/ndk-bundle
SYSROOT=/Users/tony/Library/Android/sdk/ndk-bundle/platforms/android-14/arch-arm
TOOLCHAIN=/Users/tony/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/
# cpu 架构平台,若要编译 x86 则指定 x86
CPU=armv7-a
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
function buildArm7a
{
	./configure \
    --prefix=$PREFIX \
    --enable-shared \
    --disable-static \
    --disable-doc \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-ffserver \
    --disable-avdevice \
    --disable-doc \
    --disable-symver \
    --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
    --target-os=android \
    --arch=arm \
    --enable-cross-compile \
    --sysroot=$SYSROOT \
    --extra-cflags="-Os -fpic $ADDI_CFLAGS" \
    --extra-ldflags="$ADDI_LDFLAGS" \

	make clean
	make
	make install
}
buildArm7a
echo ffmpeg_arm7a finished

大家可以拷贝这块代码,保存成ffmpeg.sh脚本文件,然后运行脚本文件即可,生成产物主要有lib文件夹(里面有相应的so文件)和include文件夹(里面有相应的头文件,用于android studio编译成android使用的库来准备)

导入android studio,android使用

cop之前脚本生成的产物到android studio,如图所示
ffmpeg学习开篇-编译
对应的文件配置
ffmpeg学习开篇-编译
CMakeLists.txt文件的脚本

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

cmake_minimum_required(VERSION 3.4.1)

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 )
message(STATUS "CMAKE_SOURCE_DIR" ${CMAKE_SOURCE_DIR})
set(distribution_dir ${CMAKE_SOURCE_DIR}/../distribution)
set(arm_dir ${CMAKE_SOURCE_DIR}/../distribution)
add_library( avcodec
             SHARED
             IMPORTED )
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${distribution_dir}/armeabi-v7a/libavcodec.so)
add_library( avfilter
             SHARED
             IMPORTED)
set_target_properties(avfilter PROPERTIES IMPORTED_LOCATION ${distribution_dir}/armeabi-v7a/libavfilter.so)
add_library( avformat
             SHARED
             IMPORTED)
set_target_properties(avformat PROPERTIES IMPORTED_LOCATION ${distribution_dir}/armeabi-v7a/libavformat.so )
add_library( avutil
             SHARED
             IMPORTED )
set_target_properties(avutil PROPERTIES IMPORTED_LOCATION ${distribution_dir}/armeabi-v7a/libavutil.so )

add_library( swresample
             SHARED
             IMPORTED )
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${distribution_dir}/armeabi-v7a/libswresample.so )
add_library( swscale
             SHARED
             IMPORTED)
set_target_properties( swscale PROPERTIES IMPORTED_LOCATION ${distribution_dir}/armeabi-v7a/libswscale.so )


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

add_library( xp-ffmpeg
             SHARED
             src/main/cpp/xp-ffmpeg.cpp
              )

#include_directories(${distribution_dir}/include)

target_include_directories(xp-ffmpeg PRIVATE ${distribution_dir}/include)

target_link_libraries( xp-ffmpeg avcodec avfilter avformat avutil swresample swscale ${log-lib} )


这样子,你就可以愉快的在android studio上使用ffmpeg的代码了
以上的所有代码我已经提交到我的github上了,大家可以在
https://github.com/tangmingscau/ffmpegstudy
看到相应的代码