如何使用ffmpeg在视频中心添加透明水印? [英] How to add transparent watermark in center of a video with ffmpeg?
问题描述
我目前正在使用这些命令:
左上角ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=10:10 [out]" outputvideo.flv右上角ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:10 [out]" outputvideo.flv左下角ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=10:main_h-overlay_h-10 [out]" outputvideo.flv右下角ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=(main_w-overlay_w-10)/2:(main_h-overlay_h-10)/2 [out]" outputvideo.flv
如何在视频中心放置水印?
使用
ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2";-codec:a copy output.mp4
或使用缩短的叠加选项:
overlay=(W-w)/2:(H-h)/2
左上
这是一个简单的方法,因为如果您没有提供叠加选项,默认设置是将图像放置在左上角.
此示例添加了 5 个像素的填充,因此图像不会接触边缘:
overlay=5:5
右上角
使用 5 个像素的填充:
overlay=main_w-overlay_w-5:5
或使用缩短的选项:
overlay=W-w-5:5
右下
使用 5 个像素的填充:
overlay=main_w-overlay_w-5:main_h-overlay_h-5
或使用缩短的选项:
overlay=W-w-5:H-h-5
左下
使用 5 个像素的填充:
overlay=5:main_h-overlay_h
或使用缩短的选项:
overlay=5:H-h-5
透明度/不透明度/alpha
使用格式和colorchannelmixer 过滤器:
ffmpeg -i input.mp4 -i watermark.jpg -filter_complex "[1]format=rgba,colorchannelmixer=aa=0.5[logo];[0][logo]overlay=(Ww)/2:(Hh)/2:format=auto,format=yuv420p"-c:a 复制 output.mp4
提高质量
在叠加中使用format=auto
选项过滤器可以使 PNG 水印看起来更好:
ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=5:H-h-5:format=auto,format=yuv420p";-c:a 复制 output.mp4
注意添加了 format 过滤器(是的,与选项同名,但是一个独立的过滤器)将其重置为 MP4 输出所需的 YUV 4:2:0.如果您不输出 MP4,请删除 ,format=yuv420p
.
与主视频相关的缩放水印
使用 scale2ref 过滤器:
使徽标占主视频大小 10% (1/10) 的示例:
ffmpeg -i input.mp4 -i watermark.png -filter_complex "[1][0]scale2ref=w=oh*mdar:h=ih*0.1[logo][video];[video][logo]overlay=5:Hh-5"-c:a 复制 output.mp4
I am currently using these commands:
Top left corner
ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=10:10 [out]" outputvideo.flv
Top right corner
ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:10 [out]" outputvideo.flv
Bottom left corner
ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=10:main_h-overlay_h-10 [out]" outputvideo.flv
Bottom right corner
ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=(main_w-overlay_w-10)/2:(main_h-overlay_h-10)/2 [out]" outputvideo.flv
How to place watermark center of the video ?
Examples to add a watermark / logo image on video using the overlay filter.
Centered
ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" -codec:a copy output.mp4
or with the shortened overlay options:
overlay=(W-w)/2:(H-h)/2
Top left
This is the easy one because the default, if you provide no options to overlay, is to place the image in the top left.
This example adds 5 pixels of padding so the image is not touching the edges:
overlay=5:5
Top right
With 5 pixels of padding:
overlay=main_w-overlay_w-5:5
or with the shortened options:
overlay=W-w-5:5
Bottom right
With 5 pixels of padding:
overlay=main_w-overlay_w-5:main_h-overlay_h-5
or with the shortened options:
overlay=W-w-5:H-h-5
Bottom left
With 5 pixels of padding:
overlay=5:main_h-overlay_h
or with the shortened options:
overlay=5:H-h-5
Transparency / opacity / alpha
Example to make watermark 50% transparent using the format and colorchannelmixer filters:
ffmpeg -i input.mp4 -i watermark.jpg -filter_complex "[1]format=rgba,colorchannelmixer=aa=0.5[logo];[0][logo]overlay=(W-w)/2:(H-h)/2:format=auto,format=yuv420p" -c:a copy output.mp4
Improved quality
Using the format=auto
option in the overlay filter can make PNG watermarks look better:
ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=5:H-h-5:format=auto,format=yuv420p" -c:a copy output.mp4
Note the addition of the format filter (yes, same name as the option, but a standalone filter) to reset it to YUV 4:2:0 which is needed for MP4 output. Remove ,format=yuv420p
if you are not outputting MP4.
Scale watermark in relation to main video
Use the scale2ref filter:
Example to make logo 10% (1/10) the size of the main video:
ffmpeg -i input.mp4 -i watermark.png -filter_complex "[1][0]scale2ref=w=oh*mdar:h=ih*0.1[logo][video];[video][logo]overlay=5:H-h-5" -c:a copy output.mp4
这篇关于如何使用ffmpeg在视频中心添加透明水印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!