调用ffmpeg库生成AAC静音帧packet
版权归博主所有,转载请注明出处
最近因为项目的原因,在某些情况下,需要用到静音帧,此处只会大概介绍如何生成静音帧,并将其进行AAC编码,对于如何编译ffmpeg,请找谷歌或者度娘!
思路是:首先生成静音帧PCM数据,然后调用ffmpeg音频编码函数将其进行AAC编码
*生成PCM帧
//配置PCM帧的数据长度,通常AAC是1024,
int frame_size_out_encode = oc->streams[st->index]->codec->frame_size;
printf("frame_size_out_encode %d \n", frame_size_out_encode);
if(!frame_size_out_encode)
frame_size_out_encode = 1024;
printf("frame_size_out_encode %d \n", frame_size_out_encode);
AVCodecContext *acout = oc->streams[st->index]->codec;
//分配帧结构内存
frame_fifo = av_frame_alloc();
frame_fifo->nb_samples = frame_size_out_encode;
frame_fifo->channel_layout = acout->channel_layout;
frame_fifo->channels = av_get_channel_layout_nb_channels(frame_fifo->channel_layout);
frame_fifo->format = acout->sample_fmt;
frame_fifo->sample_rate = acout->sample_rate;
av_frame_get_buffer(frame_fifo, 0);
//根据参数填充静音帧数据,常说的PCM数据
av_samples_set_silence(frame_fifo->data, 0, frame_fifo->nb_samples, frame_fifo->channels, (enum AVSampleFormat)frame_fifo->format);
*AAC编码
av_init_packet(pkt_out);
if ((ret = avcodec_encode_audio2(oc->streams[st->index]->codec, pkt_out, frame_fifo, &got_picture)) !=0)
printf( "avcodec_encode_audio2 %d %s \n",ret,av_err2str(ret) );
if (got_picture )
{
printf("pkt_out->size %d \n",pkt_out->size );
}
至此,一个静音帧的AAC packet就生成了!
参考资料
ffmpeg AVFrame 插入静音帧