zoukankan      html  css  js  c++  java
  • FFMPEG H264/H265 编码延迟问题

    最新使用 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

    还有这种方式:
    // 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(&param, "x265-params", "qp=20", 0);  
            av_dict_set(&param, "preset", "ultrafast", 0);  
            av_dict_set(&param, "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! 没有找到合适的编码器! ");  
            return -1;  
        }  
        if (avcodec_open2(pCodecCtx, pCodec,&param) < 0){  
            printf("Failed to open encoder! 编码器打开失败! ");  
            return -1;  
        } 

  • 相关阅读:
    python正则表达式re模块
    链表算法题之中等级别,debug调试更简单
    链表算法题二,还原题目,用debug调试搞懂每一道题
    开启算法之路,还原题目,用debug调试搞懂每一道题
    K8S线上集群排查,实测排查Node节点NotReady异常状态
    手写单链表基础之增,删,查!附赠一道链表题
    kafka初识
    docker之mysql镜像使用
    CS61B sp2018笔记 | Lists
    JSONArray.fromObject不执行且不报错问题的解决
  • 原文地址:https://www.cnblogs.com/lidabo/p/15071183.html
Copyright © 2011-2022 走看看