I am trying to write a video file using FFMPEG but i get the following errors

[libx264 @ 000002bdf90c3c00] broken ffmpeg default settings detected
[libx264 @ 000002bdf90c3c00] use an encoding preset (e.g. -vpre medium)
[libx264 @ 000002bdf90c3c00] preset usage: -vpre <speed> -vpre <profile>
[libx264 @ 000002bdf90c3c00] speed presets are listed in x264 --help
[libx264 @ 000002bdf90c3c00] profile is optional; x264 defaults to high

This is my code

#pragma warning(disable : 4996)

extern "C" {
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libswscale/swscale.h>
}

int main() {
    av_register_all();
    AVFormatContext* formatContext = nullptr;
    AVOutputFormat* outputFormat = nullptr;
    AVStream* videoStream = nullptr;

    const char* filename = "output.mp4";

    // Open the output file
    if (avformat_alloc_output_context2(&formatContext, nullptr, nullptr, filename) < 0) {
        fprintf(stderr, "Error allocating output format contextn");
        return -1;
    }

    outputFormat = formatContext->oformat;

    // Add a video stream
    videoStream = avformat_new_stream(formatContext, nullptr);
    if (!videoStream) {
        fprintf(stderr, "Error creating video streamn");
        return -1;
    }

    // Set codec parameters, you may need to adjust these based on your needs
    AVCodecContext* codecContext = avcodec_alloc_context3(nullptr);
    codecContext->codec_id = outputFormat->video_codec;
    codecContext->codec_type = AVMEDIA_TYPE_VIDEO;
    codecContext->pix_fmt = AV_PIX_FMT_YUV420P;
    codecContext->width = 640;
    codecContext->height = 480;
    codecContext->time_base = { 1, 25 };

    // Open the video codec
    AVCodec* videoCodec = avcodec_find_encoder(codecContext->codec_id);
    if (!videoCodec) {
        fprintf(stderr, "Error finding video codecn");
        return -1;
    }

    if (avcodec_open2(codecContext, videoCodec, nullptr) < 0) {
        fprintf(stderr, "Error opening video codecn");
        return -1;
    }

    videoStream->codecpar->codec_id = codecContext->codec_id;
    videoStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
    videoStream->codecpar->format = codecContext->pix_fmt;
    videoStream->codecpar->width = codecContext->width;
    videoStream->codecpar->height = codecContext->height;

    if (avformat_write_header(formatContext, nullptr) < 0) {
        fprintf(stderr, "Error writing headern");
        return -1;
    }

    // Create a frame
    AVFrame* frame = av_frame_alloc();
    frame->format = codecContext->pix_fmt;
    frame->width = codecContext->width;
    frame->height = codecContext->height;
    av_frame_get_buffer(frame, 32);

    // Fill the frame with red color
    for (int y = 0; y < codecContext->height; ++y) {
        for (int x = 0; x < codecContext->width; ++x) {
            frame->data[0][y * frame->linesize[0] + x * 3] = 255;     // Red component
            frame->data[0][y * frame->linesize[0] + x * 3 + 1] = 0;   // Green component
            frame->data[0][y * frame->linesize[0] + x * 3 + 2] = 0;   // Blue component
        }
    }

    // Write video frames
    AVPacket packet;
    for (int i = 0; i < 100; ++i) {
        // Send the frame for encoding
        if (avcodec_send_frame(codecContext, frame) < 0) {
            fprintf(stderr, "Error sending a frame for encodingn");
            return -1;
        }

        // Receive the encoded packet
        while (avcodec_receive_packet(codecContext, &packet) == 0) {
            // Write the packet to the output file
            if (av_write_frame(formatContext, &packet) != 0) {
                fprintf(stderr, "Error writing video framen");
                return -1;
            }
            av_packet_unref(&packet);
        }
    }

    // Write the trailer
    if (av_write_trailer(formatContext) != 0) {
        fprintf(stderr, "Error writing trailern");
        return -1;
    }

    // Clean up resources
    av_frame_free(&frame);
    avcodec_free_context(&codecContext);
    avformat_free_context(formatContext);

    return 0;
}

Khám phá các thẻ bài đăng