16

iFrame(キーフレーム)を好きな位置に配置できるffmpegを使ったエンコーダーを書きたいです。チュートリアルや参考資料はどこにありますか?

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() への 2 番目の呼び出しに特に注意してください - ビデオの最後のフレームを取得する必要があるため、 MPEG ビデオは順不同でエンコードされるため、数フレームの遅延が発生する場合があります。

于 2010-02-24T18:02:44.550 に答える
4

api-example.cの最新バージョンは、http://ffmpeg.org/doxygen/trunk/doc_2examples_2decoding_encoding_8c-example.htmlにあります。

ビデオエンコーディング全体を単一の比較的短い機能で実行します。したがって、これはおそらく開始するのに適した場所です。コンパイルして実行します。そして、それがあなたが望むことをするまでそれを修正し始めます。

また、オーディオエンコーディングとオーディオおよびビデオデコーディングの例もあります。

于 2012-03-18T09:37:54.450 に答える
3

GStreamerにはまともなドキュメントがあり、多くの言語のバインディングがあり(ネイティブ API は C ですが)、 gstreamer-ffmpeg経由の H.263 を含む、プラグインを見つけることができるすべてのビデオ形式をサポートしています。

于 2010-02-23T08:58:19.673 に答える
1

あなたが Java プログラマーなら、Xuggler を使用してください。

于 2011-12-04T19:49:10.847 に答える
1

libavcodec ライブラリが必要になります。最初のステップでは、ffmpeg ソース コード内の ffplay.c ファイルでの使用について学ぶことができると思います。それはあなたに多くのことを教えてくれるでしょう。rtstegvideo.sourceforge.net でビデオに関する私のプロジェクトも確認できます。

この助けを願っています。

于 2010-03-17T09:48:41.357 に答える
0

FFmpeg 2.7 で実行可能な最小限の例

Ori Pessach の回答に基づいて、フォームのフレームを生成する最小限の例を以下に示します。

  • P
  • B
  • P
  • ...

フレーム タイプを制御するコードの主要部分は次のとおりです。

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 に答える