Open In App

Creating a Slow Motion Video Using OpenCV – Python

Last Updated : 24 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Creating a Slow Motion Video Using OpenCV in Python

 

Steps to Creating a Slow Motion Video Using OpenCV

Step 1: Import the required modules. 

Python3




import cv2


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




fps = 40.0


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:

Creating a Slow Motion Video Using OpenCV in Python

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads