[파이썬] moviepy Interactive 동영상 만들기

Moviepy is a powerful Python library that allows you to create and edit videos. It provides a wide range of functionalities to manipulate videos, including adding text, images, audio, transitions, and effects. In this tutorial, we will learn how to create an interactive video using Moviepy.

Installation

Before we begin, make sure you have Moviepy installed in your Python environment. You can install it using pip:

pip install moviepy

Getting Started

To get started, let’s first import the necessary modules from Moviepy:

from moviepy.editor import *

Creating the Video Clip

Now, we will create a simple video clip by adding text and an image. Here’s an example:

# Create a video clip with a static background image
background_image = ImageClip("background.jpg").set_duration(10)

# Add a text message to the video clip
text = TextClip("Hello, Moviepy!", fontsize=70, color='white')
text = text.set_position('center').set_duration(5)

# Combine the background image and text
video_clip = CompositeVideoClip([background_image, text])

# Set the duration of the video clip
video_clip = video_clip.set_duration(10)

# Preview the video in a window
video_clip.preview()

In the above code, we created a video clip with a static background image using the ImageClip class. We then added a text message to the video clip using the TextClip class. Finally, we combined the background image and text using the CompositeVideoClip class.

Adding Interactivity

To make our video interactive, we can add event-based actions such as mouse clicks or key presses. Moviepy provides the on_click and onkeypress decorators to add interactivity to video clips.

Here’s an example of adding interactive actions to our video clip:

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip

# Define an action to perform when the video is clicked
@video_clip.on_click
def click_action(event, x, y, button):
    print(f"Video was clicked at ({x}, {y}) with button {button}")

# Define an action to perform when a key is pressed
@video_clip.onkeypress('space')
def space_action(event, key):
    print("Space key was pressed")

# Preview the video with added interactivity
video_clip.preview()

In the above code, we defined two actions: click_action and space_action. The click_action is triggered when the video is clicked, providing the position of the click and the button that was pressed. The space_action is triggered when the space key is pressed.

Exporting the Video

Once you are satisfied with the video and the interactive actions, you can export it to a video file using the write_videofile method. Here’s an example:

# Export the video clip to a file
video_clip.write_videofile("output.mp4", codec='libx264', fps=30)

In the above code, we exported the video clip to a file named “output.mp4” by specifying the codec and frames per second.

Conclusion

In this tutorial, we learned how to create an interactive video using Moviepy in Python. We covered how to create a video clip with text and images, add interactivity using event-based actions, and export the video to a file. Moviepy provides a wide range of features to create dynamic and engaging videos, and with a little creativity, you can create stunning interactive videos.