Create a Moving (Left to Right) Watermark in MoviePy – MoviePy TextClip Example

By | June 1, 2023

In python moviepy, we can use TextClip to create a watermark for a video file. In this tutorial, we will introduce a moving one.

Here is the example code:

from moviepy.editor import *

video = VideoFileClip("3.mp4")
w,h = video.size  # size of the clip
print(w,h, video.duration) #320 240, 6.04 seconds

txt_clip = TextClip("TutorialExample\n版权所有",fontsize=8,color='snow2',font = r"C:\Windows\Fonts\msyh.ttf")

txt_clip_w, txt_clip_h = txt_clip.size
max_width = w - txt_clip_w
per = max_width / video.duration

txt_mov = txt_clip.set_position( lambda t: (t*per,10) ).set_duration(video.duration).set_opacity(0.5)
result = CompositeVideoClip([video, txt_mov])
result.write_videofile("myHolidays_edited.mp4")

In this example, the key code is: txt_clip.set_position()

It will use time to determine the position of textclip. In order to understand python lambda, you can read:

Understand Python Lambda Function for Beginners – Python Tutorial

Run this code, we will see:

Create a Moving (Left to Right) Watermark in MoviePy - MoviePy TextClip Example

However, if you want to change the font and color of textclip, you can read:

Select font to change

Introduction to MoviePy TextClip Supports Chinese Character – Python Tutorial

Change color

List of Color Names, RGB, Hex in ImageMagick and MoviePy TextClip – Python Tutorial