Creating a Slow Motion Video Using OpenCV – Python
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.
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
|
Step 3: Specify the source variable and create a VideoWriter object.
We have provied 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.
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
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
|
Step 5: Releasing the VideoCapture and VideoWriter objects. Releasing objects is necessary for releasing software as well as hardware resources.
Python3
|
Complete Code
Python3
|
Output:
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.
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
|
Step 3: Specify the source variable and create a VideoWriter object.
We have provied 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.
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
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
|
Step 5: Releasing the VideoCapture and VideoWriter objects. Releasing objects is necessary for releasing software as well as hardware resources.
Python3
|
Complete Code
Python3
|
Output: