Moviepy: example script with subtitles

Created:

In our script we add subtitles from the file subtitles.srt using Arial font with white color. Then, with VideoFileClip("video-to-add-subtitles.mp4") we open the video for adding the subtitles. When the video is already loaded into the clip variables, we then use CompositeVideoClip for adding the subtitles at the center position. Finally we use write_videofile for saving the final video.

from moviepy.editor import *
from moviepy.video.tools.subtitles import SubtitlesClip

generator = lambda txt: TextClip(txt, font='Arial', fontsize=16, color='white')
subtitles = SubtitlesClip("subtitles.srt", generator)

video = VideoFileClip("video-to-add-subtitles.mp4")
result = CompositeVideoClip([video, subtitles.set_pos(('center','bottom'))])

result.write_videofile("subtitles-test.mp4", fps=video.fps, temp_audiofile="tmp-audio.m4a", remove_temp=True, codec="libx264", audio_codec="aac")

Please leave comments if you have any problems with our solution!