How to add transparent watermark in center of a video with ffmpeg?

109,475

Examples to add a watermark / logo image on video using the overlay filter.

Centered

enter image description here

ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" -codec:a copy output.mp4

or with the shortened overlay options:

overlay=(W-w)/2:(H-h)/2

Top left

This is the easy one because the default, if you provide no options to overlay, is to place the image in the top left.

This example adds 5 pixels of padding so the image is not touching the edges:

overlay=5:5

Top right

With 5 pixels of padding:

overlay=main_w-overlay_w-5:5

or with the shortened options:

overlay=W-w-5:5

Bottom right

With 5 pixels of padding:

overlay=main_w-overlay_w-5:main_h-overlay_h-5

or with the shortened options:

overlay=W-w-5:H-h-5

Bottom left

With 5 pixels of padding:

overlay=5:main_h-overlay_h

or with the shortened options:

overlay=5:H-h-5

Transparency / opacity / alpha

Example to make watermark 50% transparent using the format and colorchannelmixer filters:

ffmpeg -i input.mp4 -i watermark.jpg -filter_complex "[1]format=rgba,colorchannelmixer=aa=0.5[logo];[0][logo]overlay=(W-w)/2:(H-h)/2:format=auto,format=yuv420p" -c:a copy output.mp4

Improved quality

Using the format=auto option in the overlay filter can make PNG watermarks look better:

ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=5:H-h-5:format=auto,format=yuv420p" -c:a copy output.mp4

Note the addition of the format filter (yes, same name as the option, but a standalone filter) to reset it to YUV 4:2:0 which is needed for MP4 output. Remove ,format=yuv420p if you are not outputting MP4.

Scale watermark in relation to main video

Use the scale2ref filter:

Example to make logo 10% (1/10) the size of the main video:

ffmpeg -i input.mp4 -i watermark.png -filter_complex "[1][0]scale2ref=w=oh*mdar:h=ih*0.1[logo][video];[video][logo]overlay=5:H-h-5" -c:a copy output.mp4
Share:
109,475

Related videos on Youtube

mirza
Author by

mirza

Software Development is fun. http://mirzazeyrek.wordpress.com @mirzazeyrek - mirzazeyrek (@) gmail.com

Updated on November 06, 2020

