FFmpeg音頻播放器(4)-將mp3解碼成pcm

FFmpeg音頻播放器(1)-簡(jiǎn)介
FFmpeg音頻播放器(2)-編譯動(dòng)態(tài)庫(kù)
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的第一個(gè)強(qiáng)大之處是它的編解碼能力。它可以將市面上的任意一種音頻格式(mp3,wav,aac,ogg等)和視頻格式(mp4,avi,rm,rmvb,mov等)解碼。通過(guò)解碼器將音頻視頻解碼成一個(gè)個(gè)AVFrame,每個(gè)frame包含了音頻的pcm信息或視頻的yuv信息。通過(guò)編碼器,F(xiàn)Fmpeg有可將frame編碼成不同格式的音視頻文件。因此我們可以用FFmpeg很簡(jiǎn)單的實(shí)現(xiàn)格式轉(zhuǎn)換,而不需要了解各種格式的相關(guān)協(xié)議。
下面開(kāi)始用FFmpeg庫(kù)通過(guò)代碼方式實(shí)現(xiàn)音頻解碼
大致的解碼流程如下

av_register_all();//注冊(cè)所有codec和muxers, demuxers,protocols
        ??
avformat_alloc_context();//AVFormatContext初始化
        ??
avformat_open_input();//打開(kāi)文件
        ??
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ù)寫(xiě)入文件
        ??
   釋放相關(guān)資源

完整代碼如下
在MainActivity.kt中引入so庫(kù)

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中編寫(xiě)音頻解碼代碼

#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();//注冊(cè)所有容器解碼器
    AVFormatContext *fmt_ctx = avformat_alloc_context();

    if (avformat_open_input(&fmt_ctx, src, NULL, NULL) < 0) {//打開(kāi)文件
        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);
    //打開(kāi)解碼器
    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); //想將單個(gè)聲道pcm數(shù)據(jù)寫(xiě)入文件

            }
        }
    }
    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)限,將測(cè)試音頻test1.mp3放入手機(jī)sd卡中,點(diǎn)擊解碼按鈕,完成后,我們就可以看到pcm文件了,可以通過(guò)Audition打開(kāi)(mac下可以通過(guò)Parallels Desktop裝xp使用軟件,融合模式不要太好用),選擇48000hz,1聲道(只寫(xiě)入了一個(gè)聲道),打開(kāi)后,就可以通過(guò)Audition查看和播放pcm文件了。

Adobe Audition CS6打開(kāi)pcm文件

項(xiàng)目地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容