From aa9957304cf85e9a2bd98e146fbbcbf8744a55e8 Mon Sep 17 00:00:00 2001 From: Wesnydy Ribeiro Date: Wed, 23 Mar 2016 08:53:58 -0300 Subject: [PATCH] Ajustes no extratorSRT --- extrator/src/extratorSRT.cpp | 65 ++++++++++++++++++++++++++++++++++++----------------------------- extrator/src/extratorTXT.cpp | 66 ++++++++++++++++++++++++++++++++++++------------------------------ extrator/src/extratorVTT.cpp | 56 ++++++++++++++++++++++++++++++++++++++++---------------- extrator/src/include/extrator.h | 9 ++++++--- extrator/src/include/extratorSRT.h | 23 ++++++++++++----------- extrator/src/include/extratorTXT.h | 12 ++++++------ extrator/src/include/extratorVTT.h | 4 +++- log/log | 3 +++ renderer/src/renderer.cpp | 1 + servico/src/serviceWindowGenerationFromSubtitle.cpp | 13 ++++++++++--- servico/src/serviceWindowGenerationFromText.cpp | 18 ++++++++++++------ 11 files changed, 165 insertions(+), 105 deletions(-) diff --git a/extrator/src/extratorSRT.cpp b/extrator/src/extratorSRT.cpp index 893487a..5b68e3d 100644 --- a/extrator/src/extratorSRT.cpp +++ b/extrator/src/extratorSRT.cpp @@ -1,4 +1,5 @@ #include "extratorSRT.h" +#include ExtratorSRT::ExtratorSRT(){ listeners = new list(); @@ -11,27 +12,10 @@ ExtratorSRT::ExtratorSRT(){ ExtratorSRT::~ExtratorSRT(){ listeners->clear(); delete listeners; - //if (bff_reader) delete bff_reader; if (file_io) delete file_io; PRINTL(util::_DEBUG, "ExtratorSTR finalized!\n"); } -void ExtratorSRT::initialize(){ - - file = new lavidlib::File(filePath); - - try{ - file_io = new lavidlib::FileIO(file->getPath(), FileIO::MODE_READ); - }catch(Exception ex){ - finish = true; - Logging::instance()->writeLog("extratorSRT.cpp : Arquivo de legenda não encontrado."); - throw ExtratorException("Falha ao abrir o arquivo de legenda! Verifique se o mesmo existe.\n"); - } - - this->Start(); -} - - void ExtratorSRT::addListener(ListenerSub* listener){ listeners->push_back(listener); } @@ -49,18 +33,39 @@ void ExtratorSRT::notifyEndExtraction(int size) { } } -void ExtratorSRT::setFilePath(char* path){ - filePath = (char*) path; - string command1 = "perl -p -e 's/\r$//' < "; - command1.append(filePath).append(" > ").append(INPUT_SRT); - system(command1.c_str()); - - string command2 = "iconv -f"; - command2.append(" $(file --mime-encoding -b ") - .append(INPUT_SRT).append(")") - .append(" -t utf-8 ").append(INPUT_SRT).append(" > ").append(filePath) - .append(" && rm -f ").append(INPUT_SRT); - system(command2.c_str()); +void ExtratorSRT::encodingfiletoUTF8() { + string iconvcmd = "iconv -f"; //Recodifica o arquivo para utf-8 + + iconvcmd.append(" $(file --mime-encoding -b ") + .append(this->filePath).append(") -t utf-8 ") + .append(this->filePath).append(" > ").append(TEMP_SRT); + system(iconvcmd.c_str()); + + string perlcmd = "perl -p -e 's/\r$//' < "; //Formata a quebra de linha + + perlcmd.append(TEMP_SRT).append(" > ").append(this->filePath) + .append(" && rm -f ").append(TEMP_SRT); + system(perlcmd.c_str()); +} + +void ExtratorSRT::setFilePath(char* path) { + this->filePath = path; + encodingfiletoUTF8(); +} + +void ExtratorSRT::initialize(){ + + file = new lavidlib::File(this->filePath); + + try{ + file_io = new lavidlib::FileIO(file->getPath(), FileIO::MODE_READ); + }catch(Exception &ex){ + finish = true; + Logging::instance()->writeLog("extratorSRT.cpp : Arquivo de legenda não encontrado."); + throw lavidlib::RuntimeException("Falha ao abrir o arquivo de legenda! Verifique se o mesmo existe.\n"); + } + this->Start(); + } bool ExtratorSRT::isFinished(){ @@ -88,6 +93,7 @@ void ExtratorSRT::Run(){ } if(sub_index == 0) notifyListeners((unsigned char*)"ARQUIVO_INVALIDO", 0); + finish = true; notifyEndExtraction(sub_index); } @@ -136,6 +142,7 @@ Subtitle* ExtratorSRT::next() { sub->setSubtitleText(formatText(text_sub)); seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE; hasNextSub = false; + delete(bff_reader); return sub; } sub->setSubtitleText(formatText(text_sub)); diff --git a/extrator/src/extratorTXT.cpp b/extrator/src/extratorTXT.cpp index be00f7a..81b4bc9 100644 --- a/extrator/src/extratorTXT.cpp +++ b/extrator/src/extratorTXT.cpp @@ -8,7 +8,7 @@ * Author: Erickson Silva (erickson.silva@lavid.ufpb.br) * * * **************************************************************************/ - + #include "extratorTXT.h" ExtratorTXT::ExtratorTXT(){ @@ -17,7 +17,7 @@ ExtratorTXT::ExtratorTXT(){ PRINTL(util::_DEBUG, "ExtratorTXT Done!\n"); } -ExtratorTXT::~ExtratorTXT(){ +ExtratorTXT::~ExtratorTXT(){ listeners->clear(); delete listeners; delete file; @@ -26,19 +26,6 @@ ExtratorTXT::~ExtratorTXT(){ PRINTL(util::_DEBUG, "ExtratorTXT finalized!\n"); } -void ExtratorTXT::initialize(){ - file = new lavidlib::File(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("extratorTXT.cpp : Arquivo de texto não encontrado."); - throw ExtratorException("Falha ao abrir o arquivo de texto! Verifique se o mesmo existe."); - } - this->Start(); -} - void ExtratorTXT::addListener(ListenerTXT* listener){ listeners->push_back(listener); } @@ -46,7 +33,7 @@ void ExtratorTXT::addListener(ListenerTXT* listener){ void ExtratorTXT::notifyListeners(unsigned char* line) { for(list::iterator it = listeners->begin(); it != listeners->end(); it++){ (*it)->notifyLine(line); - } + } } void ExtratorTXT::notifyEndExtraction(int size) { @@ -56,18 +43,37 @@ void ExtratorTXT::notifyEndExtraction(int size) { } } +void ExtratorTXT::encodingfiletoUTF8() { + string iconvcmd = "iconv -f"; //Recodifica o arquivo para utf-8 + + iconvcmd.append(" $(file --mime-encoding -b ") + .append(this->filePath).append(") -t utf-8 ") + .append(this->filePath).append(" > ").append(TEMP_TXT); + system(iconvcmd.c_str()); + + string perlcmd = "perl -p -e 's/\r$//' < "; //Formata a quebra de linha + + perlcmd.append(TEMP_TXT).append(" > ").append(this->filePath) + .append(" && rm -f ").append(TEMP_TXT); + system(perlcmd.c_str()); +} + void ExtratorTXT::setFilePath(char* path){ - filePath = (char*) path; - string command1 = "perl -p -e 's/\r$//' < "; - command1.append(filePath).append(" > ").append(INPUT_TXT); - system(command1.c_str()); + this->filePath = path; + encodingfiletoUTF8(); +} - string command2 = "iconv -f"; - command2.append(" $(file --mime-encoding -b ") - .append(INPUT_TXT).append(")") - .append(" -t utf-8 ").append(INPUT_TXT).append(" > ").append(filePath) - .append(" && rm -f ").append(INPUT_TXT); - system(command2.c_str()); +void ExtratorTXT::initialize(){ + file = new lavidlib::File(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("extratorTXT.cpp : Arquivo de texto não encontrado."); + throw ExtratorException("Falha ao abrir o arquivo de texto! Verifique se o mesmo existe."); + } + this->Start(); } bool ExtratorTXT::isFinished(){ @@ -77,14 +83,14 @@ bool ExtratorTXT::isFinished(){ void ExtratorTXT::Run(){ PRINTL(util::_INFO, "Extraindo Texto...\n"); int line_index = 0; - bool hasNext = true; + bool hasNext = true; string line; while (hasNext) { try{ line = bff_reader->readLine(); - if (line.length() > 0){ - notifyListeners((unsigned char*) line.c_str()); + if (line.length() > 0){ + notifyListeners((unsigned char*) line.c_str()); line_index++; } }catch (EOFException &ex){ @@ -93,7 +99,7 @@ void ExtratorTXT::Run(){ hasNext = false; }catch (...){ Logging::instance()->writeLog("extratorTXT.cpp : Erro durante a leitura do arquivo de texto."); - throw ExtratorException("Falha desconhecida na leitura do arquivo. Tente novamente."); + throw ExtratorException("Falha desconhecida na leitura do arquivo. Tente novamente."); } } finish = true; diff --git a/extrator/src/extratorVTT.cpp b/extrator/src/extratorVTT.cpp index c6d2458..9f28b22 100644 --- a/extrator/src/extratorVTT.cpp +++ b/extrator/src/extratorVTT.cpp @@ -9,21 +9,12 @@ ExtratorVTT::ExtratorVTT() { } ExtratorVTT::~ExtratorVTT() { - listeners->clear(); + 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); } @@ -31,7 +22,7 @@ void ExtratorVTT::addListener(ListenerSub* listener){ void ExtratorVTT::notifyListeners(unsigned char* subtitle, uint64_t pts) { for(list::iterator it = listeners->begin(); it != listeners->end(); it++){ (*it)->notifySubtitle(subtitle, pts); - } + } } void ExtratorVTT::notifyEndExtraction(int size) { @@ -41,12 +32,31 @@ void ExtratorVTT::notifyEndExtraction(int size) { } } +void ExtratorVTT::encodingfiletoUTF8() { + string iconvcmd = "iconv -f"; //Recodifica o arquivo para utf-8 + + iconvcmd.append(" $(file --mime-encoding -b ") + .append(this->filePath).append(") -t utf-8 ") + .append(this->filePath).append(" > ").append(TEMP_SRT); + system(iconvcmd.c_str()); + + string perlcmd = "perl -p -e 's/\r$//' < "; //Formata a quebra de linha + + perlcmd.append(TEMP_SRT).append(" > ").append(this->filePath) + .append(" && rm -f ").append(TEMP_SRT); + system(perlcmd.c_str()); +} + +void ExtratorVTT::setFilePath(char* path) { + this->filePath = path; + encodingfiletoUTF8(); +} + 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 : Arquivo de legenda não encontrado."); @@ -55,12 +65,24 @@ void ExtratorVTT::initialize() { this->Start(); } +bool ExtratorVTT::isFinished() { + return this->finish; +} + Subtitle* ExtratorVTT::nextCue() { string line; string cueText = ""; int target_pos; int64_t time_in; + file_io->seek(seekPos); + try{ + bff_reader = new BufferedReader(file_io); + }catch(Exception &ex){ + Logging::instance()->writeLog("extratorSRT.cpp : BufferedReader não inicializado."); + throw lavidlib::RuntimeException("O BufferedReader não foi inicializado."); + } + Subtitle* cue = new Subtitle(); try{ @@ -82,11 +104,13 @@ Subtitle* ExtratorVTT::nextCue() { cue->setSubtitleText(formatText(cueText)); seekPos += (int64_t) cueText.size() + SIZE_SCAPE; hasNextCue =false; + delete(bff_reader); return cue; } cue->setSubtitleText(formatText(cueText)); seekPos += (int64_t) cueText.size() + SIZE_SCAPE; + delete(bff_reader); return cue; } @@ -113,7 +137,7 @@ int64_t ExtratorVTT::str_to_time(string str_time) { int index = 0; int values [4]; // hh, mm, ss, ms - char * str = strtok(tokens, ":,."); + char * str = strtok(tokens, ":,."); while (str != NULL) { values[index] = atoi(str); str = strtok(NULL, ":,."); @@ -122,7 +146,7 @@ int64_t ExtratorVTT::str_to_time(string str_time) { delete(tokens); /* calculate time */ - ttime = /*hour to sec*/((((values[0] * 60) * 60) + + ttime = /*hour to sec*/((((values[0] * 60) * 60) + /*min to sec*/(values[1] * 60) +/*sec*/values[2])*1000) + values[3]; return ttime; @@ -130,7 +154,7 @@ int64_t ExtratorVTT::str_to_time(string str_time) { void ExtratorVTT::Run() { PRINTL(util::_INFO, "Extraindo Legendas...\n"); - + int sub_index = 0; string cue_text = ""; while(hasNextCue){ @@ -144,4 +168,4 @@ void ExtratorVTT::Run() { notifyListeners((unsigned char*)"ARQUIVO_INVALIDO", 0); this->finish = true; notifyEndExtraction(sub_index); -} \ No newline at end of file +} diff --git a/extrator/src/include/extrator.h b/extrator/src/include/extrator.h index 24b5c86..29dd0b2 100644 --- a/extrator/src/include/extrator.h +++ b/extrator/src/include/extrator.h @@ -31,7 +31,7 @@ protected: File* file; FileIO* file_io; BufferedReader* bff_reader; - + public: enum ExtratorType {SRT, TXT, VTT}; @@ -47,14 +47,17 @@ public: */ virtual void setFilePath(char* path) = 0; + /** Recodifica o arquivo para utf-8 */ + virtual void encodingfiletoUTF8() = 0; + /** Inicializa a extração do conteúdo. */ virtual void initialize() = 0; - /** Indica o fim do processo de extração. + /** Indica o fim do processo de extração. * * \return O status do do processo. */ virtual bool isFinished() = 0; }; -#endif /* EXTRATOR_H */ \ No newline at end of file +#endif /* EXTRATOR_H */ diff --git a/extrator/src/include/extratorSRT.h b/extrator/src/include/extratorSRT.h index 0c466a7..43b1c25 100644 --- a/extrator/src/include/extratorSRT.h +++ b/extrator/src/include/extratorSRT.h @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include "jthread.h" #include "extrator.h" @@ -23,7 +23,7 @@ #define TARGET_TIME "-->" #define LESS_THAN "<" #define MORE_THAN ">" -#define INPUT_SRT "vlibras_user/inputSRT.srt" +#define TEMP_SRT "/tmp/tempsrt.srt" //#define MAX_LINE 1024 using namespace jthread; @@ -38,14 +38,14 @@ using namespace sndesc; * \headerfile extrator/src/include/extratorSRT.h */ class ExtratorSRT: public Extrator, public Thread { - + public: /** Construtor */ ExtratorSRT(); /** Destrutor */ ~ExtratorSRT(); - + /** Adiciona ouvintes do extratorSRT. * * \param listener O ouvinte a ser registrado. @@ -77,12 +77,12 @@ public: */ void initialize(); - /** Indica o fim do processo de extração das legendas. + /** Indica o fim do processo de extração das legendas. * * \return O status do do processo. */ bool isFinished(); - + /** Retorna o conteúdo da legenda extraída. * * \return Referência para um objeto Subtitle. @@ -98,17 +98,18 @@ public: /** Este método é chamado quando a Thread for iniciada. */ void Run(); - + private: list *listeners; - Subtitle *subtitle; + Subtitle *subtitle; int64_t seek_pos; bool hasNextSub; - + + void encodingfiletoUTF8(); string formatText(string line); uint64_t calcula_pts(double msec); - int64_t str_to_time(std::string str_time); + int64_t str_to_time(std::string str_time); }; -#endif /* EXTRATORSRT_H */ \ No newline at end of file +#endif /* EXTRATORSRT_H */ diff --git a/extrator/src/include/extratorTXT.h b/extrator/src/include/extratorTXT.h index f588e1d..7b449b0 100644 --- a/extrator/src/include/extratorTXT.h +++ b/extrator/src/include/extratorTXT.h @@ -14,7 +14,7 @@ #include "listenerTXT.h" #include "extratorException.h" -#define INPUT_TXT "vlibras_user/inputTXT.txt" +#define TEMP_TXT "/tmp/temptxt.txt" using namespace jthread; using namespace std; @@ -27,7 +27,7 @@ using namespace std; * \headerfile extrator/src/include/extratorTXT.h */ class ExtratorTXT: public Extrator, public Thread { - + public: /** Construtor */ ExtratorTXT(); @@ -65,18 +65,18 @@ public: */ void initialize(); - /** Indica o fim do processo de extração do texto. + /** Indica o fim do processo de extração do texto. * * \return O status do do processo. */ bool isFinished(); - + /** Este método é chamado quando a Thread for iniciada. */ void Run(); private: - list *listeners; + list *listeners; + void encodingfiletoUTF8(); }; #endif /* EXTRATORTXT_H */ - diff --git a/extrator/src/include/extratorVTT.h b/extrator/src/include/extratorVTT.h index 22a08d8..28ee0bf 100644 --- a/extrator/src/include/extratorVTT.h +++ b/extrator/src/include/extratorVTT.h @@ -16,6 +16,7 @@ #define LESS_THAN "<" #define MORE_THAN ">" #define SIZE_SCAPE 1 +#define TEMP_SRT "/tmp/tempsrt.srt" using namespace std; using namespace sndesc; @@ -45,8 +46,9 @@ private: void notifyListeners(unsigned char* subtitle, uint64_t pts); Subtitle* nextCue(); + void encodingfiletoUTF8(); string formatText(string line); int64_t str_to_time(string str_time); }; -#endif /* EXTRATORVTT_H */ \ No newline at end of file +#endif /* EXTRATORVTT_H */ diff --git a/log/log b/log/log index e69de29..b28b04f 100644 --- a/log/log +++ b/log/log @@ -0,0 +1,3 @@ + + + diff --git a/renderer/src/renderer.cpp b/renderer/src/renderer.cpp index df701ed..fd53505 100644 --- a/renderer/src/renderer.cpp +++ b/renderer/src/renderer.cpp @@ -1,4 +1,5 @@ #include "renderer.h" +#include Renderer::Renderer(char* path_Contents, char* id) { this->pathOutVideo = path_Contents; diff --git a/servico/src/serviceWindowGenerationFromSubtitle.cpp b/servico/src/serviceWindowGenerationFromSubtitle.cpp index 17c16af..b14b038 100644 --- a/servico/src/serviceWindowGenerationFromSubtitle.cpp +++ b/servico/src/serviceWindowGenerationFromSubtitle.cpp @@ -186,10 +186,17 @@ void ServiceWindowGenerationFromSubtitle::notifyTranslator(unsigned char* subtit void ServiceWindowGenerationFromSubtitle::notifySubtitle(unsigned char *subtitle, uint64_t pts){ addPTS(pts); - if (sub_language == 1) + if (sub_language == 1) { notifyTranslator(subtitle); - else{ - string glosa(reinterpret_cast(subtitle)); + }else{ + string subt(reinterpret_cast(subtitle)); + + locale loc; + string glosa = ""; + for (string::size_type i=0; i< subt.length(); ++i) { + glosa += std::toupper(subt[i], loc); + } + cout << glosa << endl; notifyRenderer(glosa); } } diff --git a/servico/src/serviceWindowGenerationFromText.cpp b/servico/src/serviceWindowGenerationFromText.cpp index 0cc2f5e..f2e863d 100644 --- a/servico/src/serviceWindowGenerationFromText.cpp +++ b/servico/src/serviceWindowGenerationFromText.cpp @@ -91,14 +91,20 @@ void ServiceWindowGenerationFromText::setSizeOfSubtitles(int sub_size) { } void ServiceWindowGenerationFromText::notifyTranslator(unsigned char* text) { - tradutor->traduz(text); + tradutor->traduz(text); } void ServiceWindowGenerationFromText::notifyLine(unsigned char* line){ if (sub_language == 1) notifyTranslator(line); else{ - string glosa(reinterpret_cast(line)); + string text(reinterpret_cast(line)); + + locale loc; + string glosa = ""; + for (string::size_type i=0; i< text.length(); ++i) { + glosa += std::toupper(text[i], loc); + } notifyRenderer(glosa); } } @@ -137,7 +143,7 @@ bool ServiceWindowGenerationFromText::isFinished() { void ServiceWindowGenerationFromText::initialize() { PRINTL(util::_DEBUG, "Service Text Initialize.\n"); setPathLibras(); - extratorTXT = (ExtratorTXT*) extrator_factory->getExtrator(Extrator::TXT); + extratorTXT = (ExtratorTXT*) extrator_factory->getExtrator(Extrator::TXT); extratorTXT->addListener(this); extratorTXT->setFilePath(path_input); @@ -148,9 +154,9 @@ void ServiceWindowGenerationFromText::initialize() { renderer = new Renderer(this->path_libras, this->user_id); renderer->addListener(this); - + try{ - extratorTXT->initialize(); + extratorTXT->initialize(); }catch(ExtratorException ex){ throw ServiceException(ex.getMessage().c_str()); }catch(RuntimeException &ex){ @@ -164,4 +170,4 @@ void ServiceWindowGenerationFromText::Run(){ usleep(200000); } finish = true; -} \ No newline at end of file +} -- libgit2 0.21.2