MoviePy: Video Editing with Python
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
- breaking MoviePy's core functionality relies on FFmpeg. You MUST have FFmpeg installed on your system and accessible in your environment's PATH. Without it, most video loading and saving operations will fail with 'OSError: MoviePy Error: the file `ffmpeg` could not be found.'
- gotcha Processing large video files can be extremely memory-intensive and slow. MoviePy often loads frames into memory, leading to out-of-memory errors or very long processing times on longer or high-resolution videos.
- gotcha Audio and video synchronization issues can occur, especially with variable frame rate (VFR) source videos or after complex edits. Output files might have desynchronized audio.
- deprecated Several modules and functions were restructured or deprecated between MoviePy 1.0 and 2.x (e.g., `moviepy.video.tools.cuts`). While many core functionalities remain similar, older tutorials or code snippets might use deprecated paths or methods.
Install
-
pip install moviepy -
pip install moviepy[optional]
Imports
- VideoFileClip
from moviepy.editor import VideoFileClip
- AudioFileClip
from moviepy.editor import AudioFileClip
- concatenate_videoclips
from moviepy.editor import concatenate_videoclips
Quickstart
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()