1. Adding Text and Animating Its Movement
MoviePy is just an awesome tool that lets you turn boring videos into masterpieces of cinema. Well, or at least into something with moving text and graphics. First, let’s figure out how to add static elements to our video, and then move on to the magic — animation.
Adding Text and Animating Its Movement
To create text and animate it in MoviePy, we use TextClip, which allows us to set text, font, color, and other parameters. Then, using the set_position() method, you can make the text move across the screen.
Text Moving Horizontally
In this example, the text will move from left to right.
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
# Loading the main video
video = VideoFileClip("sample_video.mp4")
# Creating a text clip
text = TextClip("Moving Text", fontsize=50, color='white')
text = text.set_position(lambda t: (int(200 * t), 100)).set_duration(5)
# Overlaying the text on the video
final_video = CompositeVideoClip([video, text])
final_video.write_videofile("video_with_moving_text.mp4", fps=24)
Here:
TextClip("Moving Text", fontsize=50, color='white')creates the text in white with a font size of 50.set_position(lambda t: (int(200 * t), 100))creates an animation where the text moves to the right horizontally at a constant speed.CompositeVideoClip([video, text])combines the text clip and the main video clip.
Animating Text with Changing Movement Direction
You can also create more complex text trajectories by combining horizontal and vertical movements.
Text Moving Diagonally
In this example, the text will move diagonally.
text = TextClip("Diagonal Text", fontsize=50, color='yellow')
text = text.set_position(lambda t: (int(100 + 50 * t), int(100 + 30 * t))).set_duration(5)
final_video = CompositeVideoClip([video, text])
final_video.write_videofile("video_with_diagonal_text.mp4", fps=24)
Here:
- The function
set_position(lambda t: (int(100 + 50 * t), int(100 + 30 * t)))creates diagonal movement by combining horizontal and vertical text shifts.
Animating Text with Fade-in and Fade-out Effects
To make text appear and disappear, you can change its opacity over time. MoviePy has methods fadein() and fadeout() for this.
Smooth Appearance and Disappearance of Text
# Text with smooth fade-in and fade-out effect
text = TextClip("Appearing Text", fontsize=40, color='blue')
text = text.set_position(('center', 'bottom')).set_duration(5).fadein(1).fadeout(1)
final_video = CompositeVideoClip([video, text])
final_video.write_videofile("video_with_fade_text.mp4", fps=24)
Here:
fadein(1)andfadeout(1)add smooth fade-in at the start and fade-out at the end over 1 second.
2. Adding Graphic Elements
Adding Images and Animating Their Movement
Besides text, MoviePy allows you to add images to videos using the ImageClip class. You can animate an image by setting its trajectory and changing its position with set_position().
Animating a Logo Moving from Bottom to Top
from moviepy.editor import VideoFileClip, ImageClip
# Loading video and image
video = VideoFileClip("sample_video.mp4")
logo = ImageClip("logo.png").set_duration(5)
# Animating the logo — moving from bottom to top
logo = logo.set_position(lambda t: ('center', int(500 - 100 * t)))
# Overlaying the logo on the video
final_video = CompositeVideoClip([video, logo])
final_video.write_videofile("video_with_moving_logo.mp4", fps=24)
Here:
ImageClip("logo.png").set_duration(5)loads the logo image and sets its duration.set_position(lambda t: ('center', int(500 - 100 * t)))makes the logo move vertically from bottom to top.
Image Appearing with Scaling Up Effect
Another effect you can create is an image appearing with gradual scaling up.
# Loading the image
logo = ImageClip("logo.png").set_duration(5)
# Setting image scale for zoom-in effect
logo = logo.resize(lambda t: 1 + 0.5 * t) # Scaling up
logo = logo.set_position(('center', 'center'))
# Overlaying the logo with scaling animation on the video
final_video = CompositeVideoClip([video, logo])
final_video.write_videofile("video_with_zoom_in_logo.mp4", fps=24)
Here:
logo.resize(lambda t: 1 + 0.5 * t)scales up the logo by 50% every second, creating a zoom-in effect.
3. Combining Multiple Animated Elements
MoviePy lets you combine multiple animated elements in one video. For example, you can add text moving horizontally and an image moving vertically at the same time.
Simultaneous Animation of Text and Image
# Creating text moving horizontally
text = TextClip("Text and Image", fontsize=40, color='red')
text = text.set_position(lambda t: (int(200 * t), 50)).set_duration(5)
# Animating the logo moving from bottom to top
logo = ImageClip("logo.png").set_duration(5)
logo = logo.set_position(lambda t: ('center', int(500 - 100 * t)))
# Overlaying the text and logo on the video
final_video = CompositeVideoClip([video, text, logo])
final_video.write_videofile("video_with_text_and_logo.mp4", fps=24)
Here:
- We create text animation moving from left to right and a logo moving from bottom to top.
CompositeVideoClip([video, text, logo])overlays both animated elements on the main video.
Creating Animation of Text and Graphics in One Video
In this example, we’ll create a full video clip where the text moves diagonally, and the logo smoothly appears and scales up.
from moviepy.editor import VideoFileClip, TextClip, ImageClip, CompositeVideoClip
# Loading video
video = VideoFileClip("sample_video.mp4")
# Text moving diagonally
text = TextClip("Animated Text", fontsize=40, color='yellow')
text = text.set_position(lambda t: (int(100 + 50 * t), int(100 + 30 * t))).set_duration(5)
# Logo scaling up with smooth appearance
logo = ImageClip("logo.png").set_duration(5)
logo = logo.resize(lambda t: 1 + 0.5 * t).fadein(1)
# Overlaying text and logo on the video
final_video = CompositeVideoClip([video, text, logo])
final_video.write_videofile("animated_text_and_logo.mp4", fps=24)
Errors to Watch Out For
Sometimes text or graphics might not display the way you expect. This can be due to incorrect timing or positioning. Check the logical expressions you’re using and don’t forget to debug your code.
Another common question is — why isn’t my video saving in the desired format? Make sure you have the necessary codecs installed for exporting videos. MoviePy makes it easier, but sometimes compatibility issues might arise, which can often be solved by installing extra libraries.
That wraps up our lesson. Now you know how to not only add text and graphics to videos but also make them dynamic and alive. Stay tuned for more exciting techniques in future lectures, and keep growing your skills!
GO TO FULL VERSION