summaryrefslogtreecommitdiff
path: root/src/lib/map_cli.cc
blob: be3841deb187938e996f9040a18ba276c7dcf665 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
    Copyright (C) 2023 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 "compose.hpp"
#include "config.h"
#include "util.h"
#include <dcp/cpl.h>
#include <dcp/dcp.h>
#include <dcp/interop_subtitle_asset.h>
#include <dcp/filesystem.h>
#include <dcp/font_asset.h>
#include <dcp/mono_j2k_picture_asset.h>
#include <dcp/reel.h>
#include <dcp/reel_atmos_asset.h>
#include <dcp/reel_closed_caption_asset.h>
#include <dcp/reel_file_asset.h>
#include <dcp/reel_picture_asset.h>
#include <dcp/reel_sound_asset.h>
#include <dcp/reel_subtitle_asset.h>
#include <dcp/smpte_subtitle_asset.h>
#include <dcp/sound_asset.h>
#include <dcp/stereo_j2k_picture_asset.h>
#include <boost/optional.hpp>
#include <getopt.h>
#include <algorithm>
#include <memory>
#include <string>


using std::dynamic_pointer_cast;
using std::shared_ptr;
using std::string;
using std::make_shared;
using std::vector;
using boost::optional;


static void
help(std::function<void (string)> out)
{
	out(String::compose("Syntax: %1 [OPTION} <cpl-file|ID> [<cpl-file|ID> ... ]", program_name));
	out("  -V, --version    show libdcp version");
	out("  -h, --help       show this help");
	out("  -o, --output     output directory");
	out("  -l, --hard-link  using hard links instead of copying");
	out("  -s, --soft-link  using soft links instead of copying");
	out("  -d, --assets-dir look in this directory for assets (can be given more than once)");
	out("  -r, --rename     rename all files to <uuid>.<mxf|xml>");
	out("  --config <dir>   directory containing config.xml and cinemas.xml");
}


