extratorSRT.cpp
5.7 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
#include "extratorSRT.h"
#include "listenerSRT.h"
#include "listenerMonitorPCRBase.h"
#include <stdio.h>
#include <iostream>
#include "dprintf.h"
using namespace std;
ExtratorSRT::ExtratorSRT(){
DPRINTF("Done!\n");
listeners = new list<ListenerSRT*>();
count_legendas = 0;
pcr_base = 0;
finish = false;
hasPCRBase = false;
}
ExtratorSRT::~ExtratorSRT(){
if (listeners != NULL)
delete listeners;
}
//@Deprecated
void ExtratorSRT::initialize(){
/* printf("ExtratorSRT::initialize()\n");
if(filepath)
Start();
else
printf("[ERRO] File SRT not found!\n");*/
}
void ExtratorSRT::addListener(ListenerSRT* listener){
listeners->push_back(listener);
}
void ExtratorSRT::notifyListeners(unsigned char* subtitle, int64_t pts) {
for(list<ListenerSRT*>::iterator it = listeners->begin(); it != listeners->end(); it++){
(*it)->notifySubtitle(subtitle, pts);
}
}
void ExtratorSRT::notifyEndExtraction(int sub_size) {
DDPRINTF("Extrator SRT concluiu a extração: %d legendas.\n", sub_size);
for(list<ListenerSRT*>::iterator it = listeners->begin(); it != listeners->end(); it++){
(*it)->notifyEnd(sub_size);
}
}
void ExtratorSRT::setFilePath(char* path){
filepath = (char*) path;
}
char* ExtratorSRT::getFilePath(){
return filepath;
}
bool ExtratorSRT::isFinished(){
return finish;
}
void ExtratorSRT::Run(){
DPRINTF("[AGUARDE] Extraindo SRT...\n")
FILE *srt_file = fopen(filepath, "r");
if (srt_file == NULL) {
printf("[ERROR] Can't open SRT file!\n");
finish = true;
return;
}
int sub_index = 1;
string sub_text = "";
char buffer [MAX_BUFFER];
double pts = 0;
while (!feof(srt_file)) {
cout << " . ";
if(fgets(buffer, MAX_BUFFER, srt_file) != NULL) {
// get index of subtitle
if (((int) atoi((char*)buffer)) == sub_index) {
// get time this subtitle
fgets(buffer, MAX_BUFFER, srt_file);
pts = (double) convert_pts((char*) buffer);
string strtmp = "";
// get subtitle text
do {
fgets(buffer, MAX_BUFFER, srt_file);
if (strcmp(buffer, "\r\n") == 0 || strcmp(buffer, "\n") == 0)
break;
strtmp = buffer;
// file encoding with '\r\n' or only '\n'
if (strtmp.find("\r") != std::string::npos) {
sub_text.append(strtmp.substr(0, strtmp.length()-2)).append(" ");
} else {
sub_text.append(strtmp.substr(0, strtmp.length()-1)).append(" ");
}
} while (1);
notifyListeners((unsigned char*)sub_text.c_str(), calcula_pts(pts));
sub_index++;
sub_text = "";
} else {
printf("[ERROR] Sequency of subtitles not found.\n");
}
}
}
cout << "\n";
finish = true;
notifyEndExtraction(sub_index-1);
DDDPRINTF("Extractor STR finalized!\n");
/* printf("ExtratorSRT::Run...\n");
string subtitle;
string strtmp;
int cont = 1;
int index_sub_file = 0;
FILE *file = fopen(filepath, "r");
if(file != NULL){
char buffer [MAX_BUFFER];
double presentation_time = 0;
while(!feof(file)){
if(fgets(buffer, MAX_BUFFER, file) != NULL){
index_sub_file = (int) atoi((char*)buffer);
if(index_sub_file == cont) {
if(cont > 1) {
notifyListeners((unsigned char*)subtitle.c_str(), calcula_pts(presentation_time));
}
subtitle = "";
fgets(buffer, MAX_BUFFER, file);
presentation_time = (double) convert_pts((char*) buffer);
cont++;
} else {
//FIXME: as linhas do arquivo devem ser lidas considerando apenas os caracteres válidos
if (strcmp(buffer, "\r\n") != 0){
strtmp = (string) buffer;
subtitle += strtmp.substr(0, strtmp.size()-2); // (-2): "\r\n"
}
}
} else {
// chegando aqui, é a última legenda!!!
notifyListeners((unsigned char*)subtitle.c_str(), calcula_pts(presentation_time));
}
}
printf("#A extração das legendas do arquivo .srt foram concluídas.\n\n");
finish = true;
fclose(file);
} else {
printf("#ERRO! Problemas ao tentar ler o arquivo de legendas .SRT\n");
}
*/
}
void ExtratorSRT::notifyPCRBase(uint64_t pcrbase){
this->pcr_base = pcrbase;
this->hasPCRBase = true;
}
double ExtratorSRT::convert_pts(char* strtimer){
vector<string> parts;
int tam = strlen(strtimer);
char vinput [tam];
strcpy(vinput, strtimer);
char * str;
str = strtok(vinput, ":,");
while (str != NULL) {
parts.push_back(string(str));
str = strtok(NULL, ":,");
}
int h = atoi(parts[0].c_str());
int m = atoi(parts[1].c_str());
int s = atoi(parts[2].c_str());
int ms = atoi(parts[3].c_str());
double pts = ((double)ms/1000.0) + ((double)s) + ((double)m*60) + (((double)h*60)*60);
return pts;
}
uint64_t ExtratorSRT::calcula_pts(double seconds){
return (uint64_t)(pcr_base + (seconds * 90000.0));
}