MoviePy: Video Editing with Python

2.2.1 · active · verified Thu Apr 09

MoviePy is a powerful, open-source Python library for video editing, compositing, and processing. It enables programmatic video manipulation, making it ideal for automating tasks like cutting, concatenating, adding effects, or creating custom animations. The current version is 2.2.1, and it maintains an active, though irregular, release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a video, create text clips for an intro and outro, concatenate them with the original video, and export the result. It highlights using `VideoFileClip`, `TextClip`, and `concatenate_videoclips` from `moviepy.editor`. Remember to replace 'my_video.mp4' with an actual video file and ensure FFmpeg is correctly installed.

from moviepy.editor import VideoFileClip, concatenate_videoclips, TextClip, CompositeVideoClip

def create_intro_outro(video_path='my_video.mp4'):
    try:
        clip = VideoFileClip(video_path)

        intro_text = TextClip("My Awesome Video", fontsize=70, color='white', bg_color='black')\
                        .set_duration(2).set_position('center')
        outro_text = TextClip("Thanks for Watching!", fontsize=50, color='yellow', bg_color='black')\
                         .set_duration(1.5).set_position('center')

        # Ensure text clips match video resolution for smooth concatenation
        intro_text = intro_text.set_fps(clip.fps).resize(newsize=clip.size)
        outro_text = outro_text.set_fps(clip.fps).resize(newsize=clip.size)

        final_clip = concatenate_videoclips([intro_text, clip, outro_text], method="compose")
        output_filename = video_path.replace('.mp4', '_with_intro_outro.mp4')
        final_clip.write_videofile(output_filename, fps=clip.fps, threads=4)
        print(f"Video saved to {output_filename}")

        clip.close()
        intro_text.close()
        outro_text.close()
        final_clip.close()

    except Exception as e:
        print(f"An error occurred: {e}")
        print("Please ensure 'my_video.mp4' exists and FFmpeg is installed and in your PATH.")

# To run, ensure you have a video file named 'my_video.mp4' in the same directory
# create_intro_outro()

view raw JSON →