如何编程android中执行命令行的ffmpeg的命令? [英] How to execute command line ffmpeg commands programatically in android?

查看:234
本文介绍了如何编程android中执行命令行的ffmpeg的命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 bambuser
。现在我要建立一个像MP4到3GP样品转换器应用。我知道有命令行命令的ffmpeg -i video_origine.avi video_finale.mpg 。但我不知道如何编程执行这些命令。我有样code像

I have successfully built ffmpeg for android using the bambuser . Now I have to build a sample converter application like mp4 to 3gp. I know there are command line commands ffmpeg -i video_origine.avi video_finale.mpg. But I don't know how to execute these commands programatically. I have sample code like

jint Java_com_example_ndklearning1_MainActivity_logFileInfo(JNIEnv * env, jobject this, jstring filename)
{
    av_register_all();

    AVFormatContext *pFormatCtx;
    const jbyte *str;
    str = (*env)->GetStringUTFChars(env, filename, NULL);

    if(av_open_input_file(&pFormatCtx, str, NULL, 0, NULL)!=0)
    {
        LOGE("Can't open file '%s'\n", str);
        return 1;
    }
    else
    {
        LOGI("File was opened\n");
        LOGI("File '%s', Codec %s",
            pFormatCtx->filename,
            pFormatCtx->iformat->name
        );

    }
    return 0;
}

这code打开一个文件,提取codeC的信息。我要的是,要打开的文件转换成所需格式。任何形式的帮助,例如code段或要遵循的步骤将是非常美联社preciated。

This code open's a file and extracts the codec information. All I want is that, to convert the opened file in a desired format. Any kind of help such as code snippet or steps to follow will be highly appreciated.

能否FFmpeg的API为我的目的是什么?如果有现有的API可用,这将是更有帮助。

Can ffmpeg API serve my purpose ? If there is existing API available, it will be more helpful

推荐答案

最近我碰到类似的问题就来了。我的解决办法是模拟在Java程序的命令行。

Recently I came across the similar problem. My solution is to simulate a command line in Java program.

首先,我想补充一个函数到文件ffmpeg.c

Firstly, I add a function to the file "ffmpeg.c":

int cmd_simulation(int argc, const char** argv)
{
OptionsContext o = { 0 };
// int64_t ti;

reset_options(&o, 0);

av_log_set_flags(AV_LOG_SKIP_REPEATED);
parse_loglevel(argc, argv, options);

if(argc>1 && !strcmp(argv[1], "-d")){
    run_as_daemon=1;
    av_log_set_callback(log_callback_null);
    argc--;
    argv++;
}

avcodec_register_all();

avfilter_register_all();
av_register_all();
avformat_network_init();

//show_banner(argc, argv, options);

term_init();

parse_cpuflags(argc, argv, options);

/* parse options */
parse_options(&o, argc, argv, options, opt_output_file);

if (nb_output_files <= 0 && nb_input_files == 0) {
    show_usage();
    av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
    exit_program(1);
}


if (nb_output_files <= 0) {
    av_log(NULL, AV_LOG_FATAL, "At least one output file must be specified\n");
    exit_program(1);
}

if (transcode() < 0)
    exit_program(1);

//exit_program(0);
return 7;
}

其实这个功能仅仅是一个稍加修改的主要功能的副本。

In fact this function is just a copy of the main function with a little modification.

然后创建一个本地函数:

Then create a native function:

extern const char* cmd_simulation(int, const char**);

JNIEXPORT int JNICALL Java_com_test_videowatermark_VideoUtil_test(JNIEnv * env, jobject object, jobjectArray strArray);



JNIEXPORT int JNICALL Java_com_test_videowatermark_VideoUtil_test(JNIEnv * env, jobject object, jobjectArray strArray)
{
    int arrayLength = (*env)->GetArrayLength(env, strArray);
    const char* args[arrayLength];

    int i;
    for(i = 0; i < arrayLength; i++){
        jstring jstr = (jstring)((*env)->GetObjectArrayElement(env, strArray, i));
        args[i] = (*env)->GetStringUTFChars(env, jstr, 0);
        //strcpy(args[i], arg);
        //env->ReleaseStringUTFChars(jstr, arg);
    }


    const char** argv = args;
    return  cmd_simulation(arrayLength, argv);  

}

与ffmpeg的编译后,你可以模拟excuting像ffmpeg的命令:

After compilation with ffmpeg, you can simulate excuting ffmpeg commands like:

private void executeCommand(){
    String[] command = {"ffmpeg", "-i", "some video file name",};
    int result = test(command);     
}

希望这有助于!

编辑:
Android.mk

Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_WHOLE_STATIC_LIBRARIES := libavformat libavcodec libavutil libpostproc libswscale libswresample libavfilter
LOCAL_MODULE    := VideoUtilLib
LOCAL_SRC_FILES := NativeVideoUtil.c ffmpeg.c ffmpeg_opt.c cmdutils.c ffmpeg_filter.c
LOCAL_LDLIBS := -lz -llog
include $(BUILD_SHARED_LIBRARY)
include $(call all-makefiles-under,$(LOCAL_PATH))

替换NativeVideoUtil.c与本机文件。

Replace NativeVideoUtil.c with your native file.

这篇关于如何编程android中执行命令行的ffmpeg的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