简体   繁体   English

视频时长(OpenCV)

[英]Duration of a video (OpenCV)

I want to get the fps of a video but I can't with the property CV_CAP_PROP_FPS. 我想获取视频的fps,但无法使用属性CV_CAP_PROP_FPS。 I had thought that having the number of frames of the video and the duration I could get it. 我以为拥有视频的帧数和持续时间可以得到它。 The problem is that I don't find some method that give the duration. 问题是我找不到给出持续时间的方法。 Anyone can help me? 有人可以帮助我吗?

Thanks! 谢谢!

First of all, grab at least one frame from camera/video before trying to get/set some property. 首先,在尝试获取/设置某些属性之前,请从摄像机/视频中至少获取一帧。 Sometimes it may work fine without doing it, but better just do it. 有时不做就可以正常工作,但最好还是这样做。
If it won't solve your problem you may try to use different properties: 如果它不能解决您的问题,则可以尝试使用其他属性:

cv::VideoCapture camera("some_movie.avi");
cv::Mat img;
camera >> img;
std::cout << camera.get(CV_CAP_PROP_FPS) << std::endl;
std::cout << camera.get(CV_CAP_PROP_FRAME_COUNT) << std::endl;
std::cout << camera.get(CV_CAP_PROP_POS_AVI_RATIO) << std::endl;
std::cout << camera.get(CV_CAP_PROP_POS_FRAMES) << std::endl;
std::cout << camera.get(CV_CAP_PROP_POS_MSEC) << std::endl;
std::cout << "setting pos avi ratio to 1" << std::endl;
std::cout << camera.set(CV_CAP_PROP_POS_AVI_RATIO, 1.0) << std::endl;
std::cout << camera.get(CV_CAP_PROP_POS_AVI_RATIO) << std::endl; //i think it's not working - returns bad position(same as the first call of camera.get(CV_CAP_PROP_POS_AVI_RATIO)
std::cout << camera.get(CV_CAP_PROP_POS_FRAMES) << std::endl;
std::cout << camera.get(CV_CAP_PROP_POS_MSEC) << std::endl;

The results: 结果:

30 三十
1739 1739
0.0333333 0.0333333
1 1
33.3333 33.3333
setting pos avi ratio to 1 将pos avi比率设置为1
1 1
0.0333333 0.0333333
1739 1739
57966.7 57966.7

Using total time (time of last frame) and number of frames you can calculate fps on your own. 使用总时间(最后一帧的时间)和帧数,您可以自己计算fps。 Alternatively you can use position of first frame - just divide 1.0 by position of first frame and you will get fps. 另外,您也可以使用第一帧的位置-将1.0除以第一帧的位置即可获得fps。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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