How to add my logo for the first 30 seconds in a video with ffmpeg?

9

14

I'm trying to add my logo for the first 30 seconds in a video with ffmpeg. I have a video called d1.mp4 and a logo called logo.png.

When the video starts, the watermark should be at the top left or top right corner of the video and disappear after 30 seconds.

Can you please show me how to add it?

iwocan

Posted 2013-12-03T22:25:54.870

Reputation: 199

my video name is d1.mp4 and my logo name is logo.png ,thank you.. – iwocan – 2013-12-04T20:52:36.823

Then what does ffmpeg -i d1.mp4 -i logo.png show? Please just enter that command and show us what the output is. When asking questions about ffmpeg, you should always include as many details as possible about what video/audio material you have, what you already tried, and the full command line output of everything you have tried. – slhck – 2013-12-04T21:16:29.607

here's what i get on my cmd screen . http://pastebin.com/bPCnhXh2

– iwocan – 2013-12-04T21:27:53.743

Answers

13

ffmpeg -i in.mp4 -framerate 30000/1001 -loop 1 -i logo.png -filter_complex
  "[1:v] fade=out:st=30:d=1:alpha=1 [ov]; [0:v][ov] overlay=10:10 [v]" -map "[v]"
  -map 0:a -c:v libx264 -c:a copy -shortest out.mp4

This assumes that the logo is a single still image with an alpha channel and you want to overlay it over a video with a frame rate of 30000/1001 (NTSC rate). Change the -framerate to match your input video if it is different. If your logo is a video then omit -framerate 30000/1001 -loop 1. If the logo does not have an alpha channel, add one by inserting e.g. format=yuva420p, immediately before fade.

This will display the logo at x,y position 10,10 for 30 seconds followed by a 1 second fade out.

mark4o

Posted 2013-12-03T22:25:54.870

Reputation: 4 729

I had no idea you could have two inputs! – Louis – 2014-07-18T12:55:16.563

1Very Interesting! Could you explain why the framerate and loop are required in this case and not for a simple overlay without fade filter? – Rajib – 2013-12-04T16:25:00.943

Thank you very much mark40 but when i write this code, cmd screen doesn't stop.. I dont know why but it didnt work for me.. – iwocan – 2013-12-04T17:56:54.093

Hi LordNeckbeard,

Here's a screenshot.. http://u1312.hizliresim.com/1j/4/v5ny8.png ..

– iwocan – 2013-12-04T18:26:40.003

@iwocan Screenshots are not preferred over actual text. They are not searchable and do not show the complete output. If you use @ before someone's name then they will be notified of your reply. – llogan – 2013-12-04T18:29:09.727

I can't copy anything on cmd screen that's why i sent you a screenshot.I'm sorry for that. – iwocan – 2013-12-04T18:36:35.337

1

@iwocan I believe you can copy stuff out of the windows prompt. I assumed you would search how to do it, but look at Copy To the Clipboard From the Windows Command Prompt.

– llogan – 2013-12-04T20:07:23.230

@iwocan: Sorry, add -shortest (or -t and the desired duration) before the output file name. Otherwise the png will be looped forever. – mark4o – 2013-12-04T23:11:35.890

4@Rajib: The reason for the -framerate and -loop for a still image is so that the fade out will work. If there is only a single frame then it has no way to fade out over a 1 second interval. Ideally it should be the same frame rate as the video so that the fade will be as smooth as possible. – mark4o – 2013-12-04T23:18:19.113

@mark4o Thank you very much! :) It works perfect! :) – iwocan – 2013-12-04T23:50:29.677

19

Using overlay video filter to add a logo to a video:

enter image description here

ffmpeg -i video.mp4 -i logo.png -filter_complex "[0:v][1:v]overlay" \
-codec:a copy out.mp4

To understand this command you need to know what a stream specifier is and reading the Introduction to FFmpeg Filtering will help. [0:v] refers to the video stream(s) of first input (video.mp4), and [1:v] refers to the video stream of the second input (logo.mp4). This is how you can tell overlay what inputs to use. You can omit [0:v][1:v], and overlay will still work, but it is recommended to be explicit and not rely on possibly unknown defaults.

By default the logo will be placed in the upper left.

Using -codec:a copy will stream copy the audio. This simply re-muxes the audio instead of re-encoding it. Think of it as a "copy and paste" of the audio.

Moving the logo

This example will move the logo 10 pixels to the right, and 10 pixels down: enter image description here

ffmpeg -i video.mp4 -i logo.png -filter_complex "[0:v][1:v]overlay=10:10" \
-codec:a copy out.mp4

This example will move the logo 10 pixels from the right side and 10 pixels down:

enter image description here

ffmpeg -i video.mp4 -i logo.png -filter_complex \
"[0:v][1:v]overlay=main_w-overlay_w-10:10" -codec:a copy out.mp4

main_w refers to the width of the "main" input (the background or [0:v]), and overlay_w refers to the width of the "overlay" input (the logo or [1:v]). So, in the example, this can be translated to overlay=320-90-10:10 or overlay=220:10.

Timing the overlay

Some filters can handle timeline editing which allows you to use arithmetic expressions to determine when a filter should be applied. Refer to ffmpeg -filters to see which filters support timeline editing.

This example will show the logo until 30 seconds:

ffmpeg -i video.mp4 -i logo.png -filter_complex \
"[0:v][1:v]overlay=10:10:enable=between(t\,0\,30)" -codec:a copy out.mp4

If you want to fade the logo refer to mark4o's answer.

llogan

Posted 2013-12-03T22:25:54.870

Reputation: 31 929

finally something understandable! what should i use to make it appear at the beginning than at the end? – cikatomo – 2015-10-27T00:14:09.960

I meant to appear at the beginning AND at the end? Two times. i have tried enable=between(t,0,30):enable=between(t,50,60) , but the only last timeline is in effect – cikatomo – 2015-10-27T02:40:37.303

@cikatomo enable=not(between'(t,30,50)'). This will show the overlay at all times but not at 30-60 seconds duration. – llogan – 2015-10-27T05:14:06.170

Perfect, thank you. Despite your warning to not use overlay all by itself, I did, because the default position of top left was what I wanted. (So in the end my flags were simply filter_complex overlay.) – felwithe – 2017-05-06T06:36:45.523

I think it should be accepted answer. – Pratik Butani – 2019-09-05T06:53:59.397

Thank you very much! :) I apologize for my broken English again.This command works perfect! :) – iwocan – 2013-12-04T23:49:49.690