Created
November 10, 2023 04:55
-
-
Save boulama/6f2632c870fc8300927a07f97dbf7ba0 to your computer and use it in GitHub Desktop.
change video framerate in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/opencv.hpp> | |
int main() { | |
std::string inputVideoPath = "in_vid.mp4"; // Input video file | |
std::string outputVideoPath = "out_vid.mp4"; // Output video file with the desired frame rate | |
// Open the input video file | |
cv::VideoCapture inputVideo(inputVideoPath); | |
if (!inputVideo.isOpened()) { | |
// Check if the input video opened successfully | |
std::cerr << "Error: Could not open the input video file." << std::endl; | |
return -1; | |
} | |
// input vid props | |
double fpsInput = inputVideo.get(cv::CAP_PROP_FPS); | |
int width = static_cast<int>(inputVideo.get(cv::CAP_PROP_FRAME_WIDTH)); | |
int height = static_cast<int>(inputVideo.get(cv::CAP_PROP_FRAME_HEIGHT)); | |
double fpsOutput = 24.0; | |
// VideoWriter object for the output video | |
cv::VideoWriter outputVideo(outputVideoPath, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), fpsOutput, cv::Size(width, height)); | |
// Check if the output video opened successfully | |
if (!outputVideo.isOpened()) { | |
std::cerr << "Error: Could'nt open the output video file for writing." << std::endl; | |
return -1; | |
} | |
// Convert the frame rate by dropping / dupes frames | |
while (true) { | |
cv::Mat frame; | |
inputVideo >> frame; | |
// Break the loop if the video is over | |
if (frame.empty()) { | |
break; | |
} | |
// Write frame to the output video | |
outputVideo << frame; | |
// delay to achieve wanted output frame rate | |
int delay = static_cast<int>(1000.0 / fpsOutput); | |
cv::waitKey(delay); | |
} | |
inputVideo.release(); | |
outputVideo.release(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment