美文网首页
FFmpeg音频播放器(4)-将mp3解码成pcm

FFmpeg音频播放器(4)-将mp3解码成pcm

作者: 星星y | 来源:发表于2019-01-22 10:14 被阅读0次

    FFmpeg音频播放器(1)-简介
    FFmpeg音频播放器(2)-编译动态库
    FFmpeg音频播放器(3)-将FFmpeg加入到Android中
    FFmpeg音频播放器(4)-将mp3解码成pcm
    FFmpeg音频播放器(5)-单输入filter(volume,atempo)
    FFmpeg音频播放器(6)-多输入filter(amix)
    FFmpeg音频播放器(7)-使用OpenSLES播放音频
    FFmpeg音频播放器(8)-创建FFmpeg播放器
    FFmpeg音频播放器(9)-播放控制
    FFmpeg的第一个强大之处是它的编解码能力。它可以将市面上的任意一种音频格式(mp3,wav,aac,ogg等)和视频格式(mp4,avi,rm,rmvb,mov等)解码。通过解码器将音频视频解码成一个个AVFrame,每个frame包含了音频的pcm信息或视频的yuv信息。通过编码器,FFmpeg有可将frame编码成不同格式的音视频文件。因此我们可以用FFmpeg很简单的实现格式转换,而不需要了解各种格式的相关协议。
    下面开始用FFmpeg库通过代码方式实现音频解码
    大致的解码流程如下

    av_register_all();//注册所有codec和muxers, demuxers,protocols
            ⬇️
    avformat_alloc_context();//AVFormatContext初始化
            ⬇️
    avformat_open_input();//打开文件
            ⬇️
    avformat_find_stream_info();//获取文件流信息
            ⬇️
    获取音频索引audio_stream_index
            ⬇️
    avcodec_find_decoder();//根据audio_stream_index获取解码器
            ⬇️
    while (av_read_frame(fmtCtx, packet) >= 0);//分配AVPacket内存,循环读入packet
            ⬇️
    avcodec_decode_audio4();//将packet解码成AVFrame
            ⬇️
         fwrite();//将frame中的pcm数据写入文件
            ⬇️
       释放相关资源
    

    完整代码如下
    在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内存,用于接收音频数据,解码数据
        AVPacket *packet = av_packet_alloc();
        AVFrame *frame = av_frame_alloc();
        int got_frame;//接收解码结果
        int index = 0;
        //pcm输出文件
        FILE *out_file = fopen(out, "wb");
        while (av_read_frame(fmt_ctx, packet) == 0) {//将音频数据读入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数据写入文件
    
                }
            }
        }
        LOGI("decode finish...");
        //释放资源
        av_packet_unref(packet);
        av_frame_free(&frame);
        avcodec_close(codec_ctx);
        avformat_close_input(&fmt_ctx);
        fclose(out_file);
    }
    

    注意添加文件权限,将测试音频test1.mp3放入手机sd卡中,点击解码按钮,完成后,我们就可以看到pcm文件了,可以通过Audition打开(mac下可以通过Parallels Desktop装xp使用软件,融合模式不要太好用),选择48000hz,1声道(只写入了一个声道),打开后,就可以通过Audition查看和播放pcm文件了。

    Adobe Audition CS6打开pcm文件
    项目地址

    相关文章

      网友评论

          本文标题:FFmpeg音频播放器(4)-将mp3解码成pcm

          本文链接:https://www.haomeiwen.com/subject/ltnpdqtx.html