闲来无事,想自己弄个播放器玩下,ffmpeg是一定要要的,网上搜索一圈,开始搞起,花了1下午搞定了
一.首先肯定是要安装cygwin + ndk-r6b(r5b r7也可以)并配置好
二.http://ffmpeg.org/download.html 下载ffmpeg源码
我下的是
FFmpeg 0.11.1 "Happiness"
下载地址:http://ffmpeg.org/releases/ffmpeg-0.9.2.tar.gz 解压到个jni文件夹 用于之后的android编译, 我放在E:\myProject\MyFFmpeg\project\jni里,注意:不要ffmpeg-0.11.1这个文件夹了,直接把里面的各个文件放到jni里
三.配置ffmpeg
这步耗得时间最多了。。
首先用ultraedit新建个文件 复制下面的内容 保存为config.sh 放到jni里面 然后系统增加环境变量NDK_ROOT , 我加的 D:/android-ndk-r6b
#!/bin/bash
PREBUILT=${NDK_ROOT}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows
PLATFORM=android-4
ARCH=--arch=arm
./configure \
${ARCH} \
--target-os=linux --enable-cross-compile --cross-prefix=${PREBUILT}/bin/arm-linux-androideabi- \
--extra-cflags=" -fPIC -DANDROID -std=c99 " \
--enable-version3 \
--enable-gpl \
--enable-nonfree \
--disable-stripping \
--disable-ffmpeg\
--disable-ffplay\
--disable-ffserver\
--disable-ffprobe \
--disable-encoders \
--disable-muxers \
--disable-devices \
--disable-protocols \
--enable-protocol=file \
--enable-avfilter \
--disable-network \
--disable-avdevice \
--disable-asm \
--enable-neon \
--enable-armv5te \
--sysinclude=${NDK_ROOT}/platforms/${PLATFORM}/arch-arm/usr/include \
--extra-ldflags="-Wl,-rpath-link=${NDK_ROOT}/platforms/${PLATFORM}/arch-arm/usr/lib
-L${NDK_ROOT}/platforms/${PLATFORM}/arch-arm/usr/lib -nostdlib
${NDK_ROOT}/platforms/${PLATFORM}/arch-arm/usr/lib/crtbegin_static.o
${NDK_ROOT}/platforms/${PLATFORM}/arch-arm/usr/lib/crtend_android.o -lc -lm -ldl "
这里可能出现的问题:
1.提示各种command not found
比如line 6: $'--arch=arm\r': command not found 可以看到arm后面有个'\r' unix文件换行和win下面不一样 需要用ultraedit 文件-转换-dos转unix
2. 提示
D:/android-ndk-r5b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/wind
ows/bin/arm-linux-androideabi-gcc is unable to create an executable file.
C compiler test failed. 错误
是需要增加个tmp文件夹 放配置生成的各种文件 具体错误信息可以看jni下面的config.log文件
然后更改ffmpeg自带的configure文件
# set temporary file name
: ${TMPDIR:=$TEMPDIR}
: ${TMPDIR:=$TMP}
: ${TMPDIR:=tmp}改为
# set temporary file name
#: ${TMPDIR:=$TEMPDIR}
#: ${TMPDIR:=$TMP}
: ${TMPDIR:=E:/myProject/MyFFmpeg/project/jni/tmp}
这样应该能顺利编译通过了
提示 creating config.h config.mak 之类的 在看下jni下面 如果有这2个文件就是成功了
四.修改各种文件
1. 将刚生成的config.h中的
#define restrict restrict
改为
#define restrict
- 2.编辑 libavcodec、libavfilter、libavformat、libavutil、libpostproc 和 libswscale 目录中的 Makefile 文件,删除
include $(SUBDIR)../config.mak
3.编辑 libavutil/libm.h 把所有的static方法删除了
五 .创建av.mk放在jni下 复制下面的内容到av.mk里
# LOCAL_PATH is one of libavutil, libavcodec, libavformat, or libswscale
#include $(LOCAL_PATH)/../config-$(TARGET_ARCH).mak
include $(LOCAL_PATH)/../config.mak
OBJS :=
OBJS-yes :=
MMX-OBJS-yes :=
include $(LOCAL_PATH)/Makefile
# collect objects
OBJS-$(HAVE_MMX) += $(MMX-OBJS-yes)
OBJS += $(OBJS-yes)
FFNAME := lib$(NAME)
FFLIBS := $(foreach,NAME,$(FFLIBS),lib$(NAME))
FFCFLAGS = -DHAVE_AV_CONFIG_H -Wno-sign-compare -Wno-switch -Wno-pointer-sign
FFCFLAGS += -DTARGET_CONFIG=\"config-$(TARGET_ARCH).h\"
ALL_S_FILES := $(wildcard $(LOCAL_PATH)/$(TARGET_ARCH)/*.S)
ALL_S_FILES := $(addprefix $(TARGET_ARCH)/, $(notdir $(ALL_S_FILES)))
ifneq ($(ALL_S_FILES),)
ALL_S_OBJS := $(patsubst %.S,%.o,$(ALL_S_FILES))
C_OBJS := $(filter-out $(ALL_S_OBJS),$(OBJS))
S_OBJS := $(filter $(ALL_S_OBJS),$(OBJS))
else
C_OBJS := $(OBJS)
S_OBJS :=
endif
C_FILES := $(patsubst %.o,%.c,$(C_OBJS))
S_FILES := $(patsubst %.o,%.S,$(S_OBJS))
FFFILES := $(sort $(S_FILES)) $(sort $(C_FILES))
六.创建各种mk文件
jni根目录创建 Android.mk
写入:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_WHOLE_STATIC_LIBRARIES := libavformat libavcodec libavutil libpostproc libswscale
LOCAL_MODULE := ffmpeg
include $(BUILD_SHARED_LIBRARY)
include $(call all-makefiles-under,$(LOCAL_PATH))
在libavcodec中添加Andorid.mk文件
>>>>>>>>>>>>>>>>Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=\
$(LOCAL_PATH)\
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_LDLIBS := -lz
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
在libavfilter中添加Android.mk文件
>>>>>>>>>>>>>>>>Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=\
$(LOCAL_PATH)\
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
在libavformat中添加Andorid.mk文件
>>>>>>>>>>>>>>>>Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=\
$(LOCAL_PATH)\
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_CFLAGS += -include "string.h" -Dipv6mr_interface=ipv6mr_ifindex
LOCAL_LDLIBS := -lz
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
在libavutil中添加Android.mk文件
>>>>>>>>>>>>>>>>Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=\
$(LOCAL_PATH)\
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
在libpostproc中添加Android.mk文件
>>>>>>>>>>>>>>>>>>>>>>Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=\
$(LOCAL_PATH)\
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
在libswscale中添加Android.mk文件
>>>>>>>>>>>>>>>>>>>>Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=\
$(LOCAL_PATH)\
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
七.准备工作都做完了,最后的胜利马上来了
cygwin cd到project目录下 然后执行 ndk-build
我碰到的问题 :
1.Please define the NDK_PROJECT_PATH variable to point to it 这种一般是工程路径错了 注意jni文件夹 还有Android.mk文件的位置 仔细检查下
2.multiple definition of `ff_inverse' 重复定义了
修改libavcodec/Makefile,把下面一行删掉:
OBJS-$(!CONFIG_SMALL) += inverse.o
3.utils.c编译.o的时候也可能提示错误 找不到codec_names.h 把#include "libavcodec/codec_names.h" 删了就可以了
一切顺利的话 等待个10来分钟 obj\local\armeabi下会生成libavcodec.a, libavformat.a, libavutil.a, libpostproc.a, libswscale.a, libffmpeg.so 6个文件
大功告成!
特别感谢下面2篇博文的帮助 也copy了很多东西 ,拿来主义,要避免重复造轮子嘛 O(∩_∩)O~
http://hi.baidu.com/benbearlove/item/7b4d896240611a95c4d24920
http://hi.baidu.com/gpf626487/blog/item/fcc3a9060029dea7e850cd9f.html