我想编写一个ffmpeg编码器,它可以将iFrame(关键帧)放在我想要的位置。我在哪里可以找到教程或参考资料?如何用ffmpeg编写一个显式控制关键帧位置的视频编码器?
P.S
是否有可能用mencoder或任何开源编码器做到这一点。我想编码H263文件。我正在编写用于linux的&。
我想编写一个ffmpeg编码器,它可以将iFrame(关键帧)放在我想要的位置。我在哪里可以找到教程或参考资料?如何用ffmpeg编写一个显式控制关键帧位置的视频编码器?
P.S
是否有可能用mencoder或任何开源编码器做到这一点。我想编码H263文件。我正在编写用于linux的&。
您需要查看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.
一些介绍性材料可用here和here,但我真的不知道它有多好。
您需要小心如何分配API调用所需的框架对象。在我看来,api-example.c是你最好的选择。查找函数video_encode_example() - 它很简洁,并且说明了所有需要担心的重要事情 - 特别注意对传递NULL图片参数的avcodec_encode_video()的第二次调用 - 它需要获取最后一帧视频MPEG视频按顺序编码,最终可能会延迟几帧。
顺便说一句,图片的'pict_type'成员的值是'AV_PICTURE_TYPE_I','AV_PICTURE_TYPE_P','AV_PICTURE_TYPE_B',等等...... – 2011-07-25 13:06:00
GStreamer有decent documentation,先后为number of languages绑定(虽然本机API为C),并支持所有的视频格式,你可以找到插件,包括通过gstreamer-ffmpeg H.263。
您将需要libavcodec库,对于第一步,我认为您可以了解它在ffmpeg源代码中的ffplay.c文件中的使用。它会告诉你很多。你也可以在rtstegvideo.sourceforge.net上查看我的项目。
希望得到这个帮助。
如果您是Java程序员,则使用Xuggler。
一个上行最新版本API-example.c的可以在http://ffmpeg.org/doxygen/trunk/doc_2examples_2decoding_encoding_8c-example.html
它执行了全部的视频编码在单个和相对短的功能被发现。所以这可能是一个很好的开始。编译并运行它。然后开始修改它,直到它达到你想要的。
它还具有音频编码和音频&视频解码示例。
最好是查看最新版本的官方文档:http://ffmpeg.org/doxygen/trunk/doc_2examples_2decoding_encoding_8c-example.html – 2013-12-27 10:25:55
上的FFmpeg 2.7
基于Ori Pessach's答案最小可运行的例子中,下面是产生形式的帧的最小例子。
控制帧类型代码的关键部分是:
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的执行,即使我设法克服:
#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 upstream。
你真的要做到这一点?
在大多数情况下,你最好只是控制AVCodecContext
全局参数。
如果新帧与前一帧完全不同,FFmpeg可以完成像使用关键帧这样的智能功能,而差分编码则不会获得太多效果。
例如,如果我们设置只是:
c->keyint_min = 600;
然后我们得到在上面的例子中,由于存在于所生成的视频4个突然变化帧这是合乎逻辑正好是4的关键帧。
..What语言? – Shoban 2010-02-18 14:40:21
我可以在C,C++编写,但教程或参考可以是任何一种语言。我只是想知道/了解ffmpeg的API。 – SunnyShah 2010-02-18 14:44:12