Created
December 11, 2022 13:24
-
-
Save shimpe/946589c7857eaa1bf8160bffe7d1293d to your computer and use it in GitHub Desktop.
Make caption with multiple colors in moviepy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import everything needed to edit video clips | |
from moviepy.editor import * | |
from vectortween.PointAnimation import PointAnimation | |
fps = 25 | |
duration = 10 | |
W = 2000 | |
H = 500 | |
# Generate a text clip | |
txt_clip = TextClip( | |
'Fun <b>with</b> <span foreground="#FF4567" background="black" size="x-large"><tt><i>captions</i></tt></span>!', | |
font="DejaVu Mono Sans", fontsize=75, color='white', method="pango", size=(W, H)) | |
# setting position of text in the center and duration will be 10 seconds | |
end_x_offset = 0 | |
end_y_offset = H / 2 | |
na_pt = PointAnimation((0, 0), (end_x_offset, end_y_offset), tween=['easeOutBounce'], ytween=['easeOutBounce']) | |
def position(t): | |
df = duration * fps | |
tf = t * fps | |
return na_pt.make_frame(tf, 0, 0, df / 4, df, None) | |
final_txt_clip = txt_clip.set_position(position).set_duration(duration) | |
# Overlay the text clip on the first video clip | |
video = CompositeVideoClip([final_txt_clip]) | |
# showing video | |
video.write_videofile("test.mp4", fps=fps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment