利用FFmpeg对视频进行转码压缩

基本原理
执行FFmpeg的视频压缩命令
ffmpeg -i input.mov -b:v 640k output.mp4

#include "jni.h"
#include "ffmpeg.h"
//视频转码压缩主函数入口
//ffmpeg_mod.c有一个FFmpeg视频转码主函数入口
//标记(声明有一个这样的函数提供给我调用)
//参数含义分析
//首先分析:String str = "ffmpeg -i input.mov -b:v 640k output.mp4"
// argc = str.split(" ").length()
// argv = str.split(" ")  字符串数组
//参数一:命令行字符串命令个数
//参数二:命令行字符串数组
int ffmpegmain(int argc, char **argv);
//java层调用的视频压缩函数
JNIEXPORT void JNICALL Java_com_tz_ndk_compress_FFmpegCompress_callFFmepgCompress
        (JNIEnv *env, jobject jobj,jint jlen,jobjectArray jobjArray){
    //转码
    //将java的字符串数组转成C字符串
    int argc = jlen;
    //开辟内存空间
    char **argv = (char**)malloc(sizeof(char*) * argc);

    //填充内容
    for (int i = 0; i < argc; ++i) {
        jstring str = (*env)->GetObjectArrayElement(env,jobjArray,i);
        const char* tem = (*env)->GetStringUTFChars(env,str,0);
        argv[i] = (char*)malloc(sizeof(char)*1024);
        strcpy(argv[i],tem);
    }

    //开始转码(底层实现就是只需命令)
    ffmpegmain(argc,argv);

    //释放内存空间
    for (int i = 0; i < argc; ++i) {
        free(argv[i]);
    }

    //释放数组
    free(argv);

}
//ffmpeg主函数入口
int ffmpegmain(int argc, char **argv)
{
    int ret;
    int64_t ti;
    //av_log_set_callback(av_log_callback); 
    register_exit(ffmpeg_cleanup);
    setvbuf(stderr,NULL,_IONBF,0); /* win32 runtime needs this */
    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();
#if CONFIG_AVDEVICE
    avdevice_register_all();
#endif
    avfilter_register_all();
    av_register_all();
    avformat_network_init();
    show_banner(argc, argv, options);
    term_init();
    /* parse options and open all input/output files */
    ret = ffmpeg_parse_options(argc, argv);
    if (ret < 0)
    {   ffmpeg_cleanup(1);  return 1;}
    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);
        ffmpeg_cleanup(1);
        return 1;
    }
    /* file converter / grab */
    if (nb_output_files <= 0) {
        av_log(NULL, AV_LOG_FATAL, "At least one output file must be specified\n");
        ffmpeg_cleanup(1);
        return 1;
    }
//     if (nb_input_files == 0) {
//         av_log(NULL, AV_LOG_FATAL, "At least one input file must be specified\n");
//         exit_program(1);
//     }
    current_time = ti = getutime();
    if (transcode() < 0)
    {    ffmpeg_cleanup(1);  return 1;}
    ti = getutime() - ti;
    //打印帧数
     av_log(NULL, AV_LOG_FATAL, "Transcode has Finished\n");
  // if (do_benchmark) {
   //     printf("bench: utime=%0.3fs\n", ti / 1000000.0);
   // }
   // av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" frames successfully decoded, %"PRIu64" decoding errors\n",
    //       decode_error_stat[0], decode_error_stat[1]);
    //if ((decode_error_stat[0] + decode_error_stat[1]) * max_error_rate < decode_error_stat[1])
   // {    exit_program(69); return 69;}
    ///exit_program(received_nb_signals ? 255 : main_return_code);
    ffmpeg_cleanup(0);
    return main_return_code;
}

代码下载:CSDN下载

你可能感兴趣的:(ffmpeg音视频,Android应用开发)