Moviepy is a Python library that allows us to work with video files, including various operations such as editing, concatenation, and manipulation. One common task in video processing is adjusting the frames per second (FPS) of a video file. In this blog post, we will explore how to change the FPS of a video using Moviepy in Python.
Installation
Before we begin, we need to install Moviepy. Open your terminal and run the following command:
pip install moviepy
Changing the FPS of a Video File
Let’s start by importing the necessary libraries and loading the video file:
import moviepy.editor as mp
video = mp.VideoFileClip('path/to/video.mp4')
Next, we can change the FPS of the video using the set_fps
method:
new_fps = 30 # desired FPS
video = video.set_fps(new_fps)
We can also change the speed of the video by adjusting the duration of each frame. For example, to double the speed of the video, we can set the FPS to twice the original FPS:
new_fps = video.fps * 2
video = video.set_fps(new_fps)
After changing the FPS, we can save the modified video to a new file:
output_path = 'path/to/output.mp4'
video.write_videofile(output_path)
Complete Example
Here’s a complete example that combines all the steps:
import moviepy.editor as mp
def change_fps(input_path, output_path, new_fps):
# Load the video
video = mp.VideoFileClip(input_path)
# Set the new FPS
video = video.set_fps(new_fps)
# Save the modified video
video.write_videofile(output_path)
# Usage
input_path = 'path/to/video.mp4'
output_path = 'path/to/output.mp4'
new_fps = 30
change_fps(input_path, output_path, new_fps)
Make sure to replace 'path/to/video.mp4'
with the actual path to your input video file, and 'path/to/output.mp4'
with the desired output path.
By changing the FPS of a video using Moviepy in Python, we can create slow-motion or fast-motion effects, or simply adjust the playback speed of a video to our liking. Moviepy provides a convenient and straightforward way to perform such operations with minimal code.
I hope you found this blog post helpful in understanding how to adjust the FPS of a video using Moviepy in Python. Happy video processing!