[2].FFMpeg入门之 - Android使用FFMpeg命令

1.编译ffmpeg源码生成对应android平台下的so文件。我的编译脚本:

export NDK=/home/dengxuan/Android/Sdk/ndk-bundle
export PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt
export PLATFORM=$NDK/platforms/android-15/arch-arm
export PREFIX=$(pwd)/android/build2
build_one(){
  ./configure --target-os=linux --prefix=$PREFIX \
--enable-cross-compile \
--enable-runtime-cpudetect \
--disable-asm \
--arch=arm \
--cc=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi-gcc \
--cross-prefix=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi- \
--disable-stripping \
--nm=$PREBUILT/linux-x86_64/bin/arm-linux-androideabi-nm \
--sysroot=$PLATFORM \
--enable-gpl --enable-shared --disable-static --enable-small \
--disable-ffprobe --disable-ffplay --disable-ffmpeg --disable-ffserver --disable-debug \
--extra-cflags="-fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -marm -march=armv7-a"
}
build_one
make
make install

编译成功后,会在生成android文件夹,其中build2文件夹下面的就是本次编译生成的。

[2].FFMpeg入门之 - Android使用FFMpeg命令_第1张图片
image.png

拷贝这些到你的工程中。

[2].FFMpeg入门之 - Android使用FFMpeg命令_第2张图片
image.png

其中config.h是编译后生成的,这个也要加上。在cpp目录下 加上对应的头文件

[2].FFMpeg入门之 - Android使用FFMpeg命令_第3张图片
image.png

在ffmpeg.c的main函数(前面被改名了run)return 前面加上

    nb_filtergraphs = 0;
    progress_avio = NULL;
    input_streams = NULL;
    nb_input_streams = 0;
    input_files = NULL;
    nb_input_files = 0;
    output_streams = NULL;
    nb_output_streams = 0;
    output_files = NULL;
    nb_output_files = 0;

在native-lib.cpp中添加执行命令的jni方法

JNIEXPORT jint JNICALL
Java_com_xxx_svsdk_tools_FFMpegUtils_execute(
        JNIEnv *env,
        jobject /* this */, jobjectArray commands) {

        int argc = env->GetArrayLength(commands);
        char *argv[argc];
        int i;
        for (i = 0; i < argc; i++) {
            jstring js = (jstring) env->GetObjectArrayElement(commands, i);
            argv[i] = (char *) env->GetStringUTFChars(js, 0);
        }
        return execute(argc, argv);
    }
}

在对应的java文件中添加对应的方法,这里我加了一个对应的裁剪方法

public class FFMpegUtils {

    public static int cutVideo(String in,String out,long start,long duration){
        StringBuilder builder = new StringBuilder("ffmpeg -ss ");
        builder.append(ViewUtil.parseTimeToString(start)).append(" ")
                .append("-t ").append(ViewUtil.parseTimeToString(duration))
                .append(" -i ").append(in)
                .append(" -vcodec copy -acodec copy ")
                .append(out);
        Logger.i(builder.toString());
        return execute(builder.toString().split(" "));
    }

    public static native String avcodecinfo();
    public static native int execute(String[] cmd);
    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
        System.loadLibrary("avutil55");
        System.loadLibrary("swresample2");
        System.loadLibrary("avcodec57");
        System.loadLibrary("avfilter6");
        System.loadLibrary("swscale4");
        System.loadLibrary("avformat57");
        System.loadLibrary("postproc54");
        System.loadLibrary("avdevice57");
    }
}

对应的CMakeLists.txt文件

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

cmake_minimum_required(VERSION 3.4.1)

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

#set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../../libs)



add_library( avutil55
             SHARED
             IMPORTED )
set_target_properties( avutil55
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavutil55.so )

add_library( swresample2
             SHARED
             IMPORTED )
set_target_properties( swresample2
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libswresample2.so )
add_library( avcodec57
             SHARED
             IMPORTED )
set_target_properties( avcodec57
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavcodec57.so )
add_library( avfilter6
             SHARED
             IMPORTED)
set_target_properties( avfilter6
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavfilter6.so )
add_library( swscale4
             SHARED
             IMPORTED)
set_target_properties( swscale4
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libswscale4.so )

add_library( avformat57
             SHARED
             IMPORTED)
set_target_properties( avformat57
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavformat57.so )

add_library( postproc54
             SHARED
             IMPORTED)
set_target_properties( postproc54
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libpostproc54.so )

add_library( avdevice57
             SHARED
             IMPORTED)
set_target_properties( avdevice57
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavdevice57.so )

add_library( native-lib
             SHARED
             src/main/cpp/native-lib.cpp
             src/main/cpp/cmdutils.c
             src/main/cpp/ffmpeg.c
             src/main/cpp/ffmpeg_filter.c
             src/main/cpp/ffmpeg_opt.c

              )

include_directories(libs/include)

#target_include_directories(native-lib PRIVATE libs/include)

target_link_libraries( native-lib avutil55 swresample2 avcodec57 avfilter6 swscale4 avformat57 postproc54 avdevice57 -landroid # Add this.
                       ${log-lib} )

选择一个视频去裁剪,可以看到log打出来的对应的裁剪命令:

image.png
ffmpeg -ss 00:00 -t 00:10 -i /storage/sdcard0/DCIM/Camera/VID_20170817_140402.mp4 -vcodec copy -acodec copy /storage/sdcard0/Movies/svsdk/svsdk_1505267724271.mp4

你可能感兴趣的:([2].FFMpeg入门之 - Android使用FFMpeg命令)