ffmpeg把pcm封装为wav
创始人
2024-12-17 06:36:18
0

note

1.wav格式中,音频数据未经过压缩,直接封装即可

2.对于编码器的选择,应选择和pcm裸数据一致的编码器(本次实际不须编码)

version

#define LIBSWRESAMPLE_VERSION_MAJOR 2

#define LIBSWRESAMPLE_VERSION_MINOR 9

#define LIBSWRESAMPLE_VERSION_MICRO 100

#define LIBAVCODEC_VERSION_MINOR 31

#define LIBAVCODEC_VERSION_MICRO 102

code

void CFfmpegOps::MuxPCMToWAV(const char* pcm_file, const char* wav_file) {     const AVOutputFormat* out_fmt = nullptr;     AVFormatContext* fmt_ctx = nullptr;     const AVCodec* codec = nullptr;     AVCodecContext* codec_ctx = nullptr;     AVStream* avstream = nullptr;     AVPacket* avpacket = nullptr;     AVFrame* avframe = nullptr;     int ret = -1;     FILE* in_fp = nullptr;     int sample_fmt_is_planar = 0;     size_t n = 0;     int frame_data_bytes = 0;      codec = avcodec_find_encoder(AV_CODEC_ID_PCM_F32LE);     if (!codec)     {         printf("avcodec_find_encoder error\n");         goto END;     }      codec_ctx = avcodec_alloc_context3(codec);     if (!codec_ctx)     {         printf("avcodec_alloc_context3 error\n");         goto END;     }     codec_ctx->sample_fmt = AV_SAMPLE_FMT_FLT;  // f32le     codec_ctx->sample_rate = 44100;     codec_ctx->channel_layout = AV_CH_LAYOUT_STEREO;     codec_ctx->channels = av_get_channel_layout_nb_channels(codec_ctx->channel_layout);      ret = avcodec_open2(codec_ctx, codec, nullptr);     if (ret < 0)     {         printf("avcodec_open2 error(%s)\n", GetFfmpegERR(ret));         goto END;     }     printf("frame_size:%d\n", codec_ctx->frame_size);      avframe = av_frame_alloc();     if (!avframe)     {         printf("av_frame_alloc error\n");         goto END;     }     avframe->format = AV_SAMPLE_FMT_FLT;     avframe->sample_rate = 44100;     avframe->channel_layout = AV_CH_LAYOUT_STEREO;     avframe->channels = av_get_channel_layout_nb_channels(avframe->channel_layout);     avframe->nb_samples = 1024;      ret = av_frame_get_buffer(avframe, 0);     if (ret < 0)     {         printf("av_frame_get_buffer error(%s)\n", GetFfmpegERR(ret));         goto END;     }      ret = av_samples_get_buffer_size(nullptr,                                      avframe->channels,                                     avframe->nb_samples,                                     (AVSampleFormat)(avframe->format),                                     1);     if (ret < 0)     {         printf("av_samples_get_buffer_size error(%s)\n", GetFfmpegERR(ret));         goto END;     }     frame_data_bytes = ret;      avpacket = av_packet_alloc();     if (!avpacket)     {         printf("av_packet_alloc error\n");         goto END;     }      in_fp = fopen(pcm_file, "rb");     if (!in_fp)     {         printf("fopen error\n");         goto END;     }      ret = avformat_alloc_output_context2(&fmt_ctx, nullptr, nullptr, wav_file);     if (ret < 0)     {         printf("avformat_alloc_output_context2 error(%s)\n", GetFfmpegERR(ret));         goto END;     }     out_fmt = fmt_ctx->oformat;      avstream = avformat_new_stream(fmt_ctx, codec);     if (!avstream)     {         printf("avformat_new_stream error\n");         goto END;     }      ret = avcodec_parameters_from_context(avstream->codecpar, codec_ctx);     if (ret < 0)     {         printf("avcodec_parameters_from_context error(%s)\n", GetFfmpegERR(ret));         goto END;     }      ret = avio_open(&(fmt_ctx->pb), wav_file, AVIO_FLAG_READ_WRITE);     if (ret < 0)     {         printf("avio_open error(%s)\n", GetFfmpegERR(ret));         goto END;     }      ret = avformat_write_header(fmt_ctx, nullptr);     if (ret < 0)     {         printf("avformat_write_header error(%s)\n", GetFfmpegERR(ret));         goto END;     }      while (1)     {         if (feof(in_fp))         {             break;         }          sample_fmt_is_planar = av_sample_fmt_is_planar((AVSampleFormat)(avframe->format));         if (sample_fmt_is_planar)         {             n = fread(avframe->data[0], sizeof(uint8_t), frame_data_bytes / 2, in_fp);             if ((int)n != (frame_data_bytes / 2))             {                 printf("n != (frame_data_bytes / 2)\n");             }              n = fread(avframe->data[1], sizeof(uint8_t), frame_data_bytes / 2, in_fp);             if ((int)n != (frame_data_bytes / 2))             {                 printf("n != (frame_data_bytes / 2)\n");             }         }         else         {             n = fread(avframe->data[0], sizeof(uint8_t), frame_data_bytes, in_fp);             if ((int)n != frame_data_bytes)             {                 printf("n != (frame_data_bytes)\n");             }         }          ret = avcodec_send_frame(codec_ctx, avframe);         if (ret < 0)         {             if (ret == AVERROR(EAGAIN))             {                 printf("read output first\n");             }             else             {                 printf("avcodec_send_frame error(%s)\n", GetFfmpegERR(ret));                 break;             }         }          while (1)         {             ret = avcodec_receive_packet(codec_ctx, avpacket);             if (ret < 0)             {                 if (ret == AVERROR(EAGAIN))                 {                     printf("send input first\n");                     break;                 }                 else                 {                     printf("avcodec_receive_packet error(%s)\n", GetFfmpegERR(ret));                     break;                 }             }              ret = av_write_frame(fmt_ctx, avpacket);             if (ret < 0)             {                 printf("av_write_frame error(%s)\n", GetFfmpegERR(ret));                 av_packet_unref(avpacket);                 goto END;             }              av_packet_unref(avpacket);         }     }      ret = avcodec_send_frame(codec_ctx, nullptr);     while (1)     {         ret = avcodec_receive_packet(codec_ctx, avpacket);         if (ret < 0)         {             if (ret == AVERROR(EAGAIN))             {                 printf("send input first\n");                 break;             }             else             {                 printf("avcodec_receive_packet error(%s)\n", GetFfmpegERR(ret));                 break;             }         }          ret = av_write_frame(fmt_ctx, avpacket);         if (ret < 0)         {             printf("av_write_frame error(%s)\n", GetFfmpegERR(ret));             av_packet_unref(avpacket);             goto END;         }          av_packet_unref(avpacket);     }      ret = av_write_trailer(fmt_ctx);     if (ret < 0)     {         printf("av_write_trailer error(%s)\n", GetFfmpegERR(ret));         goto END;     }  END:     if (fmt_ctx)     {         avformat_free_context(fmt_ctx);         fmt_ctx = nullptr;     }     if (in_fp)     {         fclose(in_fp);         in_fp = nullptr;     }     if (avframe)     {         av_frame_free(&avframe);         avframe = nullptr;     }     if (avpacket)     {         av_packet_free(&avpacket);         avpacket = nullptr;     }     if (codec_ctx)     {         avcodec_free_context(&codec_ctx);         codec_ctx = nullptr;     } }

performance

相关内容

热门资讯

终于发现!微信炸金花房卡哪里有... 微信游戏中心:炸金花房卡,添加微信【66336574】,进入游戏中心或相关小程序,搜索“微信炸金花房...
玩家必备!微信里上玩炸金花购买... 微信游戏中心:炸金花房卡,添加微信【82606316】,进入游戏中心或相关小程序,搜索“微信炸金花房...
终于发现!微信群链接炸金花房卡... 微信游戏中心:炸金花房卡,添加微信【71319951】,进入游戏中心或相关小程序,搜索“微信炸金花房...
一分钟了解“微信拼三张房卡怎么... 天酷大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来...
秒懂教程“炸金花房卡链接建立步... 先锋大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来...
终于发现!微信玩链接牛牛房卡,... 微信游戏中心:牛牛房卡,添加微信【56001354】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”...
玩家必备!微信炸金花购买房卡/... 微信游戏中心:炸金花房卡,添加微信【82606316】,进入游戏中心或相关小程序,搜索“微信炸金花房...
终于找到“炸金花房卡链接怎么买... 乐酷副厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来...
终于发现!微信里面拼三张房卡哪... 微信游戏中心:拼三张房卡,添加微信【66336574】,进入游戏中心或相关小程序,搜索“微信拼三张房...
终于知道”新全游房卡充值“金花... 第二也可以在游戏内商城:在游戏界面中找到 “微信金花,斗牛链接房卡”“商城”选项,选择房卡的购买选项...
终于发现!微信群链接炸金花房卡... 微信游戏中心:炸金花房卡,添加微信【71319951】,进入游戏中心或相关小程序,搜索“微信炸金花房...
微信牛牛链接金花房卡/斗牛房间... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
玩家必备!炸金花链接房间卡从哪... 微信游戏中心:炸金花房卡,添加微信【82606316】,进入游戏中心或相关小程序,搜索“微信炸金花房...
终于知道”圣游获得房卡链接渠道... 第二也可以在游戏内商城:在游戏界面中找到 “微信金花,斗牛链接房卡”“商城”选项,选择房卡的购买选项...
终于发现!微信群拼三张房卡怎么... 微信游戏中心:拼三张房卡,添加微信【56001354】,进入游戏中心或相关小程序,搜索“微信拼三张房...
终于知道”新鸿运如何买房卡“人... 来教大家如何使用如何买房卡房卡充值 添加房卡批售商:微【113857775】复制到微信搜索、直接添加...
ia实测“牛牛房卡批发平台/九... 九酷大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡...
玩家必备!微信里打炸金花房卡在... 微信游戏中心:炸金花房卡,添加微信【82606316】,进入游戏中心或相关小程序,搜索“微信炸金花房...
终于发现!微信群拼三张房卡到哪... 微信游戏中心:拼三张房卡,添加微信【66336574】,进入游戏中心或相关小程序,搜索“微信拼三张房...
金花房卡购买联系方式/在哪里买... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来享受...