ffmpeg 视频教程 添加水印附源码
本文主要讲述如何利用Ffmpeg向视频文件 添加水印这一功能,文中最后会给出源代码下载地址以及视频
下载地址,视频除了讲述添加水印的基本原理以及代码实现,还提到了要注意的一些地方,因为直接运行
demo源码可能会有问题。利用Ffmpeg向视频文件添加水印的基本原理是将视频文件的视频包解码成一帧帧
“Frame”,通过ffmpeg Filter(overlay)实现待添加水印与“Frame”的叠加,最后将叠加后的视频帧进行编码
并将编码后的数据写到输出文件里。基本的流程如下图所示:
图1 ffmpeg 添加水印基本流程
了解了添加水印的基本原理及流程以后,下面给出代码部分:
一 打开输入源:
与以往的打开输入源的方法不同,这里的多了一个入参,根据输入的参数不同既可打开视频文件,可以打开水印图片。
- int OpenInput(char *fileName,int inputIndex)
- {
- context[inputIndex] = avformat_alloc_context();
- context[inputIndex]->interrupt_callback.callback = interrupt_cb;
- AVDictionary *format_opts = nullptr;
- int ret = avformat_open_input(&context[inputIndex], fileName, nullptr, &format_opts);
- if(ret < 0)
- {
- return ret;
- }
- ret = avformat_find_stream_info(context[inputIndex],nullptr);
- av_dump_format(context[inputIndex], 0, fileName, 0);
- if(ret >= 0)
- {
- std::cout <<"open input stream successfully" << endl;
- }
- return ret;
- }
二. 读取视频包(图片帧)
- shared_ptr<AVPacket> ReadPacketFromSource(int inputIndex)
- {
- std::shared_ptr<AVPacket> packet(static_cast<AVPacket*>(av_malloc(sizeof(AVPacket))), [&](AVPacket *p) { av_packet_free(&p); av_freep(&p); });
- av_init_packet(packet.get());
- int ret = av_read_frame(context[inputIndex], packet.get());
- if(ret >= 0)
- {
- return packet;
- }
- else
- {
- return nullptr;
- }
- }
三. 创建输出上下文
- int OpenOutput(char *fileName,int inputIndex)
- {
- int ret = 0;
- ret = avformat_alloc_output_context2(&outputContext, nullptr, "mpegts", fileName);
- if(ret < 0)
- {
- goto Error;
- }
- ret = avio_open2(&outputContext->pb, fileName, AVIO_FLAG_READ_WRITE,nullptr, nullptr);
- if(ret < 0)
- {
- goto Error;
- }
- for(int i = 0; i < context[inputIndex]->nb_streams; i++)
- {
- AVStream * stream = avformat_new_stream(outputContext, outPutEncContext->codec);
- stream->codec = outPutEncContext;
- if(ret < 0)
- {
- goto Error;
- }
- }
- av_dump_format(outputContext, 0, fileName, 1);
- ret = avformat_write_header(outputContext, nullptr);
- if(ret < 0)
- {
- goto Error;
- }
- if(ret >= 0)
- cout <<"open output stream successfully" << endl;
- return ret ;
- Error:
- if(outputContext)
- {
- avformat_close_input(&outputContext);
- }
- return ret ;
- }
四. 初始化编解码code
- int InitEncoderCodec( int iWidth, int iHeight,int inputIndex)
- {
- AVCodec * pH264Codec = avcodec_find_encoder(AV_CODEC_ID_H264);
- if(NULL == pH264Codec)
- {
- printf("%s", "avcodec_find_encoder failed");
- return -1;
- }
- outPutEncContext = avcodec_alloc_context3(pH264Codec);
- outPutEncContext->gop_size = 30;
- outPutEncContext->has_b_frames = 0;
- outPutEncContext->max_b_frames = 0;
- outPutEncContext->codec_id = pH264Codec->id;
- outPutEncContext->time_base.num =context[inputIndex]->streams[0]->codec->time_base.num;
- outPutEncContext->time_base.den = context[inputIndex]->streams[0]->codec->time_base.den;
- outPutEncContext->pix_fmt = *pH264Codec->pix_fmts;
- outPutEncContext->width = iWidth;
- outPutEncContext->height = iHeight;
- outPutEncContext->me_subpel_quality = 0;
- outPutEncContext->refs = 1;
- outPutEncContext->scenechange_threshold = 0;
- outPutEncContext->trellis = 0;
- AVDictionary *options = nullptr;
- outPutEncContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
- int ret = avcodec_open2(outPutEncContext, pH264Codec, &options);
- if (ret < 0)
- {
- printf("%s", "open codec failed");
- return ret;
- }
- return 1;
- }
- int InitDecodeCodec(AVCodecID codecId,int inputIndex)
- {
- auto codec = avcodec_find_decoder(codecId);
- if(!codec)
- {
- return -1;
- }
- decoderContext[inputIndex] = context[inputIndex]->streams[0]->codec;
- if (!decoderContext) {
- fprintf(stderr, "Could not allocate video codec context\n");
- exit(1);
- }
- int ret = avcodec_open2(decoderContext[inputIndex], codec, NULL);
- return ret;
- }
五. 初始化Filter
- int InitInputFilter(AVFilterInOut *input,const char *filterName, int inputIndex)
- {
- char args[512];
- memset(args,0, sizeof(args));
- AVFilterContext *padFilterContext = input->filter_ctx;
- auto filter = avfilter_get_by_name("buffer");
- auto codecContext = context[inputIndex]->streams[0]->codec;
- sprintf_s(args, sizeof(args),
- "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
- codecContext->width, codecContext->height, codecContext->pix_fmt,
- codecContext->time_base.num, codecContext->time_base.den /codecContext->ticks_per_frame,
- codecContext->sample_aspect_ratio.num, codecContext->sample_aspect_ratio.den);
- int ret = avfilter_graph_create_filter(&inputFilterContext[inputIndex],filter,filterName, args,
- NULL, filter_graph);
- if(ret < 0) return ret;
- ret = avfilter_link(inputFilterContext[inputIndex],0,padFilterContext,input->pad_idx);
- return ret;
- }
- int InitOutputFilter(AVFilterInOut *output,const char *filterName)
- {
- AVFilterContext *padFilterContext = output->filter_ctx;
- auto filter = avfilter_get_by_name("buffersink");
- int ret = avfilter_graph_create_filter(&outputFilterContext,filter,filterName, NULL,
- NULL, filter_graph);
- if(ret < 0) return ret;
- ret = avfilter_link(padFilterContext,output->pad_idx,outputFilterContext,0);
- return ret;
- }
六. Demo
- int _tmain(int argc, _TCHAR* argv[])
- {
- //string fileInput = "D:\\test11.ts";
- string fileInput[2];
- fileInput[0]= "F:\\test.ts";
- fileInput[1]= "F:\\test1.jpg";
- string fileOutput = "F:\\codeoutput.ts";
- std::thread decodeTask;
- Init();
- int ret = 0;
- for(int i = 0; i < 2;i++)
- {
- if(OpenInput((char *)fileInput[i].c_str(),i) < 0)
- {
- cout << "Open file Input 0 failed!" << endl;
- this_thread::sleep_for(chrono::seconds(10));
- return 0;
- }
- ret = InitDecodeCodec(context[i]->streams[0]->codec->codec_id,i);
- if(ret <0)
- {
- cout << "InitDecodeCodec failed!" << endl;
- this_thread::sleep_for(chrono::seconds(10));
- return 0;
- }
- }
- ret = InitEncoderCodec(decoderContext[0]->width,decoderContext[0]->height,0);
- if(ret < 0)
- {
- cout << "open eccoder failed ret is " << ret<<endl;
- cout << "InitEncoderCodec failed!" << endl;
- this_thread::sleep_for(chrono::seconds(10));
- return 0;
- }
- //ret = InitFilter(outPutEncContext);
- if(OpenOutput((char *)fileOutput.c_str(),0) < 0)
- {
- cout << "Open file Output failed!" << endl;
- this_thread::sleep_for(chrono::seconds(10));
- return 0;
- }
- AVFrame *pSrcFrame[2];
- pSrcFrame[0] = av_frame_alloc();
- pSrcFrame[1] = av_frame_alloc();
- AVFrame *inputFrame[2];
- inputFrame[0] = av_frame_alloc();
- inputFrame[1] = av_frame_alloc();
- auto filterFrame = av_frame_alloc();
- int got_output = 0;
- int64_t timeRecord = 0;
- int64_t firstPacketTime = 0;
- int64_t outLastTime = av_gettime();
- int64_t inLastTime = av_gettime();
- int64_t videoCount = 0;
- filter_graph = avfilter_graph_alloc();
- if(!filter_graph)
- {
- cout <<"graph alloc failed"<<endl;
- goto End;
- }
- avfilter_graph_parse2(filter_graph, filter_descr, &inputs, &outputs);
- InitInputFilter(inputs,"MainFrame",0);
- InitInputFilter(inputs->next,"OverlayFrame",1);
- InitOutputFilter(outputs,"output");
- FreeInout();
- ret = avfilter_graph_config(filter_graph, NULL);
- if(ret < 0)
- {
- goto End;
- }
- decodeTask.swap(thread([&]{
- bool ret = true;
- while(ret)
- {
- auto packet = ReadPacketFromSource(1);
- ret = DecodeVideo(packet.get(),pSrcFrame[1],1);
- if(ret) break;
- }
- }));
- decodeTask.join();
- while(true)
- {
- outLastTime = av_gettime();
- auto packet = ReadPacketFromSource(0);
- if(packet)
- {
- if(DecodeVideo(packet.get(),pSrcFrame[0],0))
- {
- av_frame_ref( inputFrame[0],pSrcFrame[0]);
- if (av_buffersrc_add_frame_flags(inputFilterContext[0], inputFrame[0], AV_BUFFERSRC_FLAG_PUSH) >= 0)
- {
- pSrcFrame[1]->pts = pSrcFrame[0]->pts;
- //av_frame_ref( inputFrame[1],pSrcFrame[1]);
- if(av_buffersrc_add_frame_flags(inputFilterContext[1],pSrcFrame[1], AV_BUFFERSRC_FLAG_PUSH) >= 0)
- {
- ret = av_buffersink_get_frame_flags(outputFilterContext, filterFrame,AV_BUFFERSINK_FLAG_NO_REQUEST);
- //this_thread::sleep_for(chrono::milliseconds(10));
- if ( ret >= 0)
- {
- std::shared_ptr<AVPacket> pTmpPkt(static_cast<AVPacket*>(av_malloc(sizeof(AVPacket))), [&](AVPacket *p) { av_packet_free(&p); av_freep(&p); });
- av_init_packet(pTmpPkt.get());
- pTmpPkt->data = NULL;
- pTmpPkt->size = 0;
- ret = avcodec_encode_video2(outPutEncContext, pTmpPkt.get(), filterFrame, &got_output);
- if (ret >= 0 && got_output)
- {
- int ret = av_write_frame(outputContext, pTmpPkt.get());
- }
- //this_thread::sleep_for(chrono::milliseconds(10));
- }
- av_frame_unref(filterFrame);
- }
- }
- }
- }
- else break;
- }
- CloseInput(0);
- CloseOutput();
- std::cout <<"Transcode file end!" << endl;
- End:
- this_thread::sleep_for(chrono::hours(10));
- return 0;
- }
如需交流,可以加QQ群1038388075,766718184,或者QQ:350197870
视频下载地址:http://www.chungen90.com/?news_3/
Demo下载地址: http://www.chungen90.com/?news_2
ffmpeg 视频教程 添加水印附源码的更多相关文章
- 为SRS流媒体服务器添加HLS加密功能(附源码)
为SRS流媒体服务器添加HLS加密功能(附源码) 之前测试使用过nginx的HLS加密功能,会使用到一个叫做nginx-rtmp-module的插件,但此插件很久不更新了,网上搜索到一个中国制造的叫做 ...
- 一步步实现windows版ijkplayer系列文章之七——终结篇(附源码)
一步步实现windows版ijkplayer系列文章之一--Windows10平台编译ffmpeg 4.0.2,生成ffplay 一步步实现windows版ijkplayer系列文章之二--Ijkpl ...
- 在网站开发中很有用的8个 jQuery 效果【附源码】
jQuery 作为最优秀 JavaScript 库之一,改变了很多人编写 JavaScript 的方式.它简化了 HTML 文档遍历,事件处理,动画和 Ajax 交互,而且有成千上万的成熟 jQuer ...
- Web 开发中很实用的10个效果【附源码下载】
在工作中,我们可能会用到各种交互效果.而这些效果在平常翻看文章的时候碰到很多,但是一时半会又想不起来在哪,所以养成知识整理的习惯是很有必要的.这篇文章给大家推荐10个在 Web 开发中很有用的效果,记 ...
- MVC系列——MVC源码学习:打造自己的MVC框架(二:附源码)
前言:上篇介绍了下 MVC5 的核心原理,整篇文章比较偏理论,所以相对比较枯燥.今天就来根据上篇的理论一步一步进行实践,通过自己写的一个简易MVC框架逐步理解,相信通过这一篇的实践,你会对MVC有一个 ...
- C#进阶系列——一步一步封装自己的HtmlHelper组件:BootstrapHelper(三:附源码)
前言:之前的两篇封装了一些基础的表单组件,这篇继续来封装几个基于bootstrap的其他组件.和上篇不同的是,这篇的有几个组件需要某些js文件的支持. 本文原创地址:http://www.cnblog ...
- 轻量级通信引擎StriveEngine —— C/S通信demo(2) —— 使用二进制协议 (附源码)
在网络上,交互的双方基于TCP或UDP进行通信,通信协议的格式通常分为两类:文本消息.二进制消息. 文本协议相对简单,通常使用一个特殊的标记符作为一个消息的结束. 二进制协议,通常是由消息头(Head ...
- jquery自定义插件结合baiduTemplate.js实现异步刷新(附源码)
上一篇记录了BaiduTemplate模板引擎使用示例附源码,在此基础上对使用方法进行了封装 自定义插件jajaxrefresh.js 代码如下: //闭包限定命名空间 (function ($) { ...
- 精选9个值得学习的 HTML5 效果【附源码】
这里精选了一组很酷的 HTML5 效果.HTML5 是现 Web 开发领域的热点, 拥有很多让人期待已久的新特性,特别是在移动端,Web 开发人员可以借助 HTML5 强大功能轻松制作各种交互性强.效 ...
随机推荐
- acmer之ubuntu下安装Eclipse
ubuntu是acmer常用的系统,配置起CB还是比较简单的三行命令就OK了 //Current stable version of Code::Blocks IDE (16.01) //To ins ...
- oracle列转行 WM_CONCAT LISTAGG
开发给个SQL说给某个条件时报ORA-22922 代码段: SELECT 袋号, SUM(实际重量) AS 实际重量, SUM(材积重量) AS 材积重量, COUNT(运单号) AS 件数, TO_ ...
- win 7 取得最高权限
以管理员身份运行cmd,然后输入: net user administrator /active:yes 然后注销,就会看到你原来的用户已经是最高权限的用户了.以后做的操作都是最高权限的操作.
- JSON树节点的增删查改
最近了解到使用json字符串存到数据库的一种存储方式,取出来的json字符串可以进行相应的节点操作 故借此机会练习下递归,完成对json节点操作对应的工具类. 介绍一下我使用的依赖 复制代码 < ...
- [转] Makefile 基础 (6) —— Makefile 使用条件判断
该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客:(最原始版本) http://blog.csdn.net/haoel/article/details/2886 我转自 ...
- iOS自定义Navbar
1.修改Navigationbar navigationBar其实有三个子视图,leftBarButtonItem,rightBarButtonItem,以及titleView. 1.1 方法一:a ...
- Mondriaan's Dream(poj 2411)
题意:在n*m的方格里铺1*2的骨牌,有多少种方案 /* 第一次做插头DP,感觉和状压差不多. 这道题是利用上一行的状态来更新下一行的状态. 1代表上一行这个位置填了一个竖的(即本行可以填): 0代表 ...
- gridview中的相关事件操作
原文发布时间为:2008-07-27 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- UVA - 10050 Hartals
#include <cstdio> #include <cstring> ]; ]; int main() { int t; scanf("%d", &am ...
- 设计模式原来如此-单例模式(Singleton Pattern)
简单介绍一下我对Singleton的理解,说的不好请大家多多指点. 单例模式的简单定义就是确保一个类只有一个实例,并提供一个全局访问点. 单例模式有哪些用处呢? 有一些对象其实我们只需要一个,比方说: ...