anroid ndk編譯ffmpeg 引用librtmp libx264

     Ffmpeg 無處不在,天然android系統少不了它,折騰了很多時間完成 ndk編譯ffmpeg,生成so庫中引用了外部庫librtmp,libx264。條條大路通羅馬,linux

也許還有別的更好的方法去完成它,我沒有仔細研究。我使用的方法徹底能夠達到個人預期目的。android

一.選擇編譯環境shell

   能夠選擇windows下cygwin,也能夠選擇linux系統,我建議選擇後者。花了不少時間在cygwin下編譯,configure時老是提示連接須要的so文件找不到。無奈ubuntu

之下轉到linux環境下編譯。我用的版本是ubuntu16.04。編譯android版本的ffmpeg注意注意的地方有三個:一是configure時帶入的參數,二是放置將要用windows

到的第三方庫(so),最後一個是修改configure文件。下面聽我一一道來。async

一. 建立一個shell腳本文件build_ffmpeg.sh用來引入configure參數,這個文件的格式是unix,若是文件是dos格式,能夠用uedit轉換格式。tcp

     建立的shell腳本內容以下:ide

  

#!/bin/sh
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
NDK=/home/wgg/wgg/android-ndk-r11c
SYSROOT=$NDK/platforms/android-21/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
./configure \
    --prefix=$PREFIX \
    --target-os=linux \
    --enable-cross-compile \
    --enable-runtime-cpudetect \
    --disable-asm \
    --arch=arm \
    --enable-nonfree \
    --enable-shared \
    --enable-version3 \
    --enable-gpl \
    --disable-programs \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffprobe \
    --disable-ffserver \
    --disable-doc \
    --enable-avresample \
    --disable-everything \
    --enable-decoder=pcm_alaw \
    --enable-decoder=pcm_mulaw \
    --enable-decoder=mpeg4 \
    --enable-decoder=h264 \
    --enable-encoder=aac \
    --enable-librtmp \
    --enable-libx264 \
    --enable-parser=aac \
    --enable-parser=h264 \
    --enable-parser=mpeg4video \
    --enable-parser=mpegvideo \
    --enable-parser=mpegaudio \
    --enable-encoder=libx264 \
    --enable-muxer=mpegts \
    --enable-muxer=flv \
    --enable-muxer=h264 \
    --enable-muxer=flv \
    --enable-muxer=mp4 \
    --enable-muxer=data \
    --enable-muxer=avi \
    --enable-muxer=mpeg2video \
    --enable-muxer=pcm_alaw \
    --enable-muxer=pcm_mulaw \
    --enable-demuxer=flv \
    --enable-demuxer=mpegts \
    --enable-demuxer=rtsp \
    --enable-protocol=httpproxy \
    --enable-protocol=crypto \
    --enable-protocol=ftp\
    --enable-protocol=unix\
    --enable-protocol=pipe \
    --enable-protocol=data \
    --enable-protocol=file \
    --enable-protocol=tcp \
    --enable-protocol=http \
    --enable-protocol=udp \
    --enable-protocol=udplite \
    --enable-protocol=rtp \
    --enable-protocol=librtmp \
    --enable-protocol=librtmpe \
    --enable-protocol=librtmps \
    --enable-protocol=librtmpt \
    --enable-protocol=librtmpte \
    --enable-protocol=async \
    --enable-protocol=md5 \
    --enable-protocol=cache \
    --enable-protocol=mmst \
    --enable-protocol=mmsh \
    --sysroot=$SYSROOT \
    --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
    --enable-hwaccels \
    --enable-zlib \
    --disable-devices \
    --disable-avdevice \
    --extra-cflags="-Os -fpic $ADDI_CFLAGS" \
    --extra-cflags="-I/home/wgg/wgg/ffmpeg/extern/include" \
       --extra-ldflags="-L/home/wgg/wgg/ffmpeg/extern/lib"

 NDK指定NDK包路徑,SYSROOT是路徑前綴,編譯是編譯器會去系統路徑下(例如/usr/lib,/lib),定義了SYSROOT之後編譯原系統路徑前加上$SYSROOT,表達式ui

中android-21要根據實際android從新設定。configure 選項能夠根據須要自行刪減。--extra-cflags="-I/home/wgg/wgg/ffmpeg/extern/include" 指示外部庫的頭文件絕對路徑,--extra-ldflags="-L/home/wgg/wgg/ffmpeg/extern/lib" 指示外部so庫路徑。idea

二. 放置外部庫及頭文件

     將librtmp.so  libx264 libcrypto.so libssl.so 後兩個庫是第一個庫的依賴庫放在/home/wgg/wgg/ffmpeg/extern/lib下面。將x264.h, x264_config.h以及rtmp文件夾放在/home/wgg/wgg/ffmpeg/extern/include目錄下,rtmp文件夾中文件以下。

三.修改配置文件

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)'
enabled librtmp  && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket

 修改爲

enabled librtmp && require librtmp librtmp/rtmp.h RTMP_Socket -lrtmp

  將

enabled libx264           && { use_pkg_config x264 "stdint.h x264.h" x264_encoder_encode ||
                               { require libx264 x264.h x264_encoder_encode -lx264 &&
                                 warn "using libx264 without pkg-config"; } } &&
                             { check_cpp_condition x264.h "X264_BUILD >= 118" ||
                               die "ERROR: libx264 must be installed and version must be >= 0.118."; } &&
                             { check_cpp_condition x264.h "X264_MPEG2" &&
                               enable libx262; }

  

替換爲

enabled libx264  && require libx264 x264.h x264_encoder_encode -lx264

  修改build_ffmpeg.sh權限 chmod +x build_ffmpeg.sh

    OK,全部的準備工做都作好了。首先./build_ffmpeg.sh,接着make 最後 make install.

    注意:我這裏用的ffmepg版本是3.0.2,librtmp,libx264基本不用改動,直接引用so不用再去編譯了。獲取外部引用庫請加入QQ羣

     流媒體/Ffmpeg/音視頻 127903734。

相關文章
相關標籤/搜索