FFmpeg音頻播放器(1)-簡介
FFmpeg音頻播放器(2)-編譯動態(tài)庫
FFmpeg音頻播放器(3)-將FFmpeg加入到Android中
FFmpeg音頻播放器(4)-將mp3解碼成pcm
FFmpeg音頻播放器(5)-單輸入filter(volume,atempo)
FFmpeg音頻播放器(6)-多輸入filter(amix)
FFmpeg音頻播放器(7)-使用OpenSLES播放音頻
FFmpeg音頻播放器(8)-創(chuàng)建FFmpeg播放器
FFmpeg音頻播放器(9)-播放控制
FFmpeg的第一個強大之處是它的編解碼能力嘿棘。它可以將市面上的任意一種音頻格式(mp3,wav,aac,ogg等)和視頻格式(mp4,avi,rm,rmvb,mov等)解碼。通過解碼器將音頻視頻解碼成一個個AVFrame彰居,每個frame包含了音頻的pcm信息或視頻的yuv信息钓瞭。通過編碼器瓢姻,F(xiàn)Fmpeg有可將frame編碼成不同格式的音視頻文件墩莫。因此我們可以用FFmpeg很簡單的實現(xiàn)格式轉(zhuǎn)換翅敌,而不需要了解各種格式的相關(guān)協(xié)議羞福。
下面開始用FFmpeg庫通過代碼方式實現(xiàn)音頻解碼
大致的解碼流程如下
av_register_all();//注冊所有codec和muxers, demuxers,protocols
??
avformat_alloc_context();//AVFormatContext初始化
??
avformat_open_input();//打開文件
??
avformat_find_stream_info();//獲取文件流信息
??
獲取音頻索引audio_stream_index
??
avcodec_find_decoder();//根據(jù)audio_stream_index獲取解碼器
??
while (av_read_frame(fmtCtx, packet) >= 0)蚯涮;//分配AVPacket內(nèi)存治专,循環(huán)讀入packet
??
avcodec_decode_audio4();//將packet解碼成AVFrame
??
fwrite();//將frame中的pcm數(shù)據(jù)寫入文件
??
釋放相關(guān)資源
完整代碼如下
在MainActivity.kt中引入so庫
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun decodeAudio(v: View) {
val src = "${Environment.getExternalStorageDirectory()}/test1.mp3"
val out = "${Environment.getExternalStorageDirectory()}/out.pcm"
decodeAudio(src, out)
}
external fun decodeAudio(src: String, out: String)
companion object {
init {
System.loadLibrary("avutil-55")
System.loadLibrary("swresample-2")
System.loadLibrary("avcodec-57")
System.loadLibrary("avfilter-6")
System.loadLibrary("swscale-4")
System.loadLibrary("avformat-57")
System.loadLibrary("native-lib")
}
}
}
在native-lib.cpp中編寫音頻解碼代碼
#include <jni.h>
#include <android/log.h>
#include <string>
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswresample/swresample.h>
}
#define LOGI(FORMAT, ...) __android_log_print(ANDROID_LOG_INFO,"FFmpegAudioPlayer",FORMAT,##__VA_ARGS__);
#define LOGE(FORMAT, ...) __android_log_print(ANDROID_LOG_ERROR,"FFmpegAudioPlayer",FORMAT,##__VA_ARGS__);
extern "C" JNIEXPORT void
JNICALL
Java_io_github_iamyours_ffmpegaudioplayer_MainActivity_decodeAudio(
JNIEnv *env,
jobject /* this */, jstring _src, jstring _out) {
const char *src = env->GetStringUTFChars(_src, 0);
const char *out = env->GetStringUTFChars(_out, 0);
av_register_all();//注冊所有容器解碼器
AVFormatContext *fmt_ctx = avformat_alloc_context();
if (avformat_open_input(&fmt_ctx, src, NULL, NULL) < 0) {//打開文件
LOGE("open file error");
return;
}
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {//讀取音頻格式文件信息
LOGE("find stream info error");
return;
}
//獲取音頻索引
int audio_stream_index = -1;
for (int i = 0; i < fmt_ctx->nb_streams; i++) {
if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audio_stream_index = i;
LOGI("find audio stream index");
break;
}
}
//獲取解碼器
AVCodecContext *codec_ctx = avcodec_alloc_context3(NULL);
avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[audio_stream_index]->codecpar);
AVCodec *codec = avcodec_find_decoder(codec_ctx->codec_id);
//打開解碼器
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
LOGE("could not open codec");
return;
}
//分配AVPacket和AVFrame內(nèi)存卖陵,用于接收音頻數(shù)據(jù),解碼數(shù)據(jù)
AVPacket *packet = av_packet_alloc();
AVFrame *frame = av_frame_alloc();
int got_frame;//接收解碼結(jié)果
int index = 0;
//pcm輸出文件
FILE *out_file = fopen(out, "wb");
while (av_read_frame(fmt_ctx, packet) == 0) {//將音頻數(shù)據(jù)讀入packet
if (packet->stream_index == audio_stream_index) {//取音頻索引packet
if (avcodec_decode_audio4(codec_ctx, frame, &got_frame, packet) <
0) {//將packet解碼成AVFrame
LOGE("decode error:%d", index);
break;
}
if (got_frame > 0) {
LOGI("decode frame:%d", index++);
fwrite(frame->data[0], 1, static_cast<size_t>(frame->linesize[0]),
out_file); //想將單個聲道pcm數(shù)據(jù)寫入文件
}
}
}
LOGI("decode finish...");
//釋放資源
av_packet_unref(packet);
av_frame_free(&frame);
avcodec_close(codec_ctx);
avformat_close_input(&fmt_ctx);
fclose(out_file);
}
注意添加文件權(quán)限张峰,將測試音頻test1.mp3放入手機sd卡中泪蔫,點擊解碼按鈕,完成后喘批,我們就可以看到pcm文件了撩荣,可以通過Audition打開(mac下可以通過Parallels Desktop裝xp使用軟件,融合模式不要太好用)饶深,選擇48000hz餐曹,1聲道(只寫入了一個聲道),打開后敌厘,就可以通過Audition查看和播放pcm文件了台猴。
項目地址