2 Copyright (C) 2020 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/>.
21 #include "analyse_subtitles_job.h"
24 #include "subtitle_analysis.h"
25 #include "bitmap_text.h"
26 #include "render_text.h"
27 #include "text_content.h"
33 using std::make_shared;
34 using std::shared_ptr;
37 #if BOOST_VERSION >= 106100
38 using namespace boost::placeholders;
41 AnalyseSubtitlesJob::AnalyseSubtitlesJob (shared_ptr<const Film> film, shared_ptr<Content> content)
44 , _path (_film->subtitle_analysis_path(content))
50 AnalyseSubtitlesJob::name () const
52 return _("Analysing subtitles");
57 AnalyseSubtitlesJob::json_name () const
59 return N_("analyse_subtitles");
64 AnalyseSubtitlesJob::run ()
66 auto playlist = make_shared<Playlist>();
67 auto content = _content.lock ();
68 DCPOMATIC_ASSERT (content);
69 playlist->add (_film, content);
71 auto player = make_shared<Player>(_film, playlist);
72 player->set_ignore_audio ();
74 player->set_play_referenced ();
75 player->Text.connect (bind(&AnalyseSubtitlesJob::analyse, this, _1, _2));
77 set_progress_unknown ();
79 if (!content->text.empty()) {
80 while (!player->pass ()) {}
83 SubtitleAnalysis analysis (_bounding_box, content->text.front()->x_offset(), content->text.front()->y_offset());
84 analysis.write (_path);
87 set_state (FINISHED_OK);
92 AnalyseSubtitlesJob::analyse (PlayerText text, TextType type)
94 if (type != TextType::OPEN_SUBTITLE) {
98 for (auto const& i: text.bitmap) {
100 _bounding_box = i.rectangle;
102 _bounding_box->extend (i.rectangle);
106 if (!text.string.empty()) {
107 /* We can provide dummy values for time and frame rate here as they are only used to calculate fades */
108 dcp::Size const frame = _film->frame_size();
109 for (auto i: render_text(text.string, text.fonts, frame, dcpomatic::DCPTime(), 24)) {
110 dcpomatic::Rect<double> rect (
111 double(i.position.x) / frame.width, double(i.position.y) / frame.height,
112 double(i.image->size().width) / frame.width, double(i.image->size().height) / frame.height
114 if (!_bounding_box) {
115 _bounding_box = rect;
117 _bounding_box->extend (rect);