Open In App

Python | Play a video in reverse mode using OpenCV

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

OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. 
 

OpenCV's application areas include :

1) Facial recognition system
2) motion tracking
3) Artificial neural network
4) Deep neural network
5) video streaming  etc.

 
In Python, one can use an OpenCV library named CV2. Python does not come with cv2, so user needs to install it separately. 
 

For Windows : 

pip install opencv-python

For Linux : 

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.
Below is the implementation : 
 

Python3




# Python program to play a video
# in reverse mode using opencv 
  
# import cv2 library
import cv2
  
# videoCapture method of cv2 return video object
  
# Pass absolute address of video file 
cap = cv2.VideoCapture("video_file_location")
  
# 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.
  
# Grab the current frame.
check , vid = cap.read()
  
# counter variable for
# counting frames
counter = 0
  
# Initialize the value
# of check variable
check = True
  
frame_list = []
  
# If reached the end of the video 
# then we got False value of check.
  
# keep looping until we
# got False value of check.
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.
  
# removing the last value from the 
# frame_list by using pop method of List
frame_list.pop()
  
# looping in the List of frames.
for frame in frame_list:
      
    # show the frame.
    cv2.imshow("Frame" , frame)
      
    # waitkey method to stopping 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()


Output : 
 

 



Previous Article
Next Article

Similar Reads

Python | Play a video using OpenCV
OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. Let’s see how to play a video using the OpenCV Python. To capture a video, we need to create a VideoCapture object. VideoCapture have the de
2 min read
OpenCV C++ Program to play a video
The following is the explanation to the C++ code to play a video in C++ using the tool OpenCV. Things to know: (1) The code will only compile in Linux environment. (2) To run in windows, please use the file: 'play_video.o' and run it in cmd. However if it does not run(problem in system architecture) then compile it in windows by making suitable and
3 min read
Python | Create video using multiple images using OpenCV
As we know OpenCV is a widely used library for image processing. It provides a wide sense of image processing. Let's see how to create video using multiple images using OpenCV. Install the following libraries: PIL cv2 Also, check the path before running the code otherwise you will be full with errors. How it works ? Using PIL library we are opening
3 min read
MoviePy – Turning on auto play for video clip file
In this article we will see how we can turn on the auto play of the video file clip in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF's. Video is formed by the frames, combination of frames creates a video each frame is an individual image. We can display a video with the help of ipyt
2 min read
OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
Content has been removed on Author's request.
1 min read
Automatic Document Scanner using OpenCV (OpenCV Document Scanner)
An automatic document scanner using OpenCV is a computer vision application that automatically detects and extracts documents from images. This type of scanner can be useful in various scenarios, such as digitizing paper documents, processing scanned documents, or automating document recognition tasks. In this article, we will see how we can build
6 min read
Converting Color video to grayscale using OpenCV in Python
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. It can process images and videos to identify objects, faces, or even the handwriting of a human. In this article, we will see how to convert a colored video to a gray-scale format. Approach: Import the cv2 module.Read the video file to be converted usi
1 min read
Python - Process images of a video using OpenCV
Processing a video means, performing operations on the video frame by frame. Frames are nothing but just the particular instance of the video in a single point of time. We may have multiple frames even in a single second. Frames can be treated as similar to an image.So, whatever operations we can perform on images can be performed on frames as well
4 min read
Python - Displaying real time FPS at which webcam/video file is processed using OpenCV
We will be displaying the real-time processing FPS of the video file or webcam depending upon our choice.FPS or frame per second or frame rate can be defined as number of frames displayed per second. A video can be assumed as a collection of images or we can say frames which are displayed at some rate to produce motion. if you wish to identify the
4 min read
Get video duration using Python - OpenCV
OpenCV is one of the most popular cross-platform libraries and it is widely used in Deep Learning, image processing, video capturing, and many more. In this article, we will learn how to get the duration of a given video using python and computer vision. Prerequisites: Opencv moduledatetime moduleInstallation Opencv can be downloaded by running the
1 min read
Article Tags :
Practice Tags :