Commit be3b26fed993dc9a21c1959a1c80d275b6a084b8

Authored by Wesnydy Ribeiro
1 parent c11fbf5f
Exists in master and in 1 other branch devel

Refatoração do componente ExtratorSRT

Makefile
... ... @@ -24,10 +24,8 @@ ouvinteTradutor.o
24 24  
25 25 extratorSRTObjs = \
26 26 extratorSRT.o \
27   -reader_exception.o \
28 27 extratorSRT_exception.o \
29   -reader_srt.o \
30   -subtitle.o
  28 +subtitle.o \
31 29  
32 30 extratorTXTObjs = \
33 31 extratorTXT.o \
... ...
extratorSRT/src/extratorSRT.cpp
  1 +
  2 +
1 3 #include "extratorSRT.h"
2 4  
3 5 using namespace std;
... ... @@ -7,24 +9,31 @@ ExtratorSRT::ExtratorSRT(){
7 9 pcr_base = 0;
8 10 finish = false;
9 11 hasPCRBase = false;
  12 + seek_pos = 0;
  13 + hasNextSub = true;
10 14 DPRINTF("Done!\n");
11 15 }
12 16  
13 17 ExtratorSRT::~ExtratorSRT(){
14 18 listeners->clear();
15 19 delete listeners;
16   - //delete reader;
  20 + if (bff_reader) delete bff_reader;
  21 + if (file_io) delete file_io;
17 22 DDDPRINTF("Extractor STR finalized!\n");
18 23 }
19 24  
20   -void ExtratorSRT::initialize(){
  25 +void ExtratorSRT::initialize(){
  26 +
  27 + file = new lavidlib::File(filepath);
  28 +
21 29 try{
22   - reader = new ReaderSRT(filepath, FileIO::MODE_READ);
23   - }catch(ReaderException ex){
  30 + file_io = new lavidlib::FileIO(file->getPath(), FileIO::MODE_READ);
  31 + }catch(Exception ex){
24 32 finish = true;
25   - Util::Logger::Instance()->writeLog((char*) ex.getMessage().c_str());
26   - throw ExtratorSrtException("Falha ao abrir o arquivo de legenda! Verifique se o mesmo existe.");
  33 + Util::Logger::Instance()->writeLog((char*) "[ERRO: extratorSRT.cpp] Arquivo de texto não encontrado.");
  34 + throw ExtratorSrtException("Falha ao abrir o arquivo de texto! Verifique se o mesmo existe.\n");
27 35 }
  36 +
28 37 this->Start();
29 38 }
30 39  
... ... @@ -57,15 +66,19 @@ bool ExtratorSRT::isFinished(){
57 66 return finish;
58 67 }
59 68  
  69 +bool ExtratorSRT::hasNextSubtitle() {
  70 + return hasNextSub;
  71 +}
  72 +
60 73 void ExtratorSRT::Run(){
61 74 printf("\n");
62   - DDPRINTF("[AGUARDE] Extraindo SRT...\n")
  75 + DDPRINTF("[AGUARDE] Extraindo SRT...\n");
63 76  
64 77 int sub_index = 0;
65 78 string sub_text = "";
66 79  
67   - while(reader->hasNextSubtitle()){
68   - subtitle = reader->next();
  80 + while(hasNextSubtitle()){
  81 + subtitle = next();
69 82 sub_text = subtitle->getSubtitleText();
70 83 notifyListeners((unsigned char*)sub_text.c_str(), calcula_pts((double) subtitle->getTimeIn()));
71 84 cout << " . ";
... ... @@ -77,6 +90,88 @@ void ExtratorSRT::Run(){
77 90 notifyEndExtraction(sub_index);
78 91 }
79 92  
  93 +Subtitle* ExtratorSRT::next() {
  94 +
  95 + if (seek_pos >= file_io->getSize())
  96 + throw ExtratorSrtException("[ERRO: reader_srt.cpp] Esse arquivo já foi lido.");
  97 +
  98 + file_io->seek(seek_pos);
  99 + try{
  100 + bff_reader = new BufferedReader(file_io);
  101 + }catch(Exception &ex){
  102 + throw ExtratorSrtException("[ERRO: reader_srt.cpp] O BufferedReader não foi inicializado.");
  103 + }
  104 +
  105 + Subtitle* sub = new Subtitle();
  106 + std::string line = "";
  107 + std::string text_sub = "";
  108 +
  109 + try {
  110 + /* ID */
  111 + int id = 0;
  112 + line = bff_reader->readLine();
  113 + seek_pos += (int64_t) line.size() + SIZE_CSCAPE;
  114 + id = atoi(line.c_str());
  115 + sub->setID(id);
  116 +
  117 + /* TimeIn and TimeOut */
  118 + int64_t t_in = 0, t_out = 0;
  119 + line = bff_reader->readLine();
  120 + seek_pos += (int64_t) line.size() + SIZE_CSCAPE;
  121 +
  122 + int target_pos = line.find(TARGET_TIME);
  123 + t_in = str_to_time(line.substr(0, target_pos));
  124 + sub->setTimeIn(t_in);
  125 + t_out = str_to_time(line.substr(target_pos + strlen(TARGET_TIME)+1, line.size()));
  126 + sub->setTimeOut(t_out);
  127 +
  128 + /* Text: read until line be empty */
  129 + while ((line = bff_reader->readLine()).size() > 0) {
  130 + text_sub += line;
  131 + text_sub.append(" ");
  132 + }
  133 + seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE;
  134 +
  135 + } catch (lavidlib::EOFException &ex) {
  136 + sub->setSubtitleText(text_sub);
  137 + sub->setStatusOfReady(true);
  138 + //delete(bff_reader);
  139 + seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE;
  140 + hasNextSub = false;
  141 + return sub;
  142 + }
  143 + sub->setSubtitleText(text_sub);
  144 + sub->setStatusOfReady(true);
  145 + delete(bff_reader);
  146 + return sub;
  147 +
  148 + }
  149 +
  150 +
  151 +int64_t ExtratorSRT::str_to_time(std::string str_time) {
  152 +
  153 + int64_t ttime = 0;
  154 + char* tokens = new char[4]; // hh, mm, ss, ms
  155 + strcpy(tokens, (char*)str_time.c_str());
  156 +
  157 + int index = 0;
  158 + int values [4]; // hh, mm, ss, ms
  159 + char * str = strtok(tokens, ":,");
  160 + while (str != NULL) {
  161 + values[index] = atoi(str);
  162 + str = strtok(NULL, ":,");
  163 + index++;
  164 + }
  165 + delete(tokens);
  166 +
  167 + /* calculate time */
  168 + ttime = /*hour to sec*/((((values[0] * 60) * 60) +
  169 + /*min to sec*/(values[1] * 60) +/*sec*/values[2])*1000) + values[3];
  170 +
  171 + return ttime;
  172 +
  173 + }
  174 +
80 175  
81 176 void ExtratorSRT::notifyPCRBase(uint64_t pcrbase){
82 177 //DDPRINTF("PCRBase = %ld\n", pcrbase);
... ... @@ -86,4 +181,4 @@ void ExtratorSRT::notifyPCRBase(uint64_t pcrbase){
86 181  
87 182 uint64_t ExtratorSRT::calcula_pts(double msec) {
88 183 return (uint64_t)(pcr_base + ((msec/1000) * 90000.0));
89 184 -}
  185 +}
90 186 \ No newline at end of file
... ...
extratorSRT/src/include/extratorSRT.h
... ... @@ -8,17 +8,29 @@
8 8 #ifndef EXTRATORSRT_H
9 9 #define EXTRATORSRT_H
10 10  
11   -#include "listenerSRT.h"
12   -#include "reader_srt.h"
13   -#include "subtitle.h"
14   -#include "listenerMonitorPCRBase.h"
  11 +//#define MAX_LINE 1024
  12 +#define SIZE_CSCAPE 1
  13 +#define TARGET_TIME "-->"
  14 +
  15 +#include <string.h>
  16 +#include <stdlib.h>
  17 +#include <fstream>
  18 +#include <stdio.h>
  19 +#include <list>
  20 +#include <lavidlib/io/File.h>
  21 +#include <lavidlib/io/FileIO.h>
  22 +#include <lavidlib/io/BufferedReader.h>
  23 +#include <lavidlib/io/IOException.h>
  24 +#include <lavidlib/io/EOFException.h>
15 25 #include "jthread.h"
16 26 #include "dprintf.h"
  27 +#include "reader.h"
17 28 #include "logger.h"
18   -#include <lavidlib/io/FileIO.h>
  29 +#include "subtitle.h"
  30 +#include "listenerSRT.h"
  31 +#include "listenerMonitorPCRBase.h"
19 32 #include "extratorSRT_exception.h"
20 33  
21   -
22 34 using namespace jthread;
23 35 using namespace std;
24 36 using namespace sndesc;
... ... @@ -34,10 +46,14 @@ public:
34 46 void notifyListeners(unsigned char* subtitle, int64_t pts);
35 47 void notifyEndExtraction(int sub_size);
36 48  
  49 + bool hasNextSubtitle();
37 50 void setFilePath(char* path);
38 51 bool isFinished();
39 52 void initialize();
40 53 void Run();
  54 +
  55 + /* @Override: reader.h */
  56 + Subtitle* next();
41 57  
42 58 void notifyPCRBase(uint64_t pcrbase);
43 59  
... ... @@ -46,12 +62,18 @@ private:
46 62 list<ListenerSRT*> *listeners;
47 63 char* filepath;
48 64 bool finish;
  65 + File* file;
  66 + FileIO *file_io;
  67 + BufferedReader *bff_reader;
  68 + Subtitle * subtitle;
  69 +
49 70 uint64_t pcr_base;
  71 + int64_t seek_pos;
50 72 bool hasPCRBase;
51   - ReaderSRT * reader;
52   - Subtitle * subtitle;
53   -
  73 + bool hasNextSub;
  74 +
54 75 uint64_t calcula_pts(double msec);
  76 + int64_t str_to_time(std::string str_time);
55 77  
56 78 };
57 79  
... ...
extratorSRT/src/include/extratorSRT_exception.h
... ... @@ -9,19 +9,18 @@
9 9 * *
10 10 **************************************************************************/
11 11  
12   - #ifndef EXTRATORSRTEXCEPTION_H
13   - #define EXTRATORSRTEXCEPTION_H
  12 +#ifndef EXTRATORSRTEXCEPTION_H
  13 +#define EXTRATORSRTEXCEPTION_H
14 14  
15   - #include <lavidlib/base/RuntimeException.h>
  15 +#include <lavidlib/base/RuntimeException.h>
16 16  
17   - using namespace lavidlib;
  17 +using namespace lavidlib;
18 18  
19   -
20   - class ExtratorSrtException : public RuntimeException {
21   - public:
22   - ExtratorSrtException(const std::string message);
23   - ExtratorSrtException(const char* message);
  19 + class ExtratorSrtException : public RuntimeException {
  20 + public:
  21 + ExtratorSrtException(const std::string message);
  22 + ExtratorSrtException(const char* message);
24 23 };
25 24  
26 25  
27   - #endif /* EXTRATORSRTEXCEPTION_H */
28 26 \ No newline at end of file
  27 +#endif /* EXTRATORSRTEXCEPTION_H */
29 28 \ No newline at end of file
... ...
extratorSRT/src/include/reader_exception.h
... ... @@ -1,28 +0,0 @@
1   -/***************************************************************************
2   - * Universidade Federal da Paraíba *
3   - * Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital *
4   - * *
5   - * Centro de Informática - UFPB - Campus I *
6   - * João Pessoa - PB - Brasil *
7   - * *
8   - * Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br) *
9   - * Date: Tue Jan 14 11:09:41 BRT 2014 *
10   - * *
11   - **************************************************************************/
12   -
13   - #ifndef READEREXCEPTION_H
14   - #define READEREXCEPTION_H
15   -
16   - #include <lavidlib/base/RuntimeException.h>
17   -
18   - using namespace lavidlib;
19   -
20   - namespace sndesc {
21   - class ReaderException : public RuntimeException {
22   - public:
23   - ReaderException(const std::string message);
24   - ReaderException(const char* message);
25   - };
26   - }
27   -
28   - #endif /* READEREXCEPTION_H */
29 0 \ No newline at end of file
extratorSRT/src/include/reader_srt.h
... ... @@ -1,64 +0,0 @@
1   -/***************************************************************************
2   - * Universidade Federal da Paraíba *
3   - * Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital *
4   - * *
5   - * Centro de Informática - UFPB - Campus I *
6   - * João Pessoa - PB - Brasil *
7   - * *
8   - * Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br) *
9   - * Date: Qui Out 24 18:25:51 BRT 2013 *
10   - * *
11   - **************************************************************************/
12   -
13   - #ifndef READERSRT_H
14   - #define READERSRT_H
15   -
16   - #define MAX_LINE 1024
17   - #define SIZE_CSCAPE 1
18   - #define TARGET_TIME "-->"
19   -
20   - #include <lavidlib/io/File.h>
21   - #include <lavidlib/io/FileIO.h>
22   - #include <lavidlib/io/BufferedReader.h>
23   - #include <lavidlib/io/IOException.h>
24   - #include <lavidlib/io/EOFException.h>
25   - #include <string>
26   - #include <string.h>
27   - #include <stdlib.h>
28   - #include <fstream>
29   - #include <stdio.h>
30   - #include <list>
31   - #include "reader.h"
32   - #include "reader_exception.h"
33   - #include "subtitle.h"
34   -
35   - using namespace lavidlib;
36   -
37   - namespace sndesc {
38   -
39   - class ReaderSRT : public Reader {
40   -
41   - public:
42   -
43   - ReaderSRT(std::string _filepath, FileIO::open_flags_t _mode);
44   - ~ReaderSRT();
45   - bool hasNextSubtitle();
46   -
47   - /* @Override: reader.h */
48   - Subtitle* next();
49   -
50   - private:
51   -
52   - File *file;
53   - FileIO *file_io;
54   - BufferedReader *bff_reader;
55   - int64_t seek_pos;
56   - bool hasNextSub;
57   -
58   - int64_t str_to_time(std::string str_time);
59   -
60   - };
61   -
62   - }
63   -
64   - #endif // READERSRT_H
65 0 \ No newline at end of file
extratorSRT/src/reader_exception.cpp
... ... @@ -1,22 +0,0 @@
1   -/***************************************************************************
2   - * Universidade Federal da Paraíba *
3   - * Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital *
4   - * *
5   - * Centro de Informática - UFPB - Campus I *
6   - * João Pessoa - PB - Brasil *
7   - * *
8   - * Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br) *
9   - * Date: Tue Jan 14 11:15:37 BRT 2014 *
10   - * *
11   - **************************************************************************/
12   -
13   - #include "reader_exception.h"
14   -
15   - namespace sndesc {
16   - ReaderException::ReaderException(const std::string message)
17   - : RuntimeException(message)
18   - { /* TODO */ }
19   - ReaderException::ReaderException(const char* message)
20   - : RuntimeException(message)
21   - { /* TODO */ }
22   - }
23 0 \ No newline at end of file
extratorSRT/src/reader_srt.cpp
... ... @@ -1,128 +0,0 @@
1   -/***************************************************************************
2   - * Universidade Federal da Paraíba *
3   - * Copyright (C) 2013 by Laboratório de Aplicações de Vídeo Digital *
4   - * *
5   - * Centro de Informática - UFPB - Campus I *
6   - * João Pessoa - PB - Brasil *
7   - * *
8   - * Author: Leonardo de Araújo Domingues (leonardo.araujo@lavid.ufpb.br) *
9   - * Date: Qui Out 24 22:26:35 BRT 2013 *
10   - * *
11   - **************************************************************************/
12   -
13   - #include "reader_srt.h"
14   -
15   - namespace sndesc {
16   -
17   -
18   - ReaderSRT::ReaderSRT(std::string _filepath, FileIO::open_flags_t _mode) {
19   -
20   - file = new lavidlib::File(_filepath);
21   - try{
22   - file_io = new lavidlib::FileIO(file->getPath(), _mode);
23   - }catch(lavidlib::IOException){
24   - throw ReaderException("[ERRO: reader_srt.cpp] Arquivo de legenda não encontrado.");
25   - }
26   - seek_pos = 0;
27   - hasNextSub = true;
28   -
29   - }
30   -
31   -
32   - ReaderSRT::~ReaderSRT() {
33   -
34   - if (bff_reader != NULL) delete bff_reader;
35   - if (file_io != NULL) delete file_io;
36   -
37   - }
38   -
39   -
40   - Subtitle* ReaderSRT::next() {
41   -
42   - if (seek_pos >= file_io->getSize())
43   - throw ReaderException("[ERRO: reader_srt.cpp] Esse arquivo já foi lido.");
44   -
45   - file_io->seek(seek_pos);
46   - try{
47   - bff_reader = new BufferedReader(file_io);
48   - }catch(lavidlib::IOException){
49   - throw ReaderException("[ERRO: reader_srt.cpp] O BufferedReader não foi inicializado.");
50   - }
51   -
52   - Subtitle* sub = new Subtitle();
53   - std::string line = "";
54   - std::string text_sub = "";
55   -
56   - try {
57   - /* ID */
58   - int id = 0;
59   - line = bff_reader->readLine();
60   - seek_pos += (int64_t) line.size() + SIZE_CSCAPE;
61   - id = atoi(line.c_str());
62   - sub->setID(id);
63   -
64   - /* TimeIn and TimeOut */
65   - int64_t t_in = 0, t_out = 0;
66   - line = bff_reader->readLine();
67   - seek_pos += (int64_t) line.size() + SIZE_CSCAPE;
68   -
69   - int target_pos = line.find(TARGET_TIME);
70   - t_in = str_to_time(line.substr(0, target_pos));
71   - sub->setTimeIn(t_in);
72   - t_out = str_to_time(line.substr(target_pos + strlen(TARGET_TIME)+1, line.size()));
73   - sub->setTimeOut(t_out);
74   -
75   - /* Text: read until line be empty */
76   - while ((line = bff_reader->readLine()).size() > 0) {
77   - text_sub += line;
78   - text_sub.append(" ");
79   - }
80   - seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE;
81   -
82   - } catch (lavidlib::EOFException &ex) {
83   - sub->setSubtitleText(text_sub);
84   - sub->setStatusOfReady(true);
85   - delete(bff_reader);
86   - seek_pos += (int64_t) text_sub.size() + SIZE_CSCAPE;
87   - hasNextSub = false;
88   - return sub;
89   - }
90   -
91   - sub->setSubtitleText(text_sub);
92   - sub->setStatusOfReady(true);
93   - delete(bff_reader);
94   - return sub;
95   -
96   - }
97   -
98   -
99   - bool ReaderSRT::hasNextSubtitle() {
100   - return hasNextSub;
101   - }
102   -
103   -
104   - int64_t ReaderSRT::str_to_time(std::string str_time) {
105   -
106   - int64_t ttime = 0;
107   - char* tokens = new char[4]; // hh, mm, ss, ms
108   - strcpy(tokens, (char*)str_time.c_str());
109   -
110   - int index = 0;
111   - int values [4]; // hh, mm, ss, ms
112   - char * str = strtok(tokens, ":,");
113   - while (str != NULL) {
114   - values[index] = atoi(str);
115   - str = strtok(NULL, ":,");
116   - index++;
117   - }
118   - delete(tokens);
119   -
120   - /* calculate time */
121   - ttime = /*hour to sec*/((((values[0] * 60) * 60) +
122   - /*min to sec*/(values[1] * 60) +/*sec*/values[2])*1000) + values[3];
123   -
124   - return ttime;
125   -
126   - }
127   -
128   - }
129 0 \ No newline at end of file
main.cpp
... ... @@ -278,7 +278,7 @@ void serviceOnlySRT(char* path_file, char* transparency, char* id){
278 278 service_srt = new ServiceWindowGenerationFromSRT(path_file, (int) atoi(transparency), id, 5);
279 279 try{
280 280 service_srt->initialize();
281   - }catch(ServiceException ex){
  281 + }catch(ServiceException ex){
282 282 fail(ex.getMessage());
283 283 hasFailed();
284 284 return;
... ... @@ -356,22 +356,25 @@ void hasInvalid(){
356 356  
357 357 //Help do programa, explicando todos os parâmetros existentes...
358 358 void help() {
359   - cout <<"\n####################################################################################\n"
  359 + cout <<"\n##################################################################################\n"
360 360 <<"# SERVICE_TYPE: 1 - means Closed Caption - doesn't use INPUT_SRT #\n"
361 361 <<"# 2 - means With Subtitles (SRT) - requires INPUT_SRT #\n"
362 362 <<"# 3 - means Recognize - requires INPUT_VIDEO #\n"
363 363 <<"# 4 - means Text - requires INPUT_FILE_TEXT #\n"
364 364 <<"# 5 - means Subtitles ONLY (SRT) - requires INPUT_SRT #\n"
365   - <<"#----------------------------------------------------------------------------------#\n\n"
366   - /*<<"# INPUT_VIDEO: Path of the video file #\n"
  365 + <<"# 6 - means Audio - requires INPUT_AUDIO #\n"
  366 + <<"####################################################################################\n\n"
  367 + /*<<"# INPUT_VIDEO: Path of the video file #\n"
367 368 <<"#----------------------------------------------------------------------------------#\n"
368 369 <<"# INPUT_SRT: Path of the SRT file (only for SERVICE_TYPE = 2) #\n"
369 370 <<"#----------------------------------------------------------------------------------#\n"
370   - <<"# INPUT_FILE_TEXT: Path of the text file (doesn't use INPUT_VIDEO and INPUT_SRT) #\n"
  371 + <<"# INPUT_FILE_TEXT: Path of the text file (doesn't use INPUT_VIDEO and INPUT_SRT) #\n"
  372 + <<"#----------------------------------------------------------------------------------#\n"
  373 + <<"# INPUT_AUDIO: Path of the audio file" #\n
371 374 <<"#----------------------------------------------------------------------------------#\n"
372   - <<"# LANGUAGE: 1 - means Portuguese #\n"
373   - <<"# 2 - means Glosa #\n"
374   - <<"#----------------------------------------------------------------------------------#\n"
  375 + <<"# LANGUAGE: 1 - means Portuguese #\n"
  376 + <<"# 2 - means Glosa #\n"
  377 + <<"#----------------------------------------------------------------------------------#\n"
375 378 <<"# POSITION: 1 - means TOP_LEFT #\n"
376 379 <<"# 2 - means TOP_RIGHT #\n"
377 380 <<"# 3 - means BOTTOM_RIGHT #\n"
... ...
servico/src/serviceWindowGenerationFromSRT.cpp
... ... @@ -4,7 +4,7 @@ using namespace std;
4 4  
5 5 //Construtor Service 2
6 6 ServiceWindowGenerationFromSRT::ServiceWindowGenerationFromSRT(
7   - char* path_video, char* path_srt, int sublanguage, int position, int size, int transparency, char* id, int serviceType) {
  7 + char* path_video, char* path_srt, int sublanguage, int position, int size, int transparency, char* id, int serviceType) {
8 8 setPathInput(path_video);
9 9 setPathSRT(path_srt);
10 10 setPosition(position);
... ...