FFMPEG H264/H265 编码延迟问题

参考:http://blog.csdn.net/aoshilang2249/article/details/40397199?utm_source=tuicool&utm_medium=referral

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

开编码器时,设置AVDictionary参数即可,关键代码如下:


avcodec_open2函数:

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

解决方案:
  1. //H264, 设置为编码延迟为立即编码  
  2. if(c->codec_id == AV_CODEC_ID_H264)  
  3. {    
  4.     av_dict_set(ctx->priv_data, "preset", "superfast",   0);  

  5.     av_dict_set(ctx->priv_data, "tune",   "zerolatency", 0);  
  6. }    
  7. //H.265    
  8. if(c->codec_id == AV_CODEC_ID_H265)  
  9. {
  10. 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);

  11. }    
  12.   
  13. //使用给定的AVCodec初始化AVCodecContext  
  14. ret = avcodec_open2(参数, 参数,NULL); 

还有这种方式:

  1. // Set Option  
  2.     AVDictionary *param = 0;  
  3.     //H.264  
  4.     if(pCodecCtx->codec_id == AV_CODEC_ID_H264) {  
  5.         av_dict_set(?m, "preset""slow", 0);  
  6.         av_dict_set(?m, "tune""zerolatency", 0);  
  7.     }  
  8.     //H.265  
  9.     if(pCodecCtx->codec_id == AV_CODEC_ID_H265){  
  10.         av_dict_set(&param, "x265-params""qp=20", 0);  
  11.         av_dict_set(&param, "preset""ultrafast", 0);  
  12.         av_dict_set(&param, "tune""zero-latency", 0);  
  13.     }  
  14.   
  15.     //Dump Information 输出格式信息  
  16.     av_dump_format(pFormatCtx, 0, out_file, 1);  
  17.   
  18.     pCodec = avcodec_find_encoder(pCodecCtx->codec_id);  
  19.     if (!pCodec){  
  20.         printf("Can not find encoder! 没有找到合适的编码器!\n");  
  21.         return -1;  
  22.     }  
  23.     if (avcodec_open2(pCodecCtx, pCodec,&param) < 0){  
  24.         printf("Failed to open encoder! 编码器打开失败!\n");  
  25.         return -1;  
  26.     } 

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值