-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinput.cpp
136 lines (112 loc) · 2.22 KB
/
input.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "input.h"
CInput::CInput()
{
_url = NULL;
_timestamp_ms = -1;
_width = 0, _height = 0;
}
CInput::~CInput()
{
}
CInputVideo::CInputVideo()
{
_timestamp_ms = -1;
_bLoops = 0;
_nDuration_ms = -1;
}
CInputVideo::~CInputVideo()
{
}
int CInputVideo::Open(char *url)
{
_url = url;
if (_url == NULL)
return -1;
_cap.open(_url);
if (!_cap.isOpened())
{
cout << " video file [" << _url << "] open failed! " << endl;
return -1;
}
float fps = _cap.get(CV_CAP_PROP_FPS);
float framenum = _cap.get(CV_CAP_PROP_FRAME_COUNT);
if (fps <= 0)
return -1;
_nDuration_ms = (framenum / fps) * 1000;
//cout << "video duration : " << _nDuration_ms << endl;
_nBaseTimestamp_ms = 0;
_width = int(_cap.get(CV_CAP_PROP_FRAME_WIDTH));
_height = int(_cap.get(CV_CAP_PROP_FRAME_HEIGHT));
return 0;
}
int CInputVideo::Open(char *url, int bLoops)
{
_bLoops = bLoops;
return Open(url);
}
int CInputVideo::Get_FirstFrame(Mat &firstImg)
{
if (!_matFirst.empty())
{
firstImg = _matFirst;
return 0;
}
VideoCapture cap(_url);
if (!cap.isOpened())
return -1;
cap >> _matFirst;
firstImg = _matFirst;
return 0;
}
int CInputVideo::Get_Duration()
{
return _nDuration_ms;
}
float CInputVideo::Get_FrameRate()
{
return _cap.get(CV_CAP_PROP_FPS);
}
int CInputVideo::Close()
{
return 0;
}
int CInputVideo::Get_CurrentMat(Mat &img, int startTime, int endTime, int currentTime)
{
int hopeTimestamp_ms = currentTime - startTime - _nBaseTimestamp_ms;
if (hopeTimestamp_ms<0)
return -1;
while (1)
{
if (_timestamp_ms >= hopeTimestamp_ms)
{
if (_matCurrent.empty())
{
cout << "video _matCurrent is emtpy " << endl;
return -1;
}
img = _matCurrent;
//cout << _timestamp_ms << endl;
break;
}
else
{
_timestamp_ms = _cap.get(CV_CAP_PROP_POS_MSEC);
_cap >> _matCurrent;
if (_matCurrent.empty())
{
if (_timestamp_ms < 0 || _bLoops == 0)
return -1;
// loops == 1
_nBaseTimestamp_ms += (_timestamp_ms + 1);
hopeTimestamp_ms = currentTime - startTime - _nBaseTimestamp_ms;
_timestamp_ms = -1;
//_cap.open(_url);
_cap.set(CV_CAP_PROP_POS_MSEC, 0);
_timestamp_ms = _cap.get(CV_CAP_PROP_POS_MSEC);
_cap >> _matCurrent;
continue;
}
}
}
return 0;
}