<p>This is an addition to the excellent answer by PTS and an answer for chovy.  If you want to place the overlay at the lower right corner, FFMPEG can calculate that for you very easily.</p>
<p>Use the modified command:</p>
<pre><code>ffmpeg -i input.mp4 -i image.png \
-filter_complex &quot;[0:v][1:v] overlay=W-w:H-h:enable='between(t,0,20)'&quot; \
-pix_fmt yuv420p -c:a copy \
output.mp4
</code></pre>
<p>The W-w means main video width minus overlay width, and the same for H-h.</p>

CC BY-SA 4.0
2020-11-01T18:27:23.757Z
Thank you for telling the secret answer: what is W-w, it's really unclearly said in documentation about it.
Alex Sham
CC BY-SA 4.0
2021-03-18T22:08:11.777Z
@AlexSham `W` is an `ffmpeg` alias for the width of the video you are overlaying on, and `w` is the alias for the width of the image being overlaid. Same with `H` and `h`. These can also be referred to as `main_w` (or `_h`) and `overlay_w` (or `_h`)
BallpointBen
CC BY-SA 4.0
2021-07-22T16:16:27.183Z
Thanks for this trick. What if w is bigger than W or h is bigger than H. My objective is to center the overlay on the video no matter what their sizes are. I am afraid ffmpeg might throw an exception if there are negative values. Also does `filter_complex` make the output video the same size as the input video always? Or does it make it the same size as the bigger size of the input video and overlay png?
aytunch
CC BY-SA 4.0
2021-07-24T22:55:39.897Z
I'm not sure about the functionality if the overlay is larger, as that is an unusual use case.  Perhaps something like (W-w)/2?
Slappy_G
CC BY-SA 4.0
2020-10-02T14:31:47.723Z
2020-10-02T14:31:47.723Z
Slappy_G
<p>If you wanna resize the image.</p>
<pre class="lang-sh prettyprint-override"><code>ffmpeg -i input.mp4 -i image.png -filter_complex &quot;[1:v]scale=480:854[overlay]; [0:v][overlay]overlay=0:0:enable='between(t,0,20)'&quot; -pix_fmt yuv420p -c:a copy output.mp4
</code></pre>

CC BY-SA 4.0
2024-05-13T17:35:22.513Z
Piece of advice: please don't ever answer in comments. That is not what they are for.
Dr Mayhem
CC BY-SA 4.0
2024-04-30T07:24:32.830Z
2024-05-13T17:36:27.120Z
2024-05-13T17:36:27.120Z
requizm
<p>You can do a simple image overlay using the following syntax:</p>
<pre><code>ffmpeg -i input.mp4 -i image.png \
-filter_complex &quot;[0:v][1:v] overlay=25:25:enable='between(t,0,20)'&quot; \
-pix_fmt yuv420p -c:a copy \
output.mp4
</code></pre>
<p><code>overlay=25:25</code> means we want to position the image 25px to the right and 25px down, originating from the top left corner (0:0).</p>
<p><code>enable='between(t,0,20)'</code> means we want the image to show between second 0 and 20.</p>
<p><code>[0:v][1:v]</code> means that we want the first video file we import with <code>-i</code>, in our case input.mp4 or how ffmpeg sees it, video input file number 0, to be under video input file 1, in our case image.png. <code>:v</code> just means we want the video track from these file sources. <code>[0:a]</code> would mean we want the first imported audio track. Which would also come from input.mp4 but would point to the audio track instead of the video track in the mp4 file.</p>
<p>If you want a certain image quality/settings and not the settings ffmpeg chooses, add the image and or audio encoding options you want to use. The default video encoder will be x264. Check the <a href="https://trac.ffmpeg.org/wiki/Encode/H.264" rel="noreferrer">H.264 encoding guide</a> for possible settings.</p>
<p>The <code>-acodec copy</code> / <code>-c:a copy</code> that you have in your command f.e. would simply re-use the audio from the source file. Though you can't do that with the video of course (in this case), that has to be transcoded because we are creating a new video source.</p>
<p>If you want to transcode audio, remove the <code>-c:a copy</code> part. You may have to explicitly specify an encoder, e.g. <code>-c:a aac -strict experimental</code>. See the <a href="https://trac.ffmpeg.org/wiki/Encode/AAC" rel="noreferrer">AAC encoding guide</a> for more info.</p>

