diff options
| author | Carl Hetherington <cth@carlh.net> | 2020-04-23 00:11:38 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2020-04-24 00:46:31 +0200 |
| commit | cef7a679a59044a5c807768042deecfd56ec6fc2 (patch) | |
| tree | b0e020a2a70db228df0e96fd85b63a84d7d69b64 /src/lib/analyse_subtitles_job.cc | |
| parent | 87d5a977da0696fbe73b96b2679b7cbb471e7255 (diff) | |
Add subtitle analysis so that the outline of all subtitles
in a piece of content can be overlaid onto the preview (#1233).
Diffstat (limited to 'src/lib/analyse_subtitles_job.cc')
| -rw-r--r-- | src/lib/analyse_subtitles_job.cc | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/lib/analyse_subtitles_job.cc b/src/lib/analyse_subtitles_job.cc new file mode 100644 index 000000000..7a1c4ab00 --- /dev/null +++ b/src/lib/analyse_subtitles_job.cc @@ -0,0 +1,118 @@ +/* + Copyright (C) 2020 Carl Hetherington <cth@carlh.net> + + This file is part of DCP-o-matic. + + DCP-o-matic 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 2 of the License, or + (at your option) any later version. + + DCP-o-matic 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 DCP-o-matic. If not, see <http://www.gnu.org/licenses/>. + +*/ + +#include "analyse_subtitles_job.h" +#include "playlist.h" +#include "player.h" +#include "subtitle_analysis.h" +#include "bitmap_text.h" +#include "render_text.h" +#include "text_content.h" +#include "image.h" +#include <iostream> + +#include "i18n.h" + +using std::string; +using boost::shared_ptr; +using boost::weak_ptr; + +AnalyseSubtitlesJob::AnalyseSubtitlesJob (shared_ptr<const Film> film, shared_ptr<Content> content) + : Job (film) + , _content (content) + , _path (_film->subtitle_analysis_path(content)) +{ +} + + +string +AnalyseSubtitlesJob::name () const +{ + return _("Analysing subtitles"); +} + + +string +AnalyseSubtitlesJob::json_name () const +{ + return N_("analyse_subtitles"); +} + + +void +AnalyseSubtitlesJob::run () +{ + shared_ptr<Playlist> playlist (new Playlist()); + shared_ptr<Content> content = _content.lock (); + DCPOMATIC_ASSERT (content); + playlist->add (_film, content); + + shared_ptr<Player> player (new Player(_film, playlist)); + player->set_ignore_audio (); + player->set_fast (); + player->set_play_referenced (); + player->Text.connect (bind(&AnalyseSubtitlesJob::analyse, this, _1, _2)); + + set_progress_unknown (); + + if (!content->text.empty()) { + while (!player->pass ()) {} + } + + SubtitleAnalysis analysis (_bounding_box, content->text.front()->x_offset(), content->text.front()->y_offset()); + analysis.write (_path); + + set_progress (1); + set_state (FINISHED_OK); +} + + +void +AnalyseSubtitlesJob::analyse (PlayerText text, TextType type) +{ + if (type != TEXT_OPEN_SUBTITLE) { + return; + } + + BOOST_FOREACH (BitmapText const& i, text.bitmap) { + if (!_bounding_box) { + _bounding_box = i.rectangle; + } else { + _bounding_box->extend (i.rectangle); + } + } + + if (!text.string.empty()) { + /* We can provide dummy values for time and frame rate here as they are only used to calculate fades */ + dcp::Size const frame = _film->frame_size(); + BOOST_FOREACH (PositionImage i, render_text(text.string, text.fonts, frame, dcpomatic::DCPTime(), 24)) { + dcpomatic::Rect<double> rect ( + double(i.position.x) / frame.width, double(i.position.y) / frame.height, + double(i.image->size().width) / frame.width, double(i.image->size().height) / frame.height + ); + if (!_bounding_box) { + _bounding_box = rect; + } else { + _bounding_box->extend (rect); + } + } + } +} + |
