Search code examples
javaandroidffmpegaudio-streaming

How to build + include FFMPEG into an existing Android project


I've found multiple questions and tutorials regarding FFMPEG, but I don't seem to understand most of them. All of the guides I have read, miss out large gaps and don't tend to explain things.

I have an existing Android app that streams audio using a third party library called AAC Decoder. For various reasons, I need to switch to use FFMPEG, but cannot figure out how. I have managed to follow guides to build FFMPEG, but then I don't understand what I am supposed to do with the output.

My app needs to stream audio only, from a remote URL. The streams can be in a variety of formats.

If anyone could link me to some comprehensive, detailed guides, or provide me with instructions, it would be great.

Thanks.


Solution

  • I created scripts to build FFmpeg, see my answer here:

    arm-linux-androideabi-gcc is unable to create an executable - compile ffmpeg for android armeabi devices

    One you have FFmpeg compiled create a "jni" folder in the root of your project. In the jni folder create Android.mk with these contents:

    include $(call all-subdir-makefiles)
    

    Then create Application.mk with these contents:

    APP_ABI := armeabi armeabi-v7a x86
    

    Next, in the jni folder create the following folder structure:

    ffmpeg/ffmpeg/

    In the first ffmpeg folder create another Android.mk:

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    LOCAL_MODULE := libavcodec
    LOCAL_SRC_FILES := ffmpeg/$(TARGET_ARCH_ABI)/lib/$(LOCAL_MODULE).so
    LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/ffmpeg/$(TARGET_ARCH_ABI)/include
    include $(PREBUILT_SHARED_LIBRARY)
    
    include $(CLEAR_VARS)
    LOCAL_MODULE := libavformat
    LOCAL_SRC_FILES := ffmpeg/$(TARGET_ARCH_ABI)/lib/$(LOCAL_MODULE).so
    LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/ffmpeg/$(TARGET_ARCH_ABI)/include
    include $(PREBUILT_SHARED_LIBRARY)
    
    include $(CLEAR_VARS)
    LOCAL_MODULE := libavutil
    LOCAL_SRC_FILES := ffmpeg/$(TARGET_ARCH_ABI)/lib/$(LOCAL_MODULE).so
    LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/ffmpeg/$(TARGET_ARCH_ABI)/include
    include $(PREBUILT_SHARED_LIBRARY)
    
    LOCAL_PATH:= $(call my-dir)
    

    Finally, move the contents of the build folder (from the build script) to /jni/ffmpeg/ffmpeg/

    From the project root run:

    ndk-build clean
    

    Then run:

    ndk-build 
    

    If you are feeling lazy you can simply download the jni folder from my project here and delete the "metadata" and "player" folders:

    http://svn.code.sf.net/p/servestream/code/trunk/jni/

    Let me know if you have any additional questions.