Add option to export Interop subs with or without the font file / reference.
[dcpomatic.git] / src / lib / subtitle_encoder.cc
1 /*
2     Copyright (C) 2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "font.h"
22 #include "subtitle_encoder.h"
23 #include "player.h"
24 #include "compose.hpp"
25 #include "job.h"
26 #include <dcp/interop_subtitle_asset.h>
27 #include <dcp/raw_convert.h>
28 #include <dcp/smpte_subtitle_asset.h>
29 #include <boost/shared_ptr.hpp>
30 #include <boost/filesystem.hpp>
31 #include <boost/bind.hpp>
32
33 #include "i18n.h"
34
35 using std::string;
36 using std::make_pair;
37 using std::pair;
38 using std::vector;
39 using boost::shared_ptr;
40 using boost::optional;
41 using dcp::raw_convert;
42
43 /** @param include_font true to refer to and export any font file (for Interop; ignored for SMPTE) */
44 SubtitleEncoder::SubtitleEncoder (shared_ptr<const Film> film, shared_ptr<Job> job, boost::filesystem::path output, bool split_reels, bool include_font)
45         : Encoder (film, job)
46         , _split_reels (split_reels)
47         , _include_font (include_font)
48         , _reel_index (0)
49         , _length (film->length())
50 {
51         _player->set_play_referenced ();
52         _player->set_ignore_video ();
53         _player->set_ignore_audio ();
54         _player->Text.connect (boost::bind(&SubtitleEncoder::text, this, _1, _2, _3, _4));
55
56         int const files = split_reels ? film->reels().size() : 1;
57         for (int i = 0; i < files; ++i) {
58
59                 boost::filesystem::path filename = output;
60                 string extension = boost::filesystem::extension (filename);
61                 filename = boost::filesystem::change_extension (filename, "");
62
63                 if (files > 1) {
64                         /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
65                         /// which reel it is.  Preserve the %1; it will be replaced with the reel number.
66                         filename = filename.string() + String::compose(_("_reel%1"), i + 1);
67                 }
68
69                 _assets.push_back (make_pair(shared_ptr<dcp::SubtitleAsset>(), boost::filesystem::change_extension(filename, ".xml")));
70         }
71
72         BOOST_FOREACH (dcpomatic::DCPTimePeriod i, film->reels()) {
73                 _reels.push_back (i);
74         }
75 }
76
77 void
78 SubtitleEncoder::go ()
79 {
80         {
81                 shared_ptr<Job> job = _job.lock ();
82                 DCPOMATIC_ASSERT (job);
83                 job->sub (_("Extracting"));
84         }
85
86         _reel_index = 0;
87
88         while (!_player->pass()) {}
89
90         int reel = 0;
91         for (vector<pair<shared_ptr<dcp::SubtitleAsset>, boost::filesystem::path> >::iterator i = _assets.begin(); i != _assets.end(); ++i) {
92                 if (!i->first) {
93                         /* No subtitles arrived for this asset; make an empty one so we write something to the output */
94                         if (_film->interop()) {
95                                 shared_ptr<dcp::InteropSubtitleAsset> s (new dcp::InteropSubtitleAsset());
96                                 s->set_movie_title (_film->name());
97                                 s->set_reel_number (raw_convert<string>(reel + 1));
98                                 i->first = s;
99                         } else {
100                                 shared_ptr<dcp::SMPTESubtitleAsset> s (new dcp::SMPTESubtitleAsset());
101                                 s->set_content_title_text (_film->name());
102                                 s->set_reel_number (reel + 1);
103                                 i->first = s;
104                         }
105                 }
106
107                 if (!_film->interop() || _include_font) {
108                         BOOST_FOREACH (shared_ptr<dcpomatic::Font> j, _player->get_subtitle_fonts()) {
109                                 i->first->add_font (j->id(), default_font_file());
110                         }
111                 }
112
113                 i->first->write (i->second);
114                 ++reel;
115         }
116 }
117
118 void
119 SubtitleEncoder::text (PlayerText subs, TextType type, optional<DCPTextTrack> track, dcpomatic::DCPTimePeriod period)
120 {
121         if (type != TEXT_OPEN_SUBTITLE) {
122                 return;
123         }
124
125         if (!_assets[_reel_index].first) {
126                 shared_ptr<dcp::SubtitleAsset> asset;
127                 string lang = _film->subtitle_language ();
128                 if (_film->interop ()) {
129                         shared_ptr<dcp::InteropSubtitleAsset> s (new dcp::InteropSubtitleAsset());
130                         s->set_movie_title (_film->name());
131                         s->set_language (lang.empty() ? "Unknown" : lang);
132                         s->set_reel_number (raw_convert<string>(_reel_index + 1));
133                         _assets[_reel_index].first = s;
134                 } else {
135                         shared_ptr<dcp::SMPTESubtitleAsset> s (new dcp::SMPTESubtitleAsset());
136                         s->set_content_title_text (_film->name());
137                         if (!lang.empty()) {
138                                 s->set_language (lang);
139                         } else {
140                                 s->set_language (track->language);
141                         }
142                         s->set_edit_rate (dcp::Fraction (_film->video_frame_rate(), 1));
143                         s->set_reel_number (_reel_index + 1);
144                         s->set_time_code_rate (_film->video_frame_rate());
145                         s->set_start_time (dcp::Time());
146                         if (_film->encrypted ()) {
147                                 s->set_key (_film->key ());
148                         }
149                         _assets[_reel_index].first = s;
150                 }
151         }
152
153         BOOST_FOREACH (StringText i, subs.string) {
154                 /* XXX: couldn't / shouldn't we use period here rather than getting time from the subtitle? */
155                 i.set_in  (i.in());
156                 i.set_out (i.out());
157                 if (_film->interop() && !_include_font) {
158                         i.unset_font ();
159                 }
160                 _assets[_reel_index].first->add (shared_ptr<dcp::Subtitle>(new dcp::SubtitleString(i)));
161         }
162
163         if (_split_reels && (_reel_index < int(_reels.size()) - 1) && period.from > _reels[_reel_index].from) {
164                 ++_reel_index;
165         }
166
167         _last = period.from;
168
169         shared_ptr<Job> job = _job.lock ();
170         if (job) {
171                 job->set_progress (float(period.from.get()) / _length.get());
172         }
173 }
174
175 Frame
176 SubtitleEncoder::frames_done () const
177 {
178         if (!_last) {
179                 return 0;
180         }
181
182         /* XXX: assuming 24fps here but I don't think it matters */
183         return _last->seconds() * 24;
184 }