解码大师揭秘:Android平台下FFmpeg与Lame音频处理的秘密

引言
在Android应用开发中,音频处理是一个常见的需求。FFmpeg是一个强大的多媒体处理工具,而Lame是一个广泛使用的MP3编码库。本文将揭秘Android平台下如何使用FFmpeg和Lame进行音频处理,包括解码、编码、格式转换等操作。
FFmpeg简介
FFmpeg是一个开源项目,用于处理多媒体数据。它支持多种音频和视频格式,并提供了一系列的解码器、编码器、过滤器等工具。在Android平台上,我们可以通过JNI(Java Native Interface)调用FFmpeg库来处理音频。
Lame简介
Lame是一个开源的MP3编码库,它可以将多种音频格式编码为MP3格式。Lame库在Android平台上同样可以通过JNI进行调用。
安装FFmpeg和Lame库
首先,我们需要从FFmpeg官方网站下载FFmpeg源代码,并编译生成Android平台的库文件。以下是编译FFmpeg的步骤:
下载FFmpeg源代码。
创建一个Android NDK项目。
将FFmpeg源代码复制到NDK项目的
jni
目录下。
修改
CMakeLists.txt
和
Android.mk
文件,配置FFmpeg编译选项。
使用
ndk-build
命令编译FFmpeg库。
编译完成后,我们将在NDK项目的
libs
目录下找到生成的库文件。
编码音频
以下是一个使用FFmpeg和Lame库将音频编码为MP3格式的示例代码:
public native int encodeAudio(String inputPath, String outputPath);
extern “C” {
JNIEXPORT jint JNICALL Java_com_example_myapp_MainActivity_encodeAudio
(JNIEnv *env, jobject thiz, jstring inputPath, jstring outputPath) {
const char *inputPathC = env->GetStringUTFChars(inputPath, 0);
const char *outputPathC = env->GetStringUTFChars(outputPath, 0);

int ret = ffmpeg_encode(inputPathC, outputPathC);

env->ReleaseStringUTFChars(inputPath, inputPathC);
env->ReleaseStringUTFChars(outputPath, outputPathC);

return ret;
}
int ffmpeg_encode(const char *inputPath, const char *outputPath) {
// 初始化FFmpeg
av_register_all();

// 打开输入文件
AVFormatContext *inputFormatContext = avformat_alloc_context();
if (avformat_open_input(&inputFormatContext, inputPath, NULL, NULL) < 0) { return -1; } // 打开输出文件 AVFormatContext *outputFormatContext = avformat_alloc_context(); if (avformat_alloc_output_context2(&outputFormatContext, NULL, "mp3", outputPath) < 0) { return -1; } // 查找输入文件的流信息 if (avformat_find_stream_info(inputFormatContext, NULL) < 0) { return -1; } // 复制流信息到输出文件 for (unsigned int i = 0; i < inputFormatContext->nb_streams; i++) {
if (inputFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
avformat_new_stream(outputFormatContext, inputFormatContext->streams[i]->codecpar);
}
}

// 打开编码器
AVCodecContext *codecContext = avcodec_alloc_context3(NULL);
if (avcodec_parameters_to_context(codecContext, inputFormatContext->streams[0]->codecpar) < 0) { return -1; } AVCodec *codec = avcodec_find_encoder_by_name("libmp3lame"); if (!codec) { return -1; } if (avcodec_open2(codecContext, codec, NULL) < 0) { return -1; } // 编码音频 FILE *outputFile = fopen(outputPath, "wb"); if (!outputFile) { return -1; } AVPacket packet; AVFrame *frame = av_frame_alloc(); while (av_read_frame(inputFormatContext, &packet) >= 0) {
if (packet.stream_index == 0) {
int ret = avcodec_send_packet(codecContext, &packet);
if (ret < 0) { break; } while (ret >= 0) {
ret = avcodec_receive_frame(codecContext, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) { break; } // 写入音频数据到文件 fwrite(frame->data[0], 1, frame->linesize[0], outputFile);
}
}

av_packet_unref(&packet);
}

// 释放资源
fclose(outputFile);
av_frame_free(&frame);
avcodec_close(codecContext);
avcodec_free_context(&codecContext);
avformat_close_input(&inputFormatContext);
avformat_free_context(outputFormatContext);

