12

I'm trying to programmatically overlay either images or a video on the top of another video using FFmpeg. It seems that AVFilter can do this.

There are lots of examples of how to do this or similar things with the command line however, I have found no examples of using AVFilter programmatically apart from doc/examples/filtering.c which helps me but not really enough. I can already decode and encode a video, I just need to learn how to filter the decoded frames and add a watermark.

Are there any examples of using libavfilter programmatically?

Are there examples of using the overlay or movie filters?

Kage
  • 765
  • 1
  • 8
  • 27
  • The code example can be easily modified to apply an arbitrary filter. What is the problem? For examples of using overlay and movie filters see [How to watermark a video using FFmpeg](http://www.idude.net/index.php/how-to-watermark-a-video-using-ffmpeg). – Dmitry Shkuropatsky Feb 06 '12 at 06:48
  • 1
    The problem with the example is that it is overloaded (2 filters, filter graph, etc). For someone who is new and wants to find out how to apply one AVFilter it is confusing, because it does not make it clear of what is absolutely needed to get started. – Alex Kennberg Jul 13 '12 at 21:53

1 Answers1

6

The command:

ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=10:10 [out]" outputvideo.flv

produces the video with the image "watermarklogo.png" in the top left hand corner. Calling this command from another program should be simple enough.

Breaking this down bit by bit to understand it:

ffmpeg is the program you'll be using to add the watermark.

-i is used to specify the input files.

inputvideo.avi is your input file specified by -i.

-vf is used to specify the video filter. In this case, this is everything in the quotes.

movie=watermarklogo.png will load the file you want to use as the watermark. Here we load the file as a video source (by using movie) regardless of whether or not the file is an video. In this case it is an image.

[watermark] labels the file you just loaded. This label will be used in the rest of the code.

[in] and [out] refer to the input stream and the output stream of the video.

overlay is used right after the [watermark] label so that it refers to it. In this simple case we place the overlay at 10:10. This means the watermark will be offset by 10 pixels from the top and from the left. If you wanted bottom right you would use overlay=main_w-overlay_w-10:main_h-overlay_h-10 where main_w is the input stream's width, overlay_h is the height of the overlay file, and so on.

Lastly, outputvideo.flv is clearly the file you wish to save the results to.

Additional information:

I found this information through the site Dmitry had mentioned in the comments. Alex had mentioned that this page might be too complex for someone who's new to such things. However, I've never done anything like this and within just a couple minutes I had the results I believe are being sought.

Note: I had a moment of trouble when I was getting the error:

error while opening encoder for output stream #0.1

If you have the same problem you likely need to manually set the sampling frequency using the -ar parameter (e.g. -ar 22050).

golmschenk
  • 11,736
  • 20
  • 78
  • 137
  • Can you define timing for the watermark? lets say I want the watermark to start at 00:00 and disappear @ 01:00, is that possible? – rroche Aug 31 '12 at 23:10
  • @rroche Check out [this post](http://stackoverflow.com/questions/9160771/ffmpeg-watermark-first-30-second). Felix's answer is probably the simplest, but Dmitry's answer (the same Dmitry from above) is a bit neater and allows for a fade out of the watermark. – golmschenk Sep 01 '12 at 01:55
  • 4
    The question relates to using the AVFilter library, not calling the FFMPEG binary. – Erik Sep 04 '12 at 02:23