ffmpeg水印的制作一般遵循的步骤:
(1)编译FFmpeg支持PNG的encoder和decoder。
当然,这两个参数默认是打开的,但是如果之前为了精简ffmpeg,在编译时使用了--disable-encoders或者--disable-decoders的话,--enable-encoder=png和--enable-decoder=png就必须打开了。如果你的ffmpeg不支持png的话,会带来很严重的错误。
参考:
http://doom10.org/index.php?topic=1481.0
(2)编译FFmpeg支持avfilter,即--enable-avfilter。
(3)ffmpeg编译完成后,就可以开始添加水印了。本人使用的命令如下:
$ ffmpeg -y -i tv/3-6.mp4 -f mp4 -vf "movie=sohu.png:f=png [img]; [in] [img] overlay=main_w-overlay_w-10:10 [movie]; [movie] setdar=16:9" -vcodec libx264 -r 29.97 -s 480x320 -aspect 16:9 -b 512k -acodec copy test/3.mp4
关键点在:-vf [选项]
overlay选项的说明:
Overlay one video on top of another.
It takes two inputs and one output, the first input is the "main" video on which the second input isoverlayed.
It accepts the parameters: x:y.
x is the x coordinate of the overlayed video on the main video,y is the y coordinate. The parameters are expressions containing the following parameters:
-
‘
main_w, main_h’
-
main input width and height
‘
W, H’
-
same as main_w and main_h
‘
overlay_w,overlay_h’
-
overlay input width and height
‘
w, h’
-
same as overlay_w andoverlay_h
Be aware that frames are taken from each input video in timestamp order, hence, if their initial timestamps differ, it is a a good idea to pass the two inputs through asetpts=PTS-STARTPTS filter to have them begin in the same zero timestamp, as it does the example for themovie filter.
Follow some examples:
# draw the overlay at 10 pixels from the bottom right # corner of the main video. overlay=main_w-overlay_w-10:main_h-overlay_h-10 # insert a transparent PNG logo in the bottom left corner of the input movie=logo.png [logo]; [in][logo] overlay=10:main_h-overlay_h-10 [out] # insert 2 different transparent PNG logos (second logo on bottom # right corner): movie=logo1.png [logo1]; movie=logo2.png [logo2]; [in][logo1] overlay=10:H-h-10 [in+logo1]; [in+logo1][logo2] overlay=W-w-10:H-h-10 [out] # add a transparent color layer on top of the main video, # WxH specifies the size of the main input to the overlay filter color=red.3:WxH [over]; [in][over] overlay [out] |
You can chain togheter more overlays but the efficiency of such approach is yet to be tested.
setdar选项的说明:
Set the Display Aspect Ratio for the filter output video.
This is done by changing the specified Sample (aka Pixel) Aspect Ratio, according to the following equation:DAR = HORIZONTAL_RESOLUTION / VERTICAL_RESOLUTION * SAR
Keep in mind that this filter does not modify the pixel dimensions of the video frame. Also the display aspect ratioset by this filter may be changed by later filters in the filterchain, e.g. in case of scaling or if another "setdar" or a "setsar" filter is applied.
The filter accepts a parameter string which represents the wanted display aspect ratio. The parameter can be a floating point number string, or an expression of the formnum:den, wherenum andden are the numerator and denominator of the aspect ratio. If the parameter is not specified, it is assumed the value "0:1".
For example to change the display aspect ratio to 16:9, specify:
setdar=16:9 # the above is equivalent to setdar=1.77777 |
See also the "setsar" filter documentation (see setsar).
参考:
http://libav.org/ffmpeg.html#overlay
下面是另一种添加水印的方式:目前未测试:
用in.avi作为输入视频,out.avi作为输出视频,pp.png表示要添加的水印图片,是png格式的,调整水印大小(scale = 50:50)和位置(overlay=10:10)
ffmpeg –i in.avi -i /data/move/long.png -filter_complex '[1:v]scale=50:50[s];[0:v][s]overlay=10:10' -b:v 500000 -c:v h264 -s cif -f mpegts out.avi
使用overlay添加透明水印时,位置参数的位置在 -vfilters 里面的 overlay= 的后面的前两个参数,参数之间是冒号分隔的。第一个参数是横向的间距,第二个是纵向的间距。可以结合与视频和水印相关的四个值来设置,这四个值分别是:mainW表示主视频宽度,mainH表示主视频高度,overlayW表示水印宽度,overlayH表示水印高度。这些值加在overlay参数中,ffmpeg将自动识别。
也就是说要让水印
显示在视频的左上角overlay参数为 overlay=0:0
显示在右上角为 overlay= main_w-overlay_w:0
显示在右下角为 overlay= main_w-overlay_w:main_h-overlay_h
显示在左下角为 overlay=0: main_h-overlay_h
上面的0可以改为5,或10像素,以便多留出一些空白。
用复合过滤器添加水印:
ffmpeg -i input -i logo -filter_complex 'overlay=10:main_h-overlay_h-10' output
input:输入流
logo:水印文件,也可以是一个流。注意:需要编译时把相应的解码器编译。例如PNG图片。需要编译PNG解码器。Ffmpeg才能够识别图片文件,把图片做为一种流。注意:PNG图片必须含有alpha通道。Overlay过滤器是根据alpha通道来进行复盖的。所以,你想要透明效果时,须先制做一张透明的PNG图片。
output:输出流
也可以用下面命令:
ffmpeg -i input -vf 'movie=long.png[logo];[in][logo]overlay=10:10[out]' output
movie过滤器用来把两个流组合成一个流。它有一个输出PAD。