1. Welcome to MoviePy's Text World
Alright, get ready to dive into the magical world of video editing with text overlays. Imagine this: you're a director, and your task isn’t just to make characters talk, but to control the words that appear on the screen. Yep, it's the magic of subtitles and credits!
Before we start, keep this in mind: MoviePy isn’t just another video-editing library. It’s like your personal video designer that can help you add a bit of glam and info to your project. You’ve already learned the basics of video editing—cutting, merging, a little special effects—now it’s time for the next level.
Why Add Text?
Text in a video plays a key role—it’s your info pop-ups, fancy credits, and those lifesaving subtitles for night owls watching your work while neighbors are asleep. MoviePy makes it super easy and stylish to do this.
2. Working with Text
Let’s start with simple text elements. MoviePy gives us this cool TextClip
function for adding text to your clips.
Creating a Text Clip
The basic setup for a text clip looks something like this:
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
# Open the video file
video = VideoFileClip("sample_video.mp4")
# Create a text clip
txt_clip = TextClip("Hello, World!", fontsize=70, color='white')
# Set the duration of the text clip
txt_clip = txt_clip.set_duration(video.duration)
# Specify the position of the text
txt_clip = txt_clip.set_position('bottom')
# Combine the video with the text
video_with_text = CompositeVideoClip([video, txt_clip])
# Save the result
video_with_text.write_videofile("output_video_with_text.mp4", fps=24)
Font and Color Customization
You can easily customize the text’s color, font, and size. In the example above, the text is white, but you can choose any other color, like this:
txt_clip = TextClip("Hello, World!", fontsize=70, font='Amiri-bold', color='yellow')
Similarly, the font can be changed as long as the desired font is installed on your system.
3. Text Positioning on the Screen
Text positioning can be adjusted using the set_position()
parameter, which accepts text values (like 'center'
, 'bottom'
) or coordinates.
Placing Text in Different Positions
# Text placed at the top
text_top = TextClip("Top Text", fontsize=30, color='blue').set_position(('center', 'top')).set_duration(5)
# Text placed at the bottom
text_bottom = TextClip("Bottom Text", fontsize=30, color='green').set_position(('center', 'bottom')).set_duration(5)
# Text placed at specific coordinates (x=100, y=100)
text_coords = TextClip("Text at Coordinates", fontsize=30, color='red').set_position((100, 100)).set_duration(5)
# Combine all texts with the video
final_video = CompositeVideoClip([video, text_top, text_bottom, text_coords])
final_video.write_videofile("video_with_positioned_text.mp4")
Here’s what’s happening:
('center', 'top')
: Places the text centered at the top.('center', 'bottom')
: Places the text centered at the bottom.(100, 100)
: Sets the text’s position at the specific coordinates.
Dynamic Text Updates
It’s not just about adding text—it’s about making it come alive! Let’s try some animated text.
def move_text(t):
return 10, 100 * t # Move text downward over time
txt_clip = txt_clip.set_pos(move_text).set_duration(3)
4. Subtitles Work
Subtitles are just text that syncs with specific moments in the video. You can load them from files or create them manually.
Manually Adding Subtitles
Let’s start with a simple example of manually adding subtitles to a video. Here, we’ll create a text clip and set its timing:
# Create subtitles
subtitles = [
{"text": "Hello, World!", "start": 0, "end": 2},
{"text": "This is a subtitle!", "start": 2.5, "end": 5}
]
# Combine video with subtitles
sub_clips = [TextClip(sub['text'], fontsize=50, color='white')
.set_position(('center', 'bottom'))
.set_start(sub['start'])
.set_duration(sub['end'] - sub['start'])
for sub in subtitles]
video_with_subtitles = CompositeVideoClip([video] + sub_clips)
video_with_subtitles.write_videofile("output_video_with_subtitles.mp4", fps=24)
Loading Subtitles from a File
If you have a subtitle file in SRT format, you can load it much easier. MoviePy supports loading these files, making the process simpler.
Subtitles are usually created in SRT or VTT formats, which contain the text and time stamps for when each piece of text should appear and disappear. Here’s what an .srt
file might look like:
1
00:00:01,000 --> 00:00:04,000
Hello, this is a sample subtitle.
2
00:00:05,000 --> 00:00:08,000
This subtitle appears a bit later.
Each section includes:
- The subtitle line number.
- The time range in the format
hour:minute:second,millisecond
. - The subtitle text.
Installing Required Libraries
MoviePy doesn’t support loading .srt
files directly, but you can handle it with the pysrt
library. It helps to load and parse .srt
files, which can then be converted into text clips for your video.
Install the required libraries:
pip install moviepy pysrt
Loading Subtitles from a File Using PySRT
First, load the subtitles from an .srt
file using pysrt
and convert them into text clips for MoviePy.
Loading Subtitles and Converting Them into Text Clips
import pysrt
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
# Load the video
video = VideoFileClip("sample_video.mp4")
# Load the subtitles from a file
subtitles = pysrt.open("subtitles.srt", encoding='utf-8')
# Text clips for each subtitle
subtitle_clips = []
# Convert subtitles into text clips
for subtitle in subtitles:
# Subtitle text
text = subtitle.text
# Subtitle start and end times in seconds
start_time = subtitle.start.seconds + subtitle.start.milliseconds / 1000.0
end_time = subtitle.end.seconds + subtitle.end.milliseconds / 1000.0
duration = end_time - start_time
# Create a text clip for this subtitle
text_clip = TextClip(text, fontsize=24, color='white', font="Arial")
text_clip = text_clip.set_position(('center', 'bottom')).set_start(start_time).set_duration(duration)
# Add to the list
subtitle_clips.append(text_clip)
# Combine subtitles with the video
final_video = CompositeVideoClip([video] + subtitle_clips)
final_video.write_videofile("video_with_subtitles.mp4")
GO TO FULL VERSION