How to Watermark A Video with Python

Why Watermarking?

Preventing people from saving video contents from your web site is never possible, because, firstly, video contents are always streamed to users' device, users just cannot watch the video without retrieving it, secondly, people can always do a screen recording to save content.

Watermarking seems to be the reasonable solution to protect your content. If people download your video file and put it onto their websites, at least, people will know who made the video and/or your website.

Why Python?

There are lots of software packages that can do this, but they are not suitable for doing this to many videos in a server environment. And, of course, life is short.

The Library

MoviePy is the go to choice for this type of task.

MoviePy is a Python module for video editing, which can be used for basic operations (like cuts, concatenations, title insertions), video compositing (a.k.a. non-linear editing), video processing, or to create advanced effects. It can read and write the most common video formats, including GIF.

The Steps

First: Installing MoviePy

Actually, MoviePy is not the only thing you need to install, it is dependent on many libraries and packages.

Most dependencies are installed automatically when you run pip install moviepy. There are two dependencies need to be installed manually, ImageMagick and FFMPEG.

ImageMagick can be easily installed via your system's package management tool.

According to the document of MoviePy, FFMPEG will be automatically installed during the first use of MoviePy. However, the automatically installed FFMPEG was somehow unable to use some of the codecs depending on your system. It's better to install FFMPEG by yourself.

FFMPEG is not included in some of the system's standard package repo, fortunately, FFMPEG's website provides some useful information about how to get it running in different platforms.

Second, Writing the Script

Writing the script is easy based on the example HERE in the documentation.

The example is actually more complicated that what we want, removing the unnecessary parts of the code:

from moviepy.editor import *

my_clip = VideoFileClip("../../videos/moi_ukulele.MOV", audio=True)  #  The video file with audio enabled

w,h = my_clip.size  # size of the clip

# A CLIP WITH A TEXT AND A BLACK SEMI-OPAQUE BACKGROUND

txt = TextClip("THE WATERMARK TEXT", font='Amiri-regular',
                   color='white',fontsize=24)

txt_col = txt.on_color(size=(my_clip.w + txt.w,txt.h-10),
                  color=(0,0,0), pos=(6,'center'), col_opacity=0.6)

# This example demonstrates a moving text effect where the position is a function of time(t, in seconds).
# You can fix the position of the text manually, of course. Remember, you can use strings,
# like 'top', 'left' to specify the position
txt_mov = txt_col.set_pos( lambda t: (max(w/30,int(w-0.5*w*t)),
                                  max(5*h/6,int(100*t))) )

# Write the file to disk
final = CompositeVideoClip([my_clip,txt_mov])
final.duration = my_clip.duration
final.write_videofile("OUT.mp4",fps=24,codec='libx264')

Third: Batch Processing

Rewriting the script, wrapping it into a function which takes input_file and output_file as arguments. Then, you can list the files to be watermarked with something like glob.glob('./*.mp4') and do a iteration over them.

Notes:

  1. To make the mp4 streamable, you'll need to add ffmpeg_params=['-movflags', 'faststart'] as an argument to write_videofile method.
  2. You can also add threads as an argument to accelerate the process.
  3. You may need to try different codec to make it work.
comments powered by Disqus