Scale watermark overlay by video size with ffmpeg

ffmpegoverlayvideowatermark

I would like to scale a watermark to 5% of the video width.

I need something like that:

ffmpeg -i 1.mp4 -i logo.png -filter_complex "[1:v] scale=1_MP4_VIDEO_WIDTH*0.05:-1 [logo1]; [0:v][logo1] overlay=0:0" -y -b 1600k -c:v libx264 -profile high -level 4.1 -c:a libfaac -q:a 128k 2.mp4

How can I reference the video width?

Best Answer

  • Assuming a linux environment (or cygwin on windows), the only way I found is to execute 2 commands.

    First to get main video size and perform math on them (note: x/20 == x*0.05:

    val=`ffmpeg.exe -i 1.mp4 2>&1 | grep Video: | sed 's_.*, \([0-9]*x[0-9]*\) .*_\1_' | awk 'BEGIN {FS="x"} {print int($1/20)"x"int($2/20)}'`
    

    Second to scale and overlay the video

    ffmpeg -i 1.mp4 -i logo.png -filter_complex "[1:v] scale=$val [logo1]; [0:v][logo1] overlay=0:0" -y -b 1600k -c:v libx264 -profile high -level 4.1 -c:a libfaac -q:a 128k 2.mp4
    

    Also, you could just replace $val on second line with the first expression (including backquotes) and get the same result, but I find it a little easier to read splitting command in two.