FFMPEG + Android wrapper using the latest versions

2019-07-06 09:04发布

问题:

I am trying to compile FFMPEG to work with my Android app. I've looked at: https://github.com/halfninja/android-ffmpeg-x264 which is almost what I want but I need a later version of FFMPEG (so I'm trying to use the latest, 2.2).

I'm using most of the scripts located there, but the problem is after I run compile_make_everything.sh, I try to run ndk-build but I get errors (basically it can't find the main function in ffmpeg.c).

This is my Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

#LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
LOCAL_MODULE  := videokit
LOCAL_SRC_FILES := videokit/com_myapp_example_FFMpegService.c 

LOCAL_LDLIBS := -llog -ljnigraphics -lz -landroid

LOCAL_STATIC_LIBRARIES :=ffmpeg

include $(BUILD_SHARED_LIBRARY)
$(call import-module,ffmpeg-2.0.1/android/arm)

If I modify my LOCAL_SRC_FILES to include ffmpeg.c, it fails when I tries to find some of the helper libraries it requires: LOCAL_SRC_FILES := videokit/com_myapp_example_FFMpegService.c ffmpeg/ffmpeg.c ffmpeg/cmdutils.c

The error:

In file included from /home/me/ffmpeg/new-android-ffmpeg/Project/jni/ffmpeg/ffmpeg.c:44:0: /home/me/ffmpeg/new-android-ffmpeg/Project/jni/ffmpeg/libavformat/avformat.h:255:32: fatal error: libavcodec/avcodec.h: No such file or directory compilation terminated.

How can I include the latest ffmpeg library while still being able to call the main function?

回答1:

Adding LOCAL_C_INCLUDES += $(LOCAL_PATH)/ffmpeg will probably help a fair bit.

However, you're gonna have more trouble. Basically the ffmpeg.c in the videokit directory is a copy of the original one in the ffmpeg sources, but it's been modified a little to not terminate and to clean up for repeated use, as well as to use the android logging functions. When you build the videokit library, instead of linking in the ffmpeg/ffmpeg.c, you're linking the modified one, videokit/ffmpeg.c.

The problem you're facing is that you've updated all the sources in the ffmpeg directory (and all the libav* libraries under it), but you're still trying to link that old videokit/ffmpeg.c from 0.x against those new 2.x sources. As you can imagine, it's changed a bit.

I would suggest you diff videokit/ffmpeg.c against ffmpeg/ffmpeg.c while you have the old ffmpeg version checked out, and see what was altered. Then you need to copy the new ffmpeg.c out to the videokit directory and perform equivalent changes. The logging stuff is simple, cleaning up for repeated use instead of terminating... that one will be a bit more work. Things have moved around a fair bit.

Oh and you'll also need to copy cmdutils.c, ffmpeg_opt.c, and ffmpeg_filter.c from the new sources into videokit, and to include those in your makefile. They may need alterations too.



回答2:

IMO - the end of your error is about not finding reqd static lib=libavcodec...

where are your "FFMPEG_LIBS" ?? you have "libs=ffmpeg" but that may be null/undef based on what i see...

Its very difficult to upgrade ffmpeg beyond the commits explicitly marked in ./Project/jni

and he says that at the bottom of his readme "Updating Submodules"....

This android make sets the local static libs created by the JNI build from the earlier build of ffmpeg.

LOCAL_PATH := $(call my-dir)


include $(CLEAR_VARS)

LOCAL_MODULE  := avffmpeg

FFMPEG_LIBS := $(addprefix ffmpeg/, \
 libavfilter/libavfilter.a \
 libavcodec/libavcodec.a \
  libavformat/libavformat.a \
 libswresample/libswresample.a \
 libswscale/libswscale.a \
 libavutil/libavutil.a \
 libpostproc/libpostproc.a )

LOCAL_CFLAGS += -g -Iffmpeg -Ivideokit -Wno-deprecated-declarations 
LOCAL_LDLIBS += -fuse-ld=gold -llog -lz -ldl -landroid $(FFMPEG_LIBS) x264/libx264.a 
LOCAL_SRC_FILES := videokit/com_b2bpo_media_VideoBrowser.c ffmpeg/ffmpeg.c ffmpeg/cmdutils.c

include $(BUILD_SHARED_LIBRARY)

# Use to safely invoke ffmpeg multiple times from the same Activity
include $(CLEAR_VARS)

LOCAL_MODULE := ffmpeginvoke

LOCAL_CFLAGS += -g -Ivideokit -Wno-deprecated-declarations 
LOCAL_LDLIBS += -llog -lz -ldl
LOCAL_SRC_FILES := videokit/ffmpeg_invoke.c
include $(BUILD_SHARED_LIBRARY)

I think others have succeeded in getting to ffmpeg 2.0 or so , and you may be able to get to the heart of how they managed to upgrade the git submodules (x264 and ffmpeg).

I've long been using the older ffmpeg on android and dont return to issues regarding the linking of an upgraded ffmpeg.. Too time consuming...