From be3b26fed993dc9a21c1959a1c80d275b6a084b8 Mon Sep 17 00:00:00 2001 From: Wesnydy Ribeiro Date: Mon, 2 Feb 2015 10:16:34 -0200 Subject: [PATCH] Refatoração do componente ExtratorSRT --- Makefile | 4 +--- extratorSRT/src/extratorSRT.cpp | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- extratorSRT/src/include/extratorSRT.h | 40 +++++++++++++++++++++++++++++++--------- extratorSRT/src/include/extratorSRT_exception.h | 19 +++++++++---------- extratorSRT/src/include/reader_exception.h | 28 ---------------------------- extratorSRT/src/include/reader_srt.h | 64 ---------------------------------------------------------------- extratorSRT/src/reader_exception.cpp | 22 ---------------------- extratorSRT/src/reader_srt.cpp | 128 -------------------------------------------------------------------------------------------------------------------------------- main.cpp | 19 +++++++++++-------- servico/src/serviceWindowGenerationFromSRT.cpp | 2 +- 10 files changed, 158 insertions(+), 283 deletions(-) delete mode 100644 extratorSRT/src/include/reader_exception.h delete mode 100644 extratorSRT/src/include/reader_srt.h delete mode 100644 extratorSRT/src/reader_exception.cpp delete mode 100644 extratorSRT/src/reader_srt.cpp diff --git a/Makefile b/Makefile index 1918380..608d768 100644 --- a/Makefile +++ b/Makefile @@ -24,10 +24,8 @@ ouvinteTradutor.o extratorSRTObjs = \ extratorSRT.o \ -reader_exception.o \ extratorSRT_exception.o \ -reader_srt.o \ -subtitle.o +subtitle.o \ extratorTXTObjs = \ extratorTXT.o \ diff --git a/extratorSRT/src/extratorSRT.cpp b/extratorSRT/src/extratorSRT.cpp index 55c5226..f2a7be8 100644 --- a/extratorSRT/src/extratorSRT.cpp +++ b/extratorSRT/src/extratorSRT.cpp @@ -1,3 +1,5 @@ + + #include "extratorSRT.h" using namespace std; @@ -7,24 +9,31 @@ ExtratorSRT::ExtratorSRT(){ pcr_base = 0; finish = false; hasPCRBase = false; + seek_pos = 0; + hasNextSub = true; DPRINTF("Done!\n"); } ExtratorSRT::~ExtratorSRT(){ listeners->clear(); delete listeners; - //delete reader; + if (bff_reader) delete bff_reader; + if (file_io) delete file_io; DDDPRINTF("Extractor STR finalized!\n"); } -void ExtratorSRT::initialize(){ +void ExtratorSRT::initialize(){ + + file = new lavidlib::File(filepath); + try{ - reader = new ReaderSRT(filepath, FileIO::MODE_READ); - }catch(ReaderException ex){ + file_io = new lavidlib::FileIO(file->getPath(), FileIO::MODE_READ); + }catch(Exception ex){ finish = true; - Util::Logger::Instance()->writeLog((char*) ex.getMessage().c_str()); - throw ExtratorSrtException("Falha ao abrir o arquivo de legenda! Verifique se o mesmo existe."); + Util::Logger::Instance()->writeLog((char*) "[ERRO: extratorSRT.cpp] Arquivo de texto não encontrado."); + throw ExtratorSrtException("Falha ao abrir o arquivo de texto! Verifique se o mesmo existe.\n"); } + this->Start(); } @@ -57,15 +66,19 @@ bool ExtratorSRT::isFinished(){ return finish; } +bool ExtratorSRT::hasNextSubtitle() { + return hasNextSub; +} + void ExtratorSRT::Run(){ printf("\n"); - DDPRINTF("[AGUARDE] Extraindo SRT...\n") + DDPRINTF("[AGUARDE] Extraindo SRT...\n"); int sub_index = 0; string sub_text = ""; - while(reader->hasNextSubtitle()){ - subtitle = reader->next(); + while(hasNextSubtitle()){ + subtitle = next(); sub_text = subtitle->getSubtitleText(); notifyListeners((unsigned char*)sub_text.c_str(), calcula_pts((double) subtitle->getTimeIn())); cout << " . "; @@ -77,6 +90,88 @@ void ExtratorSRT::Run(){ notifyEndExtraction(sub_index); } +Subtitle* ExtratorSRT::next() { + + if (seek_pos >= file_io->getSize()) + throw ExtratorSrtException("[ERRO: reader_srt.cpp] Esse arquivo já foi lido."); + + file_io->seek(seek_pos); + try{ + bff_reader = new BufferedReader(file_io); + }catch(Exception &ex){ + throw ExtratorSrtException("[ERRO: reader_srt.cpp] O BufferedReader não foi inicializado."); + } + + Subtitle* sub = new Subtitle(); + std::string line = ""; + std::string text_sub = ""; + + try { + /* ID */ + int id = 0; + line = bff_reader->readLine(); + seek_pos += (int64_t) line.size() + SIZE_CSCAPE; + id = atoi(line.c_str()); + sub->setID(id); + + /* TimeIn and TimeOut */ + int64_t t_in = 0, t_out = 0; + line = bff_reader->readLine(); + seek_pos += (int64_t) line.size() + SIZE_CSCAPE; + + int target_pos = line.find(TARGET_TIME); + t_in = str_to_time(line.substr(0, target_pos)); + sub->setTimeIn(t_in); + t_out = str_to_time(line.substr(target_pos + strlen(TARGET_TIME)+1, line.size())); + sub->setTimeOut(t_out); + + /* Text: read until line be empty */ + while ((line = bff_reader->readLine()).size() > 0) { + text_sub += line; + text_sub.append(" "); + } + seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE; + + } catch (lavidlib::EOFException &ex) { + sub->setSubtitleText(text_sub); + sub->setStatusOfReady(true); + //delete(bff_reader); + seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE; + hasNextSub = false; + return sub; + } + sub->setSubtitleText(text_sub); + sub->setStatusOfReady(true); + delete(bff_reader); + return sub; + + } + + +int64_t ExtratorSRT::str_to_time(std::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 ExtratorSRT::notifyPCRBase(uint64_t pcrbase){ //DDPRINTF("PCRBase = %ld\n", pcrbase); @@ -86,4 +181,4 @@ void ExtratorSRT::notifyPCRBase(uint64_t pcrbase){ uint64_t ExtratorSRT::calcula_pts(double msec) { return (uint64_t)(pcr_base + ((msec/1000) * 90000.0)); -} +} \ No newline at end of file diff --git a/extratorSRT/src/include/extratorSRT.h b/extratorSRT/src/include/extratorSRT.h index e70477b..f76a075 100644 --- a/extratorSRT/src/include/extratorSRT.h +++ b/extratorSRT/src/include/extratorSRT.h @@ -8,17 +8,29 @@ #ifndef EXTRATORSRT_H #define EXTRATORSRT_H -#include "listenerSRT.h" -#include "reader_srt.h" -#include "subtitle.h" -#include "listenerMonitorPCRBase.h" +//#define MAX_LINE 1024 +#define SIZE_CSCAPE 1 +#define TARGET_TIME "-->" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "jthread.h" #include "dprintf.h" +#include "reader.h" #include "logger.h" -#include +#include "subtitle.h" +#include "listenerSRT.h" +#include "listenerMonitorPCRBase.h" #include "extratorSRT_exception.h" - using namespace jthread; using namespace std; using namespace sndesc; @@ -34,10 +46,14 @@ public: void notifyListeners(unsigned char* subtitle, int64_t pts); void notifyEndExtraction(int sub_size); + bool hasNextSubtitle(); void setFilePath(char* path); bool isFinished(); void initialize(); void Run(); + + /* @Override: reader.h */ + Subtitle* next(); void notifyPCRBase(uint64_t pcrbase); @@ -46,12 +62,18 @@ private: list *listeners; char* filepath; bool finish; + File* file; + FileIO *file_io; + BufferedReader *bff_reader; + Subtitle * subtitle; + uint64_t pcr_base; + int64_t seek_pos; bool hasPCRBase; - ReaderSRT * reader; - Subtitle * subtitle; - + bool hasNextSub; + uint64_t calcula_pts(double msec); + int64_t str_to_time(std::string str_time); }; diff --git a/extratorSRT/src/include/extratorSRT_exception.h b/extratorSRT/src/include/extratorSRT_exception.h index dd503d5..d53a926 100644 --- a/extratorSRT/src/include/extratorSRT_exception.h +++ b/extratorSRT/src/include/extratorSRT_exception.h @@ -9,19 +9,18 @@ * * **************************************************************************/ - #ifndef EXTRATORSRTEXCEPTION_H - #define EXTRATORSRTEXCEPTION_H +#ifndef EXTRATORSRTEXCEPTION_H +#define EXTRATORSRTEXCEPTION_H - #include +#include - using namespace lavidlib; +using namespace lavidlib; - - class ExtratorSrtException : public RuntimeException { - public: - ExtratorSrtException(const std::string message); - ExtratorSrtException(const char* message); + class ExtratorSrtException : public RuntimeException { + public: + ExtratorSrtException(const std::string message); + ExtratorSrtException(const char* message); }; - #endif /* EXTRATORSRTEXCEPTION_H */ \ No newline at end of file +#endif /* EXTRATORSRTEXCEPTION_H */ \ No newline at end of file diff --git a/extratorSRT/src/include/reader_exception.h b/extratorSRT/src/include/reader_exception.h deleted file mode 100644 index 713fe3b..0000000 --- a/extratorSRT/src/include/reader_exception.h +++ /dev/null @@ -1,28 +0,0 @@ -/*************************************************************************** - * Universidade Federal da Paraíba * - * Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital * - * * - * Centro de Informática - UFPB - Campus I * - * João Pessoa - PB - Brasil * - * * - * Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br) * - * Date: Tue Jan 14 11:09:41 BRT 2014 * - * * - **************************************************************************/ - - #ifndef READEREXCEPTION_H - #define READEREXCEPTION_H - - #include - - using namespace lavidlib; - - namespace sndesc { - class ReaderException : public RuntimeException { - public: - ReaderException(const std::string message); - ReaderException(const char* message); - }; - } - - #endif /* READEREXCEPTION_H */ \ No newline at end of file diff --git a/extratorSRT/src/include/reader_srt.h b/extratorSRT/src/include/reader_srt.h deleted file mode 100644 index e221b65..0000000 --- a/extratorSRT/src/include/reader_srt.h +++ /dev/null @@ -1,64 +0,0 @@ -/*************************************************************************** - * Universidade Federal da Paraíba * - * Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital * - * * - * Centro de Informática - UFPB - Campus I * - * João Pessoa - PB - Brasil * - * * - * Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br) * - * Date: Qui Out 24 18:25:51 BRT 2013 * - * * - **************************************************************************/ - - #ifndef READERSRT_H - #define READERSRT_H - - #define MAX_LINE 1024 - #define SIZE_CSCAPE 1 - #define TARGET_TIME "-->" - - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include - #include "reader.h" - #include "reader_exception.h" - #include "subtitle.h" - - using namespace lavidlib; - - namespace sndesc { - - class ReaderSRT : public Reader { - - public: - - ReaderSRT(std::string _filepath, FileIO::open_flags_t _mode); - ~ReaderSRT(); - bool hasNextSubtitle(); - - /* @Override: reader.h */ - Subtitle* next(); - - private: - - File *file; - FileIO *file_io; - BufferedReader *bff_reader; - int64_t seek_pos; - bool hasNextSub; - - int64_t str_to_time(std::string str_time); - - }; - - } - - #endif // READERSRT_H \ No newline at end of file diff --git a/extratorSRT/src/reader_exception.cpp b/extratorSRT/src/reader_exception.cpp deleted file mode 100644 index 1231b7c..0000000 --- a/extratorSRT/src/reader_exception.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/*************************************************************************** - * Universidade Federal da Paraíba * - * Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital * - * * - * Centro de Informática - UFPB - Campus I * - * João Pessoa - PB - Brasil * - * * - * Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br) * - * Date: Tue Jan 14 11:15:37 BRT 2014 * - * * - **************************************************************************/ - - #include "reader_exception.h" - - namespace sndesc { - ReaderException::ReaderException(const std::string message) - : RuntimeException(message) - { /* TODO */ } - ReaderException::ReaderException(const char* message) - : RuntimeException(message) - { /* TODO */ } - } \ No newline at end of file diff --git a/extratorSRT/src/reader_srt.cpp b/extratorSRT/src/reader_srt.cpp deleted file mode 100644 index 769f1d9..0000000 --- a/extratorSRT/src/reader_srt.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/*************************************************************************** - * Universidade Federal da Paraíba * - * Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital * - * * - * Centro de Informática - UFPB - Campus I * - * João Pessoa - PB - Brasil * - * * - * Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br) * - * Date: Qui Out 24 22:26:35 BRT 2013 * - * * - **************************************************************************/ - - #include "reader_srt.h" - - namespace sndesc { - - - ReaderSRT::ReaderSRT(std::string _filepath, FileIO::open_flags_t _mode) { - - file = new lavidlib::File(_filepath); - try{ - file_io = new lavidlib::FileIO(file->getPath(), _mode); - }catch(lavidlib::IOException){ - throw ReaderException("[ERRO: reader_srt.cpp] Arquivo de legenda não encontrado."); - } - seek_pos = 0; - hasNextSub = true; - - } - - - ReaderSRT::~ReaderSRT() { - - if (bff_reader != NULL) delete bff_reader; - if (file_io != NULL) delete file_io; - - } - - - Subtitle* ReaderSRT::next() { - - if (seek_pos >= file_io->getSize()) - throw ReaderException("[ERRO: reader_srt.cpp] Esse arquivo já foi lido."); - - file_io->seek(seek_pos); - try{ - bff_reader = new BufferedReader(file_io); - }catch(lavidlib::IOException){ - throw ReaderException("[ERRO: reader_srt.cpp] O BufferedReader não foi inicializado."); - } - - Subtitle* sub = new Subtitle(); - std::string line = ""; - std::string text_sub = ""; - - try { - /* ID */ - int id = 0; - line = bff_reader->readLine(); - seek_pos += (int64_t) line.size() + SIZE_CSCAPE; - id = atoi(line.c_str()); - sub->setID(id); - - /* TimeIn and TimeOut */ - int64_t t_in = 0, t_out = 0; - line = bff_reader->readLine(); - seek_pos += (int64_t) line.size() + SIZE_CSCAPE; - - int target_pos = line.find(TARGET_TIME); - t_in = str_to_time(line.substr(0, target_pos)); - sub->setTimeIn(t_in); - t_out = str_to_time(line.substr(target_pos + strlen(TARGET_TIME)+1, line.size())); - sub->setTimeOut(t_out); - - /* Text: read until line be empty */ - while ((line = bff_reader->readLine()).size() > 0) { - text_sub += line; - text_sub.append(" "); - } - seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE; - - } catch (lavidlib::EOFException &ex) { - sub->setSubtitleText(text_sub); - sub->setStatusOfReady(true); - delete(bff_reader); - seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE; - hasNextSub = false; - return sub; - } - - sub->setSubtitleText(text_sub); - sub->setStatusOfReady(true); - delete(bff_reader); - return sub; - - } - - - bool ReaderSRT::hasNextSubtitle() { - return hasNextSub; - } - - - int64_t ReaderSRT::str_to_time(std::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; - - } - - } \ No newline at end of file diff --git a/main.cpp b/main.cpp index de63242..f9102fd 100644 --- a/main.cpp +++ b/main.cpp @@ -278,7 +278,7 @@ void serviceOnlySRT(char* path_file, char* transparency, char* id){ service_srt = new ServiceWindowGenerationFromSRT(path_file, (int) atoi(transparency), id, 5); try{ service_srt->initialize(); - }catch(ServiceException ex){ + }catch(ServiceException ex){ fail(ex.getMessage()); hasFailed(); return; @@ -356,22 +356,25 @@ void hasInvalid(){ //Help do programa, explicando todos os parâmetros existentes... void help() { - cout <<"\n####################################################################################\n" + cout <<"\n##################################################################################\n" <<"# SERVICE_TYPE: 1 - means Closed Caption - doesn't use INPUT_SRT #\n" <<"# 2 - means With Subtitles (SRT) - requires INPUT_SRT #\n" <<"# 3 - means Recognize - requires INPUT_VIDEO #\n" <<"# 4 - means Text - requires INPUT_FILE_TEXT #\n" <<"# 5 - means Subtitles ONLY (SRT) - requires INPUT_SRT #\n" - <<"#----------------------------------------------------------------------------------#\n\n" - /*<<"# INPUT_VIDEO: Path of the video file #\n" + <<"# 6 - means Audio - requires INPUT_AUDIO #\n" + <<"####################################################################################\n\n" + /*<<"# INPUT_VIDEO: Path of the video file #\n" <<"#----------------------------------------------------------------------------------#\n" <<"# INPUT_SRT: Path of the SRT file (only for SERVICE_TYPE = 2) #\n" <<"#----------------------------------------------------------------------------------#\n" - <<"# INPUT_FILE_TEXT: Path of the text file (doesn't use INPUT_VIDEO and INPUT_SRT) #\n" + <<"# INPUT_FILE_TEXT: Path of the text file (doesn't use INPUT_VIDEO and INPUT_SRT) #\n" + <<"#----------------------------------------------------------------------------------#\n" + <<"# INPUT_AUDIO: Path of the audio file" #\n <<"#----------------------------------------------------------------------------------#\n" - <<"# LANGUAGE: 1 - means Portuguese #\n" - <<"# 2 - means Glosa #\n" - <<"#----------------------------------------------------------------------------------#\n" + <<"# LANGUAGE: 1 - means Portuguese #\n" + <<"# 2 - means Glosa #\n" + <<"#----------------------------------------------------------------------------------#\n" <<"# POSITION: 1 - means TOP_LEFT #\n" <<"# 2 - means TOP_RIGHT #\n" <<"# 3 - means BOTTOM_RIGHT #\n" diff --git a/servico/src/serviceWindowGenerationFromSRT.cpp b/servico/src/serviceWindowGenerationFromSRT.cpp index 685fb89..fbd6215 100644 --- a/servico/src/serviceWindowGenerationFromSRT.cpp +++ b/servico/src/serviceWindowGenerationFromSRT.cpp @@ -4,7 +4,7 @@ using namespace std; //Construtor Service 2 ServiceWindowGenerationFromSRT::ServiceWindowGenerationFromSRT( - char* path_video, char* path_srt, int sublanguage, int position, int size, int transparency, char* id, int serviceType) { + char* path_video, char* path_srt, int sublanguage, int position, int size, int transparency, char* id, int serviceType) { setPathInput(path_video); setPathSRT(path_srt); setPosition(position); -- libgit2 0.21.2