zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

android端采用FFmpeg进行音频混合与拼接剪切

Android 进行 FFMPEG 混合 采用 音频 拼接 剪切
2023-09-27 14:27:47 时间

接触FFmpeg有一段时间了,它是音视频开发的开源库,几乎其他所有播放器、直播平台都基于FFmpeg进行二次开发。本篇文章来总结下采用FFmpeg进行音频处理:音频混合、音频剪切、音频拼接与音频转码。

采用android studio进行开发,配置build.gradle文件:

[java]  view plain  copy
  1. defaultConfig {  
  2.         ......  
  3.         externalNativeBuild {  
  4.             cmake {  
  5.                 cppFlags ""  
  6.             }  
  7.         }  
  8.         ndk {  
  9.             abiFilters "armeabi-v7a"  
  10.         }  
  11.     }  
另外指定cmake文件路径:

[java]  view plain  copy
  1. externalNativeBuild {  
  2.     cmake {  
  3.         path "CMakeLists.txt"  
  4.     }  
  5. }  
  6. sourceSets {  
  7.     main {  
  8.         jniLibs.srcDirs = ['libs']  
  9.         jni.srcDirs = []  
  10.     }  
  11. }  

从FFmpeg官网下载源码,编译成ffmpeg.so动态库,并且导入相关源文件与头文件:


然后配置cMakeLists文件:

[java]  view plain  copy
  1. add_library( # Sets the name of the library.  
  2.              audio-handle  
  3.   
  4.              # Sets the library as a shared library.  
  5.              SHARED  
  6.   
  7.              # Provides a relative path to your source file(s).  
  8.              src/main/cpp/ffmpeg_cmd.c  
  9.              src/main/cpp/cmdutils.c  
  10.              src/main/cpp/ffmpeg.c  
  11.              src/main/cpp/ffmpeg_filter.c  
  12.              src/main/cpp/ffmpeg_opt.c)  
  13.   
  14. add_library( ffmpeg  
  15.              SHARED  
  16.              IMPORTED )  
  17. set_target_properties( ffmpeg  
  18.                        PROPERTIES IMPORTED_LOCATION  
  19.                        ../../../../libs/armeabi-v7a/libffmpeg.so )  
  20.   
  21. include_directories(src/main/cpp/include)  
  22.   
  23. find_library( log-lib  
  24.               log )  
  25.   
  26. target_link_libraries( audio-handle  
  27.                        ffmpeg  
  28.                        ${log-lib} )  
调用FFmpeg命令行进行音频处理:

[java]  view plain  copy
  1. /** 
  2.  * 调用ffmpeg处理音频 
  3.  * @param handleType handleType 
  4.  */  
  5. private void doHandleAudio(int handleType){  
  6.     String[] commandLine = null;  
  7.     switch (handleType){  
  8.         case 0://转码  
  9.             String transformFile = PATH + File.separator + "transform.aac";  
  10.             commandLine = FFmpegUtil.transformAudio(srcFile, transformFile);  
  11.             break;  
  12.         case 1://剪切  
  13.             String cutFile = PATH + File.separator + "cut.mp3";  
  14.             commandLine = FFmpegUtil.cutAudio(srcFile, 1015, cutFile);  
  15.             break;  
  16.         case 2://合并  
  17.             String concatFile = PATH + File.separator + "concat.mp3";  
  18.             commandLine = FFmpegUtil.concatAudio(srcFile, appendFile, concatFile);  
  19.             break;  
  20.         case 3://混合  
  21.             String mixFile = PATH + File.separator + "mix.aac";  
  22.             commandLine = FFmpegUtil.mixAudio(srcFile, appendFile, mixFile);  
  23.             break;  
  24.         default:  
  25.             break;  
  26.     }  
  27.     executeFFmpegCmd(commandLine);  
  28. }  
其中,音频混音、合并、剪切和转码的FFmpeg命令行的拼接如下:

