接触FFmpeg有一段时间了,它是音视频开发的开源库,几乎其他所有播放器、直播平台都基于FFmpeg进行二次开发。本篇文章来总结下采用FFmpeg进行音频处理:音频混合、音频剪切、音频拼接与音频转码。
采用android studio进行开发,配置build.gradle文件:
- defaultConfig {
- ......
- externalNativeBuild {
- cmake {
- cppFlags ""
- }
- }
- ndk {
- abiFilters "armeabi-v7a"
- }
- }
另外指定cmake文件路径:
- externalNativeBuild {
- cmake {
- path "CMakeLists.txt"
- }
- }
- sourceSets {
- main {
- jniLibs.srcDirs = ['libs']
- jni.srcDirs = []
- }
- }
从FFmpeg官网下载源码,编译成ffmpeg.so动态库,并且导入相关源文件与头文件:
然后配置cMakeLists文件:
- add_library( # Sets the name of the library.
- audio-handle
-
- # Sets the library as a shared library.
- SHARED
-
- # Provides a relative path to your source file(s).
- src/main/cpp/ffmpeg_cmd.c
- src/main/cpp/cmdutils.c
- src/main/cpp/ffmpeg.c
- src/main/cpp/ffmpeg_filter.c
- src/main/cpp/ffmpeg_opt.c)
-
- add_library( ffmpeg
- SHARED
- IMPORTED )
- set_target_properties( ffmpeg
- PROPERTIES IMPORTED_LOCATION
- ../../../../libs/armeabi-v7a/libffmpeg.so )
-
- include_directories(src/main/cpp/include)
-
- find_library( log-lib
- log )
-
- target_link_libraries( audio-handle
- ffmpeg
- ${log-lib} )
调用FFmpeg命令行进行音频处理:
-
-
-
-
- private void doHandleAudio(int handleType){
- String[] commandLine = null;
- switch (handleType){
- case 0:
- String transformFile = PATH + File.separator + "transform.aac";
- commandLine = FFmpegUtil.transformAudio(srcFile, transformFile);
- break;
- case 1:
- String cutFile = PATH + File.separator + "cut.mp3";
- commandLine = FFmpegUtil.cutAudio(srcFile, 10, 15, cutFile);
- break;
- case 2:
- String concatFile = PATH + File.separator + "concat.mp3";
- commandLine = FFmpegUtil.concatAudio(srcFile, appendFile, concatFile);
- break;
- case 3:
- String mixFile = PATH + File.separator + "mix.aac";
- commandLine = FFmpegUtil.mixAudio(srcFile, appendFile, mixFile);
- break;
- default:
- break;
- }
- executeFFmpegCmd(commandLine);
- }
其中,音频混音、合并、剪切和转码的FFmpeg命令行的拼接如下:
-
-
-
-
-
-
- public static String[] transformAudio(String srcFile, String targetFile){
- String transformAudioCmd = "ffmpeg -i %s %s";
- transformAudioCmd = String.format(transformAudioCmd, srcFile, targetFile);
- return transformAudioCmd.split(" ");
- }
-
-
-
-
-
-
-
-
-
- @SuppressLint("DefaultLocale")
- public static String[] cutAudio(String srcFile, int startTime, int duration, String targetFile){
- String cutAudioCmd = "ffmpeg -i %s -ss %d -t %d %s";
- cutAudioCmd = String.format(cutAudioCmd, srcFile, startTime, duration, targetFile);
- return cutAudioCmd.split(" ");
- }
-
-
-
-
-
-
-
-
- public static String[] concatAudio(String srcFile, String appendFile, String targetFile){
- String concatAudioCmd = "ffmpeg -i concat:%s|%s -acodec copy %s";
- concatAudioCmd = String.format(concatAudioCmd, srcFile, appendFile, targetFile);
- return concatAudioCmd.split(" ");
- }
-
-
-
-
-
-
-
-
- public static String[] mixAudio(String srcFile, String mixFile, String targetFile){
- String mixAudioCmd = "ffmpeg -i %s -i %s -filter_complex amix=inputs=2:duration=first -strict -2 %s";
- mixAudioCmd = String.format(mixAudioCmd, srcFile, mixFile, targetFile);
- return mixAudioCmd.split(" ");
- }
FFmpeg处理混音的公式如下,其中sample1为源文件采样率、sample2为待混合文件采样率:
混音公式:value = sample1 + sample2 - (sample1 * sample2 / (pow(2, 16-1) - 1))
开启子线程,调用native方法进行音频处理:
- public static void execute(final String[] commands, final OnHandleListener onHandleListener){
- new Thread(new Runnable() {
- @Override
- public void run() {
- if(onHandleListener != null){
- onHandleListener.onBegin();
- }
-
- int result = handle(commands);
- if(onHandleListener != null){
- onHandleListener.onEnd(result);
- }
- }
- }).start();
- }
- private native static int handle(String[] commands);
关键的native方法,是把java传入的字符串数组转成二级指针数组,然后调用FFmpeg源码中的run方法:
- JNIEXPORT jint JNICALL Java_com_frank_ffmpeg_FFmpegCmd_handle
- (JNIEnv *env, jclass obj, jobjectArray commands){
- int argc = (*env)->GetArrayLength(env, commands);
- char **argv = (char**)malloc(argc * sizeof(char*));
- int i;
- int result;
- for (i = 0; i < argc; i++) {
- jstring jstr = (jstring) (*env)->GetObjectArrayElement(env, commands, i);
- char* temp = (char*) (*env)->GetStringUTFChars(env, jstr, 0);
- argv[i] = malloc(1024);
- strcpy(argv[i], temp);
- (*env)->ReleaseStringUTFChars(env, jstr, temp);
- }
-
- result = run(argc, argv);
-
- for (i = 0; i < argc; i++) {
- free(argv[i]);
- }
- free(argv);
- return result;
- }
关于FFmpeg的run方法的源码如下,中间有部分省略:
- int run(int argc, char **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();
-
-
- int ret = ffmpeg_parse_options(argc, argv);
- if (ret < 0)
- exit_program(1);
-
-
- if (transcode() < 0)
- exit_program(1);
-
-
- exit_program(received_nb_signals ? 255 : main_return_code);
- ffmpeg_cleanup(0);
- }
其中,最关键的是文件转换部分,源码如下:
- static int transcode(void)
- {
- int ret, i;
- AVFormatContext *os;
- OutputStream *ost;
- InputStream *ist;
- int64_t timer_start;
- int64_t total_packets_written = 0;
-
- ret = transcode_init();
- if (ret < 0)
- goto fail;
-
- if (stdin_interaction) {
- av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");
- }
- timer_start = av_gettime_relative();
-
- #if HAVE_PTHREADS
- if ((ret = init_input_threads()) < 0)
- goto fail;
- #endif
-
- while (!received_sigterm) {
- int64_t cur_time= av_gettime_relative();
-
-
- if (stdin_interaction)
- if (check_keyboard_interaction(cur_time) < 0)
- break;
-
-
- if (!need_output()) {
- av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
- break;
- }
-
- ret = transcode_step();
- if (ret < 0 && ret != AVERROR_EOF) {
- char errbuf[128];
- av_strerror(ret, errbuf, sizeof(errbuf));
-
- av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", errbuf);
- break;
- }
-
-
- print_report(0, timer_start, cur_time);
- }
- #if HAVE_PTHREADS
- free_input_threads();
- #endif
-
-
- for (i = 0; i < nb_input_streams; i++) {
- ist = input_streams[i];
- if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {
- process_input_packet(ist, NULL, 0);
- }
- }
- flush_encoders();
- term_exit();
-
-
- for (i = 0; i < nb_output_files; i++) {
- os = output_files[i]->ctx;
- if ((ret = av_write_trailer(os)) < 0) {
- av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s", os->filename, av_err2str(ret));
- if (exit_on_error)
- exit_program(1);
- }
- }
-
-
- for (i = 0; i < nb_output_streams; i++) {
- ost = output_streams[i];
- if (ost->encoding_needed) {
- av_freep(&ost->enc_ctx->stats_in);
- }
- total_packets_written += ost->packets_written;
- }
-
- if (!total_packets_written && (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT)) {
- av_log(NULL, AV_LOG_FATAL, "Empty output\n");
- exit_program(1);
- }
-
-
- for (i = 0; i < nb_input_streams; i++) {
- ist = input_streams[i];
- if (ist->decoding_needed) {
- avcodec_close(ist->dec_ctx);
- if (ist->hwaccel_uninit)
- ist->hwaccel_uninit(ist->dec_ctx);
- }
- }
-
-
- return ret;
- }
好了,使用FFmpeg进行音频剪切、混音、拼接与转码介绍完毕。如果各位有什么问题或者建议,欢迎交流。
源码:https://github.com/xufuji456/FFmpegAndroid。如果对您有帮助,麻烦fork和star。