1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main() { // Open video file VideoCapture cap("path/to/your/video.mp4"); if (!cap.isOpened()) { cerr << "Error: Could not open video file!" << endl; return -1; } // Get original video properties int originalWidth = static_cast<int>(cap.get(CAP_PROP_FRAME_WIDTH)); int originalHeight = static_cast<int>(cap.get(CAP_PROP_FRAME_HEIGHT)); int fps = static_cast<int>(cap.get(CAP_PROP_FPS)); // Define compression parameters double compressionFactor = 0.5; // Reduce resolution to 50% Size newSize(static_cast<int>(originalWidth * compressionFactor), static_cast<int>(originalHeight * compressionFactor)); // Create video writer for compressed video VideoWriter writer("path/to/your/compressed_video.mp4", VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, newSize); if (!writer.isOpened()) { cerr << "Error: Could not open video writer!" << endl; return -1; } while (true) { Mat frame; cap >> frame; // Capture a new frame from the video if (frame.empty()) { break; // Exit loop if there are no more frames } Mat compressedFrame; resize(frame, compressedFrame, newSize); // Resize frame for compression writer.write(compressedFrame); // Write the compressed frame to the new video imshow("Compressed Video", compressedFrame); // Display the compressed frame int keyPressed = waitKey(30); // Wait for 30 ms for a key press if (keyPressed == 'q') { break; // Exit if 'q' is pressed } } cap.release(); // Release video file writer.release(); // Release video writer destroyAllWindows(); // Close all OpenCV windows return 0; } |
Explanation
- Include Libraries:
#include <opencv2/opencv.hpp>
: Includes OpenCV for video processing.#include <iostream>
: Used for input and output operations.
- 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.
- Get Original Video Properties:
- Retrieves the original width, height, and frames per second (fps) of the video.
- Define Compression Parameters:
- Sets the compression factor to reduce the resolution. In this case, it’s set to 50%.
- 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.
- 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.
- Cleanup:
cap.release();
: Releases the video file resource.writer.release();
: Releases the video writer resource.destroyAllWindows();
: Closes all OpenCV windows.