[java]  view plain  copy
  1. /** 
  2.  * 使用ffmpeg命令行进行音频转码 
  3.  * @param srcFile 源文件 
  4.  * @param targetFile 目标文件(后缀指定转码格式) 
  5.  * @return 转码后的文件 
  6.  */  
  7. public static String[] transformAudio(String srcFile, String targetFile){  
  8.     String transformAudioCmd = "ffmpeg -i %s %s";  
  9.     transformAudioCmd = String.format(transformAudioCmd, srcFile, targetFile);  
  10.     return transformAudioCmd.split(" ");//以空格分割为字符串数组  
  11. }  
  12.   
  13. /** 
  14.  * 使用ffmpeg命令行进行音频剪切 
  15.  * @param srcFile 源文件 
  16.  * @param startTime 剪切的开始时间(单位为秒) 
  17.  * @param duration 剪切时长(单位为秒) 
  18.  * @param targetFile 目标文件 
  19.  * @return 剪切后的文件 
  20.  */  
  21. @SuppressLint("DefaultLocale")  
  22. public static  String[] cutAudio(String srcFile, int startTime, int duration, String targetFile){  
  23.     String cutAudioCmd = "ffmpeg -i %s -ss %d -t %d %s";  
  24.     cutAudioCmd = String.format(cutAudioCmd, srcFile, startTime, duration, targetFile);  
  25.     return cutAudioCmd.split(" ");//以空格分割为字符串数组  
  26. }  
  27.   
  28. /** 
  29.  * 使用ffmpeg命令行进行音频合并 
  30.  * @param srcFile 源文件 
  31.  * @param appendFile 待追加的文件 
  32.  * @param targetFile 目标文件 
  33.  * @return 合并后的文件 
  34.  */  
  35. public static  String[] concatAudio(String srcFile, String appendFile, String targetFile){  
  36.     String concatAudioCmd = "ffmpeg -i concat:%s|%s -acodec copy %s";  
  37.     concatAudioCmd = String.format(concatAudioCmd, srcFile, appendFile, targetFile);  
  38.     return concatAudioCmd.split(" ");//以空格分割为字符串数组  
  39. }  
  40.   
  41. /** 
  42.  * 使用ffmpeg命令行进行音频混合 
  43.  * @param srcFile 源文件 
  44.  * @param mixFile 待混合文件 
  45.  * @param targetFile 目标文件 
  46.  * @return 混合后的文件 
  47.  */  
  48. public static  String[] mixAudio(String srcFile, String mixFile, String targetFile){  
  49.     String mixAudioCmd = "ffmpeg -i %s -i %s -filter_complex amix=inputs=2:duration=first -strict -2 %s";  
  50.     mixAudioCmd = String.format(mixAudioCmd, srcFile, mixFile, targetFile);  
  51.     return mixAudioCmd.split(" ");//以空格分割为字符串数组  
  52. }  
FFmpeg处理混音的公式如下,其中sample1为源文件采样率、sample2为待混合文件采样率:

混音公式:value = sample1 + sample2 - (sample1 * sample2 / (pow(2, 16-1) - 1))

开启子线程,调用native方法进行音频处理:

[java]  view plain  copy
  1. public static void execute(final String[] commands, final OnHandleListener onHandleListener){  
  2.     new Thread(new Runnable() {  
  3.         @Override  
  4.         public void run() {  
  5.             if(onHandleListener != null){  
  6.                 onHandleListener.onBegin();  
  7.             }  
  8.             //调用ffmpeg进行处理  
  9.             int result = handle(commands);  
  10.             if(onHandleListener != null){  
  11.                 onHandleListener.onEnd(result);  
  12.             }  
  13.         }  
  14.     }).start();  
  15. }  
  16. private native static int handle(String[] commands);  
关键的native方法,是把java传入的字符串数组转成二级指针数组,然后调用FFmpeg源码中的run方法:

[cpp]  view plain  copy
  1. JNIEXPORT jint JNICALL Java_com_frank_ffmpeg_FFmpegCmd_handle  
  2. (JNIEnv *env, jclass obj, jobjectArray commands){  
  3.     int argc = (*env)->GetArrayLength(env, commands);  
  4.     char **argv = (char**)malloc(argc * sizeof(char*));  
  5.     int i;  
  6.     int result;  
  7.     for (i = 0; i < argc; i++) {  
  8.         jstring jstr = (jstring) (*env)->GetObjectArrayElement(env, commands, i);  
  9.         char* temp = (char*) (*env)->GetStringUTFChars(env, jstr, 0);  
  10.         argv[i] = malloc(1024);  
  11.         strcpy(argv[i], temp);  
  12.         (*env)->ReleaseStringUTFChars(env, jstr, temp);  
  13.     }  
  14.     //执行ffmpeg命令  
  15.     result =  run(argc, argv);  
  16.     //释放内存  
  17.     for (i = 0; i < argc; i++) {  
  18.         free(argv[i]);  
  19.     }  
  20.     free(argv);  
  21.     return result;  
  22. }  
