16

我想用 ffmpeg 编写一个编码器,它可以将 iFrame(关键帧)放在我想要的位置。我在哪里可以找到它的教程或参考资料?

PS
是否可以使用mencoder或任何开源编码器来做到这一点。我想编码 H263 文件。我正在 & 为 linux 下写作。

4

6 回答 6

19

您需要查看 libavcodec 文档 - 特别是 avcodec_encode_video()。我发现最好的可用文档在 ffmpeg 头文件和 ffmpeg 源代码提供的 API 示例源代码中。具体来说,请查看 libavcodec/api-example.c 甚至 ffmpeg.c。

要强制使用 I 帧,您需要将要编码的图片的 pict_type 成员设置为 1:1 是 I 帧,2 是 P 帧,我不记得 B 帧的代码是什么从我的头顶上掉下来......另外,key_frame 成员需要设置为 1。

一些介绍性材料可在此处此处获得,但我真的不知道它有多好。

您需要注意如何分配 API 调用所需的框架对象。在我看来,api-example.c 是你最好的选择。寻找函数 video_encode_example() - 它简洁并说明了您需要担心的所有重要事情 - 特别注意传递 NULL 图片参数的对 avcodec_encode_video() 的第二次调用 - 它需要获取视频的最后一帧,因为MPEG 视频是乱序编码的,您最终可能会延迟几帧。

于 2010-02-24T18:02:44.550 回答
4

可以在http://ffmpeg.org/doxygen/trunk/doc_2examples_2decoding_encoding_8c-example.html找到最新版本的 api-example.c

它在一个单一且相对较短的功能中完成整个视频编码。所以这可能是一个很好的起点。编译并运行它。然后开始修改它,直到它完成你想要的。

它还具有音频编码和音频和视频解码示例。

于 2012-03-18T09:37:54.450 回答
3

GStreamer不错的文档,有多种语言的绑定(尽管本机 API 是 C),并且支持您可以找到插件的任何视频格式,包括通过gstreamer-ffmpeg找到的 H.263 。

于 2010-02-23T08:58:19.673 回答
1

您将需要 libavcodec 库,第一步我认为您可以了解它在 ffmpeg 源代码中的 ffplay.c 文件中的使用。它会告诉你很多。您还可以在 rtstegvideo.sourceforge.net 上查看我的视频项目。

希望这有帮助。

于 2010-03-17T09:48:41.357 回答
1

如果您是 Java 程序员,请使用 Xuggler。

于 2011-12-04T19:49:10.847 回答
0

FFmpeg 2.7 上的最小可运行示例

根据Ori Pessach 的回答,下面是一个生成窗体框架的最小示例。

  • 一世
  • ...

控制帧类型的代码的关键部分是:

c = avcodec_alloc_context3(codec);
/* Minimal distance of I-frames. This is the maximum value allowed,
or else we get a warning at runtime. */
c->keyint_min = 600;
/* Or else it defaults to 0 b-frames are not allowed. */
c->max_b_frames = 1;

和:

frame->key_frame = 0;
switch (frame->pts % 4) {
    case 0:
        frame->key_frame = 1;
        frame->pict_type = AV_PICTURE_TYPE_I;
    break;
    case 1:
    case 3:
        frame->pict_type = AV_PICTURE_TYPE_P;
    break;
    case 2:
        frame->pict_type = AV_PICTURE_TYPE_B;
    break;
}

然后我们可以通过以下方式验证帧类型:

ffprobe -select_streams v \
    -show_frames \
    -show_entries frame=pict_type \
    -of csv \
    tmp.h264

如所述:https ://superuser.com/questions/885452/extracting-the-index-of-key-frames-from-a-video-using-ffmpeg

即使我试图克服它们,FFmpeg 也会强制执行一些规则:

  • 第一帧是 I 帧
  • 不能在 I 帧之前放置 B0 帧(TODO 为什么?)

生成的输出预览

#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libavutil/opt.h>
#include <libswscale/swscale.h>

static AVCodecContext *c = NULL;
static AVFrame *frame;
static AVPacket pkt;
static FILE *file;
struct SwsContext *sws_context = NULL;

/*
Convert RGB24 array to YUV. Save directly to the `frame`,
modifying its `data` and `linesize` fields
*/
static void ffmpeg_encoder_set_frame_yuv_from_rgb(uint8_t *rgb) {
    const int in_linesize[1] = { 3 * c->width };
    sws_context = sws_getCachedContext(sws_context,
            c->width, c->height, AV_PIX_FMT_RGB24,
            c->width, c->height, AV_PIX_FMT_YUV420P,
            0, 0, 0, 0);
    sws_scale(sws_context, (const uint8_t * const *)&rgb, in_linesize, 0,
            c->height, frame->data, frame->linesize);
}

