How to add a watermark with Python and FFmpeg on your video

The FFmpeg multimedia framework offers many utilities which can be used to process video files. Having used such tools myself for some time now, I find the framework truly useful, especially when I want to do video conversion. Although FFmpeg is a great framework to use as a standalone; accessing its features through the Python computer programming language makes things more interesting.

There are times when one wants to add a watermark to their video file. With the main purpose of helping those in such situation, I will try to explain in simple words everything one needs to achieve the task of adding a watermark to their video file.

First of all, make sure you have installed FFmpeg and Python on your operating system; the installation and configuration of such tools is beyond the scope of this article.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

For those of you who don’t know how to find out if they already have FFmpeg and Python installed on their machines, the following commands can help to figure it out.

Once you have managed to open a terminal application on your operating system, run the following commands.

ffmpeg

If your own operating system has been configured with the FFmpeg multimedia framework, the following output should come on your console.

ffmpeg version 3.2 Copyright (c) 2000-2016 the FFmpeg developers
built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
configuration: --prefix=/usr/local/Cellar/ffmpeg/3.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-vda
libavutil 55. 34.100 / 55. 34.100
libavcodec 57. 64.100 / 57. 64.100
libavformat 57. 56.100 / 57. 56.100
libavdevice 57. 1.100 / 57. 1.100
libavfilter 6. 65.100 / 6. 65.100
libavresample 3. 1. 0 / 3. 1. 0
libswscale 4. 2.100 / 4. 2.100
libswresample 2. 3.100 / 2. 3.100
libpostproc 54. 1.100 / 54. 1.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Or something similar to the above output. To check if the Python computer programming language is being provided by your own operating system make use of the following command on your terminal console.

python

The output which comes out in your own console should be similar to the one being shown below.

Python 2.7.12 (default, Jun 29 2016, 14:05:02)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.

As you can see from the above console output, the version of Python which I am currently using is the 2.7.12. Any 2.7.x would work.

Once you have figured out that both Python and FFmpeg tools are already installed on your own operating system, it is time to put them in use with the main purpose of automating our tasks.

Before writing any piece of Python code, we need to test a few commands in FFmpeg.

Add a watermark to your video with ffmpeg

As you may already know, the FFMpeg multimedia framework in an open source project written in a low level computer programming language called C.

Free as in beer, the FFmpeg framework has many different tools within it; tools that help to process multimedia files.

Since the main purpose of this article is to teach how to add a watermark to a video file with the help of the ffmpeg utility part of the FFmpeg framework, let’s get into some action.

Launch the terminal application on your own operating system and try to run the following command which is specific to the FFmpeg multimedia framework.

ffmpeg -i input.mp4 -i watermark.png -filter_complex overlay=20:20 output.mp4

The above command makes use of the FFmpeg filters to add a watermark to a video file; which in the above case is the video called input.mp4. As you have already guessed, the watermark in the above practical example is named watermark.png. Make sure to provide the path to your files for the above command to properly work and run without any errors.

Now that we have managed to add a watermark with the help of the ffmpeg tool part of the FFMpeg multimedia framework, it is time to automate the task with the help of the Python computer programming language. Before going any further, make sure to create a new Python module like the one which is being shown below.

add_wattermark.py

For now the above Python module is totally empty. With the main purpose of making stuff easier to understand, open the above file in editing mode and add the comments shown below.

# adds watermark to a video file
# makes use of the ffmpeg tool part of the FFMpeg
# multimedia framework to process the video file

Once you have managed to write the above comments in the Python module called add_watermark.py, make use of the builtin import statement like shown below.

# adds watermark to a video file
# makes use of the ffmpeg tool part of the FFMpeg
# multimedia framework to process the video file

from subprocess import Popen, PIPE

Then define a list of commands specific to the FFMpeg multimedia framework like shown below.

# adds watermark to a video file
# makes use of the ffmpeg tool part of the FFMpeg
# multimedia framework to process the video file

from subprocess import Popen, PIPE

# define a list of commands to execute with Python
cmds = ['ffmpeg', '-i', 'input.mp4', '-i', 'watermark.png', '-filter_complex', 'overlay=20:20', 'output.mp4']

Now that you have defined the list of commands to be executed, it is time to launch a child process with the help of the Popen class like shown below.

# adds watermark to a video file
# makes use of the ffmpeg tool part of the FFMpeg
# multimedia framework to process the video file

from subprocess import Popen, PIPE

# define a list of commands to execute with Python
cmds = ['ffmpeg', '-i', 'input.mp4', '-i', 'watermark.png', '-filter_complex', 'overlay=60:60', 'output.mp4']
child_p = Popen(cmds, stdin=PIPE, stdout=PIPE, stderr=PIPE)

Now that we have finished to code our simple module, we can easily make use of it by running the following command on our console application.

python add_watermark.py

Wait until the code does its job and in the end the result will be a video file with an watermark on it.

As you can see from the above screenshot generated from my computer, the video in the output has a watermark.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *

*