当前位置: 代码迷 >> Android >> ffmpeg 移栽到 android 转
  详细解决方案

ffmpeg 移栽到 android 转

热度:37   发布时间:2016-05-01 20:02:13.0
ffmpeg 移植到 android 转
http://blog.csdn.net/perfectpdl/article/details/6932696

一:生成动态库(.so文件)

-->环境:

操作系统:Ubuntu9.10

ffmpeg源码版本:ffmpeg-0.6.1(可以在http://ffmpeg.org/download.html 下载源码)

androidndk版本:android-ndk-r4b-linux-x86(可以在http://androidappdocs.appspot.com/sdk/ndk/index.html 下载)

-->设置NDK环境变量:

1、用root用户登陆,打开命令窗口,输入cd/root

2、输入ls-a,会显示一个隐藏文件.bashrc

3、输入vi.bashrc,按i进入编辑模式

4、文件底部添加以下两行:
NDK_ROOT=/home/ndk。这里指你ndk的目录路径
exportNDK_ROOT
5、按esc退出,再按:wq保存

-->你可以测试一下有没有安装成功:
$cd $NDK_ROOT
$ ./ndk-buildNDK_PROJECT_PATH=$NDK_ROOT/samples/two-libs


-->找到生成的文件就代表安装正常了:

1、如果提示某个文件"Permissiondenied"之类的信息时,执行chmod777 your_filename就行了

2、如果提示找不到'cc1'等信息,执行chmod-R 777 *,就能解决了

-->配置源代码:

在/home/ndk/samples/创建一个FFMPEG文件夹,在里面再新建一个jni文件夹,然后把ffmpeg源码放在jni里面,所以最后ffmpeg源码的路径是:
/home/ndk/samples/FFMPEG/jni/ffmpeg

1. 在ffmpeg源文件夹下创建一个config.sh,内容如下:
#!/bin/bash



PREBUILT=/home/xxx/android_NDK/android-ndk-r4b/build/prebuilt/linux-x86/arm-eabi-4.4.0

PLATFORM=/home/xxx/android_NDK/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"

记得PREBUILT和PLATFORM要设置正确,它在你的NDK里面,你可以按自己的填写

2. 然后打开命令窗口,cdffmpeg源文件夹下。
$chmod +x config.sh       /*这个命令是改变config.sh执行权限而已*/
$./config.sh

如果配置正确的话显示出来的最后两行是这样的:
License:nonfree and unredistributable
Creating config.mak and config.h...


3、Configure完成后接下做NDK编译的条件了。
Android的GCC是不支持restrict关键字的,所以把ffmpeg源码下configure生成的config.h文件中的这一行:
#definerestrict restrict   改成#definerestrict   /*意思就是把后面的restrict去掉*/
如果重新Configure的话记得要把这个关键字去掉。

编辑libavutil/libm.h把其中的static的方法都删除(如果找不到libavutil/libm.h,就表明你的ffmpeg版本不是0.6而是0.5的。)

分别把libavutil、libavcodec、libavformat、libavfilter、libpostproct和libswscale下的Makefile文件中下面两行删除掉:
include$(SUBDIR)../subdir.mak
include $(SUBDIR)../config.mak

然后在ffmpeg源文件夹下新建一个av.mk文件,内容如下:(这些不用替换回车)
#LOCAL_PATH is one of libavutil, libavcodec, libavformat, orlibswscale

#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))

接下来就是从jni文件夹开始写Android.mk文件,如jni文件下新建一个Android.mk,内容如下:
include$(all-subdir-makefiles)

在ffmpeg下,Android.mk内容如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SHARED_LIBRARIES:= libavformat libavcodec libavutil libpostproc libswscalelibavfilter
LOCAL_MODULE := ffmpeg
include$(BUILD_SHARED_LIBRARY)
include $(callall-makefiles-under,$(LOCAL_PATH))

在ffmpeg\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_SHARED_LIBRARIES := libavutil libavcodec
LOCAL_MODULE:= $(FFNAME)
include $(BUILD_SHARED_LIBRARY)

在ffmpeg\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_SHARED_LIBRARIES :=libavutil
LOCAL_MODULE := $(FFNAME)
include$(BUILD_SHARED_LIBRARY)

在ffmpeg\libavfilter、ffmpeg\libavutil、ffmpeg\libpostproc、ffmpeg\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_SHARED_LIBRARIES:= (这行根据下面蓝色的来填写相应的lib)
LOCAL_MODULE:= $(FFNAME)
include $(BUILD_SHARED_LIBRARY)

----------------------------------------

ffmpeg\libavutil:不需要这一行,直接加#屏蔽掉。
ffmpeg\libswscale:LOCAL_SHARED_LIBRARIES:=libavutil
ffmpeg\libavfilter:LOCAL_SHARED_LIBRARIES:=libavutil libswscalelibavcodec
ffmpeg\libpostproc:LOCAL_SHARED_LIBRARIES:=libavutil

可能编译libavdevice也是用上面这个Android.mk,然后根据提示的错误没改变红色这行就OK了。


最后就是运行ndk-build了:
$cd $NDK_ROOT
$ ./ndk-buildNDK_PROJECT_PATH=$NDK_ROOT/samples/FFMPEG


最后生成libavcodec.so、libavutil.so、libavformat.so、libavfilter.so、libswscale.so和libpostproc.so以及libffmpeg.so,你会发现libffmpeg.so只有2K(1599个字节),其实这个文件是没有加载任何东西而编译成的空SO文件,没有任何函数的SO文件。所以不能用,但其它的都可以。估计是没有LOCAL_SRC_FILES的原因。

另外一篇不错的参考文章:http://www.eoeandroid.com/thread-42184-1-1.html,它生成的是静态库



二:生成静态库(.a文件)

-->环境配置
同上

-->源代码配置

1.
在AndroidNDK上建立一個project。


2.
將FFmpeg的sourcecode整包copy到$(PROJECT)/jni底下。


3.
在$(PROJECT)/jni/ffmpeg底下建立一個config.sh:

#!/bin/bash

PREBUILT=/android-ndk-r4b/build/prebuilt/windows/arm-eabi-4.4.0
PLATFORM=/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"

PREBUILT和PLATFORM應該要符合你自己的配置。


4.
在Cygwin中,$(PROJECT)/jni/ffmpeg底下執行config.sh:

$./config.sh

configure會依據config.sh內的設定將config.mak及config.h建立出來。


5.
在剛剛生成的config.h中找到這句:

#definerestrict restrict

因為Android的gcc並不認得restrict這個keyword,所以把它改寫成:

#definerestrict

6.
將/libavutil/libm.h中所有的static函式,全都mark起來。

7.
分別修改libavcodec、libavfilter、libavformat、libavutil、libpostproc和libswscale下的Makefile,將下面兩句mark起來:

include$(SUBDIR)../config.mak
include$(SUBDIR)../subdir.mak

8.
在$(PROJECT)/jni/ffmpeg下建立一個檔案av.mk,內容如下:

#LOCAL_PATH is one of libavutil, libavcodec, libavformat, orlibswscale

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))

9.
在$(PROJECT)/jni底下修改Android.mk,內容如下:

include$(all-subdir-makefiles)

10.
在/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))

11.
在/ffmpeg/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)




12.
在/ffmpeg/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)




13.
在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)


14.
在$(PROJECT)底下執行ndk-build:

$/android-ndk-r4b/ndk-build


15.
完成,產生出libavcodec.a、libavformat.a、libavutil.a、libpostproc.a、libswscale.a和libffmpeg.so。
  相关解决方案