Sunday, August 4, 2019

Adding Watermark To Video With FFMpeg

A pretty popular question concerning video editing is 'how do I add a watermark'? 

Overlaying a video with your logo or name is a way to get your brand out, identify your work, and get eyes on your identity.

Relatively simple to do with Ffmpeg.  You'll need as inputs a video and a suitably sized graphic.  One sweep of your FFMpeg magic wand and 'Bob's Your Uncle', pure video magic.

Lets get started:

I've referenced in past blog posts that FFMpeg and Make are a great combination for video editing, so all the steps we'll be performing can be accomplished by the following makefile:

$ cat -n Makefile 
     1 all: output.mp4
     2
     3 input.mp4 :
     4 ${SH} youtube-dl https://www.youtube.com/watch?v=Td1lyPwiM0Y -o $@
     5
     6 watermarklogo.png:
     7 ${SH} convert /mnt/svn/mal/main/trunk/FSK/Media/Graphics/logos/logo/final.gif -resize 100x100 $@
     8
     9 output.mp4: input.mp4 watermarklogo.png
    10 ${SH} ffmpeg -i $< -vf "movie=watermarklogo.png [watermark]; [in] [watermark] overlay=main_w-overlay_w-10:10 [out]" -strict -2 $@
    11
    12
    13 clean:
    14 ${RM} *.mp4 *.png

But, we'll walk through the steps individually.

Grab your video; I'm using a YouTube downloading utility to grab one of my previous videos:
$ youtube-dl https://www.youtube.com/watch?v=Td1lyPwiM0Y -o input.mp4

The video is ~47 seconds long, 640x480.


Next, find or create a 100x100 image.  I'm resizing one using ImageMagick:
$ convert final.gif -resize 100x100 watermarklogo.png



Now,  our final step is to overlay the image on the video 10 pixels from the top/right corners:

$ ffmpeg -i $< -vf "movie=watermarklogo.png [watermark]; [in] [watermark] overlay=main_w-overlay_w-10:10 [out]" -strict -2 output.mp4

abbrabracadabra


Now, go make something cool.


3 comments:

  1. Very cool but I'm browsing ffmpeg documentation and there are filters which allow a rectangle of the screen to be written over. So a fixed watermark is at risk of overwriting. So next step I think is to have a watermark scroll across the screen on one line for a while before jumping to another horizontal position and start scrolling again. (I seen sample code to do a random jump). Finally, because I'm proposing a watermark to 'streak' all over the screen it had better be very subtle and not just a sprite; instead I'd like a translucent watermark like Spyglass in opening 3 seconds of this video. https://www.youtube.com/watch?v=uKBPIdL8PKE&feature=youtu.be&start=187&end=190 Happy to collaborate, I am blogger too.

    ReplyDelete
  2. Non-static watermarks; I can relate with the idea. Only resetting the row, once per pass, is more tricky than I can figure out, but this might give a reasonable alternative.

    $ ffmpeg -i input.mp4 -vf "movie=watermarklogo.png [watermark]; [in] [watermark] overlay=x=(mod(n\,main_w)):y=(mod(n\,main_h)) [out]" -strict -2 output.mp4

    ReplyDelete
  3. The script works well. Thank you!

    ReplyDelete