Techno Blender
Digitally Yours.

Creating a Slow Motion Video Using OpenCV – Python

0 23


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

cap = cv2.VideoCapture("TestVid.mp4")

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

while True:

    

    ret, frame = cap.read()  

    

    cv2.imshow("frame", frame)  

  

    

    

    output.write(frame)

  

    

    

    k = cv2.waitKey(24)

  

    

    

    

    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

cap.release()

output.release()

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

 


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. 

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

cap = cv2.VideoCapture("TestVid.mp4")

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

while True:

    

    ret, frame = cap.read()  

    

    cv2.imshow("frame", frame)  

  

    

    

    output.write(frame)

  

    

    

    k = cv2.waitKey(24)

  

    

    

    

    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

cap.release()

output.release()

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

 

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment