Add watermark to audio and video

1. Text watermark

Adding text watermarks to videos requires many preparations. You need to have relevant files for text font processing. When compiling FFmpeg, you need to support FreeType, FontConfig, and iconv. You need to have relevant fonts in the system. You can add pure letter watermarks to FFmpeg. Use the drawtext filter for support. Let’s
take a look at the filter parameters of drawtext.

parameter type illustrate
text string letter
textfile string text file
box Boolean Text area background box (default false)
boxcolor color Display the color of the font area block
font string Font name (default is Sans font)
fontsize integer display font size
x string Default is 0
y string Default is 0
alpha floating point number Transparency (default 1), value from 0~1

(1) Add text watermark to the upper left corner of the video:

ffplay -i input.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='hello world':x=20:y=20"

Set the font color to green:

ffplay -i input.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='hello world':fontcolor=green"

If you want to adjust the position of the text watermark display, just adjust the values ​​of the x and y parameters.

ffplay -i input.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='hello world':fontcolor=green:x=400:y=200"

Modify transparency

ffplay -i input.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='hello world':fontcolor=green:x=400:y=200:alpha=0.5"

(2) You can also add a box to the text watermark, and then add a background color to the box:

ffplay -i input.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='hello world':
fontcolor=green:box=1:boxcolor=yellow"

So far, the basic function of text watermark has been added.

(3) Sometimes text watermarks want to use local time as the watermark content. This can be done with some special usage in the drawtext filter. The
current local time is displayed in the text in the format of year, month, day, hour, minute and second.

ffplay  -i input.mp4 -vf "drawtext=fontsize=60:fontfile=FreeSerif.ttf:text='%{
    
    localtime\:%Y\-%m\-%
d %H-%M-%S}':fontcolor=green:box=1:boxcolor=yellow"

When using ffmpeg to transcode and store to a file, you need to add -re, otherwise the time will be wrong.

ffmpeg -re -i input.mp4 -vf "drawtext=fontsize=60:fontfile=FreeSerif.ttf:text='%{
    
    localtime\:%Y\-%
m\-%d %H-%M-%S}':fontcolor=green:box=1:boxcolor=yellow" out.mp4

(4) In some scenes, it is necessary to display the watermark regularly or not to display the watermark regularly. This method can also be used with the drawtext filter. Just use
drawtext and enable. For example, a text watermark is displayed every 3 seconds:

ffplay -i input.mp4 -vf "drawtext=fontsize=60:fontfile=FreeSerif.ttf:text='test':fontcolor=green:box=1:
boxcolor=yellow:enable=lt(mod(t\,3)\,1)"

When using ffmpeg to transcode and store to a file, you need to add -re, otherwise the time will be wrong.

(5) Marquee effect

ffplay -i input.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='helloworld':x=mod(100*t\,w):y=
abs(sin(t))*h*0.7"

Modify font transparency and modify font color

ffplay -i input.mp4 -vf "drawtext=fontsize=40:fontfile=FreeSerif.ttf:text='liaoqingfu':x=mod(50*t\,w):y=
abs(sin(t))*h*0.7:alpha=0.5:fontcolor=white:enable=lt(mod(t\,3)\,1)"

2. Picture watermark

In addition to adding text watermarks to videos, FFmpeg can also add image watermarks, video tickers, etc. to videos. This section will focus on how to add image watermarks to videos; to add image watermarks to videos, you can use the movie filter. Let’s follow. Familiarize yourself with the parameters of movie filters

parameter type illustrate
filename string The input file name can be file, protocol, device
format_name, f string Input packaging format
stream_index, si integer The stream index number entered
seek_point, sp floating point number Seek the time position of the input stream
streams, s string Stream information for multiple input streams
loop integer Cycles
discontinuity time difference Support for jittering timestamp differences
ffmpeg -i input.mp4 -vf "movie=logo.png[watermark];[in][watermark]overlay=x=10:y=10[out]" output.mp4
Ø 原始视频文件路径:input.mp4
Ø 水印图片路径:logo.png
Ø 水印位置:(x,y)=(10,10)<=(left,top)距离左侧、顶部各10像素;
Ø 输出文件路径:output.mp4

overlay filter
Description: The foreground window (second input) covers the specified position of the background window (first input).
Syntax: overlay[=x:y[[:rgb={0, 1}]]
Parameters x and y are optional and default to 0.
Parameter rgb parameter is also optional, its value is 0 or 1, the default is 0.
Parameter description:
x is the horizontal coordinate from the upper left corner, the default value is 0
y is the vertical coordinate from the upper left corner, the default value is 0
rgb A value of 0 means that the input color space will not change, the default is 0; a value of 1 means that the color will be input Space set to RGB

parameter illustrate
main_w or W Video single frame image width
main_h or H Video single frame image height
overlay_w The width of the watermark image
overlay_h The height of the watermark image

Correspondingly, you can set the overlay parameter to the following value to change the position of the watermark image:

Watermark image location overlay value
upper left corner 10:10
upper right corner main_w-overlay_w-10:10
lower left corner 10:main_h-overlay_h-10
lower right corner main_w-overlay_w-10:main_h-overlay_h-10

There are two ways to add image watermarks to FFmpeg. One is to specify the watermark file path through movie, and the other is to read the stream of the input file through filter and specify it as a watermark. Here we focus on how to read movie image files as watermarks.


I recommend a Lingsheng Academy project class. I personally think the teacher taught it well. I would like to share it with you:
Lingsheng Platinum Learning Card (including infrastructure/high-performance storage/golang cloud native/audio and video/Linux kernel)
https://xxetb.xet .tech/s/VsFMs

Guess you like

Origin blog.csdn.net/qq_40135848/article/details/132611165