DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

最新使用 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! 没有找到合适的编码器!\n");  
        return -1;  
    }  
    if (avcodec_open2(pCodecCtx, pCodec,&param) < 0){  
        printf("Failed to open encoder! 编码器打开失败!\n");  
        return -1;  
    } 

posted on   DoubleLi  阅读(1423)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-07-28 HTTP协议中的chunked编码解析
2020-07-28 http协议里的chunked编码与测试
2020-07-28 HTTP协议之chunk介绍
2020-07-28 HTTP协议之Chunked解析
2017-07-28 webrtc学习资源
2017-07-28 HTML5视音频标签参考
2017-07-28 ffmpeg 翻译文档
点击右上角即可分享
微信分享提示