Giới thiệu
Dùng VideoCapture::get(CAP_PROP_POS_MSEC) lấy vị trí hiện tại của video tính bằng milliseconds. Chia 1000 để ra giây.
Chương trình tính elapsed time
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
Mat myImage;
namedWindow("Video Player");
VideoCapture cap("video.mp4");
if (!cap.isOpened()) {
cout << "Không phát hiện luồng video" << endl;
system("pause"); return -1;
}
while (true) {
cap >> myImage;
int elapsed_time = cap.get(CAP_PROP_POS_MSEC);
cout << "Thời gian đã trôi qua (giây): " << elapsed_time / 1000 << endl;
if (myImage.empty()) break;
imshow("Video Player", myImage);
char c = (char)waitKey(25);
if (c == 27) break;
}
cap.release();
return 0;
}
Kết quả
Video phát real-time, console in liên tục thời gian đã phát (giây).