Comments

  • mirza
    mirza over 3 years

    I am currently using these commands:

    Top left corner
    ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=10:10 [out]" outputvideo.flv
    
    Top right corner
    ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:10 [out]" outputvideo.flv
    
    Bottom left corner
    ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=10:main_h-overlay_h-10 [out]" outputvideo.flv
    
    Bottom right corner
    ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=(main_w-overlay_w-10)/2:(main_h-overlay_h-10)/2 [out]" outputvideo.flv
    

    How to place watermark center of the video ?

    • Sebastian Patten
      Sebastian Patten almost 10 years
      Bottom right corner is infact: ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]" outputvideo.flv
    • B.shruti
      B.shruti over 3 years
      @mirza Could you please tell me, where have you placed you watemarklogo.png file, as I am getting no such file or directory error
    • mirza
      mirza over 3 years
      should be in the same directory as you execute the command from. @B.shruti
  • llogan
    llogan over 9 years
    @HarishKumar Replace flv with mp4.
  • Harish Kumar
    Harish Kumar over 9 years
    i have tried replacing flv to .mp4 but that didn't worked for me
  • Michael J. Evans
    Michael J. Evans over 9 years
    .mp4 files have a restricted set of allowed codecs and profiles. Please make sure that you are requesting a the correct combination of stream codecs and codec parameters to produce a valid result.
  • Diaa Saada
    Diaa Saada almost 9 years
    how can we scale the watermark to be 2/3 of the video width and keep the ratio of the watermark can u help please
  • Diaa Saada
    Diaa Saada almost 9 years
  • The Hungry Androider
    The Hungry Androider about 8 years
    I'm trying to run this on Android using FFMPEG version n2.4.2 and I get the error message: No such filter: '"overlay', any ideas?
  • Developer
    Developer almost 8 years
    @LordNeckbeard i am trying this command with 10 sec video and it takes about five mins and with preset ultrafast it takes 1 min.
  • llogan
    llogan over 7 years
    @SyedJoharHussain You need to provide your full command and the complete console output. You can dump it to a pastebin site and provide a link here.
  • Developer
    Developer over 7 years
    @LordNeckbeard Here is my command and logs. Original video info: size20 MB, Resolution 1080x1920.
  • llogan
    llogan over 7 years
    @SyedJoharHussain From your output: using cpu capabilities: none! It should have something like ARMv7 NEON. You should investigate why x264 was compiled with no cpu capabilities. Otherwise you can downscale which should be faster to encoder.
  • mirza
    mirza over 7 years
    @LordNeckbeard what happened to the donkey picture :D
  • llogan
    llogan over 7 years
    @motto Good memory, but I thought I should replace it with an example that has transparency (alpha channel). Maybe someday I'll replace the log with a new donkey that has an alpha channel.
  • Chintan Khetiya
    Chintan Khetiya over 7 years
    I found No such filter: " invalid argument in android. anyone have idea for same ?
  • llogan
    llogan over 7 years
    @ChintanKhetiya Probably a quoting or escaping issue which is very common with Android users. Use a pastebin site to show your full ffmpeg command and the complete console output then provide the link in a comment.
  • Joshua Pinter
    Joshua Pinter over 6 years
    For others, to reduce opacity of my overlay watermark, I just edited the .png, reduced it to 50% opacity, re-saved it as a .png and used that. Worked great
  • Yupi
    Yupi about 6 years
    @LordNeckbeard I noticed you are an expert for FFmpeg library I have an issue with trying run command for creating watermark in Android. I'm really beginner in using FFmpeg could you take a look maybe you could know what I'm doing wrong: stackoverflow.com/questions/50529513/… Thanks anyway.
  • llogan
    llogan about 6 years
    @Yupi I'll take a look but I know nothing about Android or any of these wrapper things.
  • Aarif Ali
    Aarif Ali almost 4 years
    // String cmd = "-y -i " + iPath +" -c copy -map 0 -movflags faststart "+oPath; // String cmd = "-y -i "+ iPath + " -i "+lPath+ " -filter_complex overlay=W-w-5:H-h-5 -codec:a copy -preset faststart -async 1 "+oPath; // String cmd = "ffmpeg -i birds.mp4 -i watermark.png -filter_complex "overlay=10:10"; // String cmd = "-i "+iPath+" -i "+lPath+" -filter_complex overlay=10:10 "+oPath; i will try all command but not work
  • llogan
    llogan almost 4 years
    @AarifAli I am unable to help if you do not provide the actual errors.
  • Aarif Ali
    Aarif Ali almost 4 years
    @llogan Finally i was found a alternate solution.
  • Hashim Aziz
    Hashim Aziz over 2 years
    Not really related to the question, but what's the reason that format=yuv420p is needed for MP4? I can't say I've noticed anything leaving it out, but maybe that's because I wasn't even aware of what to look for.
  • llogan
    llogan over 2 years
    @HashimAziz ffmpeg tries to preserve color data so it will try to use the most "advanced" pixel format supported by the selected encoder for chroma subsampling. Default encoder for MP4 is usually libx264. libx264 supports many pixel formats (see ffmpeg -h encoder=libx264). However, most players can only support the lowest level which is YUV 4:2:0. So the format filter is added in the answer to ensure YUV 4:2:0 chroma subsampling for compatibility.
  • Hashim Aziz
    Hashim Aziz over 2 years
    Me again, thanks for a great answer that I find myself constantly coming back to on the topic of watermark overlays. A clarification on overlay transparency/opacity: I've been using a simpler filter to do this in the form of lut=a=val*0.5 without needing to set the alpha channel separately with format - how does this compare quality-wise to the colorchannelmixer solution you outlined? Are they equivalent or are there particular pros/cons for each?
  • llogan
    llogan over 2 years
    @HashimAziz Your input must already have alpha if you didn't need the format filter. With the format filter and a JPG to be overlaid, visually the results of colorchannelmixer vs lut look the same to me. lut was significantly faster (and you don't have to type as much). There are so many filters that often there are multiple methods. You'll have to take a look at the source code for each filter (libavfilter/vf_lut.c for example) for more than my lazy observations.
  • Hashim Aziz
    Hashim Aziz over 2 years
    @llogan That's more than enough for me, thank you. If you get the time I'd appreciate your input to this question.