基于FFMPEG水印添加---avfilter庫
??avfilter,是ffmpeg為圖像和語音處理提供的濾鏡子框架,代碼位于libavfilter目錄。
??libavfilter提供了一個通用框架來實現audio和video的原始數據filter處理,如對視頻添加疊加Logo圖片或者時間文字等信息、對視頻進行裁剪和翻轉。
1.硬件平臺
操作系統:Ubuntu18.04
ffmpeg版本:ffmpeg4.2.5
2.FFMPEG編譯
??在使用avfilter庫添加漢字水印時,ffmepg默認模式時不支持矢量字庫的,要實現漢字水印添加就需要進行configure配置。
2.1 freetype庫編譯
?? freetype下載地址:https://download.savannah.gnu.org/releases/freetype/
2.2 FriBidi庫編譯
?? FriBidi下載地址:https://www.linuxfromscratch.org/blfs/view/svn/general/fribidi.html
#解壓源碼
tar xvf /mnt/hgfs/ubuntu/software_pack/fribidi-1.0.11.tar.xz
#配置生成Makefile
./configure --prefix=$PWD/_install
#編譯安裝
make && make install
#編譯安裝
#將生成的文件拷貝到/usr/lib目錄下
cp ./_install/lib/pkgconfig/ /usr/lib -rf
2.3 ffmpeg庫編譯
#解壓源碼
tar xvf /mnt/hgfs/ubuntu/software_pack/ffmpeg-4.2.5.tar.bz2
#配置生成Makefile
./configure --enable-static
--enable-shared --prefix=$PWD/_install
--extra-cflags=-I/home/wbyq/src_pack/x264-master/_install/include
--extra-ldflags=-L/home/wbyq/src_pack/x264-master/_install/lib
--extra-cflags=-I/home/wbyq/src_pack/freetype-2.10.0/_install/include
--extra-ldflags=-L/home/wbyq/src_pack/freetype-2.10.0/_install/lib
--extra-cflags=-I/home/wbyq/src_pack/fribidi-1.0.11/_install/include
--extra-ldflags=-L/home/wbyq/src_pack/fribidi-1.0.11/_install/lib
--enable-ffmpeg --enable-libx264 --enable-gpl --enable-libfreetype --enable-libfontconfig --enable-libfribidi --enable-avfilter
#編譯安裝
make && make install
#將生成的文件拷貝到/usr/lib目錄下
cp ./_install/lib/pkgconfig/ /usr/lib -rf
2.4 ffmpeg命令行添加時間水印
2.4.1 以實時時間動態添加時間水印
ffmpeg -i 最后一滴水.mp4 -vf drawtext=“fontfile=msyhbd.ttc:x=100:y=10:fontcolor=white:fontsize=30:expansion=strftime:basetime=$(date +%s )000000:text=‘%Y-%m-%d %H:%M: %S’” output.mp4
drawtext 字符串水印
fontfile=msyhbd.ttc矢量字庫,可在windows下C:WindowsFonts拷貝到當期目錄下
x=100:y=10顯示位置,距離左上腳
fontcolor=white:fontsize=30字體顏色和大小
expansion=strftime:basetime=$(date +%s )000000以當前實時時間添加動態水印
text=‘%Y-%m-%d %H:%M: %S’ 時間顯示格式
2.4.1 以指定時間動態添加時間水印
ffmpeg -i 最后一滴水.mp4 -vf drawtext="fontfile=msyhbd.ttc:x=100:y=10:fontcolor=white:fontsize=30:expansion=strftime:basetime=$(date +%s -d '2020-8-8 14:10:50')000000:text='%Y-%m-%d %H:%M: %S' IT_阿水" output.mp4
3.代碼實現水印添加
??參考雷霄驊博客處理示例:https://blog.csdn.net/leixiaohua1020/article/details/50618190
??致敬雷神!致敬雷神!致敬雷神!
#include
#include
#include
#include
#include
#include
#include
#define FILE_SRC "sintel_480x272_yuv420p.yuv" //源文件
int waterMark(AVFrame *frame_in,AVFrame *frame_out,int w,int h,const char *str);
int main(int argc,char *argv[])
{
int ret;
/*打開輸入yuv文件*/
FILE *fp_in=fopen(FILE_SRC,"rb+");
if(fp_in==NULL)
{
printf("文件打開失敗n");
return 0;
}
int in_width=480;
int in_height=272;
/*處理后的文件*/
FILE *fp_out=fopen("new.yuv","wb+");
if(fp_out==NULL)
{
printf("文件創建失敗n");
return 0;
}
char buff[50];
AVFrame *frame_in=av_frame_alloc();
unsigned char *frame_buffer_in;
frame_buffer_in=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P,in_width,in_height,4));
/*根據圖像設置圖像指針和內存對齊方式*/
av_image_fill_arrays(frame_in->data,frame_in->linesize,frame_buffer_in,AV_PIX_FMT_YUV420P,in_width,in_height,4);
AVFrame *frame_out=av_frame_alloc();
unsigned char *frame_buffer_out;
frame_buffer_out=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P,in_width,in_height,4));
av_image_fill_arrays(frame_out->data,frame_out->linesize,frame_buffer_out,AV_PIX_FMT_YUV420P,in_width,in_height,4);
frame_in->width=in_width;
frame_in->height=in_height;
frame_in->format=AV_PIX_FMT_YUV420P;
char sys_time[64];
time_t sec,sec2;
while(1)
{
if(fread(frame_buffer_in,1,in_width*in_height*3/2,fp_in)!=in_width*in_height*3/2)
{
break;
}
frame_in->data[0]=frame_buffer_in;
frame_in->data[1]=frame_buffer_in+in_width*in_height;
frame_in->data[2]=frame_buffer_in+in_width*in_height*5/4;
sec=time(NULL);
if(sec!=sec2)
{
sec2=sec;
struct tm* today = localtime(&sec2);
strftime(sys_time, sizeof(sys_time), "%Y/%m/%d %H:%M:%S", today);
}
waterMark(frame_in,frame_out,in_width,in_height,sys_time);//添加水印
/*添加水印后的數據寫入到過濾器*/
if(frame_out->format==AV_PIX_FMT_YUV420P)
{
for(int i=0;iheight;i++)
{
fwrite(frame_out->data[0]+frame_out->linesize[0]*i,1,frame_out->width,fp_out);
}
for(int i=0;iheight/2;i++)
{
fwrite(frame_out->data[1]+frame_out->linesize[1]*i,1,frame_out->width/2,fp_out);
}
for(int i=0;iheight/2;i++)
{
fwrite(frame_out->data[2]+frame_out->linesize[2]*i,1,frame_out->width/2,fp_out);
}
printf("一幀數據處理完成n");
}
av_frame_unref(frame_out);
}
fclose(fp_in);
fclose(fp_out);
av_frame_free(&frame_in);
av_frame_free(&frame_out);
return 0;
}
int waterMark(AVFrame *frame_in,AVFrame *frame_out,int w,int h,const char *str)
{
int ret;
/*根據名字獲取ffmegding定義的filter*/
const AVFilter *buffersrc=avfilter_get_by_name("buffer");//原始數據
const AVFilter *buffersink=avfilter_get_by_name("buffersink");//處理后的數據
/*動態分配AVFilterInOut空間*/
AVFilterInOut *outputs=avfilter_inout_alloc();
AVFilterInOut *inputs=avfilter_inout_alloc();
/*創建AVFilterGraph,分配空間*/
AVFilterGraph *filter_graph;//對filters系統的整體管理結構體
filter_graph = avfilter_graph_alloc();
enum AVPixelFormat pix_fmts[]={AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE};//設置格式
/*過濾器參數:解碼器的解碼幀將被插入這里。*/
char args[256];
snprintf(args, sizeof(args),
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
w,h,AV_PIX_FMT_YUV420P,1,25,1,1);//圖像寬高,格式,幀率,畫面橫縱比
/*創建過濾器上下文,源數據AVFilterContext*/
AVFilterContext *buffersrc_ctx;
ret=avfilter_graph_create_filter(&buffersrc_ctx,buffersrc,"in",args,NULL,filter_graph);
if(ret<0)
{
printf("創建src過濾器上下文失敗AVFilterContextn");
return -1;
}
/*創建過濾器上下文,處理后數據buffersink_params*/
AVBufferSinkParams *buffersink_params;
buffersink_params=av_buffersink_params_alloc();
buffersink_params->pixel_fmts=pix_fmts;//設置格式
AVFilterContext *buffersink_ctx;
ret=avfilter_graph_create_filter(&buffersink_ctx,buffersink,"out",NULL,buffersink_params,filter_graph);
av_free(buffersink_params);
if(ret<0)
{
printf("創建sink過濾器上下文失敗AVFilterContextn");
return -2;
}
/*過濾器鏈輸入/輸出鏈接列表*/
outputs->name =av_strdup("in");
outputs->filter_ctx =buffersrc_ctx;
outputs->pad_idx =0;
outputs->next =NULL;
inputs->name =av_strdup("out");
inputs->filter_ctx =buffersink_ctx;
inputs->pad_idx =0;
inputs->next =NULL;
char filter_desrc[200]={0};//要添加的水印數據
snprintf(filter_desrc,sizeof(filter_desrc),"drawtext=fontfile=msyhbd.ttc:fontcolor=green:fontsize=20:x=50:y=20:text='%snIT_阿水'",str);
if(avfilter_graph_parse_ptr(filter_graph,filter_desrc,&inputs,&outputs, NULL)<0)//設置過濾器數據內容
{
printf("添加字符串信息失敗n");
return -3;
}
/*檢測配置信息是否正常*/
if(avfilter_graph_config(filter_graph,NULL)<0)
{
printf("配置信息有誤n");
return -4;
}
#if 0
/*
查找要在使用的過濾器,將要觸處理的數據添加到過濾器
注意:時間若從外面傳入(即144行數據已完整),則此處不需要查找,直接添加即可,否則需要添加下面代碼
*/
AVFilterContext* filter_ctx;//上下文
int parsed_drawtext_0_index = -1;
for(int i=0;inb_filters;i++)//查找使用的過濾器
{
AVFilterContext *filter_ctxn=filter_graph->filters[i];
printf("[%s %d]:filter_ctxn_name=%sn",__FUNCTION__,__LINE__,filter_ctxn->name);
if(!strcmp(filter_ctxn->name,"Parsed_drawtext_0"))
{
parsed_drawtext_0_index=i;
}
}
if(parsed_drawtext_0_index==-1)
{
printf("[%s %d]:no Parsed_drawtext_0n",__FUNCTION__,__LINE__);//沒有找到過濾器
}
filter_ctx=filter_graph->filters[parsed_drawtext_0_index];//保存找到的過濾器
/*獲取系統時間,將時間加入到過濾器*/
char sys_time[64];
time_t sec,sec2;
sec=time(NULL);
if(sec!=sec2)
{
sec2=sec;
struct tm* today = localtime(&sec2);
strftime(sys_time, sizeof(sys_time), "%Y/%m/%d %H:%M:%S", today); //24小時制
}
av_opt_set(filter_ctx->priv, "text", sys_time, 0 ); //設置text到過濾器
#endif
/*往源濾波器buffer中輸入待處理數據*/
if(av_buffersrc_add_frame(buffersrc_ctx,frame_in)<0)
{
return -5;
}
/*從濾波器中輸出處理數據*/
if(av_buffersink_get_frame(buffersink_ctx, frame_out)<0)
{
return -6;
}
avfilter_inout_free(&outputs);
avfilter_inout_free(&inputs);
avfilter_graph_free(&filter_graph);
return 0;
}
??Makefile文件:
OBJ=addwatermart.o
CFLAGS =-I/home/wbyq/src_pack/ffmpeg-4.2.5/_install/include -L/home/wbyq/src_pack/ffmpeg-4.2.5/_install/lib
CFLAGS +=-lpthread -lm -ldl -lSDL2_image -lavcodec -lavfilter -lavutil -lswresample -lavdevice -lavformat -lpostproc -lswscale -lpthread -lstdc++ -lm -lasound -lavfilter -lfreetype
app:$(OBJ)
gcc -o app $(OBJ) $(CFLAGS)
.PHONY=clean
clean:
rm app -f
??本示例實現效果為動態添加時間水印。示例效果:
-
水印
+關注
關注
0文章
26瀏覽量
11643 -
編譯
+關注
關注
0文章
659瀏覽量
32875 -
ffmpeg
+關注
關注
0文章
46瀏覽量
7403
發布評論請先 登錄
相關推薦
評論