Extract copy().
[dcpomatic.git] / src / lib / map_cli.cc
1 /*
2     Copyright (C) 2023 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
22 #include "compose.hpp"
23 #include "config.h"
24 #include "util.h"
25 #include <dcp/cpl.h>
26 #include <dcp/dcp.h>
27 #include <dcp/interop_subtitle_asset.h>
28 #include <dcp/filesystem.h>
29 #include <dcp/font_asset.h>
30 #include <dcp/mono_picture_asset.h>
31 #include <dcp/reel.h>
32 #include <dcp/reel_atmos_asset.h>
33 #include <dcp/reel_closed_caption_asset.h>
34 #include <dcp/reel_file_asset.h>
35 #include <dcp/reel_picture_asset.h>
36 #include <dcp/reel_sound_asset.h>
37 #include <dcp/reel_subtitle_asset.h>
38 #include <dcp/smpte_subtitle_asset.h>
39 #include <dcp/sound_asset.h>
40 #include <dcp/stereo_picture_asset.h>
41 #include <boost/optional.hpp>
42 #include <getopt.h>
43 #include <algorithm>
44 #include <memory>
45 #include <string>
46
47
48 using std::dynamic_pointer_cast;
49 using std::shared_ptr;
50 using std::string;
51 using std::make_shared;
52 using std::vector;
53 using boost::optional;
54
55
56 static void
57 help(std::function<void (string)> out)
58 {
59         out(String::compose("Syntax: %1 [OPTION} <cpl-file> [<cpl-file> ... ]", program_name));
60         out("  -V, --version    show libdcp version");
61         out("  -h, --help       show this help");
62         out("  -o, --output     output directory");
63         out("  -l, --hard-link  using hard links instead of copying");
64         out("  -s, --soft-link  using soft links instead of copying");
65         out("  -d, --assets-dir look in this directory for assets (can be given more than once)");
66         out("  -r, --rename     rename all files to <uuid>.<mxf|xml>");
67         out("  --config <dir>   directory containing config.xml and cinemas.xml");
68 }
69
70
71 optional<string>
72 map_cli(int argc, char* argv[], std::function<void (string)> out)
73 {
74         optional<boost::filesystem::path> output_dir;
75         bool hard_link = false;
76         bool soft_link = false;
77         bool rename = false;
78         vector<boost::filesystem::path> assets_dir;
79         optional<boost::filesystem::path> config_dir;
80
81         /* This makes it possible to call getopt several times in the same executable, for tests */
82         optind = 0;
83
84         int option_index = 0;
85         while (true) {
86                 static struct option long_options[] = {
87                         { "help", no_argument, 0, 'h' },
88                         { "output", required_argument, 0, 'o' },
89                         { "hard-link", no_argument, 0, 'l' },
90                         { "soft-link", no_argument, 0, 's' },
91                         { "assets-dir", required_argument, 0, 'd' },
92                         { "rename", no_argument, 0, 'r' },
93                         { "config", required_argument, 0, 'c' },
94                         { 0, 0, 0, 0 }
95                 };
96
97                 int c = getopt_long(argc, argv, "ho:lsd:rc:", long_options, &option_index);
98
99                 if (c == -1) {
100                         break;
101                 } else if (c == '?' || c == ':') {
102                         exit(EXIT_FAILURE);
103                 }
104
105                 switch (c) {
106                 case 'h':
107                         help(out);
108                         exit(EXIT_SUCCESS);
109                 case 'o':
110                         output_dir = optarg;
111                         break;
112                 case 'l':
113                         hard_link = true;
114                         break;
115                 case 's':
116                         soft_link = true;
117                         break;
118                 case 'd':
119                         assets_dir.push_back(optarg);
120                         break;
121                 case 'r':
122                         rename = true;
123                         break;
124                 case 'c':
125                         config_dir = optarg;
126                         break;
127                 }
128         }
129
130         program_name = argv[0];
131
132         if (argc <= optind) {
133                 help(out);
134                 exit(EXIT_FAILURE);
135         }
136
137         if (config_dir) {
138                 State::override_path = *config_dir;
139         }
140
141         vector<boost::filesystem::path> cpl_filenames;
142         for (int i = optind; i < argc; ++i) {
143                 cpl_filenames.push_back(argv[i]);
144         }
145
146         if (cpl_filenames.empty()) {
147                 return string{"No CPL specified."};
148         }
149
150         if (!output_dir) {
151                 return string{"Missing -o or --output"};
152         }
153
154         if (dcp::filesystem::exists(*output_dir)) {
155                 return String::compose("Output directory %1 already exists.", *output_dir);
156         }
157
158         if (hard_link && soft_link) {
159                 return string{"Specify either -s,--soft-link or -l,--hard-link, not both."};
160         }
161
162         boost::system::error_code ec;
163         dcp::filesystem::create_directory(*output_dir, ec);
164         if (ec) {
165                 return String::compose("Could not create output directory %1: %2", *output_dir, ec.message());
166         }
167
168         /* Find all the assets in the asset directories.  This assumes that the asset directories are in fact
169          * DCPs (with AssetMaps and so on).  We could search for assets ourselves here but interop fonts are
170          * a little tricky because they don't contain their own UUID within the DCP.
171          */
172         vector<shared_ptr<dcp::Asset>> assets;
173         for (auto dir: assets_dir) {
174                 dcp::DCP dcp(dir);
175                 dcp.read();
176                 auto dcp_assets = dcp.assets(true);
177                 std::copy(dcp_assets.begin(), dcp_assets.end(), back_inserter(assets));
178         }
179
180         dcp::DCP dcp(*output_dir);
181
182         /* Find all the CPLs */
183         vector<shared_ptr<dcp::CPL>> cpls;
184         for (auto filename: cpl_filenames) {
185                 try {
186                         auto cpl = make_shared<dcp::CPL>(filename);
187                         cpl->resolve_refs(assets);
188                         cpls.push_back(cpl);
189                 } catch (std::exception& e) {
190                         return String::compose("Could not read CPL %1: %2", filename, e.what());
191                 }
192         }
193
194         class CopyError : public std::runtime_error
195         {
196         public:
197                 CopyError(std::string message) : std::runtime_error(message) {}
198         };
199
200         vector<string> already_copied;
201
202         auto copy = [](
203                 boost::filesystem::path input_path,
204                 boost::filesystem::path output_path,
205                 bool hard_link,
206                 bool soft_link
207                 ) {
208                 dcp::filesystem::create_directories(output_path.parent_path());
209
210                 boost::system::error_code ec;
211                 if (hard_link) {
212                         dcp::filesystem::create_hard_link(input_path, output_path, ec);
213                         if (ec) {
214                                 throw CopyError(String::compose("Could not hard-link asset %1: %2", input_path.string(), ec.message()));
215                         }
216                 } else if (soft_link) {
217                         dcp::filesystem::create_symlink(input_path, output_path, ec);
218                         if (ec) {
219                                 throw CopyError(String::compose("Could not soft-link asset %1: %2", input_path.string(), ec.message()));
220                         }
221                 } else {
222                         dcp::filesystem::copy_file(input_path, output_path, ec);
223                         if (ec) {
224                                 throw CopyError(String::compose("Could not copy asset %1: %2", input_path.string(), ec.message()));
225                         }
226                 }
227         };
228
229         auto maybe_copy = [&assets, &already_copied, output_dir, copy](
230                 string asset_id,
231                 bool rename,
232                 bool hard_link,
233                 bool soft_link,
234                 boost::optional<boost::filesystem::path> extra = boost::none
235                 ) {
236
237                 if (std::find(already_copied.begin(), already_copied.end(), asset_id) != already_copied.end()) {
238                         return;
239                 }
240
241                 auto iter = std::find_if(assets.begin(), assets.end(), [asset_id](shared_ptr<const dcp::Asset> a) { return a->id() == asset_id; });
242                 if (iter != assets.end()) {
243                         DCP_ASSERT((*iter)->file());
244
245                         auto const input_path = (*iter)->file().get();
246                         boost::filesystem::path output_path = *output_dir;
247                         if (extra) {
248                                 output_path /= *extra;
249                         }
250
251                         if (rename) {
252                                 output_path /= String::compose("%1%2", (*iter)->id(), dcp::filesystem::extension((*iter)->file().get()));
253                                 (*iter)->rename_file(output_path);
254                         } else {
255                                 output_path /= (*iter)->file()->filename();
256                         }
257
258                         copy(input_path, output_path, hard_link, soft_link);
259                         (*iter)->set_file_preserving_hash(output_path);
260                         already_copied.push_back(asset_id);
261                 } else {
262                         boost::system::error_code ec;
263                         dcp::filesystem::remove_all(*output_dir, ec);
264                         throw CopyError(String::compose("Could not find required asset %1", asset_id));
265                 }
266         };
267
268         auto maybe_copy_from_reel = [output_dir, &maybe_copy](
269                 shared_ptr<dcp::ReelFileAsset> asset,
270                 bool rename,
271                 bool hard_link,
272                 bool soft_link,
273                 boost::optional<boost::filesystem::path> extra = boost::none
274                 ) {
275                 if (asset && asset->asset_ref().resolved()) {
276                         maybe_copy(asset->asset_ref().id(), rename, hard_link, soft_link, extra);
277                 }
278         };
279
280         auto maybe_copy_font = [&maybe_copy](shared_ptr<const dcp::SubtitleAsset> asset, bool rename, bool hard_link, bool soft_link) {
281                 auto interop = dynamic_pointer_cast<const dcp::InteropSubtitleAsset>(asset);
282                 boost::optional<boost::filesystem::path> extra;
283                 if (interop) {
284                         extra = interop->id();
285                         for (auto font_asset: interop->font_assets()) {
286                                 maybe_copy(font_asset->id(), rename, hard_link, soft_link, extra);
287                         }
288                 }
289                 return extra;
290         };
291
292         /* Copy assets that the CPLs need */
293         try {
294                 for (auto cpl: cpls) {
295                         for (auto reel: cpl->reels()) {
296                                 maybe_copy_from_reel(reel->main_picture(), rename, hard_link, soft_link);
297                                 maybe_copy_from_reel(reel->main_sound(), rename, hard_link, soft_link);
298                                 if (reel->main_subtitle()) {
299                                         auto extra = maybe_copy_font(reel->main_subtitle()->asset(), rename, hard_link, soft_link);
300                                         maybe_copy_from_reel(reel->main_subtitle(), rename, hard_link, soft_link, extra);
301                                 }
302                                 for (auto ccap: reel->closed_captions()) {
303                                         auto extra = maybe_copy_font(ccap->asset(), rename, hard_link, soft_link);
304                                         maybe_copy_from_reel(ccap, rename, hard_link, soft_link, extra);
305                                 }
306                                 maybe_copy_from_reel(reel->atmos(), rename, hard_link, soft_link);
307                         }
308
309                         dcp.add(cpl);
310                 }
311         } catch (CopyError& e) {
312                 return string{e.what()};
313         }
314
315         dcp.resolve_refs(assets);
316         dcp.set_annotation_text(cpls[0]->annotation_text().get_value_or(""));
317         try {
318                 dcp.set_creator(Config::instance()->dcp_creator());
319                 dcp.set_issuer(Config::instance()->dcp_issuer());
320                 dcp.write_xml(Config::instance()->signer_chain());
321         } catch (dcp::UnresolvedRefError& e) {
322                 return String::compose("%1\nPerhaps you need to give a -d parameter to say where this asset is located.", e.what());
323         }
324
325         return {};
326 }
327