简体   繁体   中英

OpenCV VideoCapture::get(CV_CAP_PROP_POS_MSEC) returns 0

I'm trying to timestamp the frames when recording video using OpenCV 3.1.0. However, when using VideoCapture::get(CV_CAP_PROP_POS_MSEC) to get the millisecond timestamp of the last frame grabbed the value returned is always 0.

The code I'm using:

int fps = 10;

VideoCapture cap(0); // open the default camera

cap.set(CV_CAP_PROP_FPS, fps);
cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 1024);

if(!cap.isOpened())  // check if we succeeded
    return -1;

Mat testFrame;
cap >> testFrame;
cap >> testFrame;
cap >> testFrame;

Size outSize = Size(testFrame.cols, testFrame.rows);

VideoWriter writer("video.avi", CV_FOURCC('M','J','P','G'), fps, outSize, true);

for(; ;)
{
    Mat frame;
    cap >> frame; // get a new frame from camera

    long stamp = cap.get( CV_CAP_PROP_POS_MSEC); // THIS DOESN'T SEEM TO WORK

    std::cout << "Timestamp: " << stamp << std::endl;

    writer.write(frame);
}

As output I always get many lines like the following:

Timestamp: 0

Could you help me understand what I'm doing wrong?

Thanks :)

This bug has been fixed in a recent pull request . From the link:

There is a bug in cap_v4l.cpp file the causes this problem. The value of the timestamp is copied from a buffer at line 868, but the the data in the buffer in not valid because we have already called VIDIOC_QBUF ioctl at line 865. The solution is to flip the two lines of the code, so we read the timestamp before calling VIDIOC_QBUF ioctl.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM