Exploring MoviePy 2: A Modern Approach to Video Editing in Python

Simple example for showing changes in MoviePy version 2.
February 21, 2025 by
Exploring MoviePy 2: A Modern Approach to Video Editing in Python
Hamed Mohammadi
| No comments yet

MoviePy 2 is here, and it brings a cleaner, more consistent API along with exciting new features to simplify video editing tasks in Python. In this post, we’ll dive into two sample projects that showcase the new style and improvements in MoviePy 2. Whether you’re a seasoned developer or just starting out, these examples will help you get up to speed with the updated API.

GitHub Repository.

What’s New in MoviePy 2?

MoviePy 2 introduces several breaking changes compared to previous versions. Here are a few key updates:

  • Simplified Imports:
    Instead of importing from moviepy.editor, you now import directly from moviepy. This not only makes the code cleaner but also reduces unnecessary overhead.

  • Out-of-Place Modifications:
    Most methods that modify a clip now start with with_ (e.g., with_duration(), with_position()). This pattern emphasizes immutability by returning a modified copy of the clip rather than altering the original.

  • Renamed Methods:
    Methods like subclip have been renamed to subclipped for clarity. Similarly, the set_ methods are now prefixed with with_.

  • Effects API Overhaul:
    Effects are now applied using the .with_effects() method. For instance, fade-in and fade-out effects are implemented via vfx.FadeIn and vfx.FadeOut, making it easier to chain and combine effects.

  • Audio Adjustments Made Easy:
    Instead of importing specific audio effects, you can adjust the audio by simply multiplying the audio clip by a scalar value.

Let’s see how these improvements translate into practical code.

Example 1: Video Editing with Text Overlay and Audio Adjustment

In this example, we’ll load a video, extract a subclip, overlay some text, and reduce the audio volume. Notice the use of the new methods and direct imports from the base module.

from moviepy import VideoFileClip, TextClip, CompositeVideoClip

# Load the video file and select a subclip from 10 to 20 seconds
clip = VideoFileClip("example.mp4").subclipped(10, 20)

# Reduce the audio volume to 80% of the original by multiplying the audio clip
clip = clip.with_audio(clip.audio * 0.8)

# Generate a text clip with custom font, size, and color.
# Note: Ensure the font file (e.g., "Arial.ttf") is available on your system.
txt_clip = (
    TextClip(text="Hello there!", font_size=70, color="white", font="Arial.ttf")
    .with_duration(10)
    .with_position("center")
)

# Overlay the text clip on the video clip
final_video = CompositeVideoClip([clip, txt_clip])

# Write the result to a new video file
final_video.write_videofile("result.mp4")

What’s Happening Here?

  • Video Subclipping:
    The .subclipped(10, 20) method replaces the older subclip call, clearly indicating that a new clip is produced based on a segment of the original.

  • Audio Volume Control:
    By multiplying the audio track (clip.audio * 0.8), we reduce the volume without needing a separate effect.

  • Text Overlay:
    The TextClip is now created with keyword arguments like text, font_size, and font (using the full path to your TTF file). We then position it using .with_position("center") and set its duration with .with_duration(10).

  • Composite Video Creation:
    Finally, we merge the video and text using CompositeVideoClip and export the final video with write_videofile.

Example 2: Creating a Video from Images and JSON Data

This second example demonstrates how to create a video by concatenating multiple segments generated from a JSON file. Each segment consists of an image background with overlaid text that fades in and out.

import json
from moviepy import ImageClip, TextClip, CompositeVideoClip, concatenate_videoclips, vfx

# Read the JSON file
with open("data.json", "r") as f:
    data = json.load(f)

# Define video parameters
width, height = 1920, 1080  # Video resolution
duration_per_segment = 5    # Duration per image in seconds
fps = 24                    # Frames per second for the output video

# Initialize list for video segments
segments = []

# Process each entry in the JSON file
for item in data:
    image_path = item["image"]
    text_content = item["text"]

    # Load and resize image, set duration
    image = (
        ImageClip(image_path)
        .resized((width, height))
        .with_duration(duration_per_segment)
    )

    # Create text clip with fade-in and fade-out animations
    # Use the path to your font file (e.g., "Arial.ttf") instead of just "Arial"
    text = TextClip(text=text_content, font="Arial.ttf", font_size=70, color="white")
    text = text.with_position("center").with_duration(duration_per_segment)
    text = text.with_effects([vfx.FadeIn(1), vfx.FadeOut(1)])  # Apply fade in and out

    # Composite text over image to create a segment
    segment = CompositeVideoClip([image, text])
    segments.append(segment)

# Concatenate all segments into a single video
final_video = concatenate_videoclips(segments)

# Write the final video to a file
final_video.write_videofile("output.mp4", fps=fps)

Breaking Down the Process

  • JSON Data Input:
    The code reads from a data.json file, where each JSON entry contains the path to an image and corresponding text.

  • Image Processing:
    Each image is loaded via ImageClip, resized to the target resolution, and its display duration is set using .with_duration().

  • Text Creation and Effects:
    Text is overlaid on the image using TextClip with proper font parameters. The text clip is then animated with fade-in and fade-out effects via .with_effects(), using vfx.FadeIn and vfx.FadeOut.

  • Video Composition:
    Each image and text pair forms a segment (a CompositeVideoClip), and all segments are concatenated using concatenate_videoclips to form the final video.

Conclusion

MoviePy 2’s updated API emphasizes clarity, immutability, and a more structured approach to video effects. Whether you're creating simple text overlays or assembling complex video sequences from data, MoviePy 2 makes it easier to write clean and maintainable code.

These examples are just a starting point. Experiment with different effects, transitions, and compositions to fully leverage the power of MoviePy 2 in your projects!

Happy coding and video editing with MoviePy 2!

Exploring MoviePy 2: A Modern Approach to Video Editing in Python
Hamed Mohammadi February 21, 2025
Share this post
Tags
Archive

Please visit our blog at:

https://zehabsd.com/blog

A platform for Flash Stories:

https://readflashy.com

A platform for Persian Literature Lovers:

https://sarayesokhan.com

Sign in to leave a comment