Creating a Slow Motion Video Using OpenCV - Python
Last Updated : 28 Apr, 2025
In this article, we will try to create a slow-motion video using OpenCV( Open Source Computer Vision) library in Python. OpenCV ( is an open-source computer vision and machine learning software library.
Now a video is basically a set of moving pictures moving at a rate of about 24 frames per second. If this frame rate is decreased then we get a slow-motion video and that is what we are intended to do. To capture a video we need to create a VideoCapture object. Then we will create a VideoWriter object for writing the output slow-motion video.
Input Video:
Steps to Creating a Slow Motion Video Using OpenCV
Step 1: Import the required modules.
Python3
Step 2: Create the object of the VideoCapture and read the input file.
We need to create a VideoCapture object to capture a video. It accepts either the device index or the name of a video file. A number that is specified to the camera is called the device index. We can select the camera bypassing the O or 1 as an argument. Here we are providing a video file.
Python3 # this will return a video saved at the specified location cap = cv2.VideoCapture("TestVid.mp4")
Step 3: Specify the source variable and create a VideoWriter object.
We have provide the fps value to the VideoWriter object. This fps value determines the speed of the video. The general fps value for a live action video is 24. Now if we decrease this fps value we get a slow motion video. In this case, we have decreased the value to 5. You can play with the value and change speed of the video.
Setting the Lower fps in the video writer object for exporting a video with a lower playback speed. Here fps determines the framerate of the output video. It will be passed to VideoWriter Object. Note that the lower frame rate slower than the video.
Python3 # fps here determines the framerate of the output video. # it will be passed to VideoWriter Object # Note that lower the frame rate slower is the video. fps = 5.0
Setting the Higher fps in the video writer object for exporting a video with a higher playback speed. Here fps determines the framerate of the output video. It will be passed to VideoWriter Object. Here fps value is higher than 24 which fps value for a real-time video
Python3
Step 4: Create an infinite loop and read the VideoCapture object frame by frame.
Note: You must specify a key value using waitKey to break out of the loop.
Python3 while True: # reading the cap object for a single frame of video ret, frame = cap.read() # for displaying the frame read from cap object cv2.imshow("frame", frame) # using the VideoWriter object output for writing the # frame into the output video output.write(frame) # waitKey specifies the value in millisecond # for which a particular frame will be shown k = cv2.waitKey(24) # if 'q' is pressed then above while loop will # break and video reading and writing # process will stop if k == ord("q"): break
Step 5: Releasing the VideoCapture and VideoWriter objects. Releasing objects is necessary for releasing software as well as hardware resources.
Python3 # releasing the VideoCapture object cap cap.release() # releasing the VideoWriter object output output.release() # Destroying any open window cv2.destroyAllWindows()
Complete Code
Python3 import cv2 cap = cv2.VideoCapture("TestVid.mp4") height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) fps = cap.get(cv2.CAP_PROP_FPS) path = "SlowedVideo.mp4" fourcc = cv2.VideoWriter_fourcc(*'mp4v') output = cv2.VideoWriter(path, fourcc, 2, (width, height)) while True: ret, frame = cap.read() cv2.imshow("frame", frame) output.write(frame) k = cv2.waitKey(24) if k == ord("q"): break cap.release() output.release() cv2.destroyAllWindows()
Output:
Similar Reads
Get video duration using Python - OpenCV OpenCV is one of the most popular cross-platform libraries and it is widely used in Deep Learning, image processing, video capturing, and many more. In this article, we will learn how to get the duration of a given video using python and computer vision. Prerequisites: Opencv moduledatetime module
1 min read
Python | Create video using multiple images using OpenCV Creating videos from multiple images is a great way for creating time-lapse videos. In this tutorial, weâll explore how to create a video from multiple images using Python and OpenCV. Creating a video from images involves combining multiple image frames, each captured at a specific moment in time, i
5 min read
Python | Play a video in reverse mode using OpenCV OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV's application areas include : 1) Facial recognition system 2) motion tracking 3) Artificial neural network 4) Deep neural network 5) video streaming etc
3 min read
Display date and time in videos using OpenCV - Python OpenCV-Python is a library of Python bindings designed to solve computer vision problems. It can process images and videos to identify objects, faces, or even the handwriting of a humanNote: For more information, refer to Introduction to OpenCVÂ Display date and time in videos It sometimes becomes n
2 min read
Python - Process images of a video using OpenCV Processing a video means, performing operations on the video frame by frame. Frames are nothing but just the particular instance of the video in a single point of time. We may have multiple frames even in a single second. Frames can be treated as similar to an image.So, whatever operations we can pe
4 min read
How to change video resolution in OpenCV in Python In this article, we will describe how you can capture each frame from the video and also resize them according to your needs. What we will exactly do is take the video as input from the user and capture it frame by frame. Moreover, we run the video in the loop and save the image frames with certain
3 min read