Play a video in reverse mode using OpenCV-Python
Playing a video in reverse is useful for analyzing motion backwards, creating special effects or debugging frame sequences. With OpenCV in Python, frames can be read, stored and displayed in reverse order, enabling offline analysis, reverse playback and creative motion effects.
Objective
We will take a video as input and play it in reverse by:
- Breaking it into frames.
- Storing the frames in a list.
- Reversing the list using Pythonâs reverse() method.
- Displaying the frames sequentially to simulate reverse playback.
Python Implementation
import cv2
cap = cv2.VideoCapture("video_file_location")
frames, cnt = [], 0
# Capture frames
while True:
ret, frame = cap.read()
if not ret: break
cv2.imwrite(f"frame{cnt}.jpg", frame)
frames.append(frame)
cnt += 1
cap.release()
# Play forward
for f in frames:
cv2.imshow("Frame", f)
if cv2.waitKey(25) & 0xFF == ord("q"): break
# Play reverse
for f in reversed(frames):
cv2.imshow("Frame", f)
if cv2.waitKey(25) & 0xFF == ord("q"): break
cv2.destroyAllWindows()
Output:
