ffmpeg - 使用 FFMPEG 将 YUV 帧转换为 RGBA 帧

标签 ffmpeg rgb codec yuv

我想开发一个应用程序,它能够使用 ffmpeg 库将 YUV 帧转换为 RGBA 帧。
我已经开始编写这段代码:

void Decode::video_encode_example(const char *filename, int codec_id)
{
    AVCodec *codec;
    AVCodecContext *c= NULL;
    int i, ret, x, y, got_output;
    FILE *f;
    AVFrame *frame;
    AVPacket pkt;
    uint8_t endcode[] = { 0, 0, 1, 0xb7 };

    printf("Encode video file %s\n", filename);

    /* find the mpeg1 video encoder */
    codec = avcodec_find_encoder((enum AVCodecID)codec_id);

    if (!codec) {
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }

    c = avcodec_alloc_context3(codec);
    if (!c) {
        fprintf(stderr, "Could not allocate video codec context\n");
        exit(2);
    }

    /* put sample parameters */
    c->bit_rate = 400000;
    /* resolution must be a multiple of two */
    c->width = 352; // Avant c'était du 352x288
    c->height = 288;
    /* frames per second */
    c->time_base = (AVRational){1,25};
    /* emit one intra frame every ten frames
     * check frame pict_type before passing frame
     * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
     * then gop_size is ignored and the output of encoder
     * will always be I frame irrespective to gop_size
     */
    c->gop_size = 10;
    c->max_b_frames = 1;
    printf("Avant\n");
    c->pix_fmt = PIX_FMT_RGBA;// Avant c'était AV_PIX_FMT_YUV420P
    printf("Après\n");
    if (codec_id == AV_CODEC_ID_H264)
        av_opt_set(c->priv_data, "preset", "slow", 0);

    /* open it */
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(3);
    }

    f = fopen(filename, "wb");
    if (!f) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(4);
    }

    frame = avcodec_alloc_frame();// Dans une version plus récente c'est av_frame_alloc
    if (!frame) {
        fprintf(stderr, "Could not allocate video frame\n");
        exit(5);
    }
    frame->format = c->pix_fmt;
    frame->width  = c->width;
    frame->height = c->height;

但是,每次我运行此应用程序时,我的 Linux 终端中都会出现以下错误:

[mpeg2video @ 0x10c7040] 不支持指定的 pix_fmt

请问你能帮帮我吗 ?

最佳答案

我不确定您如何相信您的代码与您的问题相关;你的问题表明你想做一个从 YUV 到 RGB 的像素格式转换,例如你可以这样做。使用 ffmpeg 的 libswscale .但是,您的代码正在创建一个 MPEG-1/2 编码器对象并尝试将 RGB 输入数据编码为 MPEG-1/2。这是不可能的,ffmpeg的MPEG-1/2编码器只支持YUV420P。除了确定您是否要编码 MPEG-1/2 视频(在这种情况下您的输入应该是 YUV420P,而不是 RGBA,或者您是否要进行像素格式转换)之外,我不太确定推荐什么如果你应该使用 libswscale ...

关于ffmpeg - 使用 FFMPEG 将 YUV 帧转换为 RGBA 帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30510473/

相关文章:

c++ - 如果 extern "C"包含与 Qt 库冲突怎么办?

linux - 创建/删除许多硬链接(hard link)的缺点?

python - 一个非常简单的轻量级视频编辑器

file - 新的音频文件格式

android - 如何创建自定义媒体播放器以在 Android 中播放自定义编解码器?

loops - 如何用ffmpeg循环一帧?所有其他框架都应该指向第一个没有变化,也许就像一个回避

android - 使用 ffmpeg-android 连接视频文件

iOS 获得不同于编程的其他颜色

python - 如何使用 Gimp/OpenCV Color 将图像分成彩色 RGB 图层?

python - 在 python 中显示 rgb 矩阵图像