-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveVideo.cpp
More file actions
253 lines (203 loc) · 6.09 KB
/
SaveVideo.cpp
File metadata and controls
253 lines (203 loc) · 6.09 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "FlyCapture2.h"
#include "SaveVideoClass.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <sstream>
#include <string> // for strings
#include <ncurses.h>
#define MINDELAY 50 // waiting time in msec between video frames for the display
using namespace std;
using namespace FlyCapture2;
//using namespace cv;
//#include "Profiler.h"
static void help()
{
cout
<< "------------------------------------------------------------------------------" << endl
<< "Capturing of videos from FlyCapture SDK cameras. Codecs: X264 MPEG DIVX " << endl
<< "if WAITMSEC=0 no display is shown. Otherwise to number of msec to wait. " << endl
<< "Usage:" << endl
<< " ./savevideo inputvideoName [CODEC WAITMSEC] " << endl
<< "Example: ./savevideo test.avi X264 500" << endl
<< "------------------------------------------------------------------------------" << endl
<< endl;
};
int selectCameras(VideoSaverFlyCapture ***pppVideoSavers, unsigned int * pNumSelected)
{
BusManager busMgr;
Error error;
unsigned int numCameras;
PGRGuid guid;
error = busMgr.GetNumOfCameras(&numCameras);
if (error != PGRERROR_OK)
{
error.PrintErrorTrace();
return -1;
}
if (numCameras==0)
{
cout << "Cannot find a FlyCapture SDK compatible camera..." << endl;
return -1;
}
// select one or both cameras
unsigned int numSelected=0;
unsigned int selected[numCameras];
initscr();
for ( unsigned int i = 0; i < numCameras; i++)
{
error = busMgr.GetCameraFromIndex( i, &guid );
if (error != PGRERROR_OK)
{
error.PrintErrorTrace();
return -1;
}
// Connect the camera
Camera camera;
error = camera.Connect( &guid);
if ( error != PGRERROR_OK )
{
cout << "Failed to connect to camera" << endl;
return -1;
}
// Get the camera info and print it out
CameraInfo camInfo;
stringstream ss;
error = camera.GetCameraInfo( &camInfo );
if ( error != PGRERROR_OK )
{
cout << "Failed to get camera info from camera" << endl;
return -1;
}
ss << camInfo.vendorName << " " << camInfo.modelName << " " << camInfo.serialNumber << " "
<< camInfo.sensorResolution;
string tmpstr = string(ss.str()); // copy
if (numCameras>1)
{
int key= 0;
clear();
printw("** Should this camera be used for recording?\n");
printw("%c: %s \n y or n :", char(65+i),tmpstr.c_str());
refresh();
key = getch();
if (key=='y')
{
selected[numSelected++] = i;
cout << " selected." << endl;
}
else
{
cout << " NOT selected." << endl;
}
} else
{
printw("Selected camera %s for recording.\n",tmpstr.c_str());
selected[numSelected++] = i;
}
camera.Disconnect();
}
endwin(); // % end the curses mode
if (!numSelected)
{
cout << "need at least one camera selected.!" <<endl;
return -1;
}
// construct the objects and init
(*pppVideoSavers) = new VideoSaverFlyCapture*[numSelected];
for ( unsigned int i = 0; i < numSelected; i++)
{
error = busMgr.GetCameraFromIndex( selected[i], &guid );
if (error != PGRERROR_OK)
{
error.PrintErrorTrace();
return -1;
}
(*pppVideoSavers)[i] = new VideoSaverFlyCapture();
if ((*pppVideoSavers)[i]->init(guid)!=0)
return -1;
}
(*pNumSelected) = numSelected;
return 0;
}
int main(int argc, const char *argv[])
{
//parse inputs
int waitMsecs;
if ((argc ==1) || (argc >4))
{
help();
cout << "Not enough parameters" << endl;
return -1;
} else if (argc == 4) {
if (!(istringstream(argv[3]) >> waitMsecs)) {
help();
cout << "Third parameter needs to be an int" << endl;
return -1;
}
} else {
waitMsecs = MINDELAY;
}
std::string fourcc;
if (argc==2) {
fourcc = string("X264");
} else {
fourcc = argv[2];
}
if (fourcc.length()!=4) {
help();
cout << "Second parameter needs to be an FOURCC code" << endl;
return -1;
}
const string fname = argv[1];
unsigned int numCameras;
// init
VideoSaverFlyCapture **ppVideoSavers;
if (selectCameras(&ppVideoSavers,&numCameras)!=0)
return -1;
// start capturing
for ( unsigned int i = 0; i < numCameras; i++)
{
string local_fname= std::string(fname);
if (numCameras>1) {
local_fname = char(65 + i) + local_fname;
}
if (ppVideoSavers[i]->startCaptureAndWrite(local_fname,fourcc)!=0)
return -1;
}
// wait and display loop
char key = 0;
int waitAmount = waitMsecs > MINDELAY? waitMsecs: MINDELAY;
cv::Mat frame;
string imname;
cv::Mat smallFrame;
cv::Size size(800,800);
while(key != 'q')
{
key = cv::waitKey(waitAmount);
if (waitMsecs>0) {
//usleep(delayFound*1000);
for ( unsigned int i = 0; i < numCameras; i++)
{
//plot
imname[0]= char(i+65);
cv::Mat frame;
double timeStamp;
int frameNumber;
ppVideoSavers[i]->getFrame(&frame,&timeStamp,&frameNumber);
cv::resize(frame,smallFrame,size);
cv::cvtColor(smallFrame,smallFrame,CV_RGB2BGR);
cv::imshow(imname.c_str(), smallFrame);
cout << char(i+65) << ": Lost frames " << ppVideoSavers[i]->getLostFrameNumber() << " | " ;
};
cout << "\r" ;
}
for ( unsigned int i = 0; i < numCameras; i++)
{
if (ppVideoSavers[i]->isFinished())
key = 'q'; // quit
}
}
for ( unsigned int i = 0; i < numCameras; i++)
ppVideoSavers[i]->close();
return 0;
}