FFMPEG中有一个类库:libavfilter。该类库提供了各种视音频过滤器。之前一直没有怎么使用过这个类库,最近看了一下它的使用说明,发现还是很强大的,有很多现成的filter供使用,完成视频的处理很方便。在此将它的一个例子基础上完成了一个水印叠加器,并且移植到了VC2010下,方便开发人员学习研究它的使用方法。

该例子完成了一个水印叠加的功能。可以将一张透明背景的PNG图片作为水印叠加到一个视频文件上。需要注意的是,其叠加工作是在解码后的YUV像素数据的基础上完成的。程序支持使用SDL显示叠加后的YUV数据。也可以将叠加后的YUV输出成文件。

流程图

下面附一张使用FFmpeg的libavfilter的流程图。可以看出使用libavfilter还是需要做不少的初始化工作的。但是使用的时候还是比较简单的,就两个重要的函数:av_buffersrc_add_frame()和av_buffersink_get_buffer_ref()。

PS:这张图中只列出了和libavfilter有关的函数和结构体。代码中其它函数可以参考:100行代码实现最简单的基于FFMPEG+SDL的视频播放器(SDL1.x)

代码

下面直接贴上代码:

  1. /**
  2. * 最简单的基于FFmpeg的AVFilter例子(叠加水印)
  3. * Simplest FFmpeg AVfilter Example (Watermark)
  4. *
  5. * 雷霄骅 Lei Xiaohua
  6. * leixiaohua1020@126.com
  7. * 中国传媒大学/数字电视技术
  8. * Communication University of China / Digital TV Technology
  9. * http://blog.csdn.net/leixiaohua1020
  10. *
  11. * 本程序使用FFmpeg的AVfilter实现了视频的水印叠加功能。
  12. * 可以将一张PNG图片作为水印叠加到视频上。
  13. * 是最简单的FFmpeg的AVFilter方面的教程。
  14. * 适合FFmpeg的初学者。
  15. *
  16. * This software uses FFmpeg's AVFilter to add watermark in a video file.
  17. * It can add a PNG format picture as watermark to a video file.
  18. * It's the simplest example based on FFmpeg's AVFilter.
  19. * Suitable for beginner of FFmpeg
  20. *
  21. */
  22. #include <stdio.h>
  23.  
  24. #define __STDC_CONSTANT_MACROS
  25.  
  26. #ifdef _WIN32
  27. #define snprintf _snprintf
  28. //Windows
  29. extern "C"
  30. {
  31. #include "libavcodec/avcodec.h"
  32. #include "libavformat/avformat.h"
  33. #include "libavfilter/avfiltergraph.h"
  34. #include "libavfilter/buffersink.h"
  35. #include "libavfilter/buffersrc.h"
  36. #include "libavutil/avutil.h"
  37. #include "libswscale/swscale.h"
  38. #include "SDL/SDL.h"
  39. };
  40. #else
  41. //Linux...
  42. #ifdef __cplusplus
  43. extern "C"
  44. {
  45. #endif
  46. #include <libavcodec/avcodec.h>
  47. #include <libavformat/avformat.h>
  48. #include <libavfilter/avfiltergraph.h>
  49. #include <libavfilter/buffersink.h>
  50. #include <libavfilter/buffersrc.h>
  51. #include <libavutil/avutil.h>
  52. #include <libswscale/swscale.h>
  53. #include <SDL/SDL.h>
  54. #ifdef __cplusplus
  55. };
  56. #endif
  57. #endif
  58.  
  59. //Enable SDL?
  60. #define ENABLE_SDL 1
  61. //Output YUV data?
  62. #define ENABLE_YUVFILE 1
  63.  
  64. const char *filter_descr = "movie=my_logo.png[wm];[in][wm]overlay=5:5[out]";
  65.  
  66. static AVFormatContext *pFormatCtx;
  67. static AVCodecContext *pCodecCtx;
  68. AVFilterContext *buffersink_ctx;
  69. AVFilterContext *buffersrc_ctx;
  70. AVFilterGraph *filter_graph;
  71. static int video_stream_index = -1;
  72.  
  73. static int open_input_file(const char *filename)
  74. {
  75. int ret;
  76. AVCodec *dec;
  77.  
  78. if ((ret = avformat_open_input(&pFormatCtx, filename, NULL, NULL)) < 0) {
  79. printf( "Cannot open input file\n");
  80. return ret;
  81. }
  82.  
  83. if ((ret = avformat_find_stream_info(pFormatCtx, NULL)) < 0) {
  84. printf( "Cannot find stream information\n");
  85. return ret;
  86. }
  87.  
  88. /* select the video stream */
  89. ret = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
  90. if (ret < 0) {
  91. printf( "Cannot find a video stream in the input file\n");
  92. return ret;
  93. }
  94. video_stream_index = ret;
  95. pCodecCtx = pFormatCtx->streams[video_stream_index]->codec;
  96.  
  97. /* init the video decoder */
  98. if ((ret = avcodec_open2(pCodecCtx, dec, NULL)) < 0) {
  99. printf( "Cannot open video decoder\n");
  100. return ret;
  101. }
  102.  
  103. return 0;
  104. }
  105.  
  106. static int init_filters(const char *filters_descr)
  107. {
  108. char args[512];
  109. int ret;
  110. AVFilter *buffersrc = avfilter_get_by_name("buffer");
  111. AVFilter *buffersink = avfilter_get_by_name("ffbuffersink");
  112. AVFilterInOut *outputs = avfilter_inout_alloc();
  113. AVFilterInOut *inputs = avfilter_inout_alloc();
  114. enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };
  115. AVBufferSinkParams *buffersink_params;
  116.  
  117. filter_graph = avfilter_graph_alloc();
  118.  
  119. /* buffer video source: the decoded frames from the decoder will be inserted here. */
  120. snprintf(args, sizeof(args),
  121. "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
  122. pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
  123. pCodecCtx->time_base.num, pCodecCtx->time_base.den,
  124. pCodecCtx->sample_aspect_ratio.num, pCodecCtx->sample_aspect_ratio.den);
  125.  
  126. ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
  127. args, NULL, filter_graph);
  128. if (ret < 0) {
  129. printf("Cannot create buffer source\n");
  130. return ret;
  131. }
  132.  
  133. /* buffer video sink: to terminate the filter chain. */
  134. buffersink_params = av_buffersink_params_alloc();
  135. buffersink_params->pixel_fmts = pix_fmts;
  136. ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
  137. NULL, buffersink_params, filter_graph);
  138. av_free(buffersink_params);
  139. if (ret < 0) {
  140. printf("Cannot create buffer sink\n");
  141. return ret;
  142. }
  143.  
  144. /* Endpoints for the filter graph. */
  145. outputs->name = av_strdup("in");
  146. outputs->filter_ctx = buffersrc_ctx;
  147. outputs->pad_idx = 0;
  148. outputs->next = NULL;
  149.  
  150. inputs->name = av_strdup("out");
  151. inputs->filter_ctx = buffersink_ctx;
  152. inputs->pad_idx = 0;
  153. inputs->next = NULL;
  154.  
  155. if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,
  156. &inputs, &outputs, NULL)) < 0)
  157. return ret;
  158.  
  159. if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
  160. return ret;
  161. return 0;
  162. }
  163.  
  164. int main(int argc, char* argv[])
  165. {
  166. int ret;
  167. AVPacket packet;
  168. AVFrame *pFrame;
  169. AVFrame *pFrame_out;
  170.  
  171. int got_frame;
  172.  
  173. av_register_all();
  174. avfilter_register_all();
  175.  
  176. if ((ret = open_input_file("cuc_ieschool.flv")) < 0)
  177. goto end;
  178. if ((ret = init_filters(filter_descr)) < 0)
  179. goto end;
  180.  
  181. #if ENABLE_YUVFILE
  182. FILE *fp_yuv=fopen("test.yuv","wb+");
  183. #endif
  184. #if ENABLE_SDL
  185. SDL_Surface *screen;
  186. SDL_Overlay *bmp;
  187. SDL_Rect rect;
  188. if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
  189. printf( "Could not initialize SDL - %s\n", SDL_GetError());
  190. return -1;
  191. }
  192. screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 0, 0);
  193. if(!screen) {
  194. printf("SDL: could not set video mode - exiting\n");
  195. return -1;
  196. }
  197. bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen);
  198.  
  199. SDL_WM_SetCaption("Simplest FFmpeg Video Filter",NULL);
  200. #endif
  201.  
  202. pFrame=av_frame_alloc();
  203. pFrame_out=av_frame_alloc();
  204.  
  205. /* read all packets */
  206. while (1) {
  207.  
  208. ret = av_read_frame(pFormatCtx, &packet);
  209. if (ret< 0)
  210. break;
  211.  
  212. if (packet.stream_index == video_stream_index) {
  213. got_frame = 0;
  214. ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_frame, &packet);
  215. if (ret < 0) {
  216. printf( "Error decoding video\n");
  217. break;
  218. }
  219.  
  220. if (got_frame) {
  221. pFrame->pts = av_frame_get_best_effort_timestamp(pFrame);
  222.  
  223. /* push the decoded frame into the filtergraph */
  224. if (av_buffersrc_add_frame(buffersrc_ctx, pFrame) < 0) {
  225. printf( "Error while feeding the filtergraph\n");
  226. break;
  227. }
  228.  
  229. /* pull filtered pictures from the filtergraph */
  230. while (1) {
  231.  
  232. ret = av_buffersink_get_frame(buffersink_ctx, pFrame_out);
  233. if (ret < 0)
  234. break;
  235.  
  236. printf("Process 1 frame!\n");
  237.  
  238. if (pFrame_out->format==AV_PIX_FMT_YUV420P) {
  239. #if ENABLE_YUVFILE
  240. //Y, U, V
  241. for(int i=0;i<pFrame_out->height;i++){
  242. fwrite(pFrame_out->data[0]+pFrame_out->linesize[0]*i,1,pFrame_out->width,fp_yuv);
  243. }
  244. for(int i=0;i<pFrame_out->height/2;i++){
  245. fwrite(pFrame_out->data[1]+pFrame_out->linesize[1]*i,1,pFrame_out->width/2,fp_yuv);
  246. }
  247. for(int i=0;i<pFrame_out->height/2;i++){
  248. fwrite(pFrame_out->data[2]+pFrame_out->linesize[2]*i,1,pFrame_out->width/2,fp_yuv);
  249. }
  250. #endif
  251.  
  252. #if ENABLE_SDL
  253. SDL_LockYUVOverlay(bmp);
  254. int y_size=pFrame_out->width*pFrame_out->height;
  255. memcpy(bmp->pixels[0],pFrame_out->data[0],y_size); //Y
  256. memcpy(bmp->pixels[2],pFrame_out->data[1],y_size/4); //U
  257. memcpy(bmp->pixels[1],pFrame_out->data[2],y_size/4); //V
  258. bmp->pitches[0]=pFrame_out->linesize[0];
  259. bmp->pitches[2]=pFrame_out->linesize[1];
  260. bmp->pitches[1]=pFrame_out->linesize[2];
  261. SDL_UnlockYUVOverlay(bmp);
  262. rect.x = 0;
  263. rect.y = 0;
  264. rect.w = pFrame_out->width;
  265. rect.h = pFrame_out->height;
  266. SDL_DisplayYUVOverlay(bmp, &rect);
  267. //Delay 40ms
  268. SDL_Delay(40);
  269. #endif
  270. }
  271. av_frame_unref(pFrame_out);
  272. }
  273. }
  274. av_frame_unref(pFrame);
  275. }
  276. av_free_packet(&packet);
  277. }
  278. #if ENABLE_YUVFILE
  279. fclose(fp_yuv);
  280. #endif
  281.  
  282. end:
  283. avfilter_graph_free(&filter_graph);
  284. if (pCodecCtx)
  285. avcodec_close(pCodecCtx);
  286. avformat_close_input(&pFormatCtx);
  287.  
  288. if (ret < 0 && ret != AVERROR_EOF) {
  289. char buf[1024];
  290. av_strerror(ret, buf, sizeof(buf));
  291. printf("Error occurred: %s\n", buf);
  292. return -1;
  293. }
  294.  
  295. return 0;
  296. }

  

