2 Copyright (C) 2020-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "analyse_subtitles_job.h"
23 #include "bitmap_text.h"
27 #include "render_text.h"
28 #include "subtitle_analysis.h"
29 #include "text_content.h"
35 using std::make_shared;
36 using std::shared_ptr;
39 #if BOOST_VERSION >= 106100
40 using namespace boost::placeholders;
44 AnalyseSubtitlesJob::AnalyseSubtitlesJob (shared_ptr<const Film> film, shared_ptr<Content> content)
47 , _path (_film->subtitle_analysis_path(content))
53 AnalyseSubtitlesJob::name () const
55 return _("Analysing subtitles");
60 AnalyseSubtitlesJob::json_name () const
62 return N_("analyse_subtitles");
67 AnalyseSubtitlesJob::run ()
69 auto playlist = make_shared<Playlist>();
70 auto content = _content.lock ();
71 DCPOMATIC_ASSERT (content);
72 playlist->add (_film, content);
74 auto player = make_shared<Player>(_film, playlist);
75 player->set_ignore_audio ();
77 player->set_play_referenced ();
78 player->Text.connect (bind(&AnalyseSubtitlesJob::analyse, this, _1, _2));
80 set_progress_unknown ();
82 if (!content->text.empty()) {
83 while (!player->pass ()) {}
86 SubtitleAnalysis analysis (_bounding_box, content->text.front()->x_offset(), content->text.front()->y_offset());
87 analysis.write (_path);
90 set_state (FINISHED_OK);
95 AnalyseSubtitlesJob::analyse (PlayerText text, TextType type)
97 if (type != TextType::OPEN_SUBTITLE) {
101 for (auto const& i: text.bitmap) {
102 if (!_bounding_box) {
103 _bounding_box = i.rectangle;
105 _bounding_box->extend (i.rectangle);
109 if (!text.string.empty()) {
110 /* We can provide dummy values for time and frame rate here as they are only used to calculate fades */
111 dcp::Size const frame = _film->frame_size();
112 for (auto i: render_text(text.string, text.fonts, frame, dcpomatic::DCPTime(), 24)) {
113 dcpomatic::Rect<double> rect (
114 double(i.position.x) / frame.width, double(i.position.y) / frame.height,
115 double(i.image->size().width) / frame.width, double(i.image->size().height) / frame.height
117 if (!_bounding_box) {
118 _bounding_box = rect;
120 _bounding_box->extend (rect);