给AVFrame图像帧加上时间显示
时间: 2024-01-03 11:03:43 浏览: 154
给相片加时间
要给AVFrame图像帧加上时间显示,可以使用FFmpeg中提供的AVFilter来实现。具体步骤如下:
1. 创建AVFilterGraph对象和AVFilterContext对象。
```
AVFilterGraph *filter_graph = avfilter_graph_alloc();
AVFilterContext *buffer_filter_ctx = NULL;
AVFilterContext *timeoverlay_filter_ctx = NULL;
```
2. 创建buffer和timeoverlay AVFilter对象,并初始化其参数。
```
AVFilter *buffer_filter = avfilter_get_by_name("buffer");
AVFilter *timeoverlay_filter = avfilter_get_by_name("timeoverlay");
char args[512];
snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
frame->width, frame->height, frame->format,
time_base.num, time_base.den,
frame->sample_aspect_ratio.num, frame->sample_aspect_ratio.den);
avfilter_graph_create_filter(&buffer_filter_ctx, buffer_filter, "in", args, NULL, filter_graph);
avfilter_graph_create_filter(&timeoverlay_filter_ctx, timeoverlay_filter, "overlay", NULL, NULL, filter_graph);
av_opt_set_int(timeoverlay_filter_ctx, "fontsize", 20, AV_OPT_SEARCH_CHILDREN);
av_opt_set("timeformat", "%Y-%m-%d %H:%M:%S", 0, timeoverlay_filter_ctx);
```
其中,args参数表示输入视频帧的属性,timeformat参数表示时间格式,这里使用“年-月-日 时:分:秒”。
3. 将AVFilter对象连接起来。
```
avfilter_link(buffer_filter_ctx, 0, timeoverlay_filter_ctx, 0);
avfilter_link(timeoverlay_filter_ctx, 0, sink_filter_ctx, 0);
avfilter_graph_config(filter_graph, NULL);
```
4. 将AVFrame送入filter_graph进行处理。
```
av_buffersrc_add_frame_flags(buffer_filter_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF);
AVFrame *filtered_frame = av_frame_alloc();
int ret = av_buffersink_get_frame(sink_filter_ctx, filtered_frame);
```
5. 释放资源。
```
avfilter_graph_free(&filter_graph);
av_frame_free(&filtered_frame);
```
通过以上步骤,可以将AVFrame图像帧加上时间显示,生成一个新的AVFrame图像帧。
阅读全文