How to add watermark and mosaic to video or picture with ffmpeg

You can use FFmpeg to add watermarks and mosaics to videos or pictures. The following is the specific method:

  1. add watermark

If you need to add watermark to the video, you can use overlayfilter . This filter overlays the two inputs, i.e. combines the video and the watermarked footage. Here is a simple example:

ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=10:10" output.mp4

Among them -i input.mp4, means to specify the input file, and -i watermark.pngmeans to specify the watermark file. filter_complexIt is a syntax for connecting multiple filters, overlay=10:10specifying the position of the watermark file on the video screen, here is the position offset by 10 pixels relative to the upper left corner. Finally, a new video file output.mp4 is generated.

If you need to adjust the size, transparency and other attributes of the watermark, you can add different parameters to set it. for example:

ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=W-w-10:H-h-10:alpha=0.5" output.mp4

Where Wand Hrepresent the width and height of the video image, wand represent the width and height of the watermark image, hrespectively . alpha=0.5Indicates that the transparency of the watermark is set to 0.5.

If you need to add a watermark to the image, you can use a similar command:

ffmpeg -i input.jpg -i watermark.png -filter_complex "overlay=10:10" output.jpg
  1. add mosaic

If you need to add mosaic to video or picture, you can use boxblurfilter . This filter turns the specified area into a blur effect to achieve a mosaic effect. Here is a simple example:

ffmpeg -i input.mp4 -filter_complex "[0:v]boxblur=10[blur];[blur]crop=200:200:300:300,boxblur=10[cropped];[0:v][cropped]overlay=300:300" output.mp4

where -i input.mp4indicates the specified input file. [0:v]boxblur=10[blur]Indicates that the video image is blurred, and the blur radius is 10 pixels, which is saved as an intermediate variable blur. [blur]crop=200:200:300:300,boxblur=10[cropped]Indicates to crop the blurred video image, and only keep the upper left corner with the starting coordinates of (300, 300) and the area with a width and height of 200, and perform blurring again, and save it as an intermediate variable cropped. Finally, use overlaythe filter to superimpose the original video and the cropped mosaic image to generate a new video file output.mp4.

If you need to adjust the size, position, shape and other properties of the mosaic, you can add different parameters for setting.

If the watermark and mosaic in the video cannot be removed by software tools, you can try to use FFmpeg or similar tools to add other layers on the video to cover these areas.

Here's how to add layers with FFmpeg:

  1. Prepare the layer that needs to be added, which can be an image, a piece of text, or another video, etc.

  2. Enter the following command at the command line:

ffmpeg -i input.mp4 -i watermark.png -filter_complex "[0:v][1:v] overlay=x=W-w-10:y=10" -c:a copy output.mp4

Among them, -i input.mp4means to specify the input file, -i watermark.pngand means to specify the layer file to be added. [0:v][1:v]Indicates that the input file and the layer file are used as the two inputs of the filter chain, overlay=x=W-w-10:y=10and that the overlay filter is used to add the layer to the specified position of the video screen, where (x, y)indicates the distance between the upper left corner of the layer and the upper left corner of the video screen, Wand represent the width of video frame and layer wrespectively . -c:a copyIndicates to preserve the audio stream in the video file. Finally, a new video file output.mp4 is generated.

  1. Make adjustments as needed, such as adjusting layer size, position, transparency, etc., and you can add different parameters to the command for setting.

Please note that adding layers may also affect the visual effect of the video, so it needs to be used with caution according to the actual situation. 

Guess you like

Origin blog.csdn.net/huapeng_guo/article/details/130152259