+

Python | Playing video in reverse using OpenCV

# Python video playback program
# in reverse using opencv

 
# cv2 library import

import cv2

 
# videoCapture cv2 method return video object

  
# Pass the absolute address of the video file

cap = cv2.VideoCapture ( "video_file_location" )

< code class = "undefined spaces">  
# method to read the video object will return
# tuple with 1 -th element means
# the frame was read successfully or not,
# 2nd element - this is the actual frame.

 
# Capture the current frame.

check, vid = cap.read ()

 
# counter variable for
# frame count

counter = 0

 
# Initialize value
# check variable

check = True

  

frame_list = [ ]

 
# If the end of the video is reached
# then we got a False validation value.

 
# keep looping while we
# got a false validation value.

while (check = = True ):

  

# imwrite cv2 method saves

  # image in the specified format.

  cv2.imwrite ( "frame% d.jpg" % counter, vid)

  check, vid = cap.read ()

  

# Add every frame in the list

# using the add list method

frame_list. append (vid)

 

# increment counter by 1

counter + = 1

 
# last value in frame_list is None
# because when the video reaches the end
# then the false value is stored in the control variable
# and None is stored in the vide variable.

  
# deleting last value from
# frame_list using the pop List method
frame_list.pop ()

 
# loop in the frame list.

for frame in frame_list:

 

# show frame.

cv2.imshow ( "Frame" , frame)

 

# waitkey method to stop the frame

# for a while. the q key is pressing,

# stop loop

if cv2.waitKey ( 25 ) and 0xFF = = ord ( "q" ):

break

 
# video release method
# object to clear input video
cap.release ()

 
# close all open windows
cv2.destroyAl lWindows ()

 
# reverse element order
# listed with
# reverse list method.
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:

Get Solution for free from DataCamp guru