[파이썬] moviepy CompositeVideoClip 사용법

Moviepy is a Python library that allows you to manipulate and create videos. One of its powerful features is the ability to compose multiple video clips together using the CompositeVideoClip class. This allows you to overlay videos, add texts or graphics, and create interesting visual effects. In this blog post, we will explore the usage of CompositeVideoClip and how to create composite videos in Python.

Installation

Before we get started, make sure you have Moviepy installed on your system. You can install it using pip:

pip install moviepy

Creating a CompositeVideoClip

To create a composite video, we need at least two video clips. Let’s start by importing the necessary modules and creating two video clips from existing video files:

from moviepy.editor import VideoFileClip, CompositeVideoClip

clip1 = VideoFileClip("video1.mp4")
clip2 = VideoFileClip("video2.mp4")

Once we have our video clips, we can create a CompositeVideoClip by passing a list of clips to its constructor:

composite_clip = CompositeVideoClip([clip1, clip2])

Overlaying Clips

Now that we have our composite clip, we can overlay one clip onto another to create interesting effects. The CompositeVideoClip provides various methods to control the positioning and duration of the clips. Here’s an example of overlaying clip2 onto clip1:

composite_clip = clip1.set_duration(10).overlay(clip2.set_duration(10).set_position(("right", "bottom")), position=(0, 0))

In this example, we set the duration of both clips to 10 seconds using set_duration method. We then overlay clip2 onto clip1 using the overlay method, specifying the position of clip2 as "right", "bottom" relative to clip1 using set_position method. The position argument in overlay method specifies the position of clip1 on the final composite clip.

Adding Text and Graphics

Apart from overlaying clips, you can also add texts, images, or graphics to a composite video. Moviepy provides various methods to add these elements. Here’s an example of adding a text clip to the composite video:

text_clip = moviepy.editor.TextClip("Hello World", fontsize=50, color='white')
composite_clip = composite_clip.set_duration(5).overlay(text_clip.set_duration(5).set_position(("center", "top")), position=(0, 0))

In this example, we create a TextClip with the text “Hello World”, specifying its fontsize and color. We then overlay this text clip onto our composite clip and set its position to be centered at the top.

Exporting the Composite Video

Once you have created your composite video, you can export it to a new video file using the write_videofile method. Here’s an example:

composite_clip.write_videofile("output.mp4", codec="libx264", fps=30)

In this example, we export the composite clip as a new video file named “output.mp4”. We specify the codec to be used as “libx264” and set the frames per second (fps) to 30.

Conclusion

Moviepy’s CompositeVideoClip class provides a convenient way to create composite videos in Python. It allows you to overlay multiple clips, add texts, graphics, and create impressive videos. By following the examples and methods discussed in this blog post, you can easily create your own composite videos using Moviepy.

Happy video editing!