结果

程序的运行效果如图所示。

需要叠加的水印为一张PNG(透明)图片(在这里是my_logo.png)。

需要叠加的视频为一个普通的FLV格式的视频(在这里是cuc_ieschool.flv)。

程序运行的时候,会通过SDL显示水印叠加的结果,如图所示。此外,也可以将水印叠加后的解码数据输出成文件。

注:SDL显示和输出YUV可以通过程序最前面的宏控制:

  1. #define ENABLE_SDL 1
  2. #define ENABLE_YUVFILE 1

  

输出的YUV文件如图所示。

原文链接:http://blog.csdn.net/leixiaohua1020/article/details/29368911

(一) ffmpeg filter学习-使用流程的更多相关文章

  1. (三) ffmpeg filter学习-编写自己的filter

    目录 目录 什么是ffmpeg filter 如何使用ffmpeg filter 1 将输入的1920x1080缩小到960x540输出 2 为视频添加logo 3 去掉视频的logo 自己写一个过滤 ...

  2. (二) ffmpeg filter学习--混音实现

    Audio 混音实现 从FFMPEG原生代码doc/examples/filtering_audio.c修改而来. ffmpeg版本信息 ffmpeg version N-82997-g557c0df ...

  3. (四) ffmpeg filter学习-filter命令学习

    http://blog.csdn.net/joee33/article/details/51946712 http://blog.csdn.net/tkp2014/article/details/53 ...

  4. FFmpeg Basic学习笔记(3)

    视频叠加--画中画效果 overlay video filter 视频叠加比较常见的应用场景是台标的叠加,在画面左上角.右上角.左下角或右下角叠加台标. ffmpeg提供的overlay filter ...

  5. FFmpeg Basics学习笔记(2)

    帧率 fps的概念 帧率,单位FPS(frame per second), 用于衡量视频每秒的处理帧数,对于编码器而言说明编码器在1s的编码的速度,通常可以使用一帧的编码时间倒数简单计算:对于解码器而 ...

  6. FFmpeg再学习 -- SDL 环境搭建和视频显示

    继续看雷霄骅的 课程资料 - 基于FFmpeg+SDL的视频播放器的制作 一.SDL 简介 参看:WIKI -- Simple DirectMedia Layer 参看:最简单的视音频播放示例9:SD ...

  7. FFmpeg再学习 -- FFmpeg解码知识

    继续看雷霄骅的 课程资料 - 基于FFmpeg+SDL的视频播放器的制作 前面用了五个篇幅来讲 FFmpeg,其主要目的是为实现将图片转视频的功能. 总的来说,对于 FFmepg 多少有一些了解了.但 ...

  8. 【FTP】Wireshark学习FTP流程

    一.Wireshark概述 在windows下, 图1 Wireshark界面展示(基于1.99.1) Wireshark是通过底层的winpcap来实现抓包的.winpcap是用于网络封包抓取的一套 ...

  9. Java虚拟机JVM学习01 流程概述

    Java虚拟机JVM学习01 流程概述 Java虚拟机与程序的生命周期 一个运行时的Java虚拟机(JVM)负责运行一个Java程序. 当启动一个Java程序时,一个虚拟机实例诞生:当程序关闭退出,这 ...

