前言
Windows下命令行轉碼壓縮形式
ffmpeg -i input.mov -b:v 640k output.mp4
Android也是利用FFmpeg命令行的形式(混編),進行視頻轉碼壓縮罪裹。
1.引入FFmpeg命令行調用庫
2.Android 核心代碼
2.1.jni Java聲明
//視頻轉碼壓縮
public native void transcodingCompress(int argc,String[] argv);
2.2.核心代碼ffmpeg_transcoding.c
#include "jni.h"
#include "ffmpeg.h"
//視頻轉碼壓縮主函數(shù)入口
//SDL(main)
//ffmpeg_mod.c有一個FFmpeg視頻轉碼主函數(shù)入口
//標記(聲明有一個這樣的函數(shù)提供給我調用)
//參數(shù)含義分析
//首先分析:String str = "ffmpeg -i input.mov -b:v 640k output.mp4"
// argc = str.split(" ").length()
// argv = str.split(" ") 字符串數(shù)組
//參數(shù)一:命令行字符串命令個數(shù)
//參數(shù)二:命令行字符串數(shù)組
int ffmpegmain(int argc, char **argv);
JNIEXPORT void JNICALL Java_com_haocai_ffmpegtest_util_VideoPlayer_transcodingCompress
(JNIEnv *env, jobject jobj,jint jlen,jobjectArray jobjArray){
//轉碼
//將java的字符串數(shù)組轉成C字符串
int argc = jlen;
//開辟內(nèi)存空間
char **argv = (char**)malloc(sizeof(char*) * argc);
//填充內(nèi)容
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);
}
//開始轉碼(底層實現(xiàn)就是只需命令)
ffmpegmain(argc,argv);
//釋放內(nèi)存空間
for (int i = 0; i < argc; ++i) {
free(argv[i]);
}
//釋放數(shù)組
free(argv);
}
調用ffmpeg_mod.c中ffmpegmain函數(shù)入口
//ffmpeg主函數(shù)入口
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;
}
2.3調用程序
public void transcodingCompress(){
final File inputFile;
final File outputFile;
final File dic = Environment.getExternalStorageDirectory();
inputFile = new File(dic,"告白氣球.avi");
outputFile = new File(dic,"告白氣球.mp4");
new Thread(new Runnable() {
@Override
public void run() {
//獲取視頻文件的路徑(sdcard路徑)
// 拼接cmd 指令 ffmpeg -i 告白氣球.avi -b:v 640k 告白氣球.mp4 (與windows 命令行相同)
StringBuilder builder = new StringBuilder();
builder.append("ffmpeg ");
builder.append("-i ");
builder.append(inputFile.getAbsolutePath()+" ");
builder.append("-b:v 640k "); //碼率越大 清晰度越高 碼率越小 清晰度越低
builder.append(outputFile.getAbsolutePath());
String[] argv = builder.toString().split(" ");
int argc = argv.length;
player.transcodingCompress(argc,argv);
}
}).start();
}
3.運行結果
3.1Log輸出
I/hwaps: JNI_OnLoad
I/main: 當前幀:36
I/main: 當前幀:48
I/main: 當前幀:73
I/main: 當前幀:85
I/main: 當前幀:93
I/main: 當前幀:99
I/main: 當前幀:106
I/main: 當前幀:113
太多省略......