In openCV, video capturing and playing video in reverse way is very easy. First we need to install openCV on your machine.
pip install opencv-python
sudo apt-get install python-opencv
OpenCv library can be used to perform multiple operations on videos. Let’s try to do something interesting using CV2
. Take a video as input and play it in a reverse mode by breaking the video into frame by frame and simultaneously store that frame in the list. After getting list of frames we perform iteration over the frames. For playing video in reverse mode, we need only to iterate reverse in the list of frames. Use reverse method of the list for reversing the order of frames in the list.
To play video in reverse direction, use following steps
Step 1: We import OpenCv library named cv2.
Step 2: Take a video as input data.
Step 3: First we break the video into a number of frames and store all these frames in a list.
Step 4: When we are getting all the frames then we shall apply iteration method.
Step 5: Here we apply iteration for reversing the list.
Step 6: Use the reverse() method for reversing the order of the frames in the list.
Detailed Steps
Import CV2 library
import cv2
Take a video as input data. videoCapture method of cv2 return video object and Pass absolute address of video file .
cap = cv2.VideoCapture("canny.mp4")
First we break the video into a number of frames and store all these frames in a list. Read method of video object will return a tuple with 1st element denotes whether the frame was read successfully or not, 2nd element is the actual frame.
So, Grab the current frame.
check , vid = cap.read()
Initialize a counter variable for counting frames.
counter = 0
Also, Initialize the value of check variable.
# Initialization
check = True
frame_list = []
If reached the end of the video then we got False value of check.
Keep looping untill we got False value of check.
#Write frames into the current folder
while(check == True):
# imwrite method of cv2 saves the
# image to the specified format.
cv2.imwrite("frame%d.jpg" %counter , vid)
check , vid = cap.read()
# Add each frame in the list by
# using append method of the List
frame_list.append(vid)
# increment the counter by 1
counter += 1
last value in the frame_list is None because when video reaches to the end then false value store in check variable and None value is store in vide variable.
Remove the last value from the frame_list by using pop method of List
frame_list.pop()
Looping in the List of frames.
# Save frames in current folder
for frame in frame_list:
# show the frame.
cv2.imshow("Frame" , frame)
# waitkey method to stoping the frame
# for some time. q key is presses,
# stop the loop
if cv2.waitKey(25) and 0xFF == ord("q"):
break
Release method of video object clean the input video.
cap.release()
Close any open windows.
cv2.destroyAllWindows()
Reverse the order of the element present in the list by using reverse method of the List.
frame_list.reverse()
for frame in frame_list:
cv2.imshow("Frame" , frame)
if cv2.waitKey(25) and 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
# import cv2 library
import cv2
cap = cv2.VideoCapture("canny.mp4")
check , vid = cap.read()
counter = 0
check = True
frame_list = []
while(check == True):
cv2.imwrite("frame%d.jpg" %counter , vid)
check , vid = cap.read()
frame_list.append(vid)
counter += 1
frame_list.pop()
for frame in frame_list:
cv2.imshow("Frame" , frame)
if cv2.waitKey(25) and 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
frame_list.reverse()
for frame in frame_list:
cv2.imshow("Frame" , frame)
if cv2.waitKey(25) and 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()