pcm—aac
ffmpeg基本流程都一样
设备管理器,麦克风的名称
#include"dshow.h"
#include"libavdevice/avdevice.h"//获取设备
static char *dup_wchar_to_ytf8(wchar_t *w)
{
char *s=NULL;
int l=WideCharToMultiByte(CP_UTF8,0,w,-1,0,0,0,0);
s=(char *)av_malloc(1);
if(s)
WideCharToMultiByte(CP_UTF8,0,W,-1,s,1,0,0);
return s;
}
string fileAudioInput=dup_wchar_to_utf8(L"audio=麦克风(设备管理器名称)");
av_log_set_level(AV_LOG_ERROR);
OpenInput(fileAudioInput);
avformat_open_input的第三个参数是下面这个
AVInputFormat *ifmt=av_find_input_format("dshow");
第四个参数 AVDictionary *format_opts
av_dict_set_int(&format_opts,"sudio_buffer_size",20,0);
flags=1代表这是关键帧
aac有一个要求送给编码器的字节要是1024
packet frame 1024
压缩编码数据 解码数据
packet+文件头 就是文件
frame-filter-frame 1024
abuffer
abuffersink
滤镜初始化
AVFrame *DecodeAudio(AVPacket *packet,AVFrame *pSrcAudioFrame)
{
AVStream *stream=inputContext->stream[0];
AVCodecContext *codecContext=stream->codec;
int gotFrame;
AVFrame *fileFrame=nullptr;
auto length=avcodec_decode_audio4(codecContext,pSrcAudioFrame,&gotFrame,packet);
if(length>=0&&gotFrame!=0)
{
if(av_buffersrc_add_frame_flags(buffersrcCtx,pSrcAudioFrame,AV_BUFFERSRC_FLAG_PUSH)<0)
{
av_log(NULL,AV_LOG_ERROR,"buffer src and frame error!\n");
return nullptr;
}
filtFrame=av_frame_alloc();
int ret=av_buffersink_get_frame_flags(buffersinkCtx,filtFrame,AV_BUFFERSINK_FLAG_NO_REQUEST);
if(ret<0)
{
av_frame_free(&filtFrame);
goto error;
}
return filtFrame;
}
errror:
return nullptr;
}
static const enum AVSampleFormat out_sample_fmts[]={AV_SAMPLE_FMT_FLTP,AV_SAMPLE_FMT_NONE};
AVCodecContext *outPutAudioEncContext=NULL;
int InitAudioEncoderCodec(AVCodecContext *inputAudioCodec)
{
int ret=0;
AVCodec *audioCode=avcodec_find_encoder(AV_CODEC_ID_AAC);
outPutAudioEnContext=avcodec_alloc_context3(audioCodec);
outPutAudioEnContext->codec=audioCodec;
outPutAudioEnContext->sample_rate=inputAudioCodec->sample_rate;
outPutAudioEnContext->channel_layout=inputAudioCodec->channel_lay_out;
outPutAudioEnContext->channels=av_get_channel_layout_nb_channels(inputAudioCodec->channel_layout);
if(outPutAudioEncContext->channel_lay_out==0)
{
outPutAudioEncContext->channel_layout=AV_CH_LAYOUT_STEREO;
outPutAudioEncContext->channels=av_get_channel_layout_nb_channels(outPutAudioEncContexet->channel_layout);
}
outPutAudioEncContext->sample_fmt=audioCodec->sample_fmts[0];
outPutAudioEncContext->codec_tag=0;
outPutAudioEncContext->flags|=CODEX_FLAG_GLOBAL_HEADER;
ret=avcodec_open2(outPutAudioEncContext,audioCodec,0);
return ret;
}
//时间戳写一下
int64_t audioCout=0;
if(frame)
{
avcodec_encode_audio2();
streamTmeBase=outputContext->streams[pkt->stream_index]->time_base.den;
codecTimeBase=outputContext->streams[pkt->stream_index]->codec->time_base.den;
pkt->pts=pkt->dts=(1024*streamTimeBase*audioCount)/codecTimeBase;
audioCount++;
}
if(packet)
音频的90000表示44100字节数 均分在9000这个单位上
1024/44100*1000表示的毫秒数