31
loading...
This website collects cookies to deliver better user experience
This is Day 12 of the #100DaysOfPython challenge.
workingoutloud.dev
.hello-moviepy
directory and install Pillow.# Make the `hello-moviepy` directory
$ mkdir hello-moviepy
$ cd hello-moviepy
# We will write everything in main.py
$ touch main.py
# Add this stage, you will need to add you video clip - I added mine to the `hello-moviepy` directory
$ cp path/to/wol-dev.mp4 .
# Init the virtual environment
$ pipenv --three
$ pipenv install moviepy
workingoutloud.dev
site. It is around 9 seconds long and I want to add some text to it.main.py
, add the following:from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
video = VideoFileClip("wol-dev.mp4")
VideoFileClip
object from the video file (relative to the project root directory).TextClip
object for each of the three points in the video.TextClip
constructor can take some relevant arguments around the text, font, color and font size.TextClip
instance itself then has access to methods to set the position, duration and start time. We will abstract our sensible defaults into a function and implement this:def text_clip(text: str, duration: int, start_time: int = 0):
"""Return a description string on the bottom-left of the video
Args:
text (str): Text to show
duration (int): Duration of clip
start_time (int, optional): Time in video to start at (in seconds). Defaults to 0.
Returns:
moviepy.editor.TextClip: A instance of a TextClip
"""
return (TextClip(text, font="Arial", fontsize=24, color='black')
.set_position((20, video.h - 44))
.set_duration(duration)
.set_start(start_time))
# Make the text. Many more options are available.
text_clip_one = text_clip("Create tasks and subtasks", 3)
text_clip_two = text_clip("Sort subtasks by table header", 3, 3)
text_clip_three = text_clip("View goals", 3, 6)
TextClip
instances for each of our different annotations that we wish to add to the video.CompositeVideoClip
object and write out the video file.I am also writing out a .gif
file to use as a preview of the video on this post.
# Overlay text on video
result = CompositeVideoClip(
[video, text_clip_one, text_clip_two, text_clip_three])
result.write_videofile("wol_dev_edited.mp4", fps=25)
result.write_gif("wol_dev_edited.gif", fps=8)
python main.py
. There will be some progress bars to indicate the progress.wol_dev_edited.mp4
and wol_dev_edited.gif
in the hello-moviepy
directory.MoviePy
package to add some text captions to a video programmatically.from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
video = VideoFileClip("wol-dev.mp4")
def text_clip(text: str, duration: int, start_time: int = 0):
"""Return a description string on the bottom-left of the video
Args:
text (str): Text to show
duration (int): Duration of clip
start_time (int, optional): Time in video to start at (in seconds). Defaults to 0.
Returns:
moviepy.editor.TextClip: A instance of a TextClip
"""
return (TextClip(text, font="Arial", fontsize=24, color='black')
.set_position((20, video.h - 44))
.set_duration(duration)
.set_start(start_time))
# Make the text. Many more options are available.
text_clip_one = text_clip("Create tasks and subtasks", 3)
text_clip_two = text_clip("Sort subtasks by table header", 3, 3)
text_clip_three = text_clip("View goals", 3, 6)
# Overlay text on video
result = CompositeVideoClip(
[video, text_clip_one, text_clip_two, text_clip_three])
result.write_videofile("wol_dev_edited.mp4", fps=25)
result.write_gif("wol_dev_edited.gif", fps=8)
element5digital