Skip to content

Instantly share code, notes, and snippets.

@bennylope
Forked from webkader/ffmpeg-watermark.md
Created April 22, 2016 23:17
Show Gist options
  • Save bennylope/d5d6029fb63648582fed2367ae23cfd6 to your computer and use it in GitHub Desktop.
Save bennylope/d5d6029fb63648582fed2367ae23cfd6 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
@heraldomode
Copy link

se consigue el mismo resultado haciendo esto:
creas una imagen con gimp en png con fondo trasnparente. el tamaño de la imagen debe ser el mismo del largo y ancho del video.
pones la ralla negra donde consideres y luego haces esto:
ffmpeg -i "videoDeprueva.mp4" -i "ruta_imagen.png" -filter_complex overlay salida.mp4
recuerda poner el fondo transparente es algo que es muy facil de hacer con gimp.

@laocoi
Copy link

laocoi commented Jun 15, 2018

Can i use video png as watermark?

@yadavkohi
Copy link

Yes, you can use the following command to add watermark to video:
-i /storage/emulated/0/Movies/Vid.mp4 -i /storage/emulated/0/Movies/logo.jpeg -filter_complex overlay=W-w-5:H-h-5 -codec:a copy -preset ultrafast -async 1 /storage/emulated/0/Movies/output.mp4

@protrolium
Copy link

Thank you so very much for sharing your methodology for doing this. I sincerely appreciate it.

@mreurob
Copy link

mreurob commented Jan 13, 2020

can somebody help me with small code, im new in general with ffmpeg and im testin some codes for info showing on videos

@SharlSherif
Copy link

Thank you, very helpful.

@jruizfdez
Copy link

Thanks for sharing. It's useful

@zia-shaadoow
Copy link

how to add two watermark into video ?

@SalimF
Copy link

SalimF commented Aug 13, 2020

Hi I want to move the source Video coordinates inside transparent image box up by 90px , how can I do that .

@TheDams77
Copy link

Hi, I need to watermark a video with an incremental number (like a serial number unique for each). Is it possible to do this with ffmpeg ?
The idea is to have for exmaple a video file "video.mp4" exported 500 times with a unique serial numebr on each (with a selecte font ideally)

@protrolium
Copy link

protrolium commented Jun 2, 2021

@TheDams77 you could possibly to this with ffmpeg and bash. Have a look at this script I recently made that uses two variables in a for loop in bash: https://github.com/protrolium/chartmetric-api-scraper/blob/main/api-request-handler/artist-metadata.sh

In that example I declare two separate arrays and iterate through them with the variable syntax, something like ${videos[$i]} + ${serialNumber[$i]}

after declaring the arrays, something like:

for ((i=0; i<${#videos[@]}; i++)); do ffmpeg -i ${videos[$i]} -i ${serialNumber[$i]} -filter_complex "overlay=x=(main_w-overlay_w)/2:y=(main_h-overlay_h)/2" "${videos[$i]}-watermarked.mp4"; done

@ravisorathiya
Copy link

    val cmd = arrayOf(
        "-i",
        video!!.path,
        "-vf",
        "drawtext=fontfile=" + font!!.path + ":text=" + text + ": fontcolor=" + color + ": fontsize=" + size + border + ": " + position,
        "-c:v",
        "libx264",
        "-c:a",
        "copy",
        "-movflags",
        "+faststart",
        outputLocation.path
    )

this is cmd for adding image watermark on video if i want to change position what should i do

like top left, top right,bottom left,bottom right etc....

@Pubghacker987
Copy link

Hey can anyone tell me how can I add a text watermark on telegram bots? Can anyone send me the code or the way ?

@BasToTheMax
Copy link

How to make the image bigger?

@janheinvanasseldonk
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment