X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fanalyse_subtitles_job.cc;fp=src%2Flib%2Fanalyse_subtitles_job.cc;h=7a1c4ab00ff751572832a74475d0a9d1ec14e9a4;hb=cef7a679a59044a5c807768042deecfd56ec6fc2;hp=0000000000000000000000000000000000000000;hpb=87d5a977da0696fbe73b96b2679b7cbb471e7255;p=dcpomatic.git 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 + + 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 . + +*/ + +#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 + +#include "i18n.h" + +using std::string; +using boost::shared_ptr; +using boost::weak_ptr; + +AnalyseSubtitlesJob::AnalyseSubtitlesJob (shared_ptr film, shared_ptr 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 (new Playlist()); + shared_ptr content = _content.lock (); + DCPOMATIC_ASSERT (content); + playlist->add (_film, content); + + shared_ptr 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 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); + } + } + } +} +