Skip to content

Instantly share code, notes, and snippets.

@fabriziosalmi
Forked from bennylope/ffmpeg-watermark.md
Created December 9, 2017 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabriziosalmi/0771154dd9f40ecfa93d20da88b3dafa to your computer and use it in GitHub Desktop.
Save fabriziosalmi/0771154dd9f40ecfa93d20da88b3dafa to your computer and use it in GitHub Desktop.
FFmpeg add a watermark to video

How to Add a Watermark to Video

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.

Once you have ffmpeg installed, adding a watermark is as easy as passing your existing source through an overlay filter like so:

ffmpeg -i test.mp4 -i watermark.png -filter_complex "overlay=10:10" test1.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 test1.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)

In some cases you might not know the exact dimensions of the videos you’ll be watermarking. Fortunately, there are variables you can use to better position your watermark depending on the size of the video. These variables include:

  • 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 test.mp4 -i watermark.png \
-filter_complex "overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2" test2.mp4

If we wanted to add branding or a watermark to the clip but not cover the existing video, we can use the pad filter to add some padding to our clip, and then position our watermark over the padding like so:

ffmpeg -i test.mp4 -i watermark2.png \
-filter_complex "pad=height=ih+40:color=#71cbf4,overlay=(main_w-overlay_w)/2:main_h-overlay_h" \
test3.mp4

Once you start getting the hang of this, you can even animate your overlays!

ffmpeg -i test.mp4 -i watermark.png \
-filter_complex "overlay='if(gte(t,1), -w+(t-1)*200, NAN)':(main_h-overlay_h)/2" test4.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment