当前位置: 首页 > news >正文

如何将MP3或WAV文件解码成PCM文件

文章目录

    • 概要
    • 整体架构流程
    • 技术细节

概要

本文介绍使用 FFmpeg,将MP3或WAV文件解码成PCM文件的方法。

整体架构流程

首先,使用的 FFmpeg 库要支持 MP3/WAV 解码功能,即编译的时候要加上(编译 FFmpeg 库可以参考:Windows编译和使用ffmpeg):

--enable-decoder=mp3float --enable-decoder=pcm_s16le --enable-demuxer=mp3 --enable-demuxer=wav

下面的函数就是利用 FFmpeg 接口,实现将 MP3 或 WAV 文件解码成PCM文件:

#ifdef __cplusplus
extern "C" {
#endif
#include "libavutil/imgutils.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
#ifndef __linux__
#include "libswscale/swscale.h"
#endif
#include "libavutil/opt.h"
#ifdef __cplusplus
}
#endif#pragma comment(lib, "libavcodec.a")
#pragma comment(lib, "libavformat.a")
#pragma comment(lib, "libavutil.a")
#pragma comment(lib, "libswresample.a")int DecodedAudioFile(const char *pFileName) {FILE *pFile = fopen("tmp.pcm", "wb");  // 输出文件AVFormatContext *pFormatCtx;AVCodecContext *pCodecCtx;AVCodec *pCodec;AVPacket *packet;AVFrame *pFrame;struct SwrContext *au_convert_ctx = NULL;av_register_all();avformat_network_init();pFormatCtx = avformat_alloc_context();if (avformat_open_input(&pFormatCtx, pFileName, NULL, NULL) != 0) {OutputDebugStringA("Couldn't open input stream.\n");return -1;}if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {OutputDebugStringA("Couldn't find stream information.\n");return -1;}av_dump_format(pFormatCtx, 0, pFileName, false);// Find the first audio streamint audioStream = -1;for (int i = 0; i < pFormatCtx->nb_streams; i++) {if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {audioStream = i;break;}}if (audioStream == -1) {OutputDebugStringA("Didn't find a audio stream.\n");return -1;}// Get a pointer to the codec context for the audio streampCodecCtx = pFormatCtx->streams[audioStream]->codec;// Find the decoder for the audio streampCodec = avcodec_find_decoder(pCodecCtx->codec_id);if (pCodec == NULL) {OutputDebugStringA("Codec not found.\n");return -1;}// Open codecif (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {OutputDebugStringA("Could not open codec.\n");return -1;}int64_t in_channel_layout = av_get_default_channel_layout(pCodecCtx->channels);packet = (AVPacket*)av_malloc(sizeof(AVPacket));av_init_packet(packet);// Out Audio Paramuint64_t out_channel_layout = in_channel_layout;int out_nb_samples = pCodecCtx->frame_size;AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;int out_sample_rate = pCodecCtx->sample_rate;int out_channels = pCodecCtx->channels;// Out Buffer Sizeint out_buffer_size = av_samples_get_buffer_size(NULL, out_channels, out_nb_samples, out_sample_fmt, 1);uint8_t *out_buffer = (uint8_t*)av_malloc(MAX_AUDIO_FRAME_SIZE * 2);pFrame = av_frame_alloc();// 如果输入文件格式不是AV_SAMPLE_FMT_S16才需要if (pCodecCtx->sample_fmt != AV_SAMPLE_FMT_S16) {au_convert_ctx = swr_alloc();au_convert_ctx = swr_alloc_set_opts(au_convert_ctx, out_channel_layout, out_sample_fmt, out_sample_rate,in_channel_layout, pCodecCtx->sample_fmt, pCodecCtx->sample_rate, 0, NULL);swr_init(au_convert_ctx);}while (av_read_frame(pFormatCtx, packet) >= 0) {if (packet->stream_index == audioStream) {int got_picture;int ret = avcodec_decode_audio4(pCodecCtx, pFrame, &got_picture, packet);if (ret < 0) {OutputDebugStringA("Error in decoding audio frame.\n");return -1;}if (got_picture > 0) {if (au_convert_ctx) {  // MP3文件格式通常是AV_SAMPLE_FMT_FLTP,要重采样、格式转换等swr_convert(au_convert_ctx, &out_buffer, MAX_AUDIO_FRAME_SIZE, (const uint8_t**)pFrame->data, pFrame->nb_samples);// Write PCMfwrite(out_buffer, 1, out_buffer_size, pFile);} else {  // WAV文件格式通常是AV_SAMPLE_FMT_S16,与输出文件一致,直接保存fwrite(pFrame->data[0], 1, pFrame->nb_samples * pCodecCtx->channels * 2, pFile);}}}av_free_packet(packet);}swr_free(&au_convert_ctx);fclose(pFile);av_free(out_buffer);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);return 0;
}int main() {DecodedAudioFile("test.mp3");DecodedAudioFile("test.wav");return 0;
}

