参考:http://blog.csdn.net/aoshilang2249/article/details/40397199?utm_source=tuicool&utm_medium=referral
最新使用FFmpeg进行H264的编码时,发现视频编码有延迟,不是实时编码,进过一番研究发现,只要在调用avcodec_open2函数
打开编码器时,设置AVDictionary参数即可,关键代码如下:
avcodec_open2函数:
- int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);
解决方案:
- //H264, 设置为编码延迟为立即编码
- if(c->codec_id == AV_CODEC_ID_H264)
- {
-
av_dict_set(ctx->priv_data, "preset", "superfast", 0);
- av_dict_set(ctx->priv_data, "tune", "zerolatency", 0);
- }
- //H.265
- if(c->codec_id == AV_CODEC_ID_H265)
- {
-
av_dict_set(ctx->priv_data, "x265-params", "qp=20", 0); av_dict_set(ctx->priv_data, "preset", "ultrafast", 0); av_dict_set(ctx->priv_data, "tune", "zero-latency", 0);
- }
- //使用给定的AVCodec初始化AVCodecContext
- ret = avcodec_open2(参数, 参数,NULL);
还有这种方式:
- // Set Option
- AVDictionary *param = 0;
- //H.264
- if(pCodecCtx->codec_id == AV_CODEC_ID_H264) {
- av_dict_set(?m, "preset", "slow", 0);
- av_dict_set(?m, "tune", "zerolatency", 0);
- }
- //H.265
- if(pCodecCtx->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);
- }
- //Dump Information 输出格式信息
- av_dump_format(pFormatCtx, 0, out_file, 1);
- pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
- if (!pCodec){
- printf("Can not find encoder! 没有找到合适的编码器!\n");
- return -1;
- }
- if (avcodec_open2(pCodecCtx, pCodec,¶m) < 0){
- printf("Failed to open encoder! 编码器打开失败!\n");
- return -1;
- }