FFmpeg AVFilter overlay/watermark programmatically

Kage picture Kage · Feb 6, 2012 · Viewed 7.1k times · Source

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?

Answer

golmschenk picture golmschenk · Aug 30, 2012

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).