43c68bc2218e6e34b642aabff6d87543a78c6ae4
[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 "subtitle_encoder.h"
22 #include "player.h"
23 #include "compose.hpp"
24 #include "job.h"
25 #include <dcp/interop_subtitle_asset.h>
26 #include <dcp/raw_convert.h>
27 #include <dcp/smpte_subtitle_asset.h>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/filesystem.hpp>
30 #include <boost/bind.hpp>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::make_pair;
36 using std::pair;
37 using std::vector;
38 using boost::shared_ptr;
39 using boost::optional;
40 using dcp::raw_convert;
41
42 SubtitleEncoder::SubtitleEncoder (shared_ptr<const Film> film, shared_ptr<Job> job, boost::filesystem::path output, bool split_reels)
43         : Encoder (film, job)
44         , _split_reels (split_reels)
45         , _reel_index (0)
46         , _length (film->length())
47 {
48         _player->set_play_referenced ();
49         _player->set_ignore_video ();
50         _player->set_ignore_audio ();
51         _player->Text.connect (boost::bind(&SubtitleEncoder::text, this, _1, _2, _3, _4));
52
53         int const files = split_reels ? film->reels().size() : 1;
54         for (int i = 0; i < files; ++i) {
55
56                 boost::filesystem::path filename = output;
57                 string extension = boost::filesystem::extension (filename);
58                 filename = boost::filesystem::change_extension (filename, "");
59
60                 if (files > 1) {
61                         /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
62                         /// which reel it is.  Preserve the %1; it will be replaced with the reel number.
63                         filename = filename.string() + String::compose(_("_reel%1"), i + 1);
64                 }
65
66                 _assets.push_back (make_pair(shared_ptr<dcp::SubtitleAsset>(), filename));
67         }
68
69         BOOST_FOREACH (dcpomatic::DCPTimePeriod i, film->reels()) {
70                 _reels.push_back (i);
71         }
72 }
73
74 void
75 SubtitleEncoder::go ()
76 {
77         {
78                 shared_ptr<Job> job = _job.lock ();
79                 DCPOMATIC_ASSERT (job);
80                 job->sub (_("Extracting"));
81         }
82
83         _reel_index = 0;
84
85         while (!_player->pass()) {}
86
87         for (vector<pair<shared_ptr<dcp::SubtitleAsset>, boost::filesystem::path> >::iterator i = _assets.begin(); i != _assets.end(); ++i) {
88                 if (i->first) {
89                         i->first->write (i->second);
90                 }
91         }
92 }
93
94 void
95 SubtitleEncoder::text (PlayerText subs, TextType type, optional<DCPTextTrack> track, dcpomatic::DCPTimePeriod period)
96 {
97         if (type != TEXT_OPEN_SUBTITLE) {
98                 return;
99         }
100
101         if (!_assets[_reel_index].first) {
102                 shared_ptr<dcp::SubtitleAsset> asset;
103                 string lang = _film->subtitle_language ();
104                 if (_film->interop ()) {
105                         shared_ptr<dcp::InteropSubtitleAsset> s (new dcp::InteropSubtitleAsset());
106                         s->set_movie_title (_film->name());
107                         s->set_language (lang.empty() ? "Unknown" : lang);
108                         s->set_reel_number (raw_convert<string>(_reel_index + 1));
109                         _assets[_reel_index].first = s;
110                 } else {
111                         shared_ptr<dcp::SMPTESubtitleAsset> s (new dcp::SMPTESubtitleAsset());
112                         s->set_content_title_text (_film->name());
113                         if (!lang.empty()) {
114                                 s->set_language (lang);
115                         } else {
116                                 s->set_language (track->language);
117                         }
118                         s->set_edit_rate (dcp::Fraction (_film->video_frame_rate(), 1));
119                         s->set_reel_number (_reel_index + 1);
120                         s->set_time_code_rate (_film->video_frame_rate());
121                         s->set_start_time (dcp::Time());
122                         if (_film->encrypted ()) {
123                                 s->set_key (_film->key ());
124                         }
125                         _assets[_reel_index].first = s;
126                 }
127         }
128
129         BOOST_FOREACH (StringText i, subs.string) {
130                 /* XXX: couldn't / shouldn't we use period here rather than getting time from the subtitle? */
131                 i.set_in  (i.in());
132                 i.set_out (i.out());
133                 _assets[_reel_index].first->add (shared_ptr<dcp::Subtitle>(new dcp::SubtitleString(i)));
134         }
135
136         if (_split_reels && (_reel_index < int(_reels.size()) - 1) && period.from > _reels[_reel_index].from) {
137                 ++_reel_index;
138         }
139
140         _last = period.from;
141
142         shared_ptr<Job> job = _job.lock ();
143         if (job) {
144                 job->set_progress (float(period.from.get()) / _length.get());
145         }
146 }
147
148 Frame
149 SubtitleEncoder::frames_done () const
150 {
151         if (!_last) {
152                 return 0;
153         }
154
155         /* XXX: assuming 24fps here but I don't think it matters */
156         return _last->seconds() * 24;
157 }