How to make a green screen video

In video production, visual effects have become essential to storytelling, allowing filmmakers to transport their audiences to imaginative worlds and scenarios. One popular technique used in video production is creating green screen videos, which allow the merging of two separate layers of footage into one, giving endless creative possibilities. In this Answer, we will delve into making a green screen video using the powerful MoviePy library, exploring its applications, and providing a step-by-step guide to understanding and implementing the code.

What is MoviePy?

MoviePy is a Python library designed for video editing and manipulation tasks. It provides a vast set of tools for video creation, editing, and effects application, making it a versatile choice for both beginners and experienced editors. With MoviePy, you can automate various video editing tasks, from simple tasks like trimming and merging clips to more complex processes like applying visual effects and filters.

Applications of MoviePy

MoviePy finds its applications in many scenarios, making it a valuable tool for content creators, filmmakers, and anyone interested in video editing. Some notable applications include:

  • Video editing and trimming: MoviePy allows you to easily edit and trim video clips, making it an essential tool for refining your footage before creating the final output.

  • Visual effects: The library provides many visual effects and filters that can be applied to video clips. From color adjustments to artistic filters, MoviePy allows you to enhance the visuals of your videos.

  • Text and subtitles: You can add dynamic text and subtitles to your videos, enabling effective communication of information and narratives.

  • Transitions: Create smooth transitions between different video clips, enhancing the flow of your storytelling.

  • Green screen videos: MoviePy is great at handling green screen videos, enabling you to seamlessly merge different layers of footage.

Program overview

  • Import the necessary libraries.

  • Load the video clip and background image.

  • Get dimensions and resize the video clip.

  • Apply color mask effect.

  • Create the final composite video clip.

Libraries used

The libraries we use in this program are:

  • MoviePy

  • OpenCV

For moviepy:

pip install moviepy

MoviePy is a Python library for video editing, enabling tasks like cutting, concatenating, and applying effects.

For opencv:

pip install opencv-python

OpenCV is a computer vision library used for image and video processing tasks, such as object detection and manipulation.

Complete code

import moviepy.editor as mp
import cv2

# Load the video clip and background image
clip = mp.VideoFileClip('clip.mp4')
background = mp.ImageClip('background.jpg')

# Get the dimensions of the video clip
w, h = clip.size

# Resize the background image to match the dimensions of the video clip
background = background.resize(width=w, height=h)

# Apply the color mask effect to the video clip
masked_clip = clip.fx(mp.vfx.mask_color, color=[0, 255, 8], thr=180, s=5)

# Create the final composite video clip
final_clip = mp.CompositeVideoClip([
    background.set_duration(clip.duration),  # Set duration to match video clip
    masked_clip.set_duration(clip.duration)   # Set duration to match video clip
])

# Convert the composite video frames and display using OpenCV
for frame in final_clip.iter_frames(fps=final_clip.fps):
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)  # Convert from RGB to BGR
    cv2.imshow("Final Clip", frame)
    
    # Check for the 'q' key press to exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

Code explanation

Line 1–2: The code begins by importing the necessary libraries, moviepy.editor as mp for video editing and cv2 for OpenCV functions.

Line 5–6: The video clip and background image are loaded using the VideoFileClip and ImageClip functions.

Line 9: The dimensions of the video clip are obtained using the size attribute and assigned to w and h.

Line 12: The background image is resized to match the dimensions of the video clip using the resize function.

Line 15: The color mask effect is applied to the video clip using the fx method and the mask_color function from the mp.vfx module. This masks the green screen area, isolating it from the rest of the video.

Line 18–21: The final composite video clip is created by combining the background and masked video clips using the CompositeVideoClip function. The duration of each clip is set to match the duration of the main video clip.

Line 24–30: The composite video frames are iterated over and converted to OpenCV format (BGR). The frames are displayed in real-time using cv2.imshow, and the loop is interrupted when the 'q' key is pressed.

Line 32: The OpenCV windows are closed after the loop exits.

Let's take a small quiz for a better understanding of this Answer.

Assessment

Q

What is the purpose of applying a color mask effect in a green screen video?

A)

To adjust the color balance of the video

B)

To remove the green background from the video

C)

To add visual effects to the video

D)

To resize the video clip

Conclusion

Making green-screen videos has become an essential skill for modern video creators, enabling them to transport their audiences to imaginative settings and scenarios. The MoviePy library, with its tools and user-friendly interface, simplifies the process of creating green-screen videos.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved