Get config_dir and output_dir more directly.
[dcpomatic.git] / src / lib / create_cli.cc
1 /*
2     Copyright (C) 2019-2021 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 "create_cli.h"
25 #include "dcp_content_type.h"
26 #include "ratio.h"
27 #include <dcp/raw_convert.h>
28 #include <iostream>
29 #include <string>
30
31
32 using std::string;
33 using std::cout;
34 using boost::optional;
35
36
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
59
60 template <class T>
61 void
62 argument_option (int& n, int argc, char* argv[], string short_name, string long_name, bool* claimed, optional<string>* error, T* out)
63 {
64         string const a = argv[n];
65         if (a != short_name && a != long_name) {
66                 return;
67         }
68
69         if ((n + 1) >= argc) {
70                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
71                 return;
72         }
73
74         *out = dcp::raw_convert<T>(string(argv[++n]));
75         *claimed = true;
76 }
77
78
79 template <class T>
80 void
81 argument_option (
82         int& n,
83         int argc,
84         char* argv[],
85         string short_name,
86         string long_name,
87         bool* claimed,
88         optional<string>* error,
89         boost::optional<T>* out,
90         std::function<boost::optional<T> (string s)> convert = [](string s) { return dcp::raw_convert<T>(s); }
91         )
92 {
93         string const a = argv[n];
94         if (a != short_name && a != long_name) {
95                 return;
96         }
97
98         if ((n + 1) >= argc) {
99                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
100                 return;
101         }
102
103         *out = convert(argv[++n]);
104         *claimed = true;
105 }
106
107
108 CreateCLI::CreateCLI (int argc, char* argv[])
109         : version (false)
110         , encrypt (false)
111         , threed (false)
112         , dcp_content_type (nullptr)
113         , container_ratio (nullptr)
114         , still_length (10)
115         , standard (dcp::Standard::SMPTE)
116         , no_use_isdcf_name (false)
117         , fourk (false)
118 {
119         string dcp_content_type_string = "TST";
120         string container_ratio_string;
121         string standard_string = "SMPTE";
122         int dcp_frame_rate_int = 0;
123         string template_name_string;
124         int j2k_bandwidth_int = 0;
125         auto next_frame_type = VideoFrameType::TWO_D;
126
127         int i = 1;
128         while (i < argc) {
129                 string const a = argv[i];
130                 bool claimed = false;
131
132                 if (a == "-v" || a == "--version") {
133                         version = true;
134                         return;
135                 } else if (a == "-h" || a == "--help") {
136                         error = "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
137                                 "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
138                                 "will be written to stdout.\n" + String::compose(_help, argv[0]);
139                         return;
140                 }
141
142                 if (a == "-e" || a == "--encrypt") {
143                         encrypt = claimed = true;
144                 } else if (a == "--no-use-isdcf-name") {
145                         no_use_isdcf_name = claimed = true;
146                 } else if (a == "--threed") {
147                         threed = claimed = true;
148                 } else if (a == "--left-eye") {
149                         next_frame_type = VideoFrameType::THREE_D_LEFT;
150                         claimed = true;
151                 } else if (a == "--right-eye") {
152                         next_frame_type = VideoFrameType::THREE_D_RIGHT;
153                         claimed = true;
154                 } else if (a == "--fourk") {
155                         fourk = true;
156                         claimed = true;
157                 }
158
159                 std::function<optional<boost::filesystem::path> (string s)> string_to_path = [](string s) {
160                         return boost::filesystem::path(s);
161                 };
162
163                 argument_option(i, argc, argv, "-n", "--name",             &claimed, &error, &name);
164                 argument_option(i, argc, argv, "-t", "--template",         &claimed, &error, &template_name_string);
165                 argument_option(i, argc, argv, "-c", "--dcp-content-type", &claimed, &error, &dcp_content_type_string);
166                 argument_option(i, argc, argv, "-f", "--dcp-frame-rate",   &claimed, &error, &dcp_frame_rate_int);
167                 argument_option(i, argc, argv, "",   "--container-ratio",  &claimed, &error, &container_ratio_string);
168                 argument_option(i, argc, argv, "-s", "--still-length",     &claimed, &error, &still_length);
169                 argument_option(i, argc, argv, "",   "--standard",         &claimed, &error, &standard_string);
170                 argument_option(i, argc, argv, "",   "--config",           &claimed, &error, &config_dir, string_to_path);
171                 argument_option(i, argc, argv, "-o", "--output",           &claimed, &error, &output_dir, string_to_path);
172                 argument_option(i, argc, argv, "",   "--j2k-bandwidth",    &claimed, &error, &j2k_bandwidth_int);
173
174                 if (!claimed) {
175                         if (a.length() > 2 && a.substr(0, 2) == "--") {
176                                 error = String::compose("%1: unrecognised option '%2'", argv[0], a) + String::compose(_help, argv[0]);
177                                 return;
178                         } else {
179                                 Content c;
180                                 c.path = a;
181                                 c.frame_type = next_frame_type;
182                                 content.push_back (c);
183                                 next_frame_type = VideoFrameType::TWO_D;
184                         }
185                 }
186
187                 ++i;
188         }
189
190         if (!template_name_string.empty()) {
191                 template_name = template_name_string;
192         }
193
194         if (dcp_frame_rate_int) {
195                 dcp_frame_rate = dcp_frame_rate_int;
196         }
197
198         if (j2k_bandwidth_int) {
199                 j2k_bandwidth = j2k_bandwidth_int * 1000000;
200         }
201
202         dcp_content_type = DCPContentType::from_isdcf_name(dcp_content_type_string);
203         if (!dcp_content_type) {
204                 error = String::compose("%1: unrecognised DCP content type '%2'", argv[0], dcp_content_type_string);
205                 return;
206         }
207
208         if (!container_ratio_string.empty()) {
209                 container_ratio = Ratio::from_id (container_ratio_string);
210                 if (!container_ratio) {
211                         error = String::compose("%1: unrecognised container ratio %2", argv[0], container_ratio_string);
212                         return;
213                 }
214         }
215
216         if (standard_string != "SMPTE" && standard_string != "interop") {
217                 error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
218                 return;
219         }
220
221         if (standard_string == "interop") {
222                 standard = dcp::Standard::INTEROP;
223         }
224
225         if (content.empty()) {
226                 error = String::compose("%1: no content specified", argv[0]);
227                 return;
228         }
229
230         if (name.empty()) {
231                 name = content[0].path.leaf().string();
232         }
233
234         if (j2k_bandwidth && (*j2k_bandwidth < 10000000 || *j2k_bandwidth > Config::instance()->maximum_j2k_bandwidth())) {
235                 error = String::compose("%1: j2k-bandwidth must be between 10 and %2 Mbit/s", argv[0], (Config::instance()->maximum_j2k_bandwidth() / 1000000));
236                 return;
237         }
238 }