2 Copyright (C) 2019-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 "compose.hpp"
23 #include "font_data.h"
26 #include "subtitle_encoder.h"
27 #include <dcp/interop_subtitle_asset.h>
28 #include <dcp/raw_convert.h>
29 #include <dcp/smpte_subtitle_asset.h>
30 #include <boost/filesystem.hpp>
31 #include <boost/bind/bind.hpp>
37 using std::make_shared;
39 using std::shared_ptr;
42 using boost::optional;
43 #if BOOST_VERSION >= 106100
44 using namespace boost::placeholders;
46 using dcp::raw_convert;
49 /** @param output Directory, if there will be multiple output files, or a filename.
50 * @param initial_name Hint that may be used to create filenames, if @ref output is a directory.
51 * @param include_font true to refer to and export any font file (for Interop; ignored for SMPTE).
53 SubtitleEncoder::SubtitleEncoder (shared_ptr<const Film> film, shared_ptr<Job> job, boost::filesystem::path output, string initial_name, bool split_reels, bool include_font)
55 , _split_reels (split_reels)
56 , _include_font (include_font)
58 , _length (film->length())
60 _player->set_play_referenced ();
61 _player->set_ignore_video ();
62 _player->set_ignore_audio ();
63 _player->Text.connect (boost::bind(&SubtitleEncoder::text, this, _1, _2, _3, _4));
65 string const extension = film->interop() ? ".xml" : ".mxf";
67 int const files = split_reels ? film->reels().size() : 1;
68 for (int i = 0; i < files; ++i) {
70 boost::filesystem::path filename = output;
71 if (boost::filesystem::is_directory(filename)) {
73 /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
74 /// which reel it is. Preserve the %1; it will be replaced with the reel number.
75 filename /= String::compose("%1_reel%2", initial_name, i + 1);
77 filename /= initial_name;
81 _assets.push_back (make_pair(shared_ptr<dcp::SubtitleAsset>(), boost::filesystem::change_extension(filename, extension)));
84 for (auto i: film->reels()) {
88 _default_font = dcp::ArrayData (default_font_file());
93 SubtitleEncoder::go ()
96 shared_ptr<Job> job = _job.lock ();
97 DCPOMATIC_ASSERT (job);
98 job->sub (_("Extracting"));
103 while (!_player->pass()) {}
106 for (auto& i: _assets) {
108 /* No subtitles arrived for this asset; make an empty one so we write something to the output */
109 if (_film->interop()) {
110 auto s = make_shared<dcp::InteropSubtitleAsset>();
111 s->set_movie_title (_film->name());
112 s->set_reel_number (raw_convert<string>(reel + 1));
115 auto s = make_shared<dcp::SMPTESubtitleAsset>();
116 s->set_content_title_text (_film->name());
117 s->set_reel_number (reel + 1);
122 if (!_film->interop() || _include_font) {
123 for (auto j: _player->get_subtitle_fonts()) {
124 i.first->add_font (j.id, _default_font);
128 i.first->write (i.second);
135 SubtitleEncoder::text (PlayerText subs, TextType type, optional<DCPTextTrack> track, dcpomatic::DCPTimePeriod period)
137 if (type != TextType::OPEN_SUBTITLE) {
141 if (!_assets[_reel_index].first) {
142 shared_ptr<dcp::SubtitleAsset> asset;
143 auto lang = _film->subtitle_languages ();
144 if (_film->interop ()) {
145 auto s = make_shared<dcp::InteropSubtitleAsset>();
146 s->set_movie_title (_film->name());
148 s->set_language (lang.first->to_string());
150 s->set_reel_number (raw_convert<string>(_reel_index + 1));
151 _assets[_reel_index].first = s;
153 auto s = make_shared<dcp::SMPTESubtitleAsset>();
154 s->set_content_title_text (_film->name());
156 s->set_language (*lang.first);
157 } else if (track->language) {
158 s->set_language (track->language.get());
160 s->set_edit_rate (dcp::Fraction (_film->video_frame_rate(), 1));
161 s->set_reel_number (_reel_index + 1);
162 s->set_time_code_rate (_film->video_frame_rate());
163 s->set_start_time (dcp::Time());
164 if (_film->encrypted ()) {
165 s->set_key (_film->key ());
167 _assets[_reel_index].first = s;
171 for (auto i: subs.string) {
172 /* XXX: couldn't / shouldn't we use period here rather than getting time from the subtitle? */
175 if (_film->interop() && !_include_font) {
178 _assets[_reel_index].first->add (make_shared<dcp::SubtitleString>(i));
181 if (_split_reels && (_reel_index < int(_reels.size()) - 1) && period.from > _reels[_reel_index].from) {
187 auto job = _job.lock ();
189 job->set_progress (float(period.from.get()) / _length.get());
195 SubtitleEncoder::frames_done () const
201 /* XXX: assuming 24fps here but I don't think it matters */
202 return _last->seconds() * 24;