File: subrip.cc

package info (click to toggle)
subtitleeditor 0.56.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 8,908 kB
  • sloc: cpp: 26,446; makefile: 1,713; perl: 434; sh: 259; xml: 149
file content (118 lines) | stat: -rw-r--r-- 3,854 bytes parent folder | download | duplicates (3)
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
// subtitleeditor -- a tool to create or edit subtitle
//
// https://subtitleeditor.github.io/subtitleeditor/
// https://github.com/subtitleeditor/subtitleeditor/
//
// Copyright @ 2005-2018, kitone
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <extension/subtitleformat.h>
#include <utility.h>

// format:
// number
// start --> end
// text
// (empty line)
class SubRip : public SubtitleFormatIO {
  public:
   void open(Reader& file) {
      Glib::RefPtr<Glib::Regex> re_num = Glib::Regex::create("^\\d+$");

      Glib::RefPtr<Glib::Regex> re_time = Glib::Regex::create("^(\\d+):(\\d+):(\\d+),(\\d+)\\s-->\\s(\\d+):(\\d+):(\\d+),(\\d+)");

      int start[4], end[4];
      Subtitles subtitles = document()->subtitles();

      Glib::ustring line;

      while (file.getline(line)) {
         // Read the subtitle time "start --> end"
         if (re_time->match(line)) {
            std::vector<Glib::ustring> group = re_time->split(line);

            start[0] = utility::string_to_int(group[1]);
            start[1] = utility::string_to_int(group[2]);
            start[2] = utility::string_to_int(group[3]);
            start[3] = utility::string_to_int(group[4]);

            end[0] = utility::string_to_int(group[5]);
            end[1] = utility::string_to_int(group[6]);
            end[2] = utility::string_to_int(group[7]);
            end[3] = utility::string_to_int(group[8]);

            Glib::ustring text;
            int count = 0;

            // Read the text lines
            while (file.getline(line) && !line.empty()) {
               if (count > 0)
                  text += '\n';

               text += line;

               ++count;
            }

            // Append a subtitle
            Subtitle sub = subtitles.append();

            sub.set_text(text);
            sub.set_start_and_end(SubtitleTime(start[0], start[1], start[2], start[3]), SubtitleTime(end[0], end[1], end[2], end[3]));
         } else {
            se_dbg_msg(SE_DBG_PLUGINS, "can not match time line: '%s'", line.c_str());
         }
      }
   }

   void save(Writer& file) {
      unsigned int count = 1;
      for (Subtitle sub = document()->subtitles().get_first(); sub; ++sub, ++count) {
         Glib::ustring text = sub.get_text();

         file.write(Glib::ustring::compose("%1\n%2 --> %3\n%4\n\n", count, time_to_subrip(sub.get_start()), time_to_subrip(sub.get_end()), text));
      }
   }

   Glib::ustring time_to_subrip(const SubtitleTime& t) {
      return build_message("%02i:%02i:%02i,%03i", t.hours(), t.minutes(), t.seconds(), t.mseconds());
   }
};

class SubRipPlugin : public SubtitleFormat {
  public:
   SubtitleFormatInfo get_info() {
      SubtitleFormatInfo info;
      info.name = "SubRip";
      info.extension = "srt";

      info.pattern =
         "\\d\\R"
         "\\d+:\\d+:\\d+,\\d+\\s-->\\s\\d+:\\d+:\\d+,\\d+"
         "(\\sX1:\\d+ X2:\\d+ Y1:\\d+ Y2:\\d+)?\\s*"  // Be cool with
                                                      // coordinates, this is not
                                                      // documented
         "\\R";

      return info;
   }

   SubtitleFormatIO* create() {
      SubRip* sf = new SubRip();
      return sf;
   }
};

REGISTER_EXTENSION(SubRipPlugin)