当前位置: 代码迷 >> Android >> 把ffmpeg-0.11.1调整到android项目中
  详细解决方案

把ffmpeg-0.11.1调整到android项目中

热度:84   发布时间:2016-05-01 13:45:12.0
把ffmpeg-0.11.1整合到android项目中


1.新建android项目。
2.在项目下新建文件夹jni
3.把解压的ffmpeg-0.11.1拷贝到jni文件中。
4.在ffmpeg-0.11.1中编写config.sh文件,内容如下:
#!/bin/bash
PREBUILT=/cygdrive/d/Android/android-ndk-r8b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows
PLATFORM=/cygdrive/d/Android/android-ndk-r8b/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-avdevice \--enable-cross-compile \--cc=/cygdrive/d/Android/android-ndk-r8b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-gcc \--cross-prefix=/cygdrive/d/Android/android-ndk-r8b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi- \--nm=/cygdrive/d/Android/android-ndk-r8b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-nm \--extra-cflags="-fPIC -DANDROID" \--disable-asm \--enable-neon \--enable-armv5te \--extra-ldflags="-L/cygdive/d/Android/android-ndk-r8b/platforms/android-8/arch-arm/usr/lib -nostdlib"

注意:1.在用EditPlus编辑config.sh文件时,./configure 后的命令不能换行。
        2.还有特别注意的是在命令中例如--cc=/cygdrive/d/Android/android-ndk-r8b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-gcc,/cygdrive/d/Android/android-ndk-r8b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows不能用$PREBUILT替代,不然找不到文件,具体原因不清楚。(解决昨天的问题)
5.在命令行中输入bash,进入到ffmpeg-0.11.1目录中,输入./config.sh
6.结果如下:生成config.mak和config.h文件




7.在刚刚生成的config.h文件中找到
#define restrict restrict
修改为
#define restrict
8.将/libavutil/libm.h中所有的static函式,全都注释
9.分別修改libavcodec、libavfilter、libavformat、libavutil、libpostproc和libswscale下的Makefile,将下面两句删掉
include $(SUBDIR)../config.mak
include $(SUBDIR)../subdir.mak
10.在ffmpeg-0.11.1下建立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))
11.在ffmpeg-0.11.1下建立Android.mk文件,如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_WHOLE_STATIC_LIBRARIES := libavformat libavcodec libavutil libpostproc libswscale
LOCAL_MODULE := ffmpeg
#FFMPEG_OBJ_PATH := `cygpath -m "$(LOCAL_PATH)/../../obj/local/armeabi"`
#LOCAL_LDLIBS := -L$(call host-path, $(FFMPEG_OBJ_PATH))
#LOCAL_LDLIBS += -lavformat -lavcodec -lavutil -lswscale -lpostproc
include $(BUILD_SHARED_LIBRARY)
include $(call all-makefiles-under,$(LOCAL_PATH))
12.在jni下建立Android.mk文件,如下:
include $(all-subdir-makefiles)
13.在/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)

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

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

17.cd 到工程目录下,运行ndk-build
18.结果:生成libavcodec.a、libavformat.a、libavutil.a、libpostproc.a、 libswscale.a和libffmpeg.so



android项目调用ffmpeg库:
1.编写java代码:
public class MainActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView tv=(TextView) findViewById(R.id.textView1);
            tv.setText(mediaPlayer());
            Log.d("hello","join into "+mediaPlayer());
        }
        private native String  mediaPlayer();
       
        static{
         System.loadLibrary("MainActivity");
        }
      
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
   
}
2.生成.h文件
3.编写MainActivity.c文件:
#include<jni.h>
#include<ffmpeg/libavcodec/avcodec.h>
#include<ffmpeg/libavformat/avformat.h>
#include<string.h>
JNIEXPORT jstring JNICALL Java_lichunxia_android_ffmpegtest001_MainActivity_mediaPlayer
  (JNIEnv *env, jobject obj){
   av_register_all();
   return (*env)->NewStringUTF(env, "调用ffmpeg里的函数av_register_all()成功!");
}
4.编辑jni下Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg
LOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)
LOCAL_LDLIBS := -lffmpeg        //需要调用的动态库
LOCAL_MODULE    :=MainActivity
LOCAL_SRC_FILES :=MainActivity.c
include $(BUILD_SHARED_LIBRARY)
5.结果出现错误:找不到-lffmpeg
解决方法:把libffmpeg.so拷贝到D:\Android\android-ndk-r8b\platforms\android-3\arch-arm\usr\lib 以及android -4,-5,-6,-7等。

  相关解决方案