最新使用 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(¶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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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 翻译文档