c - 使用 libavcodec 编码 h264 时如何限制 CPU 使用率?
问题描述
我将原始图像编码为 h264 视频,并在调用之前设置我的编码器参数avocdec_open2()
:
void set_codec_params(AVFormatContext *&fctx, AVCodecContext *&codec_ctx,
double width, double height, int fps) {
const AVRational dst_fps = {fps, 1};
codec_ctx->codec_tag = 0;
codec_ctx->bit_rate = target_bitrate;
codec_ctx->thread_count = 1; // <----- does nothing
codec_ctx->codec_id = AV_CODEC_ID_H264;
codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
codec_ctx->width = width;
codec_ctx->height = height;
codec_ctx->gop_size = 12;
codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
codec_ctx->framerate = dst_fps;
codec_ctx->time_base = av_inv_q(dst_fps);
if (fctx->oformat->flags & AVFMT_GLOBALHEADER) {
codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
}
我这样设置配置文件参数
AVDictionary *codec_options = nullptr;
av_dict_set(&codec_options, "profile", "high", 0);
av_dict_set(&codec_options, "preset", "ultrafast", 0);
av_dict_set(&codec_options, "tune", "zerolatency", 0);
无论我做什么,在编码过程中所有内核都会被最大化,但我想将其限制为一定数量的线程。thread_count
结构成员似乎被忽略了。
一般来说,可以采取哪些步骤来限制用于编码的线程数?某些设置是否与用户定义的线程数冲突?
解决方案
我将假设您使用 x264 作为 h264 编码器。
这AVCodecContex
是所有现有编解码器的通用结构。因此,如果所选编码器不支持,设置某些属性将无效。然而,调整线程数是大多数编码器应该支持的设置。尽管如此,某些设置仍将被忽略或覆盖。在这种情况下,您需要使用以下方法设置它们AVDictionary
:
av_dict_set(&codec_options, "threads", "1", 0);
推荐阅读
- angular8 - Angular 8 生成 c.querySelectorAll 不是在多个地方单击事件的函数
- ios - 检查领域创建函数返回新对象或更新对象
- angular - 以编程方式创建唯一且确定性的数据测试 ID
- r - 如何使用 gsub 编辑多个模式
- javascript - 如何过滤 html table td 属性?
- python - 使用 virtualenv 的多阶段 python docker 映像
- primefaces - p:commandButton 在点击时不更新其他组件
- c# - 需要在创建唯一组合的 2 列上过滤数据表?
- php - 被投诉的 MySQL 查询
- html - 使用自定义复选框是否需要使用 ARIA 属性?