最新使用FFmpeg进行H264的编码时,发现视频编码有延迟,不是实时编码。进过一番研究发现,仅仅要在调用avcodec_open2函数

开编码器时,设置AVDictionary參数就可以。关键代码例如以下:


avcodec_open2函数:

int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);

解决方式:

AVDictionary *param = NULL;	
//H264, 设置为编码延迟为马上编码
if(c->codec_id == AV_CODEC_ID_H264)
{  
	av_dict_set(¶m, "preset", "superfast",   0);
	av_dict_set(¶m, "tune",   "zerolatency", 0);
}  
//H.265  
if(c->codec_id == AV_CODEC_ID_H265)
{  
	av_dict_set(¶m, "x265-params", "qp=20", 0); 
	av_dict_set(¶m, "preset", "ultrafast", 0);  
	av_dict_set(¶m, "tune", "zero-latency", 0); 
}  

//使用给定的AVCodec初始化AVCodecContext
ret = avcodec_open2(參数, 參数, ¶m);