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

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

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

 1 /*
 2  * =====================================================================================
 3  *
 4  *       Filename:  count_the_vedeo_time.cpp
 5  *      Environment:    
 6  *    Description:  用于计算视频文件列表里的视频文件各个时长及其总和,用于统计视频数据库的时长
 7  *
 8  *
 9  *        Version:  1.0
10  *        Created:  2013/10/21 8:50:09
11  *         Author:  yuliyang
12 I*
13  *             Mail:  wzyuliyang911@gmail.com
14  *             Blog:  http://www.cnblogs.com/yuliyang
15  *
16  * =====================================================================================
17  */
18 
19 #include <opencv2/core/core.hpp>
20 #include <opencv2/highgui/highgui.hpp>
21 #include <opencv2/imgproc/imgproc.hpp>
22 #include <iostream>
23 #include "fstream"
24 
25 
26 using namespace std;
27 using namespace cv;
28 
29 int main(int args,char* argv[])
30 {
31     string buf;    
32     ifstream svm_data(argv[1]);                 /*读入视频文件列表  */
33     ofstream result(argv[2]);                   /* 保存结果文件 */
34     double sum=0;
35     while (svm_data)
36     {
37         if (getline(svm_data,buf))
38         {
39             result<<"计算视频"<<buf.c_str()<<"的时长"<<endl;
40             //打开视频文件:其实就是建立一个VideoCapture结构
41             VideoCapture capture(buf.c_str());
42             //检测是否正常打开:成功打开时,isOpened返回ture
43             if(!capture.isOpened())
44                 result<<"fail to open!"<<endl;
45             //获取整个帧数
46             double totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);
47             //获取帧率
48             double rate = capture.get(CV_CAP_PROP_FPS);
49             
50             double vedio_time= totalFrameNumber/rate;
51             result<<"该个视频共"<<totalFrameNumber<<"帧,"<<"帧率为:"<<rate<<"该视频时长为:"<<vedio_time<<" s"<<endl;
52             capture.release();
53             sum +=vedio_time;
54             
55         }
56     }
57 
58     result<<"视频时长总和为:"<<sum/60<<"mins"<<endl;
59     result.close();
60     return 0;
61 }

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/yuliyang/p/3379904.html