Video Compression Gaming Project in C++

Explanation

  1. Include Libraries:
    • #include <opencv2/opencv.hpp>: Includes OpenCV for video processing.
    • #include <iostream>: Used for input and output operations.
  2. Open Video File:
    • VideoCapture cap("path/to/your/video.mp4");: Opens the video file for reading.
    • Checks if the file was opened successfully. If not, prints an error message and exits.
  3. Get Original Video Properties:
    • Retrieves the original width, height, and frames per second (fps) of the video.
  4. Define Compression Parameters:
    • Sets the compression factor to reduce the resolution. In this case, it’s set to 50%.
  5. Create Video Writer:
    • VideoWriter writer("path/to/your/compressed_video.mp4", VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, newSize);: Initializes the video writer to save the compressed video.
    • Checks if the writer was opened successfully. If not, prints an error message and exits.
  6. Processing Loop:
    • Captures frames from the video.
    • Resizes each frame according to the compression factor.
    • Writes the resized frame to the new video file.
    • Displays the compressed frame in a window.
    • waitKey(30); waits for 30 milliseconds for a key press. If ‘q’ is pressed, the loop exits.
  7. Cleanup:
    • cap.release();: Releases the video file resource.
    • writer.release();: Releases the video writer resource.
    • destroyAllWindows();: Closes all OpenCV windows.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top