.移植ffmpeg到android
1. 安装cygwin
必须是1.7或者更新的版本
安装cygwin的时候,选择安装gcc和make,目的是为了提供编译环境,
make的版本至少是3.81
2.安装ndk
? 2.1 下载ndk-r4b-windows,并将其放到cygwin的/home/Administator目录
? 设置环境变量
将/etc/skel/目录的 .bash_profile .bashrc .inputrc拷贝到/home/administrator目录,
编辑/home/administrator目录的 .bashrc文件,在后面增加两行:
NDK_ROOT=~/android-ndk-r4b-windows/android-ndk-r4b/
export NDK_ROOT
~的实际目录是cygwin的/home/Administrator目录
3.下载ffmpeg
版本是0.6.1
在android-ndk-r4b/samples目录,创建文件夹FFMPEG,在ffmpeg目录创建jni目录,
将ffmpeg解压并将全部源码拷贝到jni目录
ffmpeg源码的路径变成:
~/android-ndk-r4b-windows/android-ndk-r4b/samples/FFMPEG/jni/ffmpeg
4.编译准备
? cd到ffmpeg目录,执行dos2unix configure,转换文件格式
?在ffmpeg源码目录新建config.sh,并转换成 unix格式,
内容如下:
#!/bin/bash
PREBUILT=/home/Administrator/android-ndk-r4b-windows/android-ndk-r4b/build/prebuilt/windows/arm-eabi-4.4.0
PLATFORM=/home/Administrator/android-ndk-r4b-windows/android-ndk-r4b/build/platforms/android-8/arch-arm
./configure --target-os=linux --arch=arm --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-mpegaudio-hp --disable-avdevice --enable-cross-compile --cc=$PREBUILT/bin/arm-eabi-gcc --cross-prefix=$PREBUILT/bin/arm-eabi- --nm=$PREBUILT/bin/arm-eabi-nm --extra-cflags="-fPIC -DANDROID" --disable-asm --enable-neon --enable-armv5te --extra-ldflags="-Wl,-T,$PREBUILT/arm-eabi/lib/ldscripts/armelf.x -Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib $PREBUILT/lib/gcc/arm-eabi/4.4.0/crtbegin.o $PREBUILT/lib/gcc/arm-eabi/4.4.0/crtend.o -lc -lm -ldl"
?执行: chmod +x config.sh && ./config.sh
输出 :creating config.mak and config.h...
执行成功
? 修改生成的config.h,将其中的
#define restrict restrict 改成 #define restrict
转换文件格式
? 编辑libavutil/libm.h,将其中的所有static方法删除
转换文件格式
? 修改libavcodec,libavfilter,libavformat,libavutil,libpostproc和libswscale目录的MakeFile文件,
将以下内容删除:
include $( SUBDIR ) ../config.mak
include $ (SUBDIR) .. / subdir.mak
修改之后转换文件格式
? 在ffmpeg源码目录新建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))
? 创建一系列Android.mk文件
在jni目录,创建Android.mk,内容如下:
include $(all-subdir-makefiles)
在ffmpeg目录,创建Android.mk,文件如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_STATIC_LIBRARIES := libavformat libavcodec libavutil libpostproc libswscale
LOCAL_MODULE := ffmpeg
include $(BUILD_SHARED_LIBRARY)
include $(call all-makefiles-under,$(LOCAL_PATH))
在libavformat/目录,创建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)
在libavcodec/目录,创建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、libavutil、libpostproc和libswscale目录,创建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)
5.进行编译
执行: ndk-build NDK_PROJECT_PATH=$NDK_ROOT/samples/FFMPEG
编译成功,在生成的libs目录看到.a和.so文件
6.可能出现的错误
6.1 执行config.sh时,出现错误:
Unknown option "--target-os=linux".
See ./configure --help for available options.
解决办法:ffmpeg的版本不支持,换成0.6.1即可
6.2 执行config.sh是,出现错误:
./config.sh: line 8: --enable-nonfree: command not found
./config.sh: line 9: --disable-ffprobe: command not found
.........
解决办法:config.sh中的configure命令,后面跟的参数有换行符,导致系统认为
参数是新的命令行。将换行符删除即可
7.其他问题
编辑的文件,都要转换成unix文件格式。
在unix文件格式中, '\' 可以代表空格。