How to Play a Video in Slow Motion Using OpenCV in Python

Introduction

Have you ever wanted to play a video in slow motion using Python? Whether you are analyzing motion, working on video processing projects, or just experimenting with OpenCV, playing a video in slow motion can be useful.

In this blog, we will walk through how to play a video in slow motion using OpenCV, explain how frame rates work, and provide a step-by-step Python script to achieve smooth slow-motion playback.

 

Understanding Frame Rate and Slow Motion

Videos are essentially a sequence of images (frames) displayed rapidly to create motion. The frame rate (FPS – Frames Per Second) determines how many frames are shown per second.

For example:

  • 30 FPS means 30 frames are displayed per second (standard for most videos).
  • 60 FPS means 60 frames per second (smoother motion).

To play a video in slow motion, we need to reduce the playback speed by increasing the delay between frames.

For example:

  • A 30 FPS video played at half speed should display each frame for twice as long.
  • A 60 FPS video played at one-quarter speed should display each frame for four times as long.

Installing OpenCV

Before we start coding, ensure you have OpenCV installed. If not, install it using:

pip install opencv-python

Playing a Video in Slow Motion Using OpenCV

Step 1: Load the Video File

We use cv2.VideoCapture() to open the video file.

Step 2: Read Frames from the Video

We use a loop to read frames one by one.

Step 3: Adjust the Delay for Slow Motion

We introduce a delay using cv2.waitKey() to slow down playback.

import cv2  

# Load the video file
video_path = "video.mp4"  # Replace with your video file path
cap = cv2.VideoCapture(video_path)  

# Get the original FPS of the video
fps = int(cap.get(cv2.CAP_PROP_FPS))
print(f"Original FPS: {fps}")

# Set slow-motion factor (2x slower)
slow_factor = 2  
delay = int(1000 / fps) * slow_factor  # Increase delay for slow motion

while cap.isOpened():
    ret, frame = cap.read()  # Read a frame

    if not ret:
        break  # If no more frames, exit loop

    cv2.imshow("Slow Motion Video", frame)  # Show frame

    if cv2.waitKey(delay) & 0xFF == ord('q'):  # Press 'q' to exit
        break  

cap.release()  # Release the video
cv2.destroyAllWindows()  # Close all OpenCV windows

How Does This Code Work?

1️⃣ Load the Video

cap = cv2.VideoCapture(“video.mp4”)

This opens the video file for reading.

2️⃣ Get the Video’s FPS

fps = int(cap.get(cv2.CAP_PROP_FPS))

This extracts the frames per second (FPS) of the video.

3️⃣ Adjust Playback Speed

slow_factor = 2
delay = int(1000 / fps) * slow_factor

  • We increase the delay between frames to slow down playback.
  • 1000 / fps gives the frame delay in milliseconds.
  • Multiplying by slow_factor slows the video down.
4️⃣ Play Video Frame-by-Frame
while cap.isOpened():
ret, frame = cap.read()
  • This reads and displays each frame.
  • If there are no more frames, the loop exits.

5️⃣ Show Frames with Slow Motion

cv2.imshow(“Slow Motion Video”, frame)
cv2.waitKey(delay)
  • Each frame is displayed for a longer duration, creating the slow-motion effect.
  • Press ‘q’ to exit playback.

Customizing the Slow Motion Effect

1️⃣ Make the Video Slower or Faster

Change the slow_factor:

slow_factor = 3 # 3x slower

A higher value slows the video more.

2️⃣ Play Only a Specific Portion of the Video

Modify the loop to start and stop at specific timestamps:

start_time = 5  # Start from 5 seconds
end_time = 15  # Stop at 15 seconds

cap.set(cv2.CAP_PROP_POS_MSEC, start_time * 1000)  # Set start position

while cap.isOpened():
    current_time = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000  # Convert to seconds
    if current_time > end_time:
        break  # Stop at end_time

This will play only the portion between 5 and 15 seconds in slow motion.

Handling Common Errors

1️⃣ “Could not open video file”

If OpenCV fails to open your video file, check the path:

if not cap.isOpened():
print(“Error: Could not open video file.”)

Make sure the file exists and is in the correct format (MP4, AVI, etc.).

2️⃣ “Video is lagging too much”

If the video lags, try a lower slow_factor or use a smaller video resolution:

frame = cv2.resize(frame, (640, 480))
This reduces the frame size for smoother playback.

Final Thoughts

Playing a video in slow motion using OpenCV is simple and highly customizable. You can:
✔ Adjust slow-motion speed using slow_factor.
✔ Play only a specific portion of the video.
✔ Resize frames to optimize performance.

This technique can be used for sports analysis, motion detection, or just for fun! 🎥

🚀 Try it out with your own videos and experiment with different speeds!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top