調用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 插入靜音幀