最新使用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);
时间: 2024-11-11 02:28:12