2 Copyright (C) 2019-2021 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"
24 #include "create_cli.h"
25 #include "dcp_content_type.h"
27 #include <dcp/raw_convert.h>
34 using boost::optional;
37 string CreateCLI::_help =
38 "\nSyntax: %1 [OPTION] <CONTENT> [OPTION] [<CONTENT> ...]\n"
39 " -v, --version show DCP-o-matic version\n"
40 " -h, --help show this help\n"
41 " -n, --name <name> film name\n"
42 " -t, --template <name> template name\n"
43 " -e, --encrypt make an encrypted DCP\n"
44 " -c, --dcp-content-type <type> FTR, SHR, TLR, TST, XSN, RTG, TSR, POL, PSA or ADV\n"
45 " -f, --dcp-frame-rate <rate> set DCP video frame rate (otherwise guessed from content)\n"
46 " --container-ratio <ratio> 119, 133, 137, 138, 166, 178, 185 or 239\n"
47 " -s, --still-length <n> number of seconds that still content should last\n"
48 " --standard <standard> SMPTE or interop (default SMPTE)\n"
49 " --no-use-isdcf-name do not use an ISDCF name; use the specified name unmodified\n"
50 " --no-sign do not sign the DCP\n"
51 " --config <dir> directory containing config.xml and cinemas.xml\n"
52 " --fourk make a 4K DCP rather than a 2K one\n"
53 " -o, --output <dir> output directory\n"
54 " --threed make a 3D DCP\n"
55 " --j2k-bandwidth <Mbit/s> J2K bandwidth in Mbit/s\n"
56 " --left-eye next piece of content is for the left eye\n"
57 " --right-eye next piece of content is for the right eye\n"
58 " --channel <channel> next piece of content should be mapped to audio channel L, R, C, Lfe, Ls or Rs\n";
63 argument_option (int& n, int argc, char* argv[], string short_name, string long_name, bool* claimed, optional<string>* error, T* out)
65 string const a = argv[n];
66 if (a != short_name && a != long_name) {
70 if ((n + 1) >= argc) {
71 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
75 *out = dcp::raw_convert<T>(string(argv[++n]));
89 optional<string>* error,
90 boost::optional<T>* out,
91 std::function<boost::optional<T> (string s)> convert = [](string s) { return dcp::raw_convert<T>(s); }
94 string const a = argv[n];
95 if (a != short_name && a != long_name) {
99 if ((n + 1) >= argc) {
100 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
104 auto const arg = argv[++n];
105 auto const value = convert(arg);
107 *error = String::compose("%1: %2 is not valid for %3", argv[0], arg, long_name);
117 CreateCLI::CreateCLI (int argc, char* argv[])
121 , dcp_content_type (nullptr)
122 , container_ratio (nullptr)
124 , standard (dcp::Standard::SMPTE)
125 , no_use_isdcf_name (false)
128 string dcp_content_type_string = "TST";
129 string container_ratio_string;
130 string standard_string = "SMPTE";
131 int dcp_frame_rate_int = 0;
132 string template_name_string;
133 int j2k_bandwidth_int = 0;
134 auto next_frame_type = VideoFrameType::TWO_D;
135 optional<dcp::Channel> channel;
139 string const a = argv[i];
140 bool claimed = false;
142 if (a == "-v" || a == "--version") {
145 } else if (a == "-h" || a == "--help") {
146 error = "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
147 "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
148 "will be written to stdout.\n" + String::compose(_help, argv[0]);
152 if (a == "-e" || a == "--encrypt") {
153 encrypt = claimed = true;
154 } else if (a == "--no-use-isdcf-name") {
155 no_use_isdcf_name = claimed = true;
156 } else if (a == "--threed") {
157 threed = claimed = true;
158 } else if (a == "--left-eye") {
159 next_frame_type = VideoFrameType::THREE_D_LEFT;
161 } else if (a == "--right-eye") {
162 next_frame_type = VideoFrameType::THREE_D_RIGHT;
164 } else if (a == "--fourk") {
169 std::function<optional<boost::filesystem::path> (string s)> string_to_path = [](string s) {
170 return boost::filesystem::path(s);
173 argument_option(i, argc, argv, "-n", "--name", &claimed, &error, &name);
174 argument_option(i, argc, argv, "-t", "--template", &claimed, &error, &template_name_string);
175 argument_option(i, argc, argv, "-c", "--dcp-content-type", &claimed, &error, &dcp_content_type_string);
176 argument_option(i, argc, argv, "-f", "--dcp-frame-rate", &claimed, &error, &dcp_frame_rate_int);
177 argument_option(i, argc, argv, "", "--container-ratio", &claimed, &error, &container_ratio_string);
178 argument_option(i, argc, argv, "-s", "--still-length", &claimed, &error, &still_length);
179 argument_option(i, argc, argv, "", "--standard", &claimed, &error, &standard_string);
180 argument_option(i, argc, argv, "", "--config", &claimed, &error, &config_dir, string_to_path);
181 argument_option(i, argc, argv, "-o", "--output", &claimed, &error, &output_dir, string_to_path);
182 argument_option(i, argc, argv, "", "--j2k-bandwidth", &claimed, &error, &j2k_bandwidth_int);
184 std::function<optional<dcp::Channel> (string)> convert_channel = [](string channel) -> optional<dcp::Channel>{
185 if (channel == "L") {
186 return dcp::Channel::LEFT;
187 } else if (channel == "R") {
188 return dcp::Channel::RIGHT;
189 } else if (channel == "C") {
190 return dcp::Channel::CENTRE;
191 } else if (channel == "Lfe") {
192 return dcp::Channel::LFE;
193 } else if (channel == "Ls") {
194 return dcp::Channel::LS;
195 } else if (channel == "Rs") {
196 return dcp::Channel::RS;
202 argument_option(i, argc, argv, "", "--channel", &claimed, &error, &channel, convert_channel);
205 if (a.length() > 2 && a.substr(0, 2) == "--") {
206 error = String::compose("%1: unrecognised option '%2'", argv[0], a) + String::compose(_help, argv[0]);
211 c.frame_type = next_frame_type;
213 content.push_back (c);
214 next_frame_type = VideoFrameType::TWO_D;
222 if (!template_name_string.empty()) {
223 template_name = template_name_string;
226 if (dcp_frame_rate_int) {
227 dcp_frame_rate = dcp_frame_rate_int;
230 if (j2k_bandwidth_int) {
231 j2k_bandwidth = j2k_bandwidth_int * 1000000;
234 dcp_content_type = DCPContentType::from_isdcf_name(dcp_content_type_string);
235 if (!dcp_content_type) {
236 error = String::compose("%1: unrecognised DCP content type '%2'", argv[0], dcp_content_type_string);
240 if (!container_ratio_string.empty()) {
241 container_ratio = Ratio::from_id (container_ratio_string);
242 if (!container_ratio) {
243 error = String::compose("%1: unrecognised container ratio %2", argv[0], container_ratio_string);
248 if (standard_string != "SMPTE" && standard_string != "interop") {
249 error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
253 if (standard_string == "interop") {
254 standard = dcp::Standard::INTEROP;
257 if (content.empty()) {
258 error = String::compose("%1: no content specified", argv[0]);
263 name = content[0].path.leaf().string();
266 if (j2k_bandwidth && (*j2k_bandwidth < 10000000 || *j2k_bandwidth > Config::instance()->maximum_j2k_bandwidth())) {
267 error = String::compose("%1: j2k-bandwidth must be between 10 and %2 Mbit/s", argv[0], (Config::instance()->maximum_j2k_bandwidth() / 1000000));