CC BY-SA 3.0
2015-03-20T11:11:20.213Z
The following iteration of the above command worked for me on Android since the original one gives and error like aac is an experimental feature:
ffmpeg -i input.mp4 strict -2 -i image.png -filter_complex [0:v][1:v] overlay=25:25:enable='between(t,0,20)' output.mp4
muneikh
CC BY-SA 3.0
2016-10-26T13:22:25.923Z
Is there any method or calculation which makes the image appear the the center?
utdev
CC BY-SA 3.0
2017-06-04T20:47:10.727Z
Your command **replaces** a fragment of the original video file, but a user may want to **add** (thus increasing the video length) an image before and/or after the video. Please help with this
porton
CC BY-SA 3.0
2017-06-04T21:15:30.093Z
@porton This is question was about adding an overlay, if you need help with a different issue, feel free to open a new question at the top right of this page with the "Ask Question" button.
timonsku
CC BY-SA 3.0
2017-08-22T11:49:09.467Z
Can we use GIF instead of PNG for same purpose?@PTS
Akash Dubey
CC BY-SA 3.0
2017-08-22T13:41:30.470Z
Sure, every format supported by FFMPEG. You could also use a video file.
timonsku
CC BY-SA 4.0
2018-05-11T01:54:21.690Z
I think the -strict-experimental option is no longer needed.
stib
CC BY-SA 4.0
2018-07-08T08:46:32.967Z
@PTS In fact, that is exactly the question being asked here, which you did not answer *at all*. Nowhere did he ask how to overlay an image *on top* of the video, but how to insert it before the video starts (using the overlay *filter*). Note the wording of the question, the -itsoffset in his example code, and the undesirable alternative (rendering it separately).
Wlerin
CC BY-SA 4.0
2018-07-08T21:55:45.463Z
He may have tried it because of the apparently confusing wording. If it wasn't what he wanted then he probably wouldn't have accepted it as an answer to this question. If you have a problem with that you are always free to ask a new question covering the case you are looking for.
timonsku
CC BY-SA 3.0
2016-08-26T04:08:24.027Z
is there anyway to position at bottom without knowing the height? or far right without knowing the width?
chovy
CC BY-SA 3.0
2016-08-26T21:33:06.413Z
sure, just analyse the video with ffprobe and extract the dimensions of the video from the output. Maybe there is also an option integrated in ffmpeg but I'm not aware of it.
timonsku
CC BY-SA 4.0
2019-03-28T12:18:07.607Z
And what is the `pix_fmt` argument for? I see this in most ffmpeg commands out there, but whether I include it or remove it, the result is the same. Is it actually useful to put it in the command?
laurent
CC BY-SA 4.0
2019-03-29T14:24:18.087Z
@laurent that depends on your source file and or output. pix_fmt stands for pixel format. There are a lot of different pixel formats available for different codecs, specifying one ensure you don't end up with something that doesn't work out for you. yuv420p is the most supported pixel format when it comes to h264 but there is also yuv422p and yuv444p for example.
timonsku
CC BY-SA 4.0
2019-11-21T16:48:18.883Z
@Deepak if you got a new question please open one. You can try encoding your image into a video of specific length though. Again please open a new question, comments are not the place for this.
timonsku
CC BY-SA 4.0
2020-11-24T08:00:22.967Z
wow this worked right out of the box for me! thanks
zzapper
CC BY-SA 4.0
2021-03-18T22:04:30.190Z
Is there a way to also resize the image before overlaying it, or does that have to be done beforehand?
BallpointBen
CC BY-SA 4.0
2021-04-21T15:32:35.713Z
@BallpointBen did u find a way to change size?
reverie_ss
CC BY-SA 4.0
2014-07-08T20:03:44.813Z
2021-10-20T17:37:30.377Z
2021-10-20T17:37:30.377Z
timonsku
<p>I have many videos and I need to put an image in front of them for about 5 seconds, but adding it manually and rendering it each time would take really long, so I am asking if it is possible to do so via ffmpeg and if you could, please, help me out with it since I have no experience with ffmpeg. I've found some commands already but none of them worked.
This one for example.</p>

