2 Copyright (C) 2023 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"
27 #include <dcp/interop_subtitle_asset.h>
28 #include <dcp/font_asset.h>
29 #include <dcp/mono_picture_asset.h>
31 #include <dcp/reel_atmos_asset.h>
32 #include <dcp/reel_closed_caption_asset.h>
33 #include <dcp/reel_file_asset.h>
34 #include <dcp/reel_picture_asset.h>
35 #include <dcp/reel_sound_asset.h>
36 #include <dcp/reel_subtitle_asset.h>
37 #include <dcp/smpte_subtitle_asset.h>
38 #include <dcp/sound_asset.h>
39 #include <dcp/stereo_picture_asset.h>
40 #include <boost/optional.hpp>
47 using std::dynamic_pointer_cast;
48 using std::shared_ptr;
50 using std::make_shared;
52 using boost::optional;
56 help(std::function<void (string)> out)
58 out(String::compose("Syntax: %1 [OPTION} <cpl-file> [<cpl-file> ... ]", program_name));
59 out(" -V, --version show libdcp version");
60 out(" -h, --help show this help");
61 out(" -o, --output output directory");
62 out(" -l, --hard-link using hard links instead of copying");
63 out(" -s, --soft-link using soft links instead of copying");
64 out(" -d, --assets-dir look in this directory for assets (can be given more than once)");
65 out(" -r, --rename rename all files to <uuid>.<mxf|xml>");
66 out(" --config <dir> directory containing config.xml and cinemas.xml");
71 map_cli(int argc, char* argv[], std::function<void (string)> out)
73 optional<boost::filesystem::path> output_dir;
74 bool hard_link = false;
75 bool soft_link = false;
77 vector<boost::filesystem::path> assets_dir;
78 optional<boost::filesystem::path> config_dir;
80 /* This makes it possible to call getopt several times in the same executable, for tests */
85 static struct option long_options[] = {
86 { "help", no_argument, 0, 'h' },
87 { "output", required_argument, 0, 'o' },
88 { "hard-link", no_argument, 0, 'l' },
89 { "soft-link", no_argument, 0, 's' },
90 { "assets-dir", required_argument, 0, 'd' },
91 { "rename", no_argument, 0, 'r' },
92 { "config", required_argument, 0, 'c' },
96 int c = getopt_long(argc, argv, "ho:lsd:rc:", long_options, &option_index);
100 } else if (c == '?' || c == ':') {
118 assets_dir.push_back(optarg);
129 program_name = argv[0];
131 if (argc <= optind) {
137 State::override_path = *config_dir;
140 vector<boost::filesystem::path> cpl_filenames;
141 for (int i = optind; i < argc; ++i) {
142 cpl_filenames.push_back(argv[i]);
145 if (cpl_filenames.empty()) {
146 return string{"No CPL specified."};
150 return string{"Missing -o or --output"};
153 if (boost::filesystem::exists(*output_dir)) {
154 return String::compose("Output directory %1 already exists.", *output_dir);
157 if (hard_link && soft_link) {
158 return string{"Specify either -s,--soft-link or -l,--hard-link, not both."};
161 boost::system::error_code ec;
162 boost::filesystem::create_directory(*output_dir, ec);
164 return String::compose("Could not create output directory %1: %2", *output_dir, ec.message());
167 /* Find all the assets in the asset directories. This assumes that the asset directories are in fact
168 * DCPs (with AssetMaps and so on). We could search for assets ourselves here but interop fonts are
169 * a little tricky because they don't contain their own UUID within the DCP.
171 vector<shared_ptr<dcp::Asset>> assets;
172 for (auto dir: assets_dir) {
175 auto dcp_assets = dcp.assets(true);
176 std::copy(dcp_assets.begin(), dcp_assets.end(), back_inserter(assets));
179 dcp::DCP dcp(*output_dir);
181 /* Find all the CPLs */
182 vector<shared_ptr<dcp::CPL>> cpls;
183 for (auto filename: cpl_filenames) {
185 auto cpl = make_shared<dcp::CPL>(filename);
186 cpl->resolve_refs(assets);
188 } catch (std::exception& e) {
189 return String::compose("Could not read CPL %1: %2", filename, e.what());
193 class CopyError : public std::runtime_error
196 CopyError(std::string message) : std::runtime_error(message) {}
199 vector<string> already_copied;
201 auto maybe_copy = [&assets, &already_copied, output_dir](
206 boost::optional<boost::filesystem::path> extra = boost::none
209 if (std::find(already_copied.begin(), already_copied.end(), asset_id) != already_copied.end()) {
213 auto iter = std::find_if(assets.begin(), assets.end(), [asset_id](shared_ptr<const dcp::Asset> a) { return a->id() == asset_id; });
214 if (iter != assets.end()) {
215 DCP_ASSERT((*iter)->file());
217 auto const input_path = (*iter)->file().get();
218 boost::filesystem::path output_path = *output_dir;
220 output_path /= *extra;
224 output_path /= String::compose("%1%2", (*iter)->id(), boost::filesystem::extension((*iter)->file().get()));
225 (*iter)->rename_file(output_path);
227 output_path /= (*iter)->file()->filename();
230 boost::filesystem::create_directories(output_path.parent_path());
232 boost::system::error_code ec;
234 boost::filesystem::create_hard_link(input_path, output_path, ec);
236 throw CopyError(String::compose("Could not hard-link asset %1: %2", input_path.string(), ec.message()));
238 } else if (soft_link) {
239 boost::filesystem::create_symlink(input_path, output_path, ec);
241 throw CopyError(String::compose("Could not soft-link asset %1: %2", input_path.string(), ec.message()));
244 boost::filesystem::copy_file(input_path, output_path, ec);
246 throw CopyError(String::compose("Could not copy asset %1: %2", input_path.string(), ec.message()));
249 (*iter)->set_file_preserving_hash(output_path);
250 already_copied.push_back(asset_id);
252 boost::system::error_code ec;
253 boost::filesystem::remove_all(*output_dir, ec);
254 throw CopyError(String::compose("Could not find required asset %1", asset_id));
258 auto maybe_copy_from_reel = [output_dir, &maybe_copy](
259 shared_ptr<dcp::ReelFileAsset> asset,
263 boost::optional<boost::filesystem::path> extra = boost::none
265 if (asset && asset->asset_ref().resolved()) {
266 maybe_copy(asset->asset_ref().id(), rename, hard_link, soft_link, extra);
270 /* Copy assets that the CPLs need */
272 for (auto cpl: cpls) {
273 for (auto reel: cpl->reels()) {
274 maybe_copy_from_reel(reel->main_picture(), rename, hard_link, soft_link);
275 maybe_copy_from_reel(reel->main_sound(), rename, hard_link, soft_link);
276 boost::optional<boost::filesystem::path> extra;
277 if (reel->main_subtitle()) {
278 auto interop = dynamic_pointer_cast<dcp::InteropSubtitleAsset>(reel->main_subtitle()->asset());
280 extra = interop->id();
281 for (auto font_asset: interop->font_assets()) {
282 maybe_copy(font_asset->id(), rename, hard_link, soft_link, extra);
286 maybe_copy_from_reel(reel->main_subtitle(), rename, hard_link, soft_link, extra);
287 for (auto ccap: reel->closed_captions()) {
288 maybe_copy_from_reel(ccap, rename, hard_link, soft_link);
290 maybe_copy_from_reel(reel->atmos(), rename, hard_link, soft_link);
295 } catch (CopyError& e) {
296 return string{e.what()};
299 dcp.resolve_refs(assets);
300 dcp.set_annotation_text(cpls[0]->annotation_text().get_value_or(""));
302 dcp.write_xml(Config::instance()->signer_chain());
303 } catch (dcp::UnresolvedRefError& e) {
304 return String::compose("%1\nPerhaps you need to give a -d parameter to say where this asset is located.", e.what());