ffmpeg hevc (x265) encoding

ffmpeg hevc (x265) encoding

本文关键字:encoding x265 hevc ffmpeg      更新时间:2023-10-16

我正在尝试在 FFMPEG 库中使用 H265 编码器,但给我这个错误:

Cannot open libx265 encoder.

这是我的代码:

formatContext = avformat_alloc_context();
videoStream = avformat_new_stream(formatContext,0);
if(!videoStream ) {
    //error
    return;
}
av_init_packet(&packet);
codecContext=videoStream->codec;
codecContext->codec_type = AVMEDIA_TYPE_VIDEO;
codecContext->width = width;
codecContext->height = height;
codecContext->time_base.den = fps;
codecContext->time_base.num = 1;
codecContext->pix_fmt = AV_PIX_FMT_YUV420P;
codecContext->codec_id = AV_CODEC_ID_HEVC;
AVDictionary *param = 0;
av_dict_set(&param, "x265-params", "qp=20", 0);
av_dict_set(&param, "preset", "ultrafast", 0);
av_dict_set(&param, "tune", "zero-latency", 0);
av_dict_set(&param, "qmin", "0", 0);
av_dict_set(&param, "qmax", "69", 0);
av_dict_set(&param, "qdiff", "4", 0);
codec = avcodec_find_encoder(codecContext->codec_id);
if (!codec) {
    //codec not found
    return;
}
int rt = avcodec_open2(codecContext, codec, &param); // <----- fails here
if (rt < 0) {
    // fails here!!
    return;
}

此代码适用于 h264 编码器,有人知道为什么不适用于 hevc?

如果您设置

codecContext->sample_aspect_ratio.num = 4;
codecContext->sample_aspect_ratio.den = 3;

HEVC 编码器可以工作,但输出上没有视频。

嗨,你有没有打开编码器libx265?我遇到了同样的麻烦,并尝试使用AV_CODEC_ID_H265。我以前遇到过同样的麻烦libx264但我通过设置解决了它:

av_dict_set(&param, "profile", "high", 0);

编解码器配置中似乎缺少某些内容。