FFmpeg+x264出现pix_fmt为-1

我最近使用FFmpeg6.0与x264想写个功能,在编码器的时候碰上一个问题:

qDebug()<<"================================================================start======================================================================";
    int ret;
    int imagewidth = 1920;
    int imageheight = 1080;
    // 初始化 FFmpeg
    qDebug()<<"avdevice_register_all()";
    avdevice_register_all(); //初始化所有设备
    qDebug()<<"formatContext = avformat_alloc_context()";
    formatContext = avformat_alloc_context();//分配format上下文
    QString outputFileName("/sdcard/ffmep.mp4");
    qDebug()<<"avformat_alloc_output_context2(&formatContext, nullptr, nullptr, outputFileName.toUtf8().constData())";
    ret = avformat_alloc_output_context2(&formatContext, nullptr, nullptr, outputFileName.toUtf8().constData());
    qDebug()<<"ret===="<<ret;
    qDebug()<<"formatContext===="<<formatContext;
    if(!formatContext)        //如果根据文件名没有找到对应的格式则默认mpeg格式
    {
        qDebug()<<"!formatContext ======mpeg";
        ret = avformat_alloc_output_context2(&formatContext, NULL, "mpeg",  outputFileName.toUtf8().constData());    //没有找到文件类型默认mpeg(MP4)
    }
    
    qDebug()<<"formatContext->oformat = av_guess_format(nullptr, outputFileName.toUtf8().constData(), nullptr);";
    formatContext->oformat = av_guess_format(nullptr, outputFileName.toUtf8().constData(), nullptr);
    qDebug() << "avio_open(&formatContext->pb, outputFileName.toUtf8().constData(), AVIO_FLAG_WRITE) < 0";
    // 打开输出文件
    if (avio_open(&formatContext->pb, outputFileName.toUtf8().constData(), AVIO_FLAG_WRITE) < 0) {
        qDebug() << "Failed to open output file";
        return;
    }
    qDebug() << "AVStream* stream = avformat_new_stream(formatContext, nullptr);";
    // 创建一个AVStream对象
    AVStream* stream = avformat_new_stream(formatContext, nullptr);
    if (!stream) {
        qDebug() << "Failed to create output stream";
        return;
    }
    
    qDebug() << "AVCodecParameters* codecParameters = stream->codecpar;";
     // 配置AVCodecContext
    AVCodecParameters* codecParameters = stream->codecpar;
    codecParameters->codec_type = AVMEDIA_TYPE_VIDEO;
    codecParameters->codec_id = AV_CODEC_ID_H264; // 使用H.264编码器
    codecParameters->width = imagewidth;
    codecParameters->height = imageheight;
    qDebug() << " const AVCodec* codec = avcodec_find_encoder(codecParameters->codec_id);";
    
    qDebug() << "const AVCodec* codec = avcodec_find_encoder(codecParameters->codec_id);";
     // 打开编解码器
    const AVCodec* codec = avcodec_find_encoder(codecParameters->codec_id);
    AVCodecContext* codecContext = avcodec_alloc_context3(codec);
    codecContext->width = imagewidth;
    codecContext->height = imageheight;
    codecContext->pix_fmt = AV_PIX_FMT_YUV420P;
    codecContext->time_base = {1, 30}; // 设置编码器的时间基为 1秒/30帧
    
    qDebug() << "avcodec_parameters_to_context(codecContext, codecParameters);";
    avcodec_parameters_to_context(codecContext, codecParameters);
    // 检查编解码器支持的像素格式
    const AVPixelFormat* pixFmt = codec->pix_fmts;
    qDebug() << "while";
    while (*pixFmt != AV_PIX_FMT_NONE) {
        qDebug() << av_get_pix_fmt_name(*pixFmt);
        ++pixFmt;
    }
    qDebug() << "AV_PIX_FMT_YUV420P====="<<AV_PIX_FMT_YUV420P;
    qDebug() << "codecContext->pix_fmt====="<<codecContext->pix_fmt;
    qDebug() << "avcodec_open2(codecContext, codec, nullptr);";
    ret = avcodec_open2(codecContext, codec, nullptr);
    qDebug() << "avcodec_open2 ret==="<<ret;
    qDebug() << " avformat_write_header(formatContext, nullptr);";
    // 写入头部信息
    avformat_write_header(formatContext, nullptr);
    qDebug() << " SwsContext* swsContext = sws_getContext(width, height, AV_PIX_FMT_RGB24, width, height, codecContext->pix_fmt, SWS_BILINEAR, nullptr, nullptr, nullptr);=";
     // 创建SwsContext用于图像转换
    //SwsContext* swsContext = sws_getContext(imagewidth, imageheight, AV_PIX_FMT_RGB24, imagewidth, imageheight, codecContext->pix_fmt, SWS_BILINEAR, nullptr, nullptr, nullptr);
    qDebug() << "  AVFrame* frame = av_frame_alloc();";
    // 逐个写入图像帧
    AVFrame* frame = av_frame_alloc();
    
    qDebug()<<"=============================================================stop=========================================================================";

我的log打印为:

================================================================start======================================================================
avdevice_register_all()
formatContext = avformat_alloc_context()
avformat_alloc_output_context2(&formatContext, nullptr, nullptr, outputFileName.toUtf8().constData())
ret==== 0
formatContext==== 0x4033a4f0
formatContext->oformat = av_guess_format(nullptr, outputFileName.toUtf8().constData(), nullptr);
avio_open(&formatContext->pb, outputFileName.toUtf8().constData(), AVIO_FLAG_WRITE) < 0
AVStream* stream = avformat_new_stream(formatContext, nullptr);
AVCodecParameters* codecParameters = stream->codecpar;
 const AVCodec* codec = avcodec_find_encoder(codecParameters->codec_id);
const AVCodec* codec = avcodec_find_encoder(codecParameters->codec_id);
avcodec_parameters_to_context(codecContext, codecParameters);
while
yuv420p
yuvj420p
yuv422p
yuvj422p
yuv444p
yuvj444p
nv12
nv16
nv21
yuv420p10le
yuv422p10le
yuv444p10le
nv20le
gray
gray10le
AV_PIX_FMT_YUV420P===== 0
codecContext->pix_fmt===== -1
avcodec_open2(codecContext, codec, nullptr);
[libx264 @ 0x4033a8a0] Specified pixel format -1 is invalid or not supported
avcodec_open2 ret=== -22
 avformat_write_header(formatContext, nullptr);
 SwsContext* swsContext = sws_getContext(width, height, AV_PIX_FMT_RGB24, width, height, codecContext->pix_fmt, SWS_BILINEAR, nullptr, nullptr, nullptr);=
  AVFrame* frame = av_frame_alloc();
=============================================================stop=========================================================================

我明明设置了codecContext->pix_fmt = AV_PIX_FMT_YUV420P,但是还是出现了Specified pixel format -1 is invalid or not supported,有人能告诉我什么情况吗,我之前没玩过这东西!

调用avcodec_parameters_to_context(codecContext, codecParameters);,我记得会重置之前设置的像素格式。