使用opencv统计视频库的总时长

时间:2022-03-29 15:02:46

统计视频库里的视频文件的总时长

废话不多说,直接上代码:

 /*
* =====================================================================================
*
* Filename: count_the_vedeo_time.cpp
* Environment:
* Description: 用于计算视频文件列表里的视频文件各个时长及其总和,用于统计视频数据库的时长
*
*
* Version: 1.0
* Created: 2013/10/21 8:50:09
* Author: yuliyang
I*
* Mail: wzyuliyang911@gmail.com
* Blog: http://www.cnblogs.com/yuliyang
*
* =====================================================================================
*/ #include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include "fstream" using namespace std;
using namespace cv; int main(int args,char* argv[])
{
string buf;
ifstream svm_data(argv[]); /*读入视频文件列表 */
ofstream result(argv[]); /* 保存结果文件 */
double sum=;
while (svm_data)
{
if (getline(svm_data,buf))
{
result<<"计算视频"<<buf.c_str()<<"的时长"<<endl;
//打开视频文件:其实就是建立一个VideoCapture结构
VideoCapture capture(buf.c_str());
//检测是否正常打开:成功打开时,isOpened返回ture
if(!capture.isOpened())
result<<"fail to open!"<<endl;
//获取整个帧数
double totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
//获取帧率
double rate = capture.get(CV_CAP_PROP_FPS); double vedio_time= totalFrameNumber/rate;
result<<"该个视频共"<<totalFrameNumber<<"帧,"<<"帧率为:"<<rate<<"该视频时长为:"<<vedio_time<<" s"<<endl;
capture.release();
sum +=vedio_time; }
} result<<"视频时长总和为:"<<sum/<<"mins"<<endl;
result.close();
return ;
}