FFMPEG filters provide a powerful way to programmatically enhance or alter videos, and it’s fairly simple to add a watermark to a video using the overlay filter. The easiest way to install ffmpeg is to download a pre-built binary for your specific platform. Then you don’t have to worry about including and installing all the right dependencies and codecs you will be using.
Here’s a short video I took while mountain biking that we’ll be working with.
Video Player
ffmpeg -i birds.mp4 -i watermark.png -filter_complex "overlay=10:10" birds1.mp4
Basically, we’re passing in the original video, and an overlay image as inputs, then passing it through the filter, and saving the output as birds1.mp4.
We specify a specific position of the overlay in pixels – 10:10 puts the video 10 pixels from the top and 10 pixels from the right. (x:y coordinates)
Here is our result:
Video Player
- main_h – the video’s height
- main_w – the video’s width
- overlay_h – the overlay’s height
- overlay_w – the overlay’s width
Using these variable we can position the watermark right in the center of the video like so:
ffmpeg -i birds.mp4 -i watermark.png \ -filter_complex "overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2" birds2.mp4
Video Player
ffmpeg -i birds.mp4 -i watermark2.png \ -filter_complex "pad=height=ih+40:color=#71cbf4,overlay=(main_w-overlay_w)/2:main_h-overlay_h" \ birds3.mp4
ffmpeg -i birds.mp4 -i watermark.png \ -filter_complex "overlay='if(gte(t,1), -w+(t-1)*200, NAN)':(main_h-overlay_h)/2" birds4.mp4
Video Player
ffmpeg – watermark positions
This are the command lines that I use to add the watermark picture named watermark.png on the video named source.avi and export to output.flv.
Tested on a DigitalOcean Virtual server! 😉
The 10 values are the paddings!
Top left
ffmpeg –i source.avi -vf "movie=watermark.png [watermark]; [in][watermark] overlay=10:10 [out]" output.flv
Top right
ffmpeg –i source.avi -vf "movie=watermark.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:10 [out]" output.flv
Bottom left
ffmpeg –i source.avi -vf "movie=watermark.png [watermark]; [in][watermark] overlay=10:main_h-overlay_h-10 [out]" output.flv
Bottom right
ffmpeg –i source.avi -vf "movie=watermark.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]" output.flv
Center?
ffmpeg –i source.avi -vf “movie=watermark.png [watermark]; [in][watermark] overlay=main_w/2-overlay_w/2:main_h/2-overlay_h/2 [out]” output.flv