In this content, we will learn how to create a slow motion video using openCV in python.
Introduction
Creating a slow motion video effect using OpenCV in python involves increasing the duration of each frame in the video. It reduces the frame rate of the video while maintaining the original playback time.
Creating the slow motion video using openCV in python
import ffmpeg from tkinter import* from tkinter.filedialog import askopenfile import tkinter.messagebox import cv2 content = "" name = "" def convert_slow(): flag1 = True file = askopenfile(mode = 'r',title = "Choose a video file") fileName = file.name cap = cv2.VideoCapture(fileName) if(cap.isOpened() == False): print("Error reading video file") frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) size = (frame_width, frame_height) result= cv2.VideoWriter(NAME.get()+'.avi',cv2.VideoWriter_fourcc(*MJPG'),5,size) while(cap.isOpened()): ret, frame = cap.read() if ret==True: result.write(frame) if flag1: tkinter.messagebox.showinfo("Task Completed.","Video Conversion Done.\n\n Your slow Motion Video") flag1 = False if cv2.waitKey(0)==27: break else: cap.release() result.release() cv2.destroyAllWindows()
Steps for creating a Slow Motion Video using OpenCV:
Step 1: Import all the required modules.
Step 2: askopenfile() function in python tkinter is only used to open a text file as python can read the context of the text file and print it out in a readable manner. Use the OpenCV to read the video file. You’ll also need to get the original frame rate and frame size of the video.
Step 3: Create the object of the VideoCapture and read the input file and capture the video. It accepts the name of the video file.
Step 4: Specify the source variable and create a VideoWriter object to write the new slow motion video. You should set lower frame rate for the output video to create slow motion effect.
Step 5: Create an infinite loop and read the VideoCapture object frame by frame.
Step 6: Releasing the VideoCapture and VideoWriter objects for releasing software.
The output video’s length will be the same as the input video, but each frame will be displayed for the longer period, creating the slow motion effect.