一、查看视频属性(帧率、视频总帧数、视频时长)
#include <iostream> #include <filesystem> #include <string> #include <windows.h> #include <opencv2/opencv.hpp> namespace fs = std::filesystem; using namespace cv; using namespace std; // 查看视频属性 void test_play_fps() { //帧率 fps:每秒播放的帧数 //视频总帧数 fcount //视频时长 fcount:视频总帧数 / 帧率 //VideoCapture video; //video.open("D:/opencv/console/x64/Debug/test.mp4"); VideoCapture video("F:/opencv/console/x64/Debug/test.mp4"); if (!video.isOpened()) { cout << "打开失败" << endl; } cout << "打开成功" << endl; //namedWindow("video"); Mat frame; int fps = video.get(CAP_PROP_FPS); int fcount = video.get(CAP_PROP_FRAME_COUNT); int s = 30; if (fps != 0) { s = 1000 / fps; } // s = s / 2; // 2倍速 cout << "帧率 " << fps << endl; // fps cout << "视频总帧数 " << fcount << endl; // frame_count cout << "视频时长 " << float(fcount) / float(fps) << " s" << endl; // time for (;;) { if (!video.read(frame)) { break; } imshow("video", frame); waitKey(s); } } int main() { // 查看视频属性 test_play_fps(); waitKey(0); destroyAllWindows(); return 0; }
二、视频循环播放
#include <iostream> #include <filesystem> #include <string> #include <windows.h> #include <opencv2/opencv.hpp> namespace fs = std::filesystem; using namespace cv; using namespace std; // 查看视频属性 void test_play_fps() { //帧率 fps:每秒播放的帧数 //视频总帧数 fcount //视频时长 fcount:视频总帧数 / 帧率 //VideoCapture video; //video.open("D:/opencv/console/x64/Debug/test.mp4"); VideoCapture video("F:/opencv/console/x64/Debug/test.mp4"); if (!video.isOpened()) { cout << "打开失败" << endl; } cout << "打开成功" << endl; //namedWindow("video"); Mat frame; int fps = video.get(CAP_PROP_FPS); int fcount = video.get(CAP_PROP_FRAME_COUNT); int s = 30; if (fps != 0) { s = 1000 / fps; } // s = s / 2; // 2倍速 cout << "帧率 " << fps << endl; // fps cout << "视频总帧数 " << fcount << endl; // frame_count cout << "视频时长 " << float(fcount) / float(fps) << " s" << endl; // time for (;;) { if (!video.read(frame)) { break; } int cur = video.get(CAP_PROP_POS_FRAMES); // 当前帧 // 当前帧索引大于(fcount - 1),则重新播放。 if (cur > (fcount - 1) ) { video.set(CAP_PROP_POS_FRAMES, 0); // 设置当前帧为0,循环播放。 continue; } imshow("video", frame); waitKey(s); } } int main() { // 查看视频属性 test_play_fps(); waitKey(0); destroyAllWindows(); return 0; }