#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>
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);
}
}
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());
}
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());
}
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));
}
};
}
};
+ 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);
}
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 {};
}