optional<string>
map_cli(int argc, char* argv[], std::function<void (string)> out)
{
	optional<boost::filesystem::path> output_dir;
	bool hard_link = false;
	bool soft_link = false;
	bool rename = false;
	vector<boost::filesystem::path> assets_dir;
	optional<boost::filesystem::path> config_dir;

	/* This makes it possible to call getopt several times in the same executable, for tests */
	optind = 0;

	int option_index = 0;
	while (true) {
		static struct option long_options[] = {
			{ "help", no_argument, 0, 'h' },
			{ "output", required_argument, 0, 'o' },
			{ "hard-link", no_argument, 0, 'l' },
			{ "soft-link", no_argument, 0, 's' },
			{ "assets-dir", required_argument, 0, 'd' },
			{ "rename", no_argument, 0, 'r' },
			{ "config", required_argument, 0, 'c' },
			{ 0, 0, 0, 0 }
		};

		int c = getopt_long(argc, argv, "ho:lsd:rc:", long_options, &option_index);

		if (c == -1) {
			break;
		} else if (c == '?' || c == ':') {
			exit(EXIT_FAILURE);
		}

		switch (c) {
		case 'h':
			help(out);
			exit(EXIT_SUCCESS);
		case 'o':
			output_dir = optarg;
			break;
		case 'l':
			hard_link = true;
			break;
		case 's':
			soft_link = true;
			break;
		case 'd':
			assets_dir.push_back(optarg);
			break;
		case 'r':
			rename = true;
			break;
		case 'c':
			config_dir = optarg;
			break;
		}
	}

	program_name = argv[0];

	if (argc <= optind) {
		help(out);
		exit(EXIT_FAILURE);
	}

	if (config_dir) {
		State::override_path = *config_dir;
	}

	vector<string> cpl_filenames_or_ids;
	for (int i = optind; i < argc; ++i) {
		cpl_filenames_or_ids.push_back(argv[i]);
	}

	if (cpl_filenames_or_ids.empty()) {
		return string{"No CPL specified."};
	}

	if (!output_dir) {
		return string{"Missing -o or --output"};
	}

	if (dcp::filesystem::exists(*output_dir)) {
		return String::compose("Output directory %1 already exists.", *output_dir);
	}

	if (hard_link && soft_link) {
		return string{"Specify either -s,--soft-link or -l,--hard-link, not both."};
	}

	boost::system::error_code ec;
	dcp::filesystem::create_directory(*output_dir, ec);
	if (ec) {
		return String::compose("Could not create output directory %1: %2", *output_dir, ec.message());
	}

	/* Find all the assets in the asset directories.  This assumes that the asset directories are in fact
	 * DCPs (with AssetMaps and so on).  We could search for assets ourselves here but interop fonts are
	 * a little tricky because they don't contain their own UUID within the DCP.
	 */
	vector<shared_ptr<dcp::Asset>> assets;
	for (auto dir: assets_dir) {
		dcp::DCP dcp(dir);
		dcp.read();
		auto dcp_assets = dcp.assets(true);
		std::copy(dcp_assets.begin(), dcp_assets.end(), back_inserter(assets));
	}

	dcp::DCP dcp(*output_dir);

	/* Find all the CPLs */
	vector<shared_ptr<dcp::CPL>> cpls;
	for (auto filename_or_id: cpl_filenames_or_ids) {
		if (boost::filesystem::exists(filename_or_id)) {
			try {
				auto cpl = make_shared<dcp::CPL>(filename_or_id);
				cpl->resolve_refs(assets);
				cpls.push_back(cpl);
			} catch (std::exception& e) {
				return String::compose("Could not read CPL %1: %2", filename_or_id, e.what());
			}
		} else {
			auto cpl_iter = std::find_if(assets.begin(), assets.end(), [filename_or_id](shared_ptr<dcp::Asset> asset) {
				return asset->id() == filename_or_id;
			});
			if (cpl_iter == assets.end()) {
				return String::compose("Could not find CPL with ID %1", filename_or_id);
			}
			if (auto cpl = dynamic_pointer_cast<dcp::CPL>(*cpl_iter)) {
				cpl->resolve_refs(assets);
				cpls.push_back(cpl);
			} else {
				return String::compose("Could not find CPL with ID %1", filename_or_id);
			}
		}
	}

	class CopyError : public std::runtime_error
	{
	public:
		CopyError(std::string message) : std::runtime_error(message) {}
	};

	vector<string> already_copied;

	auto copy = [](
		boost::filesystem::path input_path,
		boost::filesystem::path output_path,
		bool hard_link,
		bool soft_link
		) {
		dcp::filesystem::create_directories(output_path.parent_path());

		boost::system::error_code ec;
		if (hard_link) {
			dcp::filesystem::create_hard_link(input_path, output_path, ec);
			if (ec) {
				throw CopyError(String::compose("Could not hard-link asset %1: %2", input_path.string(), ec.message()));
			}
		} else if (soft_link) {
			dcp::filesystem::create_symlink(input_path, output_path, ec);
			if (ec) {
				throw CopyError(String::compose("Could not soft-link asset %1: %2", input_path.string(), ec.message()));
			}
		} else {
			dcp::filesystem::copy_file(input_path, output_path, ec);
			if (ec) {
				throw CopyError(String::compose("Could not copy asset %1: %2", input_path.string(), ec.message()));
			}
		}
	};

	auto maybe_copy = [&assets, &already_copied, output_dir, copy](
		string asset_id,
		bool rename,
		bool hard_link,
		bool soft_link,
		boost::optional<boost::filesystem::path> extra = boost::none
		) {

		if (std::find(already_copied.begin(), already_copied.end(), asset_id) != already_copied.end()) {
			return;
		}

		auto iter = std::find_if(assets.begin(), assets.end(), [asset_id](shared_ptr<const dcp::Asset> a) { return a->id() == asset_id; });
		if (iter != assets.end()) {
			DCP_ASSERT((*iter)->file());

			auto const input_path = (*iter)->file().get();
			boost::filesystem::path output_path = *output_dir;
			if (extra) {
				output_path /= *extra;
			}

			if (rename) {
				output_path /= String::compose("%1%2", (*iter)->id(), dcp::filesystem::extension((*iter)->file().get()));
				(*iter)->rename_file(output_path);
			} else {
				output_path /= (*iter)->file()->filename();
			}

			copy(input_path, output_path, hard_link, soft_link);
			(*iter)->set_file_preserving_hash(output_path);
			already_copied.push_back(asset_id);
		} else {
			boost::system::error_code ec;
			dcp::filesystem::remove_all(*output_dir, ec);
			throw CopyError(String::compose("Could not find required asset %1", asset_id));
		}
	};

	auto maybe_copy_from_reel = [output_dir, &maybe_copy](
		shared_ptr<dcp::ReelFileAsset> asset,
		bool rename,
		bool hard_link,
		bool soft_link,
		boost::optional<boost::filesystem::path> extra = boost::none
		) {
		if (asset && asset->asset_ref().resolved()) {
			maybe_copy(asset->asset_ref().id(), rename, hard_link, soft_link, extra);
		}
	};

	auto maybe_copy_font_and_images = [&maybe_copy, output_dir, copy](shared_ptr<const dcp::SubtitleAsset> asset, bool rename, bool hard_link, bool soft_link) {
		auto interop = dynamic_pointer_cast<const dcp::InteropSubtitleAsset>(asset);
		boost::optional<boost::filesystem::path> extra;
		if (interop) {
			extra = interop->id();
			for (auto font_asset: interop->font_assets()) {
				maybe_copy(font_asset->id(), rename, hard_link, soft_link, extra);
			}
			for (auto subtitle: interop->subtitles()) {
				if (auto image = dynamic_pointer_cast<const dcp::SubtitleImage>(subtitle)) {
					auto const output_path = *output_dir / asset->id() / image->file()->filename();
					copy(*image->file(), output_path, hard_link, soft_link);
				}
			}
		}
		return extra;
	};

	/* Copy assets that the CPLs need */
	try {
		for (auto cpl: cpls) {
			for (auto reel: cpl->reels()) {
				maybe_copy_from_reel(reel->main_picture(), rename, hard_link, soft_link);
				maybe_copy_from_reel(reel->main_sound(), rename, hard_link, soft_link);
				if (reel->main_subtitle()) {
					auto extra = maybe_copy_font_and_images(reel->main_subtitle()->asset(), rename, hard_link, soft_link);
					maybe_copy_from_reel(reel->main_subtitle(), rename, hard_link, soft_link, extra);
				}
				for (auto ccap: reel->closed_captions()) {
					auto extra = maybe_copy_font_and_images(ccap->asset(), rename, hard_link, soft_link);
					maybe_copy_from_reel(ccap, rename, hard_link, soft_link, extra);
				}
				maybe_copy_from_reel(reel->atmos(), rename, hard_link, soft_link);
			}

			dcp.add(cpl);
		}
	} catch (CopyError& e) {
		return string{e.what()};
	}

	dcp.resolve_refs(assets);
	dcp.set_annotation_text(cpls[0]->annotation_text().get_value_or(""));
	try {
		dcp.set_creator(Config::instance()->dcp_creator());
		dcp.set_issuer(Config::instance()->dcp_issuer());
		dcp.write_xml(Config::instance()->signer_chain());
	} catch (dcp::UnresolvedRefError& e) {
		return String::compose("%1\nPerhaps you need to give a -d parameter to say where this asset is located.", e.what());
	}

	return {};
}