<pre><code>ffmpeg -itsoffset 5 -i in.mp4 -r 25 -loop 1 -i intro.png -filter_complex "[1:v] fade=out:125:25:alpha=1 [intro]; [0:v][intro] overlay [v]" -map "[v]" -map 0:a -acodec copy out.mp4
</code></pre>

CC BY-SA 3.0
2014-07-07T12:18:11.547Z
Did your just copy that command or wrote that according to your needs? I'm asking because this command involves things like a fade out and all kinds of other stuff not covered in your question. Do you just need to overlay an image or do you need more than that?
timonsku
CC BY-SA 3.0
2014-07-08T09:11:48.580Z
I copied it from somewhere, all I need is just to put the image in front of video.
Azthy
<video><editing><ffmpeg>
Add an image overlay in front of video using ffmpeg
Azthy
# Add an image overlay in front of video using ffmpeg

(Asked by: username_0 on 2014-07-07)

I have many videos and I need to put an image in front of them for about 5 seconds, but adding it manually and rendering it each time would take really long, so I am asking if it is possible to do so via ffmpeg and if you could, please, help me out with it since I have no experience with ffmpeg. I've found some commands already but none of them worked.
This one for example.
ffmpeg -itsoffset 5 -i in.mp4 -r 25 -loop 1 -i intro.png -filter_complex "[1:v] fade=out:125:25:alpha=1 [intro]; [0:v][intro] overlay [v]" -map "[v]" -map 0:a -acodec copy out.mp4

**Tags:** video, editing, ffmpeg

> **Comments:**
> **username_1**: Did your just copy that command or wrote that according to your needs? I'm asking because this command involves things like a fade out and all kinds of other stuff not covered in your question. Do you just need to overlay an image or do you need more than that?  
> **username_0**: I copied it from somewhere, all I need is just to put the image in front of video.  

---

## 3 answers

### Answer by username_1 on 2014-07-08. Score: 112 | Accepted answer

You can do a simple image overlay using the following syntax:
ffmpeg -i input.mp4 -i image.png \
-filter_complex "[0:v][1:v] overlay=25:25:enable='between(t,0,20)'" \
-pix_fmt yuv420p -c:a copy \
output.mp4

overlay=25:25 means we want to position the image 25px to the right and 25px down, originating from the top left corner (0:0).
enable='between(t,0,20)' means we want the image to show between second 0 and 20.
[0:v][1:v] means that we want the first video file we import with -i, in our case input.mp4 or how ffmpeg sees it, video input file number 0, to be under video input file 1, in our case image.png. :v just means we want the video track from these file sources. [0:a] would mean we want the first imported audio track. Which would also come from input.mp4 but would point to the audio track instead of the video track in the mp4 file.
If you want a certain image quality/settings and not the settings ffmpeg chooses, add the image and or audio encoding options you want to use. The default video encoder will be x264. Check the H.264 encoding guide for possible settings.
The -acodec copy / -c:a copy that you have in your command f.e. would simply re-use the audio from the source file. Though you can't do that with the video of course (in this case), that has to be transcoded because we are creating a new video source.
If you want to transcode audio, remove the -c:a copy part. You may have to explicitly specify an encoder, e.g. -c:a aac -strict experimental. See the AAC encoding guide for more info.

