Watermarking Video with ffmpeg

In this post, I will be explaining how to watermark videos with a PNG image watermark that is transparent where it needs to be, I will cover both Linux and Windows (Not much is different on the ffmpeg side, the difference is when you want to traverse a directory (The script).

The watermark you see here is what I want to overlay over the video, If you right click and view the image, you should be able to see that around the text, it is transparent.

The PNG with transparency to be overlaid over the video

now, let us assume that the file in the directory is called x.mp4, and this watermark image is called watermark.png, then the following commands should overlay this image over the video

ffmpeg -i x.mp4 -i watermark.png -filter_complex "overlay=10:10" x1.mp4

The code above will create a new file (x1.mp4) which has the overlaid watermark, as you might be able to see if you execute the above the watermark is positioned at the top left corner of the video, which is not necessarily what you want, now because we know the dimensions of the watermark image, we can ask ffmpeg to center it horizontally (and if you like vertically to have it in the center of the image, but this is not what i want.

So let’s assume the video is full HD, meaning it has the dimensions 1920 x 1080 (Width x Height), and the image, as you can see has the dimensions (500 x 100), what i want here is to have the watermark centered horizontally and nudged down 100 pixels vertically, the code to do that would be

ffmpeg -i z.mp4 -i watermark.png -filter_complex "overlay=x=(1920-500)/2:y=100" z3.mp4

And in case this is not clear, here is a code to place it in the bottom right side of the screen

ffmpeg -i z.mp4 -i watermark.png -filter_complex "overlay=x=(1920-500):y=(1080-100)" z6.mp4

Now with the process of watermarking out of the way, How do we batch process videos under windows and under linux ?

Under Linux it is simple, I put all the input files in a directory named “in” and all the output is to be put in an directory called “out”, the shell script (batch file) is at the root where those 2 directories exist, the shell script is this

!/bin/bash
OIFS="$IFS"
IFS=$'\n'
for filename in "in/"*.mp4; do
ffmpeg -i "$filename" -i /apth_to_watermark/watermark.png -filter_complex "overlay=x=(1920-500):y=(1080-100)" "out/$(basename "$filename" .mp4).mp4"
done
IFS="$OIFS"

I have never been good at windows, so i looked around for a script to traverse a directory, I found some stuff, and here is my final result, if you can clean it up and make it more robust, please do leave me a comment and i will improve with your recommendations.

Leave a Reply

Your email address will not be published. Required fields are marked *