ffmpeg下libavfilter的安装配置以及利用实现添加水印

本文介绍使用FFmpeg和libavfilter库添加水印至视频的方法。首先介绍了如何下载并配置支持水印功能的FFmpeg版本,接着给出了具体的命令行示例,用于调整水印的大小和位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

水印又叫作"watermark"
  以前ffmpeg使用-vhook来调用某个库实现水印添加.后来被废弃.现在添加了-vfilters参数调用libavfilter库来添加水印.但是这个库一直到本文发表的时候文档以及安装方法都不是很完善.
  首先要先获得libavfilter
  因为,直接从ffmpeg的download页面下载的ffmpeg是不能编译出-vfilters参数的.有了-vfilters参数我们才能得到使用filter的机会.添加水印就是把影片加上一副图片么.当然所添加图片的背景色需要去掉,只留下图片的图案和字那是最完美的.不过这里先只说如何加上一附图片.至于去掉背景色先不说了.因为我还没有弄出来.
  要想使用filter需要先下载SoC版本的libavfilter,这个下好后里边有一个checkout.sh脚本.脚本的内容很简单.主要就是下载特定版本的ffmpeg并且打补丁等等.
  SoC版libavfilter下载方法:
  svn checkout svn://svn.mplayerhq.hu/soc/libavfilter 
  
  得到libavfilter之后进入这个目录执行checkout.sh脚本.执行完毕libavfilter里边就有了ffmpeg目录了..进入ffmpeg然后编译:
  ./configure --enable-avfilter --enable-avfilter-lavf
  
  --enable-avfilter和 --enable-avfilter-lavf两个参数都要加,尤其是加水印需要用到movie filter,这个就需要加上--enable-avfilter-lavf才能激活.而前一个libavfilter的README文档是教程要求加的.
  
  剩下的就是make/make install了.
  
  下面说说如何使用:
  这个文档不多.可以说搜索就找不到文档(在我学习如何使用的时候就是这样子.包括外文文档,我用的是google)
  最详细的文档都在SoC版本的libavfilter里变呢,里边有README文档和doc目录.
  README主要讲解安装,doc目录主要是一些用法,用法讲解的比较简单.其实不够详细.再就是从网络上搜索各种用法了.也可以看看libavfilter库的那些源代码.
  只说一种粗糙地加水印的方法:
  
  用in.avi作为输入视频,out.avi作为输出视频,pp.png表示要添加的水印图片,是png格式的,
  调整水印大小(scale = 50:50)和位置(overlay=0:0)
  
  $ ffmpeg -i in.avi -s 240x320 -vfilters "movie=0:png:pp.png, scale = 50:50 [over1], [in] [over1] overlay=0:0 [out]" out.avi
  
  好就到这里.唯一遗憾就是就是水印图片的背景色没有去除,看起来很粗糙.这一部分也许我以后会补上
要在视频中添加水印,可以使用 FFmpeg 库中提供的过滤器来实现。以下是一个基本的 C 代码示例,它使用 FFmpeg 库中的过滤器来将文本水印添加到视频中: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <libavformat/avformat.h> #include <libavfilter/avfiltergraph.h> #include <libavfilter/buffersrc.h> #include <libavfilter/buffersink.h> int main(int argc, char **argv) { AVFilterContext *buffersrc_ctx; AVFilterContext *buffersink_ctx; AVFilterGraph *filter_graph; int ret; if (argc != 3) { fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]); exit(1); } av_register_all(); avfilter_register_all(); AVFormatContext *fmt_ctx = NULL; ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL); if (ret < 0) { fprintf(stderr, "Could not open input file '%s'", argv[1]); exit(1); } ret = avformat_find_stream_info(fmt_ctx, NULL); if (ret < 0) { fprintf(stderr, "Could not find stream information"); exit(1); } av_dump_format(fmt_ctx, 0, argv[1], 0); AVCodecContext *codec_ctx = NULL; int video_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); if (video_stream_index < 0) { fprintf(stderr, "Could not find video stream in input file"); exit(1); } AVStream *video_stream = fmt_ctx->streams[video_stream_index]; codec_ctx = video_stream->codec; AVCodec *decoder = avcodec_find_decoder(codec_ctx->codec_id); if (!decoder) { fprintf(stderr, "Failed to find decoder for stream #%u codec ID %d\n", video_stream_index, codec_ctx->codec_id); exit(1); } ret = avcodec_open2(codec_ctx, decoder, NULL); if (ret < 0) { fprintf(stderr, "Failed to open decoder for stream #%u\n", video_stream_index); exit(1); } AVFrame *frame = av_frame_alloc(); AVPacket packet; av_init_packet(&packet); filter_graph = avfilter_graph_alloc(); char args[512]; snprintf(args, sizeof(args), "drawtext=text='My Watermark':x=(w-text_w)/2:y=(h-text_h)/2:fontfile=/usr/share/fonts/truetype/freefont/FreeSerif.ttf:fontsize=48:fontcolor=white"); const AVFilter *buffersrc = avfilter_get_by_name("buffer"); const AVFilter *buffersink = avfilter_get_by_name("buffersink"); AVFilterInOut *outputs = avfilter_inout_alloc(); AVFilterInOut *inputs = avfilter_inout_alloc(); enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE}; sprintf(args, "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d", codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, video_stream->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值