[파이썬] moviepy VideoFileClip 기본 사용법

MoviePy is a Python library for video editing and manipulation. It provides an easy-to-use interface to perform various operations on video files, such as cutting, merging, resizing, and adding effects.

This blog post will focus on the basic usage of MoviePy’s VideoFileClip class, which allows us to work with video files. We will cover how to create a VideoFileClip object, access and modify video properties, and save the edited video to a file.

Installation

First, make sure you have MoviePy installed on your Python environment. You can install it using pip:

pip install moviepy

Creating a VideoFileClip object

To get started, import the VideoFileClip class from the moviepy.editor module:

from moviepy.editor import VideoFileClip

Now, let’s create a VideoFileClip object by passing the path to a video file as a parameter:

video = VideoFileClip("path/to/video.mp4")

Replace “path/to/video.mp4” with the actual path to your video file.

Accessing and modifying video properties

Once the VideoFileClip object is created, we can access various properties of the video, such as duration, size, and framerate.

duration = video.duration  # Get the duration of the video in seconds
size = video.size  # Get the size (width, height) of the video in pixels
fps = video.fps  # Get the frame rate of the video

print(f"Duration: {duration} seconds")
print(f"Size: {size[0]}x{size[1]} pixels")
print(f"Frame rate: {fps} fps")

We can also modify these properties if needed. For example, to resize the video to a specific width and height:

video_resized = video.resize((1280, 720))  # Resize the video to 1280x720 pixels

Saving the edited video

Finally, we can save the edited video to a file using the write_videofile method. Specify the output file path and format as parameters:

output_path = "path/to/output.mp4"
video_resized.write_videofile(output_path)

Replace “path/to/output.mp4” with the desired output file path.

Conclusion

MoviePy’s VideoFileClip class provides a convenient way to work with video files in Python. In this blog post, we covered the basic usage of creating a VideoFileClip object, accessing and modifying video properties, and saving the edited video to a file.

For more advanced video editing tasks, such as adding text, overlays, or effects, MoviePy offers additional features that can be explored further. Check out the MoviePy documentation and examples for more information.