ffmpeg()音频转码

这里写图片描述

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表示的毫秒数
敬告:该系列的课程在抓紧录制更新中,敬请大家关注。敬告: 该系列的课程涉及:FFmpeg,WebRTC,SRS,Nginx,Darwin,Live555,等。包括:音视频、流媒体、直播、Android、视频监控28181、等。  我将带领大家一起来学习:采集麦克风、PCM重采样、AAC或MP3音频编码存储、并直播。具体内容包括:1.FFmpeg采集麦克风并存储为PCM。2.PCM重采样原理及实战。3.采集麦克风并实时转码AAC或MP3并保存文件。4.采集麦克风并实时转码AAC并可以直播。 音视频与流媒体是一门很复杂的技术,涉及的概念、原理、理论非常多,很多初学者不学 基础理论,而是直接做项目,往往会看到c/c++的代码时一头雾水,不知道代码到底是什么意思,这是为什么呢? 因为没有学习音视频和流媒体的基础理论,就比如学习英语,不学习基本单词,而是天天听英语新闻,总也听不懂。所以呢,一定要认真学习基础理论,然后再学习播放器、转码器、非编、流媒体直播、视频监控、等等。 梅老师从事音视频与流媒体行业18年;曾在永新视博、中科大洋、百度、美国Harris广播事业部等公司就职,经验丰富;曾亲手主导广电直播全套项目,精通h.264/h.265/aac,曾亲自参与百度app上的网页播放器等实战产品。 目前全身心自主创业,主要聚焦音视频+流媒体行业,精通音视频加密、流媒体在线转码快编等热门产品。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值