2vi*_*on2 2 c++ opencv image-processing computer-vision
如何在opencv中录制视频仅30秒.我使用下面的代码,但它只录制了24秒的视频?有什么问题?
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <time.h>
using namespace cv;
using namespace std;
int ex = 1734701162;
int main( int argc, const char** argv )
{
VideoCapture cap(0);
int i_fps = 15;
time_t start = time(0);
string OutputVideoPath = "output.mov";
VideoWriter outputVideo(OutputVideoPath, ex, i_fps, Size(640,480), true);
if(!cap.isOpened()) {
cout << "Not opened" << endl; // check if we succeeded
return -1;
}
while ( 1 ) {
Mat frame;
cap >> frame;
outputVideo << frame;
if ( difftime( time(0), start) == 30) break;
}
outputVideo.release();
cap.release();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
ber*_*rak 11
这里有2件事:
你正在查看壁挂时间.difftime返回一个double,所以它是极不可能的,你永远得到确切 30的结果.改为:
if ( difftime( time(0), start) >= 30) break;
您为VideoWriter指定15fps,但您测量的时间是播放(和写入)视频所花费的时间.(这完全是武断的)
如果输入视频的帧速率不同于15fps,则必须通过丢弃或复制帧来自行处理该比率.
你可能最好不要计算帧数,如果达到则停止30 * 15.