ffmpeg 代码实现视频添加水印功能.pdf
《ffmpeg 代码实现视频添加水印功能.pdf》由会员分享,可在线阅读,更多相关《ffmpeg 代码实现视频添加水印功能.pdf(15页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、ffmpeg 代码实现视频添加水印功能备忘:用 ffmpeg 给视频添加水印的命令为:ffmpeg-i in.mp4-ixxx.png-filter_complex overlay=5:5out.mp4 代码实现如下,主要是用了参考了 ffmpeg.c 里面的 filter_complex 的代码:cpp view plaincopy#include<unistd.h>#include<stdio.h>#include<assert.h>#include<libavcodec/avcodec.h>#include<libavformat/a
2、vformat.h>#include<libavfilter/avfiltergraph.h>#include<libavfilter/avcodec.h>#include<libavfilter/buffersink.h>#include<libavfilter/buffersrc.h>#include<libswscale/swscale.h>const char*filter_descr=overlay=5:5;#define ENABLE_YUV_FILE 1AVFormatContext*input_fmt_ctx;A
3、VCodecContext*input_dec_ctx;AVFormatContext*overlay_fmt_ctx;AVCodecContext*overlay_dec_ctx;intinput_video_stream_idx,overlay_video_stream_idx;AVFilterGraph*filter_graph;AVFilterInOut*inputs;AVFilterInOut*outputs;AVFilterContext*buffersrc_ctx;AVFilterContext*bufferoverlay_ctx;AVFilterContext*buffersi
4、nk_ctx;int ret;int got_frame;intvideo_eof_reached=0;int overlay_eof_reached=0;int active_stream_index=-1;FILE*fp_yuv;voidyuv420p_save(AVFrame*pFrame);intvideo_transcode_step(AVFrame*mVideoFrame);intoverlay_transcode_step(AVFrame*mOverlayFrame);intvideo_output_eof_packet(const char*tag,AVStream*ist,A
5、VFilterContext*ifilter);static intopen_input_file(const char*filename)int ret;AVCodec*dec;if(ret=avformat_open_input(&input_fmt_ctx,filename,NULL,NULL)<0)av_log(NULL,AV_LOG_ERROR,Cannot open input filen);return ret;if(ret=avformat_find_stream_info(input_fmt_ctx,NULL)<0)av_log(NULL,AV_LOG_E
6、RROR,Cannot findstream informationn);return ret;/*select the video stream*/ret=av_find_best_stream(input_fmt_ctx,AVMEDIA_TYPE_VIDEO,-1,-1,&dec,0);if(ret<0)av_log(NULL,AV_LOG_ERROR,Cannot find a video stream in the input filen);return ret;input_video_stream_idx=ret;input_dec_ctx=input_fmt_ctx-
7、>streamsinput_video_stream_idx->codec;/*init the video decoder*/if(ret=avcodec_open2(input_dec_ctx,dec,NULL)<0)av_log(NULL,AV_LOG_ERROR,Cannotopen video decodern);return ret;return 0;static int open_overlay_file(const char*filename)int ret;AVCodec*dec;if(ret=avformat_open_input(&overlay
8、_fmt_ctx,filename,NULL,NULL)<0)av_log(NULL,AV_LOG_ERROR,Cannot open input filen);return ret;if(ret=avformat_find_stream_info(overlay_fmt_ctx,NULL)<0)av_log(NULL,AV_LOG_ERROR,Cannot findstream informationn);return ret;/*select the video stream*/ret=av_find_best_stream(overlay_fmt_ctx,AVMEDIA_TY
9、PE_VIDEO,-1,-1,&dec,0);if(ret<0)av_log(NULL,AV_LOG_ERROR,Cannot find a video stream in the input filen);return ret;overlay_video_stream_idx=ret;overlay_dec_ctx=overlay_fmt_ctx->streamsoverlay_video_stream_idx->codec;/*init the video decoder*/if(ret=avcodec_open2(overlay_dec_ctx,dec,NULL
10、)<0)av_log(NULL,AV_LOG_ERROR,Cannotopen video decodern);return ret;printf(overlay format=%sn,overlay_fmt_ctx->iformat->name);return0;static intvideo_config_input_filter(AVFilterInOut*inputs,AVFilterContext*input_filter_ctx)char args512;memset(args,0,sizeof(args);AVFilterContext*first_filter
11、=inputs->filter_ctx;int pad_idx=inputs->pad_idx;AVFilter*filter=avfilter_get_by_name(buffer);/AVRationaltime_base=input_dec_ctx->time_base;AVStream*video_st=input_fmt_ctx->streamsinput_video_stream_idx;AVRational time_base=video_st->time_base;snprintf(args,sizeof(args),video_size=%dx%
12、d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d:sws_param=flags=%d:frame_rate=%d/%d,input_dec_ctx->width,input_dec_ctx->height,input_dec_ctx->pix_fmt,input_dec_ctx->time_base.num,input_dec_ctx->time_base.den,input_dec_ctx->sample_aspect_ratio.num,input_dec_ctx->sample_aspect_rat
13、io.den,SWS_BILINEAR+(video_st->codec->flags&CODEC_FLAG_BITEXACT)?SWS_BITEXACT:0),video_st->r_frame_rate.num,video_st->r_frame_rate.den);printf(inputargs=%sn,args);ret=avfilter_graph_create_filter(input_filter_ctx,filter,src_in,args,NULL,filter_graph);if(ret<0)printf(video config i
14、nput filter fail.n);return-1;ret=avfilter_link(*input_filter_ctx,0,first_filter,pad_idx);assert(ret>=0);printf(video_config_input_filter avfilter_link ret=%dn,ret);return ret;static intvideo_config_overlay_filter(AVFilterInOut*inputs,AVFilterContext*overlay_filter_ctx)charargs512;memset(args,0,si
15、zeof(args);AVFilterContext*first_filter=inputs->filter_ctx;int pad_idx=inputs->pad_idx;AVFilter*filter=avfilter_get_by_name(buffer);/AVRationaltime_base=overlay_dec_ctx->time_base;AVStream*overlay_st=overlay_fmt_ctx->streamsoverlay_video_stream_idx;AVRational time_base=overlay_st->tim
16、e_base;snprintf(args,sizeof(args),video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d:sws_param=flags=%d:frame_rate=%d/%d,overlay_dec_ctx->width,overlay_dec_ctx->height,overlay_dec_ctx->pix_fmt,time_base.num,time_base.den,overlay_dec_ctx->sample_aspect_ratio.num,overlay_dec_ct
17、x->sample_aspect_ratio.den,SWS_BILINEAR+(overlay_st->codec->flags&CODEC_FLAG_BITEXACT)?SWS_BITEXACT:0),overlay_st->r_frame_rate.num,overlay_st->r_frame_rate.den);printf(overlay args=%sn,args);ret=avfilter_graph_create_filter(overlay_filter_ctx,filter,overlay_in,args,NULL,filter_gr
18、aph);if(ret<0)printf(video config overlay filter fail.n);return-1;ret=avfilter_link(*overlay_filter_ctx,0,first_filter,pad_idx);assert(ret>=0);printf(video_config_overlay_filter ret=%dn,ret);avfilter_inout_free(&inputs);return ret;static int video_config_output_filter(AVFilterInOut*outputs
19、,AVFilterContext*out_filter_ctx)char args512;AVFilterContext*last_filter=outputs->filter_ctx;int pad_idx=outputs->pad_idx;AVFilter*buffersink=avfilter_get_by_name(ffbuffersink);int ret=avfilter_graph_create_filter(out_filter_ctx,buffersink,video_out,NULL,NULL,filter_graph);assert(ret>=0);if
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ffmpeg 代码实现视频添加水印功能 代码 实现 视频 添加 水印 功能
限制150内