OpenCV VideoWriter帧率问题 [英] OpenCV VideoWriter Framerate Issue
问题描述
我正在尝试将1080p网络摄像头中的视频录制到文件中.我在视频中设置了计时器,在每次试用中,视频播放器报告的时间戳(我使用的是VLC)不会与视频中的时间同步.它总是关闭几秒钟(通常,视频内定时器比播放器报告的时间要快).
I'm attempting to record video from a 1080p webcam into a file. I held a timer up in the video and in every trial the timestamp reported by a video player (VLC is what I used) doesn't sync up with the time in the video. It's always off a few seconds (usually in-video timer is faster than player-reported time).
如下所示,我设置了C ++程序以在一个线程中捕获视频,并在另一个线程中进行记录.由于我的CPU使用率约为200%(可能最大输出了吗?),这可以正常工作.我在Mac OS X操作系统上,装有OS X 10.8 @ 1.8 GHz Intel Core i7.
As seen below, I set up the C++ program to capture video in one thread, and record in another thread. This is working fine as my CPU usage is ~200% (possible max out?). I'm on a Macbook Air w/ OS X 10.8 @ 1.8 GHz Intel Core i7.
我尝试将帧速率更改为15fps,这会导致视频断断续续/缓慢.我也尝试设置CV_CAP_PROP_FRAME_WIDTH
& CV_CAP_PROP_FRAME_HEIGHT
分辨率较低,会导致视频缓慢.看起来1080p @ 30fps可以产生稳定的视频,但是它的播放速度始终比预期的要快.我也尝试过在record << frame;
之后放入waitKey(10);
,但是它没有任何影响.
I've tried changing the framerate to 15fps and that results in very choppy/slow video. I've also tried setting CV_CAP_PROP_FRAME_WIDTH
& CV_CAP_PROP_FRAME_HEIGHT
to a lower resolution and it results in slow video. It appears that 1080p @ 30fps results in good steady video, but it is still always plays faster than it's supposed to. I've also tried putting in waitKey(10);
after record << frame;
but it did not affect anything.
关于如何使视频及时匹配的任何建议?
Any recommendations on how to make the video match up in time?
谢谢! 阿卡什(Aakash)
Thanks! Aakash
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <boost/thread.hpp>
using namespace cv;
void captureFunc(Mat *frame, VideoCapture *capture){
for(;;){
// get a new frame from camera
(*capture) >> (*frame);
}
}
int main(int, char**)
{
VideoCapture capture(0); // open the default camera
if( !capture.isOpened() ) {
printf("Camera failed to open!\n");
return -1;
}
capture.set(CV_CAP_PROP_FPS,30); //set capture rate to 30fps
Mat frame;
capture >> frame; // get first frame for size
// initialize recording of video
VideoWriter record("test.avi", CV_FOURCC('D','I','V','X'), 30, frame.size(), true);
if( !record.isOpened() ) {
printf("VideoWriter failed to open!\n");
return -1;
}
boost::thread captureThread(captureFunc, &frame, &capture); //start capture thread
sleep(1); //just to make sure capture thread is ready
for(;;)
{
// add frame to recorded video
record << frame;
}
return 0;
}
推荐答案
经过一些调试,我解决了问题; VideoWriter挑剔帧的速率是个问题.
I resolved my issue after a bit of debugging; it was an issue with VideoWriter being picky on the rate at which frames were fed to it.
这篇关于OpenCV VideoWriter帧率问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!