Python | Play a video in reverse mode using OpenCV

Last Updated : 04 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 d
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
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
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
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 - 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
How to draw Filled rectangle to every frame of video by using Python-OpenCV?
In this article, we will discuss how to draw a filled rectangle on every frame of video through OpenCV in Python. Stepwise Implementation:Import the required libraries into the working space.Read the video on which you have to write. Syntax: cap = cv2.VideoCapture("path") Create an output file using cv2.VideoWriter_fourcc() method. Here you will ha
2 min read
Click response on video output using Events in OpenCV – Python
OpenCV 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. In this article, we will create a program with a click response on video output using events in OpenCV Python library. We will be using “cv2.EVENT_LBUTTONDOWN” in case wh
3 min read
Creating a Slow Motion Video Using OpenCV - 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 slo
4 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
Transition from OpenCV 2 to OpenCV 3.x
OpenCV is one of the most popular and most used Computer vision libraries. It contains tools to carry out image and video processing. When OpenCV 3..4.1 is an improved version of OpenCV 2.4 as it introduced new algorithms and features. Although some of the existing modules were rewritten and moved to sub-modules. In this articles, I will focus on t
2 min read
Top Books for Learning OpenCV: Computer Vision with OpenCV Library
OpenCV or Open Source Computer Vision Library, is an open-source computer vision and machine learning software library. It's extensively used for real-time computer vision tasks such as object detection, face recognition, image processing, etc. Whether you're a beginner or an experienced developer looking to deepen your understanding of OpenCV, her
5 min read
Saving a Video using OpenCV
OpenCV is an open-source and most popular computer vision library that contains several computer vision algorithms. You can read, display, write and doing lots of other operations on images and videos using OpenCV. The OpenCV module generally used in popular programming languages like C++, Python, Java. To save a video in OpenCV cv.VideoWriter() me
2 min read
Saving Operated Video from a webcam using OpenCV
OpenCV is a vast library that helps in providing various functions for image and video operations. With OpenCV, we can perform operations on the input video. OpenCV also allows us to save that operated video for further usage. For saving images, we use cv2.imwrite() which saves the image to a specified file location. But, for saving a recorded vide
4 min read
Python OpenCv: Write text on video
OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. cv2.putText() method inserts a text on the
2 min read
Python - Writing to video with OpenCV
In this article, we will discuss how to write to a video using OpenCV in Python. ApproachImport the required libraries into the working space.Read the video on which you have to write. Syntax: cap = cv2.VideoCapture("path")Create a output file using cv2.VideoWriter_fourcc() method Syntax: output = cv2.VideoWriter("path",cv2.VideoWriter_fourcc(*'MPE
1 min read
Save frames of live video with timestamps - Python OpenCV
Images are very important data nowadays from which we can obtain a lot of information which will helpful for analysis. Sometimes data to be processed can be in the form of video. To process this kind of data we have to read video extract frames and save them as images. Security camera videos we have seen that there is a timestamp and date at top of
3 min read
How to change video resolution in OpenCV in Python
In this article, we will describe how you can capture each frame from the video and also resize them according to your needs. What we will exactly do is take the video as input from the user and capture it frame by frame. Moreover, we run the video in the loop and save the image frames with certain names after resizing those frames. Stepwise Implem
3 min read
Python OpenCV: Capture Video from Camera
Python provides various libraries for image and video processing. One of them is OpenCV. OpenCV is a vast library that helps in providing various functions for image and video operations. With OpenCV, we can capture a video from the camera. It lets you create a video capture object which is helpful to capture videos through webcam and then you may
4 min read
OpenCV | Loading Video
This article aims to learn how to display a video in OpenCV. To do so is a simple task just like displayed as a single image. But here, this display of frames have to be read in a loop as a part of the sequence. Lets's understand the complete process line by line in detail. Code : CvCapture* capture = cvCreateFileCapture("...input\\video_file.
3 min read
Faster video file FPS with cv2.VideoCapture and OpenCV
Sometimes we need to speed up the FPS(Frame Per Second) of a video file to perform certain operations on the video such as surveillance footage analysis, video editing, and video game recording. In this article, we will learn to increase the FPS of our input video and download the resultant output video with the help of cv2.VideoCapture in Python.
4 min read
Saving key event video clips with OpenCV
OpenCV is an open-source and popular computer vision library that contains several computer vision algorithms. You can read, display, write and do lots of other operations on images and videos using OpenCV. The OpenCV module is generally used in popular programming languages like C++, Python, and Java. To save a video in OpenCV cv.VideoWriter() met
5 min read
OpenCV C++ Program to blur a Video
The following is the explanation to the C++ code to blur 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: 'blur_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
Pafy - Getting Video ID of the given Video
In this article we will see how we can get video ID of the given youtube video in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. A Youtube Video ID is an unique ID to identify a video which is uploaded to Youtube. The Youtube video
2 min read
Pafy - Getting Best Video Stream of the given Video
In this article we will see how we can get best video stream of the given youtube video in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. Best video stream is basically a video stream which is available and in highest resolution. T
2 min read
10 Interesting modules in Python to play with
Python is a high level, interpreted and general-purpose dynamic programming language that focuses on code readability. It is used in many organizations as it supports multiple programming paradigms. It also performs automatic memory management. It is one of the world’s most popular, in-demand programming languages. This is for many reasons: It’s ea
7 min read
Python VLC MediaPlayer - Setting MRL to play
In this article we will see how we can set mrl the MediaPlayer object in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. MediaPlayer object is the basic object in vlc module for playing the video. We can create a MediaPlayer
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg