c++ - 创建可用的 H.264 视频文件

标签 c++ qt ffmpeg h.264 libavcodec

我正在尝试使用 libavcodec 从单个帧生成 mp4 视频文件。每个输入帧都是一个 qt QImage,输出文件使用 Qt QFile 类写入。

我通过 VideoTarget 类完成了此操作,该类在初始化时打开给定的“目标”文件,在调用 addFrame(image) 时记录帧,然后保存/调用析构函数时关闭文件。

该类具有以下字段:

AVCodec* m_codec = nullptr;
AVCodecContext *m_context = nullptr;
AVPacket* m_packet = nullptr;
AVFrame* m_frame = nullptr;

QFile m_target;

看起来像这样:

VideoTarget::VideoTarget(QString target, QObject *parent) : QObject(parent), m_target(target)
{
    // Find video codec
    m_codec = avcodec_find_encoder_by_name("libx264rgb");
    if (!m_codec) throw std::runtime_error("Unable to find codec.");

    // Make codec context
    m_context = avcodec_alloc_context3(m_codec);
    if (!m_context) throw std::runtime_error("Unable to allocate codec context.");

    // Make codec packet
    m_packet = av_packet_alloc();
    if (!m_packet) throw std::runtime_error("Unable to allocate packet.");

    // Configure context
    m_context->bit_rate = 400000;
    m_context->width = 1280;
    m_context->height = 720;
    m_context->time_base = (AVRational){1, 60};
    m_context->framerate = (AVRational){60, 1};
    m_context->gop_size = 10;
    m_context->max_b_frames = 1;
    m_context->pix_fmt = AV_PIX_FMT_RGB24;

    if (m_codec->id == AV_CODEC_ID_H264)
        av_opt_set(m_context->priv_data, "preset", "slow", 0);

    // Open Codec
    int ret = avcodec_open2(m_context, m_codec, nullptr);
    if (ret < 0) {
        throw std::runtime_error("Unable to open codec.");
    }

    // Open file
    if (!m_target.open(QIODevice::WriteOnly))
        throw std::runtime_error("Unable to open target file.");

    // Allocate frame
    m_frame = av_frame_alloc();
    if (!m_frame) throw std::runtime_error("Unable to allocate frame.");

    m_frame->format = m_context->pix_fmt;
    m_frame->width = m_context->width;
    m_frame->height = m_context->height;
    m_frame->pts = 0;

    ret = av_frame_get_buffer(m_frame, 24);
    if (ret < 0) throw std::runtime_error("Unable to allocate frame buffer.");
}

void VideoTarget::addFrame(QImage &image)
{
    // Ensure frame data is writable
    int ret = av_frame_make_writable(m_frame);
    if (ret < 0) throw std::runtime_error("Unable to make frame writable.");

    // Prepare image
    for (int y = 0; y < m_context->height; y++) {
        for (int x = 0; x < m_context->width; x++) {
            auto pixel = image.pixelColor(x, y);
            int pos = (y * 1024 + x) * 3;
            m_frame->data[0][pos] = pixel.red();
            m_frame->data[0][pos + 1] = pixel.green();
            m_frame->data[0][pos + 2] = pixel.blue();
        }
    }

    m_frame->pts++;

    // Send the frame
    ret = avcodec_send_frame(m_context, m_frame);
    if (ret < 0) throw std::runtime_error("Unable to send AV frame.");

    while (ret >= 0) {
        ret = avcodec_receive_packet(m_context, m_packet);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;
        else if (ret < 0) throw std::runtime_error("Error during encoding.");

        m_target.write((const char*)m_packet->data, m_packet->size);
        av_packet_unref(m_packet);
    }
}

VideoTarget::~VideoTarget()
{
    int ret = avcodec_send_frame(m_context, nullptr);
    if (ret < 0) throw std::runtime_error("Unable to send AV null frame.");

    while (ret >= 0) {
        ret = avcodec_receive_packet(m_context, m_packet);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;
        else if (ret < 0) throw std::runtime_error("Error during encoding.");

        m_target.write((const char*)m_packet->data, m_packet->size);
        av_packet_unref(m_packet);
    }

    // Magic number at the end of the file
    uint8_t endcode[] = { 0, 0, 1, 0xb7 };
    m_target.write((const char*)endcode, sizeof(endcode));
    m_target.close();

    // Free codec stuff
    avcodec_free_context(&m_context);
    av_frame_free(&m_frame);
    av_packet_free(&m_packet);
}

使用时,该类似乎 可以工作,并且数据被写入文件,但我无法在任何应用程序中播放生成的文件。

我主要怀疑这些行:

    // Prepare image
    for (int y = 0; y < m_context->height; y++) {
        for (int x = 0; x < m_context->width; x++) {
            auto pixel = image.pixelColor(x, y);
            int pos = (y * 1024 + x) * 3;
            m_frame->data[0][pos] = pixel.red();
            m_frame->data[0][pos + 1] = pixel.green();
            m_frame->data[0][pos + 2] = pixel.blue();
        }
    }

libavcodec 文档关于图像数据的布局非常模糊,所以我实际上不得不猜测并为第一个没有崩溃的东西感到高兴,所以我很可能正在写这个不正确。我的 pixel 颜色数据调用(提供 int 值)和我选择的每像素 24 位 RGB 格式之间还存在大小不匹配的问题。

如何调整此代码以输出实际的、正常运行的视频文件?

最佳答案

The libavcodec documentation was extremely vague regarding the layout of image data

这是因为每个编解码器都不同。我建议您使用 yuv420p 而不是 RGB24。很多玩家不能玩h264 rgb。您可以使用 libswscale 在两者之间进行转换。

接下来,您正在制作的流是什么格式? Annex B可以直接播放,但是如果你使用的是extradata + NALU size (AVCC),你需要将流包装在一个容器中。

最后,为什么要使用 libavcodec?在我看来,libx264 提供了更简洁的 API。除非您以后玩切换编解码器,否则请避免抽象。

关于c++ - 创建可用的 H.264 视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55976834/

相关文章:

c++ - 传递多个参数 HttpPostRequest c++

c++ - 使用外部库推送 QT Project GIT

xcode - 尝试在 OS X 上安装 Qt 给出 'You need to install Xcode 5.0.0' ,但这个版本太旧了,不可用

android - FFmpeg - 转换视频以在 Android 设备上播放的命令

c++ - 使用 C++ 流从文本文件中读取数字

c++ - 成员变量驻留在哪个内存段?

c++ - Qt Loading 消息应位于父窗口小部件的顶部并居中

ffmpeg - 使用 FFMPEG 将 mp4 转换为最大移动支持的 MP4

android - 处理输入时发现无效数据

c++ - 什么是声明和声明符?标准如何解释它们的类型?