Heya! You know what, I love making slowmo videos. Who doesn’t love slow motion videos but unfortunately not every smartphone has this feature.

I know many of you wonder about making your own tool for creating slow motion videos. Don’t worry peeps! We got it covered for you. By the end of this tutorial, you would be able to create your own slow motion tool.

Overview

OpenCV has a simple video previewing method. It can modify the frames per second to be viewed, we are going to use it to make our own slow motion videos.

Importing libraries

We’ll be using opencv library cv2 for performing video processing.

import cv2

Loading input Video

We’ll be reading a video “output.avi”, which we want to convert into slow motion.

cap = cv2.VideoCapture('output.avi')

Processing the Video

Now, we will open a video writer named ‘out‘ and pass the output video name, fourcc ,fps and dimension of output video as arguments to it.

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('slow_motion.avi', fourcc, 5, (640,  480))

Here fourcc is writing method, fps is frames per second and we’ll be using 5 frames per second, and (640, 480) is thedimension of output video frame..

You can adjust the frames per second to slower (less than 5) or faster (more than 5) your video.

Saving the output

We’ll be using a while loop to save the slowmo video. The loop continues until it reaches the last video frame.

It returns the frame and ret variable, which denotes the output frame and its return status. If the return status is true, we write the frame into output varible ‘out‘.

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        print("video ended")
        break    
    out.write(frame)    
    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()

At the last, we have performed a bit cleanup by closing all the writers and open windows.

We have saved slow motion video file as ‘slow_motion.avi‘, you can change it to whatever you want while declaring the ‘out’ variable.

Implementation

Conclusion

In this blog, you have learned how to convert your video into slow motion in just few lines of code.

If you have enjoyed the article, feel free to share it and use the comments section for feedback.

-Suniti Jain


0 Comments

Leave a Reply

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

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert