FFmpeg4.2.2和RTMP结合编译
程序员文章站
2022-06-06 17:44:25
...
一、编译librtmp
下载librtmp源码,在GitHub上下载:
可以用git下载:
git clone https://github.com/yixia/librtmp.git
编译librtmp脚本:
#!/bin/bash
NDK_ROOT=/root/ndk/android-ndk-r17c
CPU=arm-linux-androideabi
TOOLCHAIN=$NDK_ROOT/toolchains/$CPU-4.9/prebuilt/linux-x86_64
export XCFLAGS="-isysroot $NDK_ROOT/sysroot -isystem $NDK_ROOT/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=17"
export XLDFLAGS="--sysroot=${NDK_ROOT}/platforms/android-17/arch-arm "
export CROSS_COMPILE=$TOOLCHAIN/bin/arm-linux-androideabi-
make install SYS=android prefix=`pwd`/result CRYPTO= SHARED= XDEF=-DNO_SSL
二、修改FFmpeg Configure:
最新版本的ffmpeg默认使用clang编译,可以修改configure文件来关闭:
注释4210-4213行,关闭掉:
4209
4210 #set_default target_os
4211 #if test "$target_os" = android; then
4212 # cc_default="clang"
4213 #fi
4214
由于ffmpeg 默认开启librtmp需要pkgconfig,这里我们手动关闭,修改ffmpeg的configure文件:
注释掉6256行:
6254 enabled libpulse && require_pkg_config libpulse libpulse pulse/pulseaudio.h pa_context_new
6255 enabled librsvg && require_pkg_config librsvg librsvg-2.0 librsvg-2.0/librsvg/rsvg.h rsvg_handle_render_cairo
6256 #enabled librtmp && require_pkg_config librtmp librtmp librtmp/rtmp.h RTMP_Socket
6257 enabled librubberband && require_pkg_config librubberband "rubberband >= 1.8.1" rubberband/rubberband-c.h rubberband_new -lstdc++ && app end librubberband_extralibs "-lstdc++"
三、编译FFmpeg
FFmpeg的编译脚本:
#!/bin/bash
NDK_ROOT=/root/ndk/android-ndk-r17c
CPU=arm-linux-androideabi
TOOLCHAIN=$NDK_ROOT/toolchains/$CPU-4.9/prebuilt/linux-x86_64
ANDROID_API=17
PREFIX=./android/armeabi-v7a/ffmpeg_rtmp
#配置rtmp库的路径
RTMP=/root/rtmp/librtmp/result
./configure \
--prefix=$PREFIX \
--enable-small \
--disable-programs \
--disable-avdevice \
--disable-encoders \
--disable-muxers \
--disable-filters \
--enable-librtmp \
--enable-cross-compile \
--cross-prefix=$TOOLCHAIN/bin/$CPU- \
--disable-shared \
--enable-static \
--sysroot=$NDK_ROOT/platforms/android-$ANDROID_API/arch-arm \
--extra-cflags="-isysroot $NDK_ROOT/sysroot -isystem $NDK_ROOT/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=$ANDROID_API -U_FILE_OFFSET_BITS -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fPIC -I$RTMP/include" \
--extra-ldflags="-L$RTMP/lib" \
--extra-libs="-lrtmp" \
--arch=arm \
--target-os=android
make clean
make install
四、集成到AndroidStudio
CMakeLists.txt:
# 导入FFmpeg的头文件
include_directories(${CMAKE_SOURCE_DIR}/ffmpeg/include)
# 导入FFmpeg的库文件
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/ffmpeg/libs/${CMAKE_ANDROID_ARCH_ABI}")
# 导入Rtmp的库文件
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/rtmp/libs/${CMAKE_ANDROID_ARCH_ABI}")
target_link_libraries(
my-player
${log-lib}
-Wl,--start-group
avcodec avfilter avformat avutil swresample swscale
-Wl,--end-group
z
)
上一篇: PHP笔记:正则表达式验证实例