Note that newer libsub version is required.
[dcpomatic.git] / src / lib / map_cli.cc
index c95d7f1a1aaa4c2f3bb2b5f2eda6ddf2b80c7bb0..b158499a8d204628a87ddeb275d102d5f9556242 100644 (file)
@@ -25,6 +25,7 @@
 #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_picture_asset.h>
 #include <dcp/reel.h>
@@ -55,7 +56,7 @@ using boost::optional;
 static void
 help(std::function<void (string)> out)
 {
-       out(String::compose("Syntax: %1 [OPTION} <cpl-file> [<cpl-file> ... ]", program_name));
+       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");
@@ -137,12 +138,12 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
                State::override_path = *config_dir;
        }
 
-       vector<boost::filesystem::path> cpl_filenames;
+       vector<string> cpl_filenames_or_ids;
        for (int i = optind; i < argc; ++i) {
-               cpl_filenames.push_back(argv[i]);
+               cpl_filenames_or_ids.push_back(argv[i]);
        }
 
-       if (cpl_filenames.empty()) {
+       if (cpl_filenames_or_ids.empty()) {
                return string{"No CPL specified."};
        }
 
@@ -150,7 +151,7 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
                return string{"Missing -o or --output"};
        }
 
-       if (boost::filesystem::exists(*output_dir)) {
+       if (dcp::filesystem::exists(*output_dir)) {
                return String::compose("Output directory %1 already exists.", *output_dir);
        }
 
@@ -159,7 +160,7 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
        }
 
        boost::system::error_code ec;
-       boost::filesystem::create_directory(*output_dir, ec);
+       dcp::filesystem::create_directory(*output_dir, ec);
        if (ec) {
                return String::compose("Could not create output directory %1: %2", *output_dir, ec.message());
        }
@@ -180,13 +181,28 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
 
        /* Find all the CPLs */
        vector<shared_ptr<dcp::CPL>> cpls;
-       for (auto filename: cpl_filenames) {
-               try {
-                       auto cpl = make_shared<dcp::CPL>(filename);
-                       cpl->resolve_refs(assets);
-                       cpls.push_back(cpl);
-               } catch (std::exception& e) {
-                       return String::compose("Could not read CPL %1: %2", filename, e.what());
+       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);
+                       }
                }
        }
 
@@ -196,13 +212,47 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
                CopyError(std::string message) : std::runtime_error(message) {}
        };
 
-       auto maybe_copy = [&assets, output_dir](
+       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());
@@ -214,35 +264,18 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
                        }
 
                        if (rename) {
-                               output_path /= String::compose("%1%2", (*iter)->id(), boost::filesystem::extension((*iter)->file().get()));
+                               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();
                        }
 
-                       boost::filesystem::create_directories(output_path.parent_path());
-
-                       boost::system::error_code ec;
-                       if (hard_link) {
-                               boost::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) {
-                               boost::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 {
-                               boost::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()));
-                               }
-                       }
-                       (*iter)->set_file(output_path);
+                       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;
-                       boost::filesystem::remove_all(*output_dir, ec);
+                       dcp::filesystem::remove_all(*output_dir, ec);
                        throw CopyError(String::compose("Could not find required asset %1", asset_id));
                }
        };
@@ -259,25 +292,37 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
                }
        };
 
+       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);
-                               boost::optional<boost::filesystem::path> extra;
                                if (reel->main_subtitle()) {
-                                       auto interop = dynamic_pointer_cast<dcp::InteropSubtitleAsset>(reel->main_subtitle()->asset());
-                                       if (interop) {
-                                               extra = interop->id();
-                                               for (auto font_asset: interop->font_assets()) {
-                                                       maybe_copy(font_asset->id(), rename, hard_link, soft_link, extra);
-                                               }
-                                       }
+                                       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);
                                }
-                               maybe_copy_from_reel(reel->main_subtitle(), rename, hard_link, soft_link, extra);
                                for (auto ccap: reel->closed_captions()) {
-                                       maybe_copy_from_reel(ccap, rename, hard_link, soft_link);
+                                       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);
                        }
@@ -290,7 +335,13 @@ map_cli(int argc, char* argv[], std::function<void (string)> out)
 
        dcp.resolve_refs(assets);
        dcp.set_annotation_text(cpls[0]->annotation_text().get_value_or(""));
-       dcp.write_xml(Config::instance()->signer_chain());
+       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 {};
 }