> **Comments:**
> **username_8**: The following iteration of the above command worked for me on Android since the original one gives and error like aac is an experimental feature: ffmpeg -i input.mp4 strict -2 -i image.png -filter_complex [0:v][1:v] overlay=25:25:enable='between(t,0,20)' output.mp4  
> **username_14**: is there anyway to position at bottom without knowing the height? or far right without knowing the width?  
> **username_1**: sure, just analyse the video with ffprobe and extract the dimensions of the video from the output. Maybe there is also an option integrated in ffmpeg but I'm not aware of it.  
> **username_9**: Is there any method or calculation which makes the image appear the the center?  
> **username_10**: Your command **replaces** a fragment of the original video file, but a user may want to **add** (thus increasing the video length) an image before and/or after the video. Please help with this  
> **username_1**: @username_10 This is question was about adding an overlay, if you need help with a different issue, feel free to open a new question at the top right of this page with the "Ask Question" button.  
> **username_11**: Can we use GIF instead of PNG for same purpose?@PTS  
> **username_1**: Sure, every format supported by FFMPEG. You could also use a video file.  
> **username_12**: I think the -strict-experimental option is no longer needed.  
> **username_13**: @PTS In fact, that is exactly the question being asked here, which you did not answer *at all*. Nowhere did he ask how to overlay an image *on top* of the video, but how to insert it before the video starts (using the overlay *filter*). Note the wording of the question, the -itsoffset in his example code, and the undesirable alternative (rendering it separately).  
> **username_1**: He may have tried it because of the apparently confusing wording. If it wasn't what he wanted then he probably wouldn't have accepted it as an answer to this question. If you have a problem with that you are always free to ask a new question covering the case you are looking for.  
> **username_15**: And what is the `pix_fmt` argument for? I see this in most ffmpeg commands out there, but whether I include it or remove it, the result is the same. Is it actually useful to put it in the command?  
> **username_1**: @username_15 that depends on your source file and or output. pix_fmt stands for pixel format. There are a lot of different pixel formats available for different codecs, specifying one ensure you don't end up with something that doesn't work out for you. yuv420p is the most supported pixel format when it comes to h264 but there is also yuv422p and yuv444p for example.  
> **username_1**: @Deepak if you got a new question please open one. You can try encoding your image into a video of specific length though. Again please open a new question, comments are not the place for this.  
> **username_16**: wow this worked right out of the box for me! thanks  
> **username_4**: Is there a way to also resize the image before overlaying it, or does that have to be done beforehand?  
> **username_17**: @username_4 did u find a way to change size?  

---

### Answer by username_2 on 2020-10-02. Score: 18

This is an addition to the excellent answer by PTS and an answer for username_14.  If you want to place the overlay at the lower right corner, FFMPEG can calculate that for you very easily.
Use the modified command:
ffmpeg -i input.mp4 -i image.png \
-filter_complex "[0:v][1:v] overlay=W-w:H-h:enable='between(t,0,20)'" \
-pix_fmt yuv420p -c:a copy \
output.mp4

The W-w means main video width minus overlay width, and the same for H-h.

> **Comments:**
> **username_3**: Thank you for telling the secret answer: what is W-w, it's really unclearly said in documentation about it.  
> **username_4**: @AlexSham `W` is an `ffmpeg` alias for the width of the video you are overlaying on, and `w` is the alias for the width of the image being overlaid. Same with `H` and `h`. These can also be referred to as `main_w` (or `_h`) and `overlay_w` (or `_h`)  
> **username_5**: Thanks for this trick. What if w is bigger than W or h is bigger than H. My objective is to center the overlay on the video no matter what their sizes are. I am afraid ffmpeg might throw an exception if there are negative values. Also does `filter_complex` make the output video the same size as the input video always? Or does it make it the same size as the bigger size of the input video and overlay png?  
> **username_2**: I'm not sure about the functionality if the overlay is larger, as that is an unusual use case.  Perhaps something like (W-w)/2?  

---

### Answer by username_6 on 2024-04-30. Score: 1

If you wanna resize the image.
ffmpeg -i input.mp4 -i image.png -filter_complex "[1:v]scale=480:854[overlay]; [0:v][overlay]overlay=0:0:enable='between(t,0,20)'" -pix_fmt yuv420p -c:a copy output.mp4

> **Comments:**
> **username_7**: Piece of advice: please don't ever answer in comments. That is not what they are for.  

---

