extratorVTT.cpp
4.01 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
#include "extratorVTT.h"
ExtratorVTT::ExtratorVTT() {
listeners = new list<ListenerSub*>();
seekPos = 0;
this->finish = false;
hasNextCue = true;
PRINTL(util::_DEBUG, "ExtratorVTT Done!\n");
}
ExtratorVTT::~ExtratorVTT() {
listeners->clear();
delete listeners;
if (bff_reader) delete bff_reader;
if (file_io) delete file_io;
PRINTL(util::_DEBUG, "ExtratorVTT finalized!\n");
}
void ExtratorVTT::setFilePath(char* path) {
this->filePath = path;
}
bool ExtratorVTT::isFinished() {
return this->finish;
}
void ExtratorVTT::addListener(ListenerSub* listener){
listeners->push_back(listener);
}
void ExtratorVTT::notifyListeners(unsigned char* subtitle, uint64_t pts) {
for(list<ListenerSub*>::iterator it = listeners->begin(); it != listeners->end(); it++){
(*it)->notifySubtitle(subtitle, pts);
}
}
void ExtratorVTT::notifyEndExtraction(int size) {
PRINTL(util::_DEBUG, "Extrator webVTT concluiu a extração: %d legendas.\n", size);
for(list<ListenerSub*>::iterator it = listeners->begin(); it != listeners->end(); it++){
(*it)->notifyEnd(size);
}
}
void ExtratorVTT::initialize() {
file = new lavidlib::File(this->filePath);
try{
file_io = new lavidlib::FileIO(file->getPath(), FileIO::MODE_READ);
bff_reader = new BufferedReader(file_io);
}catch(Exception &ex){
finish = true;
Logging::instance()->writeLog("extratorVTT.cpp <Error>: Arquivo de legenda não encontrado.");
throw lavidlib::RuntimeException("Falha ao abrir o arquivo de legenda! Verifique se o mesmo existe.\n");
}
this->Start();
}
Subtitle* ExtratorVTT::nextCue() {
string line;
string cueText = "";
int target_pos;
int64_t time_in;
Subtitle* cue = new Subtitle();
try{
do{
line = bff_reader->readLine();
seekPos += line.size() + SIZE_SCAPE;
}while(line.find(TARGET_TIME) == string::npos);
target_pos = line.find(TARGET_TIME);
time_in = str_to_time(line.substr(0, target_pos));
cue->setTimeIn(time_in);
while((line = bff_reader->readLine()) != ""){
cueText += line;
cueText += " ";
}
}catch(lavidlib::EOFException &ex){
cue->setSubtitleText(formatText(cueText));
seekPos += (int64_t) cueText.size() + SIZE_SCAPE;
hasNextCue =false;
return cue;
}
cue->setSubtitleText(formatText(cueText));
seekPos += (int64_t) cueText.size() + SIZE_SCAPE;
return cue;
}
string ExtratorVTT::formatText(string line){
int lessThanPos;
int moreThanPos;
string f_line = line;
lessThanPos = f_line.find_first_of(LESS_THAN); //pega a posição do simbolo '<'
moreThanPos = f_line.find_first_of(MORE_THAN); //pega a posição do simbolo '>'
while(lessThanPos != string::npos && moreThanPos != string::npos){
f_line = f_line.erase(lessThanPos, moreThanPos - (lessThanPos-1)); //remove o trecho '<string>'
lessThanPos = f_line.find_first_of(LESS_THAN);
moreThanPos = f_line.find_first_of(MORE_THAN);
}
return f_line;
}
int64_t ExtratorVTT::str_to_time(string str_time) {
int64_t ttime = 0;
char* tokens = new char[4]; // hh, mm, ss, ms
strcpy(tokens, (char*)str_time.c_str());
int index = 0;
int values [4]; // hh, mm, ss, ms
char * str = strtok(tokens, ":,.");
while (str != NULL) {
values[index] = atoi(str);
str = strtok(NULL, ":,.");
index++;
}
delete(tokens);
/* calculate time */
ttime = /*hour to sec*/((((values[0] * 60) * 60) +
/*min to sec*/(values[1] * 60) +/*sec*/values[2])*1000) + values[3];
return ttime;
}
void ExtratorVTT::Run() {
PRINTL(util::_INFO, "Extraindo Legendas...\n");
int sub_index = 0;
string cue_text = "";
while(hasNextCue){
subtitle = nextCue();
cue_text = subtitle->getSubtitleText();
notifyListeners((unsigned char*)cue_text.c_str(), (uint64_t) subtitle->getTimeIn());
sub_index++;
free(subtitle);
}
if(sub_index == 0)
notifyListeners((unsigned char*)"ARQUIVO_INVALIDO", 0);
this->finish = true;
notifyEndExtraction(sub_index);
}