/*
Generate 2 different images with four colored rectangles, each 25 frames long:

Image 1:

    black | red
    ------+-----
    green | blue

Image 2:

    yellow | red
    -------+-----
    green  | white
*/
uint8_t* generate_rgb(int width, int height, int pts, uint8_t *rgb) {
    int x, y, cur;
    rgb = realloc(rgb, 3 * sizeof(uint8_t) * height * width);
    for (y = 0; y < height; y++) {
        for (x = 0; x < width; x++) {
            cur = 3 * (y * width + x);
            rgb[cur + 0] = 0;
            rgb[cur + 1] = 0;
            rgb[cur + 2] = 0;
            if ((frame->pts / 25) % 2 == 0) {
                if (y < height / 2) {
                    if (x < width / 2) {
                        /* Black. */
                    } else {
                        rgb[cur + 0] = 255;
                    }
                } else {
                    if (x < width / 2) {
                        rgb[cur + 1] = 255;
                    } else {
                        rgb[cur + 2] = 255;
                    }
                }
            } else {
                if (y < height / 2) {
                    rgb[cur + 0] = 255;
                    if (x < width / 2) {
                        rgb[cur + 1] = 255;
                    } else {
                        rgb[cur + 2] = 255;
                    }
                } else {
                    if (x < width / 2) {
                        rgb[cur + 1] = 255;
                        rgb[cur + 2] = 255;
                    } else {
                        rgb[cur + 0] = 255;
                        rgb[cur + 1] = 255;
                        rgb[cur + 2] = 255;
                    }
                }
            }
        }
    }
    return rgb;
}

/* Allocate resources and write header data to the output file. */
void ffmpeg_encoder_start(const char *filename, int codec_id, int fps, int width, int height) {
    AVCodec *codec;
    int ret;
    codec = avcodec_find_encoder(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(1);
    }
    c->bit_rate = 400000;
    c->width = width;
    c->height = height;
    c->time_base.num = 1;
    c->time_base.den = fps;
    /* I, P, B frame placement parameters. */
    c->gop_size = 600;
    c->max_b_frames = 1;
    c->keyint_min = 600;
    c->pix_fmt = AV_PIX_FMT_YUV420P;
    if (codec_id == AV_CODEC_ID_H264)
        av_opt_set(c->priv_data, "preset", "slow", 0);
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }
    file = fopen(filename, "wb");
    if (!file) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }
    frame = av_frame_alloc();
    if (!frame) {
        fprintf(stderr, "Could not allocate video frame\n");
        exit(1);
    }
    frame->format = c->pix_fmt;
    frame->width  = c->width;
    frame->height = c->height;
    ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height, c->pix_fmt, 32);
    if (ret < 0) {
        fprintf(stderr, "Could not allocate raw picture buffer\n");
        exit(1);
    }
}

/*
Write trailing data to the output file
and free resources allocated by ffmpeg_encoder_start.
*/
void ffmpeg_encoder_finish(void) {
    uint8_t endcode[] = { 0, 0, 1, 0xb7 };
    int got_output, ret;
    do {
        fflush(stdout);
        ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
        if (ret < 0) {
            fprintf(stderr, "Error encoding frame\n");
            exit(1);
        }
        if (got_output) {
            fwrite(pkt.data, 1, pkt.size, file);
            av_packet_unref(&pkt);
        }
    } while (got_output);
    fwrite(endcode, 1, sizeof(endcode), file);
    fclose(file);
    avcodec_close(c);
    av_free(c);
    av_freep(&frame->data[0]);
    av_frame_free(&frame);
}

/*
Encode one frame from an RGB24 input and save it to the output file.
Must be called after ffmpeg_encoder_start, and ffmpeg_encoder_finish
must be called after the last call to this function.
*/
void ffmpeg_encoder_encode_frame(uint8_t *rgb) {
    int ret, got_output;
    ffmpeg_encoder_set_frame_yuv_from_rgb(rgb);
    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;
    switch (frame->pts % 4) {
        case 0:
            frame->key_frame = 1;
            frame->pict_type = AV_PICTURE_TYPE_I;
        break;
        case 1:
        case 3:
            frame->key_frame = 0;
            frame->pict_type = AV_PICTURE_TYPE_P;
        break;
        case 2:
            frame->key_frame = 0;
            frame->pict_type = AV_PICTURE_TYPE_B;
        break;
    }
    ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
    if (ret < 0) {
        fprintf(stderr, "Error encoding frame\n");
        exit(1);
    }
    if (got_output) {
        fwrite(pkt.data, 1, pkt.size, file);
        av_packet_unref(&pkt);
    }
}

/* Represents the main loop of an application which generates one frame per loop. */
static void encode_example(const char *filename, int codec_id) {
    int pts;
    int width = 320;
    int height = 240;
    uint8_t *rgb = NULL;
    ffmpeg_encoder_start(filename, codec_id, 25, width, height);
    for (pts = 0; pts < 100; pts++) {
        frame->pts = pts;
        rgb = generate_rgb(width, height, pts, rgb);
        ffmpeg_encoder_encode_frame(rgb);
    }
    ffmpeg_encoder_finish();
}

int main(void) {
    avcodec_register_all();
    encode_example("tmp.h264", AV_CODEC_ID_H264);
    encode_example("tmp.mpg", AV_CODEC_ID_MPEG1VIDEO);
    /* TODO: is this encoded correctly? Possible to view it without container? */
    /*encode_example("tmp.vp8", AV_CODEC_ID_VP8);*/
    return 0;
}

在 Ubuntu 15.10 上测试。GitHub 上游.

真的想这样做吗?

在大多数情况下,最好只控制AVCodecContext.

如果新帧与前一帧完全不同,FFmpeg 会做一些聪明的事情,比如使用关键帧,并且从差分编码中不会获得太多。

例如,如果我们只设置:

c->keyint_min = 600;

那么我们在上面的例子中正好得到了 4 个关键帧,这是合乎逻辑的,因为生成的视频有 4 个突然的帧变化。

于 2016-04-04T21:10:00.077 回答