main.cpp
9.75 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* File: main.cpp
* Author: felipe
*
* Created on 15 de Outubro de 2009, 10:56
*/
/*
* File: main.cpp
* Author: Erickson
*
* Edit on 03 de Fevereiro de 2014
*/
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "logging.h"
#include "argParser.h"
#include "serviceTester.h"
#include "serviceException.h"
#include "serviceWindowGenerationFromSRT.h"
#include "serviceWindowGenerationFromRec.h"
#include "serviceWindowGenerationFromText.h"
#include <lavidlib/base/RuntimeException.h>
#define MAX_SIZE_PATH 256
#define PATH_VBOX_UPLOADS "vlibras_user/.vlibras-config/uploads"
#define PATH_DEVEL_CONTENTS "vlibras_user/vlibras-contents/videos"
#define PATH_DEVEL_UPLOADS "vlibras_user/vlibras-contents/uploads"
using namespace std;
using namespace util;
void serviceSRT(int service, string path_video, string path_srt, int language, int position, int size, int background, string id, int mode);
void serviceREC(int service, string path_video, int position, int size, int background, string id, int mode);
void serviceOnlySRT(int service, string path_srt, int language, int background, string id, int mode);
void serviceOnlyAudio(int service, string path_audio, int background, string id, int mode);
void serviceText(string path_text, int language, int background, string id, int mode);
void serviceRECWithoutMixing(int service, string path_video, int background, string id, int mode);
void serviceTest(int position, int size, int background);
void help();
void hasFailed();
void hasInvalid();
void fail(string msg);
void setPathContents(int mode, string id);
bool isFailed;
bool isInvalid;
int main(int argc, char* argv[]) {
int service;
int language;
int position;
int size;
int background;
int mode;
int nomixer;
string input;
string input_srt;
string id;
ArgParser *parser;
struct timeval tv1, tv2;
double t1, t2;
gettimeofday(&tv1, NULL);
t1 = (double)(tv1.tv_sec) + (double)(tv1.tv_usec)/ 1000000.00;
parser = new ArgParser();
try{
parser->readArgs(argv, argc);
}catch(lavidlib::RuntimeException &ex){
exit(1);
}
//printf("\n################## LAViD : VLibras ##################\n\n");
service = parser->getService();
util::Logging::instance()->setLevel(parser->getLog());
switch(service){
case 1:
PRINTL(util::_INFO, "Service Type: Video with Subtitles\n");
input = parser->getInput();
input_srt = parser->getInputSRT();
language = parser->getLanguage();
position = parser->getPosition();
size = parser->getSize();
background = parser->getBackground();
id = parser->getId();
mode = parser->getMode();
serviceSRT(service, input, input_srt, language, position, size, background, id, mode);
break;
case 2:
PRINTL(util::_INFO, "Service Type: Video Recognize\n");
input = parser->getInput();
position = parser->getPosition();
size = parser->getSize();
background = parser->getBackground();
id = parser->getId();
mode = parser->getMode();
serviceREC(service, input, position, size, background, id, mode);
break;
case 3:
PRINTL(util::_INFO, "Service Type: Text\n");
input = parser->getInput();
language = parser->getLanguage();
background = parser->getBackground();
id = parser->getId();
mode = parser->getMode();
serviceText(input, language, background, id, mode);
break;
case 4:
PRINTL(util::_INFO, "Service Type: Subtitles only\n");
input = parser->getInput();
language = parser->getLanguage();
background = parser->getBackground();
id = parser->getId();
mode = parser->getMode();
serviceOnlySRT(service, input, language, background, id, mode);
break;
case 5: //reconhecimento de audio
PRINTL(util::_INFO, "Service Type: Audio Recognize\n");
input = parser->getInput();
background = parser->getBackground();
id = parser->getId();
mode = parser->getMode();
serviceREC(service, input, 0, 0, background, id, mode);
break;
case 6: //Reconhecimento de video sem mixagem
PRINTL(util::_INFO, "Service Type: Video Recognize (Mixer Disabled)\n");
input = parser->getInput();
background = parser->getBackground();
id = parser->getId();
mode = parser->getMode();
serviceREC(service, input, 0, 0, background, id, mode);//como service != 2 então não há mixagem
break;
case 7: // serviço de teste
position = parser->getPosition();
size = parser->getSize();
background = parser->getBackground();
serviceTest(position, size, background);
break;
case 8: //ajuda
help();
break;
default:
PRINTL(util::_ERROR, "Opção de serviço não reconhecida!\n");
hasInvalid();
}
if(isFailed)
exit(1);
else if(isInvalid)
exit(127);
gettimeofday(&tv2, NULL);
t2 = (double)(tv2.tv_sec) + (double)(tv2.tv_usec)/ 1000000.00;
PRINTL(util::_DEBUG, "Time: %lf\n", (t2-t1));
PRINTL(util::_INFO, "VLibras finalized!\n\n");
exit(0);
}
void serviceSRT(int service, string path_video, string path_srt, int language, int position, int size, int background, string id, int mode){
char* video = new char[MAX_SIZE_PATH];
char* srt = new char[MAX_SIZE_PATH];
char* name = new char[64];
strcpy(video, path_video.c_str());
strcpy(srt, path_srt.c_str());
strcpy(name, id.c_str());
setPathContents(mode, id);
ServiceWindowGenerationFromSRT* service_srt;
service_srt = new ServiceWindowGenerationFromSRT(video, srt, language, position, size, background, name, mode, service);
try{
service_srt->initialize();
}catch(ServiceException ex){
fail(ex.getMessage());
hasFailed();
return;
}
while(!service_srt->isFinished()){
sleep(5);
}
delete [] video;
delete [] srt;
delete [] name;
delete service_srt;
}
void serviceREC(int service, string path_video, int position, int size, int background, string id, int mode){
char* video = new char[MAX_SIZE_PATH];
char* name = new char[64];
strcpy(video, path_video.c_str());
strcpy(name, id.c_str());
setPathContents(mode, id);
ServiceWindowGenerationFromRec* service_rec;
service_rec = new ServiceWindowGenerationFromRec(video, position, size, background, name, mode, service);
try{
service_rec->initialize();
}catch(ServiceException ex){
fail(ex.getMessage());
hasFailed();
return;
}
while(!service_rec->isFinished()){
sleep(5);
}
delete [] video;
delete [] name;
delete service_rec;
}
void serviceText(string path_text, int language, int background, string id, int mode){
char* text = new char[MAX_SIZE_PATH];
char* name = new char[64];
strcpy(text, path_text.c_str());
strcpy(name, id.c_str());
setPathContents(mode, id);
ServiceWindowGenerationFromText* service_text;
service_text = new ServiceWindowGenerationFromText(text, language, background, name, mode);
try{
service_text->initialize();
}catch(ServiceException ex){
fail(ex.getMessage());
hasFailed();
return;
}
while (!service_text->isFinished()) {
usleep(100000); //100ms
}
delete [] text;
delete [] name;
delete service_text;
}
void serviceOnlySRT(int service, string path_srt, int language, int background, string id, int mode){
char* srt = new char[MAX_SIZE_PATH];
char* name = new char[64];
strcpy(srt, path_srt.c_str());
strcpy(name, id.c_str());
setPathContents(mode, id);
ServiceWindowGenerationFromSRT* service_srt;
service_srt = new ServiceWindowGenerationFromSRT(srt, language, background, name, mode, service);
try{
service_srt->initialize();
}catch(ServiceException ex){
fail(ex.getMessage());
hasFailed();
return;
}
while (!service_srt->isFinished()) {
usleep(100000); //100ms
}
delete [] srt;
delete [] name;
delete service_srt;
}
void serviceTest(int position, int size, int background) {
ServiceTester* service_t;
service_t = new ServiceTester(position, size, background);
try{
service_t->Start();
}catch(ServiceException &ex){
fail(ex.getMessage());
hasFailed();
return;
}
while (!service_t->isFinished()) {
sleep(5);
}
delete service_t;
}
void fail(string msg){
PRINTL(util::_ERROR, "\nOps... Tivemos um problema! :(\n");
PRINTL(util::_ERROR, "Possível causa do erro: %s\n\n", msg.c_str());
}
void hasFailed(){
isFailed = true;
}
void hasInvalid(){
isInvalid = true;
}
void setPathContents(int mode, string id){
string command = "mkdir -p ";
switch(mode){
case 1://devel
char* vlStorage;
char* vlUploads;
vlStorage = getenv("VLSTORAGE");
vlUploads = getenv("VLUPLOADS");
if(vlStorage == NULL || vlUploads == NULL){
command.append(PATH_DEVEL_CONTENTS).append(" && mkdir -p ")
.append(PATH_DEVEL_UPLOADS).append("/").append(id);
system(command.c_str());
}
break;
case 2://produção
command.append(PATH_VBOX_UPLOADS).append("/").append(id);
system(command.c_str());
break;
default:
fail("Modo de execução não reconhecido!");
exit(127);
}
}
void help()
{
std::cout << std::endl
<< "Usage Summary: vlibras [Service_option] filepath [Options] --id [name] --mode [devel,prod]"
<< "\n\nService_option:"
<< "\n\tVideo with Subtitles -V, --video [filePath] -S, --subtitle [filePath]"
<< "\n\tVideo Recognize -V, --video [filePath]"
<< "\n\tAudio Recognize -A, --audio [filepath]"
<< "\n\tText -T, --text [filePath]"
<< "\n\tSubtitles only -S, --subtitle [filePath]"
<< "\n\nOptions:"
<< "\n\t-l, --language [portugues,glosa]"
<< "\n\t-b, --background [opaque,transp]"
<< "\n\t-r, --resolution [small,medium,large]"
<< "\n\t-p, --position [top_left,top_right,bottom_left,bottom_right]"
<< "\n\t-m, --mode [devel,prod]"
<< "\n\t-v, --loglevel [quiet,error,warning,info,debug]"
<< "\n\t--id [name] Relative to the unique ID on the Database."
<< "\n\t--no-mixer Disables mixing with the original video."
<< "\n\nSee man vlibras for detailed descriptions."
<< std::endl;
exit(0);
}