相关文章推荐
咆哮的抽屉  ·  electron ...·  2 月前    · 

之所以需要这个扩展是因为做播放器的时候发现exoplayer不支持ac3音频,来来回回走了几趟弯路,终于搞定了,写下来记录一下

一、编译FFMPEG(Linux环境)

编译平台:ubuntu 16.04LTS

ffmpeg版本:release/4.2

ndk版本:android-ndk-r20b

下载ndk,我使用的google推荐的 android-ndk-r20b ,注意下载linux版本,下载后提取到文件夹。此处我的ndk文件夹为:/home/ye/ndk/android-ndk-r20b

clone ffmpeg,切换分支到release/4.2,同样是google推荐的4.2版本

 git clone https://git.videolan.org/git/ffmpeg.git
 git checkout release/4.2

打开ffmpeg所在文件夹,如我的文件夹是/home/ye/Project,新建文件:build_ffmpeg.sh

复制ExoPlayer中build_ffmpeg.sh内容,粘贴到自己创建的build_ffmpeg.sh中,同时修改如下(脚本贴在最下方):

sudo ./build_ffmpeg.sh,生成so库在ffmpeg/android-libs目录下

如果执行命令后,报“C compiler test failed.”之类的,应该是路径配置有问题,多检查一下

二、编译libffmpeg_jni.so(Windows环境)

本来编译出四个库之后我以为结束了的,没想到放到项目里始终不行,日志里发现FfmpegAudioRenderer为空。检查FfmpegLibrary发现缺少了libffmpeg_jin.so,libavresample.so是多余的。

结果找不到文档,又不懂cmake规则,浪费了好多时间

下载android-ndk-r20b,注意这次选Windows了,解压,在AndroidStudio配置好路径

clone ExoPlayer

 git clone https://github.com/google/ExoPlayer.git	

clone ffmpeg,切换分支到release/4.2

 git clone https://git.videolan.org/git/ffmpeg.git
 git checkout release/4.2

复制ffmpeg至ExoPlayer中ffmpeg模块下的jni文件夹

将在liunx编译好的android-libs复制到ffmpeg文件夹内

配置CMakeLists.txt位置,修改build.gradle,增加红框内配置

编译,Gradle->extension-ffmpeg->Tasks->build->assemble

修复错误:'libavutil/avconfig.h' file not found

在ffmpeg/libavutil文件夹下新建头文件avconfig.h,在默认内容下加入以下内容

 /* Generated by ffconf */
 #ifndef AVUTIL_AVCONFIG_H
 #define AVUTIL_AVCONFIG_H
 #define AV_HAVE_BIGENDIAN 0
 #define AV_HAVE_FAST_UNALIGNED 0
 #endif /* AVUTIL_AVCONFIG_H */

重复5,在buildoutput/imtermediates/cmake/release/obj下有各个内核的so库,里面包含了libffmpeg_jni.so

感觉这些操作应该在linux也能搞定吧,但是我自己试了下,要引入很多头文件,而且最后还是报错,最后找到在Windows编译的方法。如果有人知道怎么弄,欢迎补充。

三、在自己的项目中使用

将四个so库(libavcodec.so、libavutil.so、libffmpeg_jni.so、libswresample.so)移动到项目的libs文件夹(记得在build.gradle配置好sourceSets和abiFilters)

复制ExoPlayer下ffmpeg扩展中的java文件夹到对应位置(com.google.android.exoplayer2.ext.ffmpeg)

新建RenderersFactory文件,添加FfmpegAudioRenderer,并开启扩展

使用renderer构建播放器

成功播放!ac3音频终于可以正常播放,且日志中也能看到FfmpegAudioRenderer已添加。

四、编译脚本(build_ffmpeg.sh)

#!/bin/bash
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#      http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 1.配置看自己需求
COMMON_OPTIONS="
    --target-os=android
    --disable-static
    --enable-shared
    --disable-doc
    --disable-programs
    --disable-everything
    --disable-avdevice
    --disable-avformat
    --disable-swscale
    --disable-postproc
    --disable-avfilter
    --disable-symver
    --disable-avresample
    --enable-swresample
    --enable-avresample
    --enable-decoder=vorbis
    --enable-decoder=opus
    --enable-decoder=flac
    --enable-decoder=alac
    --enable-decoder=pcm_mulaw
    --enable-decoder=pcm_alaw
    --enable-decoder=mp3
    --enable-decoder=amrnb
    --enable-decoder=amrwb
    --enable-decoder=aac
    --enable-decoder=ac3
    --enable-decoder=eac3
    --enable-decoder=dca
    --enable-decoder=mlp
    --enable-decoder=truehd
    --extra-ldexeflags=-pie
# 2.NDK路径,记得修改
NDK_PATH="/home/ye/ndk/android-ndk-r20b"
HOST_PLATFORM="linux-x86_64"
TOOLCHAIN_PREFIX="${NDK_PATH}/toolchains/llvm/prebuilt/${HOST_PLATFORM}/bin"
# 3.FFMPEG路径,记得修改
cd "/home/ye/Project/ffmpeg"
./configure \
    --libdir=android-libs/armeabi-v7a \
    --arch=arm \
    --cpu=armv7-a \
    --cross-prefix="${TOOLCHAIN_PREFIX}/armv7a-linux-androideabi16-" \
    --nm="${TOOLCHAIN_PREFIX}/arm-linux-androideabi-nm" \
    --strip="${TOOLCHAIN_PREFIX}/arm-linux-androideabi-strip" \
    --extra-cflags="-march=armv7-a -mfloat-abi=softfp" \
    --extra-ldflags="-Wl,--fix-cortex-a8" \
    ${COMMON_OPTIONS}
make -j4
make install-libs
make clean
./configure \
    --libdir=android-libs/arm64-v8a \
    --arch=aarch64 \
    --cpu=armv8-a \
    --cross-prefix="${TOOLCHAIN_PREFIX}/aarch64-linux-android21-" \
    --nm="${TOOLCHAIN_PREFIX}/aarch64-linux-android-nm" \
    --strip="${TOOLCHAIN_PREFIX}/aarch64-linux-android-strip" \
    ${COMMON_OPTIONS}
make -j4
make install-libs
make clean
./configure \
    --libdir=android-libs/x86 \
    --arch=x86 \
    --cpu=i686 \
    --cross-prefix="${TOOLCHAIN_PREFIX}/i686-linux-android16-" \
    --nm="${TOOLCHAIN_PREFIX}/i686-linux-android-nm" \
    --strip="${TOOLCHAIN_PREFIX}/i686-linux-android-strip" \
    --disable-asm \
    ${COMMON_OPTIONS}
make -j4
make install-libs
make clean
./configure \
    --libdir=android-libs/x86_64 \
    --arch=x86_64 \
    --cpu=x86_64 \
    --cross-prefix="${TOOLCHAIN_PREFIX}/x86_64-linux-android21-" \
    --nm="${TOOLCHAIN_PREFIX}/x86_64-linux-android-nm" \
    --strip="${TOOLCHAIN_PREFIX}/x86_64-linux-android-strip" \
    --disable-asm \
    ${COMMON_OPTIONS}
make -j4
make install-libs
make clean
复制代码
分类:
Android
标签: