[파이썬] moviepy 동영상에 스티커 추가하기

Python provides a powerful library called Moviepy that allows us to manipulate video files. One interesting feature of Moviepy is the ability to add stickers or overlays to videos. In this tutorial, we will learn how to add stickers to a video using Moviepy in Python.

Prerequisites

Before we get started, make sure you have Moviepy installed. You can install it using the following command:

pip install moviepy

Adding a Sticker to a Video

To add a sticker to a video, we need to perform the following steps:

  1. Load the video using Moviepy.
  2. Load the sticker image using Moviepy.
  3. Resize the sticker image to fit the video frame.
  4. Define the position and duration of the sticker in the video.
  5. Composite the sticker image onto the video frame.
  6. Write the modified video to a new file.

Let’s see how each of these steps can be implemented in Python:

import moviepy.editor as mp

# Step 1: Load the video
video = mp.VideoFileClip("video.mp4")

# Step 2: Load the sticker image
sticker = mp.ImageClip("sticker.png")

# Step 3: Resize the sticker image
sticker = sticker.resize(height=100)

# Step 4: Define position and duration of the sticker
position = (50, 50)  # Top-left corner coordinates
duration = video.duration  # Sticker duration (same as video duration)

# Step 5: Composite the sticker onto the video frame
video = mp.composite(video, sticker.set_position(position).set_duration(duration))

# Step 6: Write the modified video to a new file
video.write_videofile("video_with_sticker.mp4")

Make sure to replace “video.mp4” with the path to your video file and “sticker.png” with the path to your sticker image.

Conclusion

In this tutorial, we learned how to add stickers to a video using Moviepy in Python. We covered the steps of loading the video and sticker, resizing the sticker, defining the position and duration of the sticker, compositing the sticker onto the video frame, and writing the modified video to a new file. Experiment with different stickers and positions to create unique and fun videos with stickers.

Remember to explore the Moviepy documentation to discover more advanced features and techniques for video manipulation.