File: bitc.cc

package info (click to toggle)
subtitleeditor 0.56.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,880 kB
  • sloc: cpp: 26,446; makefile: 1,713; perl: 434; sh: 259; xml: 149
file content (141 lines) | stat: -rw-r--r-- 4,724 bytes parent folder | download
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
// 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 <gtkmm_utility.h>
#include <gui/dialogutility.h>
#include <player.h>
#include <subtitleeditorwindow.h>
#include <utility.h>

#include <memory>

// BITC (Burnt-in timecode)
class BITC : public SubtitleFormatIO {
  public:
   BITC() : m_framerate(FRAMERATE_23_976) {
      m_framerate_value = get_framerate_value(m_framerate);
   }

   void open(Reader& file) {
      // Ask for the framerate value
      FramerateChooserDialog fcd(FramerateChooserDialog::IMPORT);

      // Define the default value of the framerate from the player
      Player* player = SubtitleEditorWindow::get_instance()->get_player();
      if (player->get_state() != Player::NONE) {
         float player_framerate = player->get_framerate();
         if (player_framerate > 0)
            fcd.set_default_framerate(get_framerate_from_value(player_framerate));
      }
      FRAMERATE framerate = fcd.execute();
      m_framerate_value = get_framerate_value(framerate);

      document()->set_framerate(framerate);

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

      Subtitles subtitles = document()->subtitles();

      int start[4], end[4];
      Glib::ustring line;
      Glib::ustring text;

      Subtitle sub;

      while (file.getline(line)) {
         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]);

            file.getline(text);
            // Replace '|' by a newline
            utility::replace(text, "|", "\n");

            // last 00 are frame, not time!
            start[3] = static_cast<int>(start[3] * 1000 / m_framerate_value);
            end[3] = static_cast<int>(end[3] * 1000 / m_framerate_value);

            // Append a 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]));
         }
      }
   }

   void save(Writer& file) {
      // Ask for the framerate value
      FramerateChooserDialog fcd(FramerateChooserDialog::EXPORT);
      fcd.set_default_framerate(document()->get_framerate());

      m_framerate_value = get_framerate_value(fcd.execute());

      for (Subtitle sub = document()->subtitles().get_first(); sub; ++sub) {
         Glib::ustring text = sub.get_text();

         utility::replace(text, "\n", "|");

         file.write(Glib::ustring::compose("%1 %2\n%3\n\n", to_bitc_time(sub.get_start()), to_bitc_time(sub.get_end()), text));
      }
   }

   // Convert time from SE to BITC
   // 0:00:00.000 -> 00:00:00:00 (last 00 are frames, not time!)
   Glib::ustring to_bitc_time(const SubtitleTime& t) {
      int frame = static_cast<int>(t.mseconds() * m_framerate_value * 0.001);

      return build_message("%02i:%02i:%02i:%02i", t.hours(), t.minutes(), t.seconds(), frame);
   }

  protected:
   FRAMERATE m_framerate;
   double m_framerate_value;
};

class BITCPlugin : public SubtitleFormat {
  public:
   SubtitleFormatInfo get_info() {
      SubtitleFormatInfo info;

      info.name = "BITC (Burnt-in timecode)";
      info.extension = "txt";
      info.pattern =
         "\\d+:\\d+:\\d+:\\d+\\s\\d+:\\d+:\\d+:\\d+\\R"
         ".*\\R";
      return info;
   }

   SubtitleFormatIO* create() {
      return new BITC;
   }
};

REGISTER_EXTENSION(BITCPlugin)