Add transparent watermark to video center using ffmpeg

Reference:ffmpeg sets watermark transparency - Programming Paradise

"FFmpeg Basics" Chinese version-22- Color Correction - Simple Book

ffmpeg adds watermark_Peninsula Iron Box.’s blog-CSDN blog_ffmpeg watermark

Addwatermark to the video usingoverlay filter /Example of logo image.

center

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 use the shortened overlay option:

overlay=(W-w)/2:(H-h)/2

upper left

This is easy because if no overlay option is provided, the default is to place the image in the top left.

This example adds 5 padding pixels so the image doesn't touch the edges:

overlay=5:5

upper right

Use a padding of 5 pixels:

overlay=main_w-overlay_w-5:5

or shortened option:

overlay=W-w-5:5

lower right

Use a padding of 5 pixels:

overlay=main_w-overlay_w-5:main_h-overlay_h-5

or shortened option:

overlay=W-w-5:H-h-5

lower left

Use a padding of 5 pixels:

overlay=5:main_h-overlay_h

or shortened option:

overlay=5:H-h-5

transparency/opacity/alpha

Useformat and colorchannelmixer filters to make watermark 50 %TransparentExample:

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

Quality improvement

Use theOverride option in the format=auto filter to make the PNG watermark look better: a>

ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=5:H-h-5:format=auto,format=yuv420p" -c:a copy output.mp4

Note that the Format filter has been added (yes, with the same name as the option, but as a separate filter), to reset it to YUV 4:2:0, which is required for MP4 output. ,format=yuv420pIf you do not output MP4, please delete it.

Scale watermark relative to main video

Usescale2refProduct:

Example of sizing your logo to 10% (1/10) of the main video size:

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

Guess you like

Origin blog.csdn.net/qq_36437693/article/details/133977730