return 0;
}
解码音频
以下是一个使用FFmpeg解码音频文件的示例代码:
public native int decodeAudio(String inputPath, String outputPath);
extern “C” {
JNIEXPORT jint JNICALL Java_com_example_myapp_MainActivity_decodeAudio
(JNIEnv *env, jobject thiz, jstring inputPath, jstring outputPath) {
const char *inputPathC = env->GetStringUTFChars(inputPath, 0);
const char *outputPathC = env->GetStringUTFChars(outputPath, 0);

int ret = ffmpeg_decode(inputPathC, outputPathC);

env->ReleaseStringUTFChars(inputPath, inputPathC);
env->ReleaseStringUTFChars(outputPath, outputPathC);

return ret;
}
int ffmpeg_decode(const char *inputPath, const char *outputPath) {
// 初始化FFmpeg
av_register_all();

// 打开输入文件
AVFormatContext *inputFormatContext = avformat_alloc_context();
if (avformat_open_input(&inputFormatContext, inputPath, NULL, NULL) < 0) { return -1; } // 查找输入文件的流信息 if (avformat_find_stream_info(inputFormatContext, NULL) < 0) { return -1; } // 查找音频流 int audioStreamIndex = -1; for (unsigned int i = 0; i < inputFormatContext->nb_streams; i++) {
if (inputFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStreamIndex = i;
break;
}
}

if (audioStreamIndex == -1) {
return -1;
}

// 打开解码器
AVCodecContext *codecContext = avcodec_alloc_context3(NULL);
if (avcodec_parameters_to_context(codecContext, inputFormatContext->streams[audioStreamIndex]->codecpar) < 0) { return -1; } AVCodec *codec = avcodec_find_decoder_by_name("libmp3lame"); if (!codec) { return -1; } if (avcodec_open2(codecContext, codec, NULL) < 0) { return -1; } // 解码音频 FILE *outputFile = fopen(outputPath, "wb"); if (!outputFile) { return -1; } AVPacket packet; AVFrame *frame = av_frame_alloc(); while (av_read_frame(inputFormatContext, &packet) >= 0) {
if (packet.stream_index == audioStreamIndex) {
int ret = avcodec_send_packet(codecContext, &packet);
if (ret < 0) { break; } while (ret >= 0) {
ret = avcodec_receive_frame(codecContext, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) { break; } // 写入解码后的音频数据到文件 fwrite(frame->data[0], 1, frame->linesize[0], outputFile);
}
}

av_packet_unref(&packet);
}

// 释放资源
fclose(outputFile);
av_frame_free(&frame);
avcodec_close(codecContext);
avcodec_free_context(&codecContext);
avformat_close_input(&inputFormatContext);

return 0;
}
格式转换
以下是一个使用FFmpeg将音频格式从WAV转换为MP3的示例代码:
public native int convertAudioFormat(String inputPath, String outputPath);
extern “C” {
JNIEXPORT jint JNICALL Java_com_example_myapp_MainActivity_convertAudioFormat
(JNIEnv *env, jobject thiz, jstring inputPath, jstring outputPath) {
const char *inputPathC = env->GetStringUTFChars(inputPath, 0);
const char *outputPathC = env->GetStringUTFChars(outputPath, 0);

int ret = ffmpeg_convert_audio_format(inputPathC, outputPathC);

env->ReleaseStringUTFChars(inputPath, inputPathC);
env->ReleaseStringUTFChars(outputPath, outputPathC);

return ret;
}
int ffmpeg_convert_audio_format(const char *inputPath, const char *outputPath) {
// 初始化FFmpeg
av_register_all();

// 打开输入文件
AVFormatContext *inputFormatContext = avformat_alloc_context();
if (avformat_open_input(&inputFormatContext, inputPath, NULL, NULL) < 0) { return -1; } // 查找输入文件的流信息 if (avformat_find_stream_info(inputFormatContext, NULL) < 0) { return -1; } // 查找音频流 int audioStreamIndex = -1; for (unsigned int i = 0; i < inputFormatContext->nb_streams; i++) {
if (inputFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStreamIndex = i;
break;
}
}

if (audioStreamIndex == -1) {
return -1;
}

// 打开解码器
AVCodecContext *decoderContext = avcodec_alloc_context3(NULL);
if (avcodec_parameters_to_context(decoderContext, inputFormatContext->streams[audioStreamIndex]->codecpar) < 0) { return -1; } AVCodec *decoderCodec = avcodec_find_decoder_by_name("libmp3lame"); if (!decoderCodec) { return -1; } if (avcodec_open2(decoderContext, decoderCodec, NULL) < 0) { return -1; } // 打开编码器 AVCodecContext *encoderContext = avcodec_alloc_context3(NULL); if (avcodec_parameters_to_context(encoderContext, decoderContext->codecpar) < 0) { return -1; } AVCodec *encoderCodec = avcodec_find_encoder_by_name("libmp3lame"); if (!encoderCodec) { return -1; } if (avcodec_open2(encoderContext, encoderCodec, NULL) < 0) { return -1; } // 编码音频 FILE *outputFile = fopen(outputPath, "wb"); if (!outputFile) { return -1; } AVPacket packet; AVFrame *frame = av_frame_alloc(); while (av_read_frame(inputFormatContext, &packet) >= 0) {
if (packet.stream_index == audioStreamIndex) {
int ret = avcodec_send_packet(decoderContext, &packet);
if (ret < 0) { break; } while (ret >= 0) {
ret = avcodec_receive_frame(decoderContext, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) { break; } // 编码音频数据 avcodec_send_frame(encoderContext, frame); while (ret >= 0) {
ret = avcodec_receive_packet(encoderContext, &packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) { break; } // 写入编码后的音频数据到文件 fwrite(packet.data, 1, packet.size, outputFile); av_packet_unref(&packet); } } } av_packet_unref(&packet); } // 释放资源 fclose(outputFile); av_frame_free(&frame); avcodec_close(decoderContext); avcodec_free_context(&decoderContext); avcodec_close(encoderContext); avcodec_free_context(&encoderContext); avformat_close_input(&inputFormatContext); return 0; } 总结 通过本文的介绍,我们了解了在Android平台下如何使用FFmpeg和Lame库进行音频处理。通过JNI调用FFmpeg库,我们可以实现音频的解码、编码、格式转换等功能。这些功能在Android应用开发中非常有用,可以帮助我们处理各种音频需求。