How to scale and add correct a logo in ffmpeg comm

2020-04-20 21:47发布

问题:

I am trying to apply a watermark and also to scale it to the current video size via ffmpeg command:

Here is my inital comand that works without watermark

ffmpeg -v 0 -vcodec h264_qsv -i 'udp://@some.ip:1234?fifo_size=1000000&overrun_nonfatal=1&buffer_size=1000000' -vf scale=iw:ih -profile baseline -acodec aac -ac 1 -ar 44100 -ab 64k -deinterlace -vcodec h264_qsv -bufsize 4000k -maxrate 3500k -preset veryfast -vb 2000k -f flv rtmp://127.0.0.1/app/720 

Now I tried to add the picture as a watermark. There was a conflict while using with -vf scale=-1:ih*.5, in order to eliminate the problem I used -s 1280x720 to specify the resolution for the video stream, it worked but not properly.

ffmpeg -v 0 -vcodec h264_qsv -i 'udp://@some.ip:1234?fifo_size=1000000&overrun_nonfatal=1&buffer_size=1000000' -i logo.png -filter_complex "overlay=10:10" -s 1280x720 -profile baseline -acodec aac -ac 1 -ar 44100 -ab 64k -deinterlace -vcodec h264_qsv -bufsize 4000k -maxrate 3500k -preset veryfast -vb 2000k -f flv rtmp://some.ip/app/720 

The problem:

How can I specify in the ffmpeg command the both sizes of video and logo(watermark) so they don't conflict with each other and they auto adjust like -vf scale=-1:ih*.5 dose.

Thank you!

回答1:

The scale2ref filter allows one to a video/image stream with reference to the dimensions of another video or image stream

e.g.

ffmpeg -v 0 -vcodec h264_qsv -i 'udp://@some.ip:1234?fifo_size=1000000&overrun_nonfatal=1&buffer_size=1000000' \
-loop 1 -i logo.png \
-filter_complex "[1:v][0:v]scale2ref=iw/8:-1[logo][0v];[0v][logo]overlay=10:10[v]" \
-map "[v]" -map 0:a \
-profile baseline -acodec aac -ac 1 -ar 44100 -ab 64k \
-deinterlace -vcodec h264_qsv -bufsize 4000k -maxrate 3500k \
-preset veryfast -vb 2000k \
-f flv rtmp://some.ip/app/720

Here 1:v - the logo image - is being scaled to 1/8th the width of [0:v], the H.264 stream.


For the command given in the comments:

ffmpeg -v 0 -vcodec h264_qsv -i 'input' \
-loop 1 -i logo.png \
-filter_complex "[0:v]scale=iw:ih[v0]; \
                [1:v][v0]scale2ref=iw/8:-1[logo][0v];[0v][logo]overlay=10:10[v]" \
-map "[v]" -map 0:a \
-profile baseline -acodec aac -ac 1 -ar 44100 -ab 64k \
-deinterlace -vcodec h264_qsv -bufsize 4000k -maxrate 3500k \
-preset veryfast -vb 2000k \
-f flv out1 \
-filter_complex "[0:v]scale=-1:ih/2[v0]; \
                [1:v][v0]scale2ref=iw/8:-1[logo][0v];[0v][logo]overlay=10:10[v2]" \
-map "[v2]" -map 0:a \
-profile baseline -acodec aac -ac 1 -ar 44100 -ab 64k \
-deinterlace -vcodec h264_qsv -bufsize 4000k -maxrate 2000k \
-preset veryfast -vb 1000k \
-f flv out2 \
-filter_complex "[0:v]scale=-1:ih/4[v0]; \
                [1:v][v0]scale2ref=iw/8:-1[logo][0v];[0v][logo]overlay=10:10[v3]" \
-map "[v3]" -map 0:a \
-profile baseline -acodec aac -ac 1 -ar 44100 -ab 64k \
-deinterlace -vcodec h264_qsv -bufsize 4000k -maxrate 1000k \
-preset veryfast -vb 512k \
-f flv out3 \