本文主要是介绍音频采样率 wav->aac,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
音频采样率
1.采样率sample_rate 44100(CD)
2.通道channels (左右声道)
3.样本大小(格式) sample_size
-AV_SAMPLE_FMT_S16
-AV_SAMPLE_FMT_FLTP
wav_to_aac.cpp
extern "C"
{#include <libavformat/avformat.h>#include <libswscale/swscale.h>#include <libswresample/swresample.h>
}
#include <iostream>
using namespace std;
int main()
{char infile[] = "16.wav";char outfile[] = "out.aac";//muxer,demutersav_register_all();avcodec_register_all();//1.打开音频编码格式AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_AAC);if(!codec){cout<<"avcodec_find_encoder error"<<endl;return -1;}AVCodecContext *c = avcodec_alloc_context3(codec);if(!c){cout<<"avcodec_alloc_context3 error"<<endl;return -1;}c->bit_rate = 64000;c->sample_rate = 44100;c->sample_fmt = AV_SAMPLE_FMT_FLTP;c->channel_layout = AV_CH_LAYOUT_STEREO;c->channels = 2;c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;int ret = avcodec_open2(c,codec,NULL);if(ret < 0){cout<<"avcodec_open2 error"<<endl;return -1;}cout<<"avcodec_open2 success"<<endl;//2. 打开输出封装的上下文AVFormatContext *oc = NULL;avformat_alloc_output_context2(&oc,NULL,NULL,outfile);if(!oc){cout<<"avformat_alloc_output_context2 error "<<endl;return -1;}AVStream *st = avformat_new_stream(oc,NULL);st->codecpar->codec_tag = 0;avcodec_parameters_from_context(st->codecpar,c);av_dump_format(oc,0,outfile,1);//3. open io, write headret = avio_open(&oc->pb,outfile,AVIO_FLAG_WRITE);if(ret < 0){cout<<"avio_open error"<<endl;return -1;} ret = avformat_write_header(oc,NULL);//44100 16 2//5 创建音频重采样上下文SwrContext *actx = NULL;actx = swr_alloc_set_opts(actx,c->channel_layout,c->sample_fmt,c->sample_rate,//输出格式AV_CH_LAYOUT_STEREO,AV_SAMPLE_FMT_S16,44100, //输入格式0,0);if(!actx){cout<<"swr_alloc_set_opts error "<<endl;return -1;}std::cout<<"swr_alloc_set_opts success"<<endl;ret = swr_init(actx);if(ret < 0){cout<<"swr init error"<<endl;}cout<<"swr_init success "<<endl;//5 打开输入音频文件,进行重采样AVFrame *frame = av_frame_alloc();frame->format = AV_SAMPLE_FMT_FLTP;frame->channels = 2;frame->channel_layout = AV_CH_LAYOUT_STEREO;frame->nb_samples = 1024;//一帧音频的样本数量ret = av_frame_get_buffer(frame,0);if(ret < 0){cout<<"av_frame_get_buffer error"<<endl;return -1;}std::cout<<"av_frame_get_buffer success"<<endl;int readSize = frame->nb_samples * 2 * 2;char *pcm = new char[readSize];FILE *fp = fopen(infile,"rb");std::cout<<fp;for(;;){int len = fread(pcm,1,readSize,fp);if(len < 0) break;const uint8_t *data[1];data[0] = (uint8_t *)pcm;swr_convert(actx,frame->data,frame->nb_samples,data,frame->nb_samples);if(len <= 0)break;AVPacket pkt;av_init_packet(&pkt);//6 音频编码ret = avcodec_send_frame(c,frame);if(ret != 0) continue;ret = avcodec_receive_packet(c,&pkt);if(ret != 0) continue;//7.音频封装如acc文件pkt.stream_index = 0;pkt.pts = 0;pkt.dts = 0;ret = av_interleaved_write_frame(oc,&pkt);cout<<"["<<len<<"]";}delete pcm;//写入视频索引av_write_trailer(oc);//关闭视频输出avio_close(oc->pb);//清理封装输出上下文avformat_free_context(oc);//关闭编码器avcodec_close(c);//清理编码器上下文avcodec_free_context(&c);return 0;
}
这篇关于音频采样率 wav->aac的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!