随机推荐

  1. Could not initialize class sun.awt.X11FontManager 异常问题

    查了很多资料都是建议在  catalina.sh 中增加 -Djava.awt.headless=true \  的配置. 加了没有效果. 后来发现是因为JDK版本问题,将JDK 8换成 JDK 7后 ...

  2. 再也不学AJAX了!(三)跨域获取资源 ② - JSONP & CORS

    浏览器的"同源策略"固然保障了互联网世界的数据隐私与数据安全,但是如果当我们需要使用AJAX跨域请求资源时,"同源策略"又会成为开发者的阻碍.在本文中,我们会简 ...

  3. HTTP协议Keep-Alive模式详解和HTTP头字段总结

    1.什么是Keep-Alive模式? 我们知道HTTP协议采用“请求-应答”模式,当使用普通模式,即非KeepAlive模式时,每个请求/应答客户和服务器都要新建一个连接,完成 之后立即断开连接(HT ...

  4. SpringMVC基于注解方式的quartz

    项目框架: SpringMVC.MyBatis.JSP 1. 首先配置spring.xml文件 <?xml version="1.0" encoding="UTF- ...

  5. You only look once

    计算MAP https://www.zhihu.com/question/53405779 http://tarangshah.com/blog/2018-01-27/what-is-map-unde ...

  6. 03_MySQL DQL_排序查询

    #进阶3:排序查询/*语法: select 查询列表 from 表名 [where 筛选条件] order by 排序列表 [asc|desc] 特点: 1.asc升序,desc降序, 如果都不写,默 ...

  7. Quartz(自动任务)中的触发器Trigger

    1.Quartz中的触发器TriggerJob 包含了要执行任务的逻辑,但是 Job 对何时该执行却一无所知.这个事情留给了 Trigger.Quartz Trigger 继承了抽象的 org.qua ...

  8. SSH密钥登陆免密码方法

    原帖地址:http://ask.apelearn.com/question/798 用Putty实现A机器远程登陆B机器,具体实现请看链接:http://www.cnblogs.com/ImJerry ...

  9. Chrome自动化搭建

    工具安装: 1.selenium-java-2.53.0-srcs.jar 2.selenium-java-2.53.0.jar 3.下载chromedriver.exe(下载地址:http://ch ...

  10. WPF应用的一些小总结(模板、样式,上下文)

    1: 后台:XXX.DataContext = ViewModel(数据对象)(XXX可以是当前的UserControl.Windows.Grid.TextBlock.Lable等UI控件,选择不同的 ...