技术细节

WAV 文件通常是未压缩的 PCM 音频,解码的步骤与压缩格式(如 MP3)有所不同。在解码 WAV 文件时,解码器可能会直接输出 PCM 数据,而不是像 MP3 那样的压缩数据。对于 WAV 文件,如果格式是 AV_SAMPLE_FMT_S16,则不需要使用 swr_convert,因为音频数据已经是 PCM 格式,可以直接写入文件。例如上述代码中,对文件格式的检查:

// ...
// 如果输入文件格式不是AV_SAMPLE_FMT_S16才需要
if (pCodecCtx->sample_fmt != AV_SAMPLE_FMT_S16) {au_convert_ctx = swr_alloc();au_convert_ctx = swr_alloc_set_opts(au_convert_ctx, out_channel_layout, out_sample_fmt, out_sample_rate,in_channel_layout, pCodecCtx->sample_fmt, pCodecCtx->sample_rate, 0, NULL);swr_init(au_convert_ctx);
}
// ...
if (au_convert_ctx) {  // MP3文件格式通常是AV_SAMPLE_FMT_FLTP,要重采样、格式转换等swr_convert(au_convert_ctx, &out_buffer, MAX_AUDIO_FRAME_SIZE, (const uint8_t**)pFrame->data, pFrame->nb_samples);// Write PCMfwrite(out_buffer, 1, out_buffer_size, pFile);
} else {  // WAV文件格式通常是AV_SAMPLE_FMT_S16,与输出文件一致,直接保存fwrite(pFrame->data[0], 1, pFrame->nb_samples * pCodecCtx->channels * 2, pFile);
}
// ...
http://www.lryc.cn/news/403043.html

相关文章:

  • OpenAI 推出 GPT-4o mini,一种更小、更便宜的人工智能模型
  • Nacos 服务发现(订阅)源码分析(服务端)
  • DICOM CT\MR片子免费在线查看工具;python pydicom包加载查看;mayavi 3d查看
  • VSCode远程连接Ubuntu/Linux
  • 【Nginx80端口被占用】80端口被System占用如何解决【已解决】
  • 云计算的发展历程与边缘计算
  • 199.二叉树的右视图(DFS)
  • 机器学习基础入门(1)
  • mybatis的xml中,where标签不自动删除多余的and之类的问题
  • RK3588 编译opencvopencv_contrib记录
  • Eureka: 微服务架构中的服务发现与注册实践
  • 8、添加第三方包
  • 【算法】算法模板
  • 特征工程方法总结
  • Unity | AssetBundle
  • 【虚幻引擎】C++网络通信TCP和HTTP实战开发全流程,以接入科大讯飞星火大模型和文心一言千帆大模型为案例讲解
  • .NET单元测试使用AutoFixture按需填充的方法总结
  • 求职学习day5
  • 微服务常用的中间件有哪些?都有什么用途?
  • 华为云认证
  • 【Linux学习】常用基本指令
  • windows上安装Apache
  • wps office 2019 Pro Plus 集成序列号Vba安装版教程
  • 院内影像一体化平台PACS源码,C#语言的PACS/RIS系统,二级医院应用案例
  • 基于java的设计模式学习
  • 组合数学+费用背包+刷表,G2 - Playlist for Polycarp (hard version)
  • 阿尔泰科技利用485模块搭建自动灌溉系统实现远程控制
  • Python正则表达式中的分组
  • openstack设置IP直接登录,不需要加dashboard后缀
  • PHP宠物店萌宠小程序系统源码