summaryrefslogtreecommitdiff
path: root/src/lib/subtitle_film_encoder.cc
blob: 2f1fc7099d0a2353ed977dd6be3cad073902a0cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
    Copyright (C) 2019-2021 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 "film.h"
#include "job.h"
#include "player.h"
#include "subtitle_film_encoder.h"
#include <dcp/filesystem.h>
#include <dcp/interop_text_asset.h>
#include <dcp/smpte_text_asset.h>
#include <fmt/format.h>
#include <boost/filesystem.hpp>
#include <boost/bind/bind.hpp>

#include "i18n.h"


using std::make_pair;
using std::make_shared;
using std::pair;
using std::shared_ptr;
using std::string;
using std::vector;
using boost::optional;
#if BOOST_VERSION >= 106100
using namespace boost::placeholders;
#endif


/** @param output Directory, if there will be multiple output files, or a filename.
 *  @param initial_name Hint that may be used to create filenames, if @ref output is a directory.
 *  @param include_font true to refer to and export any font file (for Interop; ignored for SMPTE).
 */
SubtitleFilmEncoder::SubtitleFilmEncoder(shared_ptr<const Film> film, shared_ptr<Job> job, boost::filesystem::path output, string initial_name, bool split_reels, bool include_font)
	: FilmEncoder(film, job)
	, _split_reels (split_reels)
	, _include_font (include_font)
	, _reel_index (0)
	, _length (film->length())
{
	_player.set_play_referenced();
	_player.set_ignore_video();
	_player.set_ignore_audio();
	_player.Text.connect(boost::bind(&SubtitleFilmEncoder::text, this, _1, _2, _3, _4));

	string const extension = film->interop() ? ".xml" : ".mxf";

	int const files = split_reels ? film->reels().size() : 1;
	for (int i = 0; i < files; ++i) {

		boost::filesystem::path filename = output;
		if (dcp::filesystem::is_directory(filename)) {
			if (files > 1) {
				/// TRANSLATORS: _reel{} here is to be added to an export filename to indicate
				/// which reel it is.  Preserve the {}; it will be replaced with the reel number.
				filename /= fmt::format("{}_reel{}", initial_name, i + 1);
			} else {
				filename /= initial_name;
			}
		}

		_assets.push_back(make_pair(shared_ptr<dcp::TextAsset>(), dcp::filesystem::change_extension(filename, extension)));
	}

	for (auto i: film->reels()) {
		_reels.push_back (i);
	}

	_default_font = dcp::ArrayData (default_font_file());
}


void
SubtitleFilmEncoder::go()
{
	{
		shared_ptr<Job> job = _job.lock ();
		DCPOMATIC_ASSERT (job);
		job->sub (_("Extracting"));
	}

	_reel_index = 0;

	while (!_player.pass()) {}

	int reel = 0;
	for (auto& i: _assets) {
		if (!i.first) {
			/* No subtitles arrived for this asset; make an empty one so we write something to the output */
			if (_film->interop()) {
				auto s = make_shared<dcp::InteropTextAsset>();
				s->set_movie_title (_film->name());
				s->set_reel_number(fmt::to_string(reel + 1));
				i.first = s;
			} else {
				auto s = make_shared<dcp::SMPTETextAsset>();
				s->set_content_title_text (_film->name());
				s->set_reel_number (reel + 1);
				i.first = s;
			}
		}

		if (!_film->interop() || _include_font) {
			for (auto j: _player.get_subtitle_fonts()) {
				i.first->add_font(j->id(), j->data().get_value_or(_default_font));
			}
		}

		i.first->write (i.second);
		++reel;
	}
}


void
SubtitleFilmEncoder::text(PlayerText subs, TextType type, optional<DCPTextTrack> track, dcpomatic::DCPTimePeriod period)
{
	if (type != TextType::OPEN_SUBTITLE) {
		return;
	}

	if (!_assets[_reel_index].first) {
		shared_ptr<dcp::TextAsset> asset;
		auto const lang = _film->open_text_languages();
		if (_film->interop ()) {
			auto s = make_shared<dcp::InteropTextAsset>();
			s->set_movie_title (_film->name());
			if (lang.first) {
				s->set_language(lang.first->as_string());
			}
			s->set_reel_number(fmt::to_string(_reel_index + 1));
			_assets[_reel_index].first = s;
		} else {
			auto s = make_shared<dcp::SMPTETextAsset>();
			s->set_content_title_text (_film->name());
			if (lang.first) {
				s->set_language (*lang.first);
			} else if (track->language) {
				s->set_language (track->language.get());
			}
			s->set_edit_rate (dcp::Fraction (_film->video_frame_rate(), 1));
			s->set_reel_number (_reel_index + 1);
			s->set_time_code_rate (_film->video_frame_rate());
			s->set_start_time (dcp::Time());
			if (_film->encrypted ()) {
				s->set_key (_film->key ());
			}
			_assets[_reel_index].first = s;
		}
	}

	for (auto i: subs.string) {
		/* XXX: couldn't / shouldn't we use period here rather than getting time from the subtitle? */
		i.set_in  (i.in());
		i.set_out (i.out());
		if (_film->interop() && !_include_font) {
			i.unset_font ();
		}
		_assets[_reel_index].first->add(make_shared<dcp::TextString>(i));
	}

	if (_split_reels && (_reel_index < int(_reels.size()) - 1) && period.from > _reels[_reel_index].from) {
		++_reel_index;
	}

	_last = period.from;

	auto job = _job.lock ();
	if (job) {
		job->set_progress (float(period.from.get()) / _length.get());
	}
}


Frame
SubtitleFilmEncoder::frames_done() const
{
	if (!_last) {
		return 0;
	}

	/* XXX: assuming 24fps here but I don't think it matters */
	return _last->seconds() * 24;
}