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>");
70 map_cli(int argc, char* argv[], std::function<void (string)> out)
72 optional<boost::filesystem::path> output_dir;
73 bool hard_link = false;
74 bool soft_link = false;
76 vector<boost::filesystem::path> assets_dir;
78 /* This makes it possible to call getopt several times in the same executable, for tests */
83 static struct option long_options[] = {
84 { "help", no_argument, 0, 'h' },
85 { "output", required_argument, 0, 'o' },
86 { "hard-link", no_argument, 0, 'l' },
87 { "soft-link", no_argument, 0, 's' },
88 { "assets-dir", required_argument, 0, 'd' },
89 { "rename", no_argument, 0, 'r' },
93 int c = getopt_long(argc, argv, "ho:lsd:r", long_options, &option_index);
97 } else if (c == '?' || c == ':') {
115 assets_dir.push_back(optarg);
123 program_name = argv[0];
125 if (argc <= optind) {
130 vector<boost::filesystem::path> cpl_filenames;
131 for (int i = optind; i < argc; ++i) {
132 cpl_filenames.push_back(argv[i]);
135 if (cpl_filenames.empty()) {
136 return string{"No CPL specified."};
140 return string{"Missing -o or --output"};
143 if (boost::filesystem::exists(*output_dir)) {
144 return String::compose("Output directory %1 already exists.", *output_dir);
147 if (hard_link && soft_link) {
148 return string{"Specify either -s,--soft-link or -l,--hard-link, not both."};
151 boost::system::error_code ec;
152 boost::filesystem::create_directory(*output_dir, ec);
154 return String::compose("Could not create output directory %1: %2", *output_dir, ec.message());
157 /* Find all the assets in the asset directories. This assumes that the asset directories are in fact
158 * DCPs (with AssetMaps and so on). We could search for assets ourselves here but interop fonts are
159 * a little tricky because they don't contain their own UUID within the DCP.
161 vector<shared_ptr<dcp::Asset>> assets;
162 for (auto dir: assets_dir) {
165 auto dcp_assets = dcp.assets(true);
166 std::copy(dcp_assets.begin(), dcp_assets.end(), back_inserter(assets));
169 dcp::DCP dcp(*output_dir);
171 /* Find all the CPLs */
172 vector<shared_ptr<dcp::CPL>> cpls;
173 for (auto filename: cpl_filenames) {
175 auto cpl = make_shared<dcp::CPL>(filename);
176 cpl->resolve_refs(assets);
178 } catch (std::exception& e) {
179 return String::compose("Could not read CPL %1: %2", filename, e.what());
183 class CopyError : public std::runtime_error
186 CopyError(std::string message) : std::runtime_error(message) {}
189 auto maybe_copy = [&assets, output_dir](
194 boost::optional<boost::filesystem::path> extra = boost::none
196 auto iter = std::find_if(assets.begin(), assets.end(), [asset_id](shared_ptr<const dcp::Asset> a) { return a->id() == asset_id; });
197 if (iter != assets.end()) {
198 DCP_ASSERT((*iter)->file());
200 auto const input_path = (*iter)->file().get();
201 boost::filesystem::path output_path = *output_dir;
203 output_path /= *extra;
207 output_path /= String::compose("%1%2", (*iter)->id(), boost::filesystem::extension((*iter)->file().get()));
208 (*iter)->rename_file(output_path);
210 output_path /= (*iter)->file()->filename();
213 boost::filesystem::create_directories(output_path.parent_path());
215 boost::system::error_code ec;
217 boost::filesystem::create_hard_link(input_path, output_path, ec);
219 throw CopyError(String::compose("Could not hard-link asset %1: %2", input_path.string(), ec.message()));
221 } else if (soft_link) {
222 boost::filesystem::create_symlink(input_path, output_path, ec);
224 throw CopyError(String::compose("Could not soft-link asset %1: %2", input_path.string(), ec.message()));
227 boost::filesystem::copy_file(input_path, output_path, ec);
229 throw CopyError(String::compose("Could not copy asset %1: %2", input_path.string(), ec.message()));
232 (*iter)->set_file(output_path);
234 boost::system::error_code ec;
235 boost::filesystem::remove_all(*output_dir, ec);
236 throw CopyError(String::compose("Could not find required asset %1", asset_id));
240 auto maybe_copy_from_reel = [output_dir, &maybe_copy](
241 shared_ptr<dcp::ReelFileAsset> asset,
245 boost::optional<boost::filesystem::path> extra = boost::none
247 if (asset && asset->asset_ref().resolved()) {
248 maybe_copy(asset->asset_ref().id(), rename, hard_link, soft_link, extra);
252 /* Copy assets that the CPLs need */
254 for (auto cpl: cpls) {
255 for (auto reel: cpl->reels()) {
256 maybe_copy_from_reel(reel->main_picture(), rename, hard_link, soft_link);
257 maybe_copy_from_reel(reel->main_sound(), rename, hard_link, soft_link);
258 boost::optional<boost::filesystem::path> extra;
259 if (reel->main_subtitle()) {
260 auto interop = dynamic_pointer_cast<dcp::InteropSubtitleAsset>(reel->main_subtitle()->asset());
262 extra = interop->id();
263 for (auto font_asset: interop->font_assets()) {
264 maybe_copy(font_asset->id(), rename, hard_link, soft_link, extra);
268 maybe_copy_from_reel(reel->main_subtitle(), rename, hard_link, soft_link, extra);
269 for (auto ccap: reel->closed_captions()) {
270 maybe_copy_from_reel(ccap, rename, hard_link, soft_link);
272 maybe_copy_from_reel(reel->atmos(), rename, hard_link, soft_link);
277 } catch (CopyError& e) {
278 return string{e.what()};
281 dcp.resolve_refs(assets);
282 dcp.set_annotation_text(cpls[0]->annotation_text().get_value_or(""));
283 dcp.write_xml(Config::instance()->signer_chain());