I N F O A R Y A N

Computer Vision Project | PYTHON-OPENCV Project | Reverse Video in 5 min | Code and Explanation

In the world of computer vision, OpenCV stands as a powerful library that provides a plethora of tools for image and video processing. One intriguing application is the reversal of video footage, a task that can be effortlessly accomplished with OpenCV. In this blog post, we’ll delve into the process of reversing a video using Python and OpenCV.

Flow of The Article:

  1. Key Concepts
  2. Explaining the essential steps to setup OpenCV
  3. Code Explanation for reversing the video project
  4. Conclusion
 

Key Concepts

  1. FourCC Code: The Four Character Code represents the video codec used for compression. In this example, we’ve used MJPG, which stands for Motion-JPEG.

  2. VideoWriter: This class in OpenCV provides a convenient way to write video files. It requires the output file name, FourCC code, frames per second, and frame size as parameters.

  3. Frame Resizing: Resizing frames is often performed to optimize processing speed or reduce file size. Here, we’ve resized frames to half of their original dimensions.

 

Setting the Stage

Firstly, let’s ensure we have the necessary tools at our disposal. Import the OpenCV library and create a VideoCapture instance to work with a sample video file (sample.mp4 in this case). Extracting essential properties of the video, such as frame count, frames per second (fps), height, and width, is crucial for subsequent processing.

# Import the library
import cv2

# Video Capture Instance
cap = cv2.VideoCapture(‘sample.mp4’)

# Properties of Video
frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
fps = cap.get(cv2.CAP_PROP_FPS)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)

 

Reversing the Video

Now, let’s initiate the output writer for the reversed video. This involves configuring the FourCC code and creating a VideoWriter object (reversed.avi). The frame-by-frame reversal process is then carried out, with the progress being printed at regular intervals.

# Initiating the Output writer for Video
fourcc = cv2.VideoWriter_fourcc(*’MJPG’)
out = cv2.VideoWriter(‘reversed.avi’, fourcc, fps, (int(width*0.5), int(height*0.5)))

print(“No. of frames are: {}”.format(frames))
print(“FPS is: {}”.format(fps))

# We get the index of the last frame of the video file
frame_index = frames – 1

# Checking if the video instance is ready
if(cap.isOpened()):
# Reading till the end of the video
while(frame_index != 0):
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_index)
ret, frame = cap.read()

# Resize the frame
frame = cv2.resize(frame, (int(width*0.5), int(height*0.5)))

# OPTIONAL: To show the reversing video
# cv2.imshow(‘winname’, frame)

# Writing the reversed video
out.write(frame)
frame_index = frame_index – 1

# Printing the progress
if(frame_index % 100 == 0):
print(frame_index)

out.release()
cap.release()
cv2.destroyAllWindows()

OpenCV Projects, Video Reverse using Opencv, OpenCV Python, Python OpenCV, Aryan Verma, Infoaryan, Infoaryan.com, Computer Vision Projects, OpenCV projects, OpenCV Python Projects, Image Processing Projects, Image Processing with openCV