Ubuntu下为AndroidStudio编译并使用FFmpeg(一)ndk部分
1.1 下载ndk,并编译,做示例程序测试
1、下载Android NDK自解压包,
官方地址:
https://developer.android.com/ndk/downloads/index.html#download
下载:
$ wget -c
2、解压,
将Android NDK压缩包解压到你想要的目录下,如
/opt/android/ndk/
ndk$ chmod a+x android-ndk-r10e-linux-x86_64.bin
ndk$ ./android-ndk-r10c-linux-x86_64.bin
解压后目录结构为:
/opt/android/ndk/android-ndk-r10e
3、配置PATH路径:
命令:
$ sudo vim /etc/profile,在文件末尾加入如下内容:
#set NDK env
export NDK_HOME=/opt/android/ndk/android-ndk-r10e
export PATH=$NDK_HOME:$PATH
【保存并退出】
$ source /etc/profile
至此 android NDK 安装配置完毕。
4、编译sample工程:
执行命令
$ cd /opt/android/ndk/android-ndk-r10e/samples/hello-jni
进入示例项目根目录,然后执行如下命令:
$ ndk-build
您将看到系统会编译出libhello-jni.so
至此示例项目编译完成,可将hello-jni项目导入ADT进行进一步的测试。
1.2 使用ndk编译ffmpeg
1、进入ffmpeg官网, 下载最新版的ffmpgeg压缩包。
我下的是 ffmpeg-2.8.1
进入ffmpeg目录:
$ cd /opt/android/ndk/android-ndk-r10e/sources/ffmpeg-2.8.1
2、 修改 configure 文件
将该文件中的如下四行,目的是去掉默认生成的库名字libavcodec.so.56最后那个”56″的版本号:
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)'
3、在源码目录下创建,ndk_build_config.sh
#!/bin/bash
NDK=/opt/android/ndk/android-ndk-r10e
SYSROOT=$NDK/platforms/android-9/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64
function build_one
{
./configure \
--prefix=$PREFIX \
--enable-shared \
--disable-static \
--disable-doc \
--disable-ffserver \
--enable-gpl\
--enable-cross-compile \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--target-os=linux \
--arch=arm \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
}
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
build_one
4. 编译运行
$ chmod a+x ndk_build_config.sh
$ ./ndk_build_config.sh
$ make
$ make install
5. 检查编译结果
ffmpeg-2.8.1下面会多出一个android目录,里面就是我们想要的编译好的库。
$ ls -R android/
android/:
arm
android/arm:
bin include lib share
android/arm/bin:
ffmpeg ffprobe
android/arm/include:
libavcodec libavdevice libavfilter libavformat libavutil libpostproc libswresample libswscale
android/arm/include/libavcodec:
avcodec.h d3d11va.h dxva2.h qsv.h vda.h version.h vorbis_parser.h
avfft.h dv_profile.h old_codec_ids.h vaapi.h vdpau.h videotoolbox.h xvmc.h
android/arm/include/libavdevice:
avdevice.h version.h
android/arm/include/libavfilter:
asrc_abuffer.h avcodec.h avfiltergraph.h avfilter.h buffersink.h buffersrc.h version.h
android/arm/include/libavformat:
avformat.h avio.h version.h
android/arm/include/libavutil:
adler32.h avutil.h channel_layout.h eval.h intfloat.h mem.h pixfmt.h stereo3d.h xtea.h
aes.h base64.h common.h ffversion.h intreadwrite.h motion_vector.h random_seed.h tea.h
attributes.h blowfish.h cpu.h fifo.h lfg.h murmur3.h rational.h threadmessage.h
audioconvert.h bprint.h crc.h file.h log.h old_pix_fmts.h replaygain.h timecode.h
audio_fifo.h bswap.h dict.h frame.h lzo.h opt.h ripemd.h time.h
avassert.h buffer.h display.h hash.h macros.h parseutils.h samplefmt.h timestamp.h
avconfig.h camellia.h downmix_info.h hmac.h mathematics.h pixdesc.h sha512.h twofish.h
avstring.h cast5.h error.h imgutils.h md5.h pixelutils.h sha.h version.h
android/arm/include/libpostproc:
postprocess.h version.h
android/arm/include/libswresample:
swresample.h version.h
android/arm/include/libswscale:
swscale.h version.h
android/arm/lib:
libavcodec-56.so libavdevice.so.56.4.100 libavformat.so libpostproc-53.so libswresample.so.1.2.101
libavcodec.so libavfilter-5.so libavformat.so.56.40.101 libpostproc.so libswscale-3.so
libavcodec.so.56.60.100 libavfilter.so libavutil-54.so libpostproc.so.53.3.100 libswscale.so
libavdevice-56.so libavfilter.so.5.40.101 libavutil.so libswresample-1.so libswscale.so.3.1.101
libavdevice.so libavformat-56.so libavutil.so.54.31.100 libswresample.so pkgconfig
android/arm/lib/pkgconfig:
libavcodec.pc libavdevice.pc libavfilter.pc libavformat.pc libavutil.pc libpostproc.pc libswresample.pc libswscale.pc
android/arm/share:
ffmpeg
android/arm/share/ffmpeg:
examples ffprobe.xsd libvpx-1080p50_60.ffpreset libvpx-1080p.ffpreset libvpx-360p.ffpreset libvpx-720p50_60.ffpreset libvpx-720p.ffpreset
android/arm/share/ffmpeg/examples:
avio_dir_cmd.c demuxing_decoding.c filtering_audio.c Makefile qsvdec.c resampling_audio.c transcoding.c
avio_reading.c extract_mvs.c filtering_video.c metadata.c README scaling_video.c
decoding_encoding.c filter_audio.c http_multiclient.c muxing.c remuxing.c transcode_aac.c
其中,
libavcodec.so、
libavfilter.so、
libavformat.so、
libavutil.so、
libswresample.so、
libswscale.so
都是软链,没有用,可以删掉。
6. 给ffmpeg库编写Android.mk, 使其可用
创建/ffmpeg-2.8.1/android/arm/Android.mk文件,内容如下:
要注意其中.so前面的数字应该改成你的ffmpeg版本编译出来的数字。
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= libavcodec
LOCAL_SRC_FILES:= lib/libavcodec-56.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libavformat
LOCAL_SRC_FILES:= lib/libavformat-56.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libswscale
LOCAL_SRC_FILES:= lib/libswscale-3.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libavutil
LOCAL_SRC_FILES:= lib/libavutil-54.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libavfilter
LOCAL_SRC_FILES:= lib/libavfilter-5.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= libswresample
LOCAL_SRC_FILES:= lib/libswresample-1.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
【保存并退出】
至此ndk配置完毕,后面是配置Android Studio的部分。
阅读(3307) | 评论(0) | 转发(0) |