关于FFmpeg的run方法的源码如下,中间有部分省略:
[cpp]  view plain  copy
  1. int run(int argc, char **argv)  
  2. {  
  3.     /****************省略********************/  
  4.     //注册各个模块  
  5.     avcodec_register_all();  
  6. #if CONFIG_AVDEVICE  
  7.     avdevice_register_all();  
  8. #endif  
  9.     avfilter_register_all();  
  10.     av_register_all();  
  11.     avformat_network_init();  
  12.     show_banner(argc, argv, options);  
  13.     term_init();  
  14.     /****************省略********************/  
  15.     //解析命令选项与打开输入输出文件  
  16.     int ret = ffmpeg_parse_options(argc, argv);  
  17.     if (ret < 0)  
  18.         exit_program(1);  
  19.     /****************省略********************/  
  20.     //文件转换  
  21.     if (transcode() < 0)  
  22.         exit_program(1);  
  23.     /****************省略********************/  
  24.     //退出程序操作:关闭文件、释放内存  
  25.     exit_program(received_nb_signals ? 255 : main_return_code);  
  26.     ffmpeg_cleanup(0);  
  27. }  
其中,最关键的是文件转换部分,源码如下:

[cpp]  view plain  copy
  1. static int transcode(void)  
  2. {  
  3.     int ret, i;  
  4.     AVFormatContext *os;  
  5.     OutputStream *ost;  
  6.     InputStream *ist;  
  7.     int64_t timer_start;  
  8.     int64_t total_packets_written = 0;  
  9.     //转码方法初始化  
  10.     ret = transcode_init();  
  11.     if (ret < 0)  
  12.         goto fail;  
  13.   
  14.     if (stdin_interaction) {  
  15.         av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");  
  16.     }  
  17.     timer_start = av_gettime_relative();  
  18.   
  19. #if HAVE_PTHREADS  
  20.     if ((ret = init_input_threads()) < 0)  
  21.         goto fail;  
  22. #endif  
  23.     //transcode循环处理  
  24.     while (!received_sigterm) {  
  25.         int64_t cur_time= av_gettime_relative();  
  26.   
  27.         //如果遇到"q"命令,则退出循环  
  28.         if (stdin_interaction)  
  29.             if (check_keyboard_interaction(cur_time) < 0)  
  30.                 break;  
  31.   
  32.         //判断是否还有输出流  
  33.         if (!need_output()) {  
  34.             av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");  
  35.             break;  
  36.         }  
  37.   
  38.         ret = transcode_step();  
  39.         if (ret < 0 && ret != AVERROR_EOF) {  
  40.             char errbuf[128];  
  41.             av_strerror(ret, errbuf, sizeof(errbuf));  
  42.   
  43.             av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", errbuf);  
  44.             break;  
  45.         }  
  46.   
  47.         //打印音视频流信息  
  48.         print_report(0, timer_start, cur_time);  
  49.     }  
  50. #if HAVE_PTHREADS  
  51.     free_input_threads();  
  52. #endif  
  53.   
  54.     //文件末尾最后一个stream,刷新解码器buffer  
  55.     for (i = 0; i < nb_input_streams; i++) {  
  56.         ist = input_streams[i];  
  57.         if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {  
  58.             process_input_packet(ist, NULL, 0);  
  59.         }  
  60.     }  
  61.     flush_encoders();  
  62.     term_exit();  
  63.   
  64.     //写文件尾,关闭文件  
  65.     for (i = 0; i < nb_output_files; i++) {  
  66.         os = output_files[i]->ctx;  
  67.         if ((ret = av_write_trailer(os)) < 0) {  
  68.             av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s", os->filename, av_err2str(ret));  
  69.             if (exit_on_error)  
  70.                 exit_program(1);  
  71.         }  
  72.     }  
  73.   
  74.     //关闭所有编码器  
  75.     for (i = 0; i < nb_output_streams; i++) {  
  76.         ost = output_streams[i];  
  77.         if (ost->encoding_needed) {  
  78.             av_freep(&ost->enc_ctx->stats_in);  
  79.         }  
  80.         total_packets_written += ost->packets_written;  
  81.     }  
  82.   
  83.     if (!total_packets_written && (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT)) {  
  84.         av_log(NULL, AV_LOG_FATAL, "Empty output\n");  
  85.         exit_program(1);  
  86.     }  
  87.   
  88.     //关闭所有解码器  
  89.     for (i = 0; i < nb_input_streams; i++) {  
  90.         ist = input_streams[i];  
  91.         if (ist->decoding_needed) {  
  92.             avcodec_close(ist->dec_ctx);  
  93.             if (ist->hwaccel_uninit)  
  94.                 ist->hwaccel_uninit(ist->dec_ctx);  
  95.         }  
  96.     }  
  97.   
  98.     //省略最后的释放内存  
  99.     return ret;  
  100. }  

好了,使用FFmpeg进行音频剪切、混音、拼接与转码介绍完毕。如果各位有什么问题或者建议,欢迎交流。

源码:https://github.com/xufuji456/FFmpegAndroid。如果对您有帮助,麻烦fork和star。