two modules
moviepy and
ffmeg can do it easily.
Here is a example script.
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
from moviepy.video.tools.drawing import color_gradient
def add_caption_to_video(video_path, caption_text, caption_duration=5, output_path="output_video.mp4"):
# Load the video
video_clip = VideoFileClip(video_path)
# Create a text clip with the caption
caption_clip = TextClip(caption_text,
fontsize=30,
color='white',
bg_color='black',
size=video_clip.size,
method='caption',
align='center')
# Set the duration of the caption clip
caption_clip = caption_clip.set_duration(caption_duration)
# Composite the caption clip with the original video
final_clip = CompositeVideoClip([video_clip, caption_clip.set_position(("center", "bottom"))])
# Write the final video to the specified output path
final_clip.write_videofile(output_path, codec='libx264', audio_codec='aac')
if __name__ == "__main__":
video_path = "path/to/your/video.mp4"
caption_text = "Your caption text here"
caption_duration = 5 # Duration of the caption in seconds
output_path = "output_video.mp4"
add_caption_to_video(video_path, caption_text, caption_duration, output_path)
This chatgpt code by the way. I think if you add a loop with the captions like show a new line every 5 second duration. Then i guess it will work.
If you want proper code let me know I can PM you that. [For free obviously]