gpt4 book ai didi

ffmpeg - 编码为 h264 无法使用 ffmpeg c api 发送一些帧

转载 作者:行者123 更新时间:2023-12-04 23:12:33 26 4
gpt4 key购买 nike

使用 FFMPEG C API,我试图将生成的图像推送到 MP4 格式。
当我逐帧推送时,avcodec_receive_packet(...) 上的复用似乎失败了返回 AVERROR(EAGAIN)在第一帧,但过了一会儿开始添加我的帧,但第一个。
我的意思是,当将第 1 帧推到第 13 帧时,我有错误,但是在第 14 帧结束(36)之后,该帧被添加到视频中,但编码的图像不是 14 到 36,而是它的帧添加的 1 到 23 个。
我不明白,这是帧率问题(我想要 12 fps)还是关键帧/帧间问题?
这里是类不同部分的代码,
笔记:

  • m_filename = "C:\tmp\test.mp4"
  • m_framerate = 12
  • m_width = 1080
  • m_height = 1080

  • 导演
    // Allocate the temporary buffer that hold the our generated image in RGB.
    picture_rgb24 = av_frame_alloc();
    picture_rgb24->pts = 0;
    picture_rgb24->data[0] = NULL;
    picture_rgb24->linesize[0] = -1;
    picture_rgb24->format = AV_PIX_FMT_RGB24;
    picture_rgb24->height = m_height;
    picture_rgb24->width = m_width;

    if ((_ret = av_image_alloc(picture_rgb24->data, picture_rgb24->linesize, m_width, m_height, (AVPixelFormat)picture_rgb24->format, 24)) < 0)
    throw ...

    // Allocate the temporary frame that will be convert from RGB to YUV using ffmpeg api.
    frame_yuv420 = av_frame_alloc();
    frame_yuv420->pts = 0;
    frame_yuv420->data[0] = NULL;
    frame_yuv420->linesize[0] = -1;
    frame_yuv420->format = AV_PIX_FMT_YUV420P;
    frame_yuv420->width = m_height;
    frame_yuv420->height = m_width;

    if ((_ret = av_image_alloc(frame_yuv420->data, frame_yuv420->linesize, m_width, m_height, (AVPixelFormat)frame_yuv420->format, 32)) < 0)
    throw ...

    init_muxer(); // see below.

    m_inited = true;

    m_pts_increment = av_rescale_q(1, { 1, m_framerate }, ofmt_ctx->streams[0]->time_base);

    // Context that convert the RGB24 to YUV420P format (using this instead of filter similar to GIF).
    swsCtx = sws_getContext(m_width, m_height, AV_PIX_FMT_RGB24, m_width, m_height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, 0, 0, 0);
    初始化复用器:
    AVOutputFormat* oformat = av_guess_format(nullptr, m_filename.c_str(), nullptr);
    if (!oformat) throw ...

    _ret = avformat_alloc_output_context2(&ofmt_ctx, oformat, nullptr, m_filename.c_str());
    if (_ret) throw ...

    AVCodec *codec = avcodec_find_encoder(oformat->video_codec);
    if (!codec) throw ...

    AVStream *stream = avformat_new_stream(ofmt_ctx, codec);
    if (!stream) throw ...

    o_codec_ctx = avcodec_alloc_context3(codec);
    if (!o_codec_ctx) throw ...

    stream->codecpar->codec_id = oformat->video_codec;
    stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
    stream->codecpar->width = m_width;
    stream->codecpar->height = m_height;
    stream->codecpar->format = AV_PIX_FMT_YUV420P;
    stream->codecpar->bit_rate = 400000;

    avcodec_parameters_to_context(o_codec_ctx, stream->codecpar);
    o_codec_ctx->time_base = { 1, m_framerate };

    // Using gop_size == 0, we want 'intra' frame, so no b-frame will be generated.
    o_codec_ctx->max_b_frames = 0;
    o_codec_ctx->gop_size = 0;
    o_codec_ctx->b_quant_offset = 0;
    //o_codec_ctx->framerate = { m_framerate , 1 };

    if (stream->codecpar->codec_id == AV_CODEC_ID_H264)
    av_opt_set(o_codec_ctx, "preset", "ultrafast", 0); // Lossless H.264
    else if (stream->codecpar->codec_id == AV_CODEC_ID_H265)
    av_opt_set(o_codec_ctx, "preset", "ultrafast", 0); // Lossless H.265

    avcodec_parameters_from_context(stream->codecpar, o_codec_ctx);

    if ((_ret = avcodec_open2(o_codec_ctx, codec, NULL)) < 0)
    throw ...

    if ((_ret = avio_open(&ofmt_ctx->pb, m_filename.c_str(), AVIO_FLAG_WRITE)) < 0)
    throw ...

    if ((_ret = avformat_write_header(ofmt_ctx, NULL)) < 0)
    throw ...

    av_dump_format(ofmt_ctx, 0, m_filename.c_str(), 1);
    添加帧:
    // loop to transfer our image format to ffmpeg one.
    for (int y = 0; y < m_height; y++)
    {
    for (int x = 0; x < m_width; x++)
    {
    picture_rgb24->data[0][idx] = ...;
    picture_rgb24->data[0][idx + 1] = ...;
    picture_rgb24->data[0][idx + 2] = ...;
    }
    }

    // From RGB to YUV
    sws_scale(swsCtx, (const uint8_t * const *)picture_rgb24->data, picture_rgb24->linesize, 0, m_height, frame_yuv420->data, frame_yuv420->linesize);

    // mux the YUV frame
    muxing_one_frame(frame_yuv420);

    // Increment the FPS of the picture for the next add-up to the buffer.
    picture_rgb24->pts += m_pts_increment;
    frame_yuv420->pts += m_pts_increment;
    muxing_one_frame:
    int ret = avcodec_send_frame(o_codec_ctx, frame);
    AVPacket *pkt = av_packet_alloc();
    av_init_packet(pkt);

    while (ret >= 0) {
    ret = avcodec_receive_packet(o_codec_ctx, pkt);
    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break;
    av_write_frame(ofmt_ctx, pkt);
    }
    av_packet_unref(pkt);
    关闭文件:
    av_write_trailer(ofmt_ctx);
    avio_close(ofmt_ctx->pb);

    最佳答案

    关于ffmpeg - 编码为 h264 无法使用 ffmpeg c api 发送一些帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62802593/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com