Add --encrypted/-e to dcpomatic_create.
[dcpomatic.git] / src / tools / dcpomatic_create.cc
1 /*
2     Copyright (C) 2013-2017 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 #include "lib/version.h"
22 #include "lib/film.h"
23 #include "lib/util.h"
24 #include "lib/content_factory.h"
25 #include "lib/job_manager.h"
26 #include "lib/signal_manager.h"
27 #include "lib/job.h"
28 #include "lib/dcp_content_type.h"
29 #include "lib/ratio.h"
30 #include "lib/image_content.h"
31 #include "lib/video_content.h"
32 #include "lib/cross.h"
33 #include "lib/config.h"
34 #include "lib/dcp_content.h"
35 #include <dcp/exceptions.h>
36 #include <libxml++/libxml++.h>
37 #include <boost/filesystem.hpp>
38 #include <boost/foreach.hpp>
39 #include <getopt.h>
40 #include <string>
41 #include <iostream>
42 #include <cstdlib>
43 #include <stdexcept>
44
45 using std::string;
46 using std::cout;
47 using std::cerr;
48 using std::list;
49 using std::exception;
50 using boost::shared_ptr;
51 using boost::dynamic_pointer_cast;
52 using boost::optional;
53
54 static void
55 syntax (string n)
56 {
57         cerr << "Syntax: " << n << " [OPTION] <CONTENT> [<CONTENT> ...]\n"
58              << "  -v, --version                 show DCP-o-matic version\n"
59              << "  -h, --help                    show this help\n"
60              << "  -n, --name <name>             film name\n"
61              << "  -t, --template <name>         template name\n"
62              << "  -e, --encrypt                 make an encrypted DCP\n"
63              << "  -c, --dcp-content-type <type> FTR, SHR, TLR, TST, XSN, RTG, TSR, POL, PSA or ADV\n"
64              << "  -f, --dcp-frame-rate <rate>   set DCP video frame rate (otherwise guessed from content)\n"
65              << "      --container-ratio <ratio> 119, 133, 137, 138, 166, 178, 185 or 239\n"
66              << "      --content-ratio <ratio>   119, 133, 137, 138, 166, 178, 185 or 239\n"
67              << "  -s, --still-length <n>        number of seconds that still content should last\n"
68              << "      --standard <standard>     SMPTE or interop (default SMPTE)\n"
69              << "      --no-use-isdcf-name       do not use an ISDCF name; use the specified name unmodified\n"
70              << "      --no-sign                 do not sign the DCP\n"
71              << "      --config <dir>            directory containing config.xml and cinemas.xml\n"
72              << "  -o, --output <dir>            output directory\n";
73 }
74
75 static void
76 help (string n)
77 {
78         cerr << "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
79              << "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
80              << "will be written to stdout.\n";
81
82         syntax (n);
83 }
84
85 class SimpleSignalManager : public SignalManager
86 {
87 public:
88         /* Do nothing in this method so that UI events happen in our thread
89            when we call SignalManager::ui_idle().
90         */
91         void wake_ui () {}
92 };
93
94 int
95 main (int argc, char* argv[])
96 {
97         dcpomatic_setup_path_encoding ();
98         dcpomatic_setup ();
99
100         string name;
101         optional<string> template_name;
102         bool encrypt = false;
103         DCPContentType const * dcp_content_type = DCPContentType::from_isdcf_name ("TST");
104         optional<int> dcp_frame_rate;
105         Ratio const * container_ratio = 0;
106         Ratio const * content_ratio = 0;
107         int still_length = 10;
108         dcp::Standard standard = dcp::SMPTE;
109         optional<boost::filesystem::path> output;
110         bool sign = true;
111         bool use_isdcf_name = true;
112         optional<boost::filesystem::path> config;
113
114         int option_index = 0;
115         while (true) {
116                 static struct option long_options[] = {
117                         { "version", no_argument, 0, 'v'},
118                         { "help", no_argument, 0, 'h'},
119                         { "name", required_argument, 0, 'n'},
120                         { "template", required_argument, 0, 't'},
121                         { "encrypt", no_argument, 0, 'e'},
122                         { "dcp-content-type", required_argument, 0, 'c'},
123                         { "dcp-frame-rate", required_argument, 0, 'f'},
124                         { "container-ratio", required_argument, 0, 'A'},
125                         { "content-ratio", required_argument, 0, 'B'},
126                         { "still-length", required_argument, 0, 's'},
127                         { "standard", required_argument, 0, 'C'},
128                         { "no-use-isdcf-name", no_argument, 0, 'D'},
129                         { "no-sign", no_argument, 0, 'E'},
130                         { "output", required_argument, 0, 'o'},
131                         { "config", required_argument, 0, 'F'},
132                         { 0, 0, 0, 0}
133                 };
134
135                 int c = getopt_long (argc, argv, "vhn:t:ec:f:A:B:C:s:o:DEF:", long_options, &option_index);
136                 if (c == -1) {
137                         break;
138                 }
139
140                 switch (c) {
141                 case 'v':
142                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
143                         exit (EXIT_SUCCESS);
144                 case 'h':
145                         help (argv[0]);
146                         exit (EXIT_SUCCESS);
147                 case 'n':
148                         name = optarg;
149                         break;
150                 case 't':
151                         template_name = optarg;
152                         break;
153                 case 'e':
154                         encrypt = true;
155                         break;
156                 case 'c':
157                         dcp_content_type = DCPContentType::from_isdcf_name (optarg);
158                         if (dcp_content_type == 0) {
159                                 cerr << "Bad DCP content type.\n";
160                                 syntax (argv[0]);
161                                 exit (EXIT_FAILURE);
162                         }
163                         break;
164                 case 'f':
165                         dcp_frame_rate = atoi (optarg);
166                         break;
167                 case 'A':
168                         container_ratio = Ratio::from_id (optarg);
169                         if (container_ratio == 0) {
170                                 cerr << "Bad container ratio.\n";
171                                 syntax (argv[0]);
172                                 exit (EXIT_FAILURE);
173                         }
174                         break;
175                 case 'B':
176                         content_ratio = Ratio::from_id (optarg);
177                         if (content_ratio == 0) {
178                                 cerr << "Bad content ratio " << optarg << ".\n";
179                                 syntax (argv[0]);
180                                 exit (EXIT_FAILURE);
181                         }
182                         break;
183                 case 'C':
184                         if (strcmp (optarg, "interop") == 0) {
185                                 standard = dcp::INTEROP;
186                         } else if (strcmp (optarg, "SMPTE") != 0) {
187                                 cerr << "Bad standard " << optarg << ".\n";
188                                 syntax (argv[0]);
189                                 exit (EXIT_FAILURE);
190                         }
191                         break;
192                 case 'D':
193                         use_isdcf_name = false;
194                         break;
195                 case 'E':
196                         sign = false;
197                         break;
198                 case 'F':
199                         config = optarg;
200                         break;
201                 case 's':
202                         still_length = atoi (optarg);
203                         break;
204                 case 'o':
205                         output = optarg;
206                         break;
207                 case '?':
208                         syntax (argv[0]);
209                         exit (EXIT_FAILURE);
210                 }
211         }
212
213         if (optind > argc) {
214                 help (argv[0]);
215                 exit (EXIT_FAILURE);
216         }
217
218         if (config) {
219                 Config::override_path = *config;
220         }
221
222         if (!content_ratio) {
223                 cerr << argv[0] << ": missing required option --content-ratio.\n";
224                 exit (EXIT_FAILURE);
225         }
226
227         if (!container_ratio) {
228                 container_ratio = content_ratio;
229         }
230
231         if (optind == argc) {
232                 cerr << argv[0] << ": no content specified.\n";
233                 exit (EXIT_FAILURE);
234         }
235
236         signal_manager = new SimpleSignalManager ();
237
238         if (name.empty ()) {
239                 name = boost::filesystem::path (argv[optind]).leaf().string ();
240         }
241
242         try {
243                 shared_ptr<Film> film (new Film (output));
244                 if (template_name) {
245                         film->use_template (template_name.get());
246                 }
247                 film->set_name (name);
248
249                 film->set_container (container_ratio);
250                 film->set_dcp_content_type (dcp_content_type);
251                 film->set_interop (standard == dcp::INTEROP);
252                 film->set_use_isdcf_name (use_isdcf_name);
253                 film->set_signed (sign);
254                 film->set_encrypted (encrypt);
255
256                 for (int i = optind; i < argc; ++i) {
257                         boost::filesystem::path const can = boost::filesystem::canonical (argv[i]);
258                         list<shared_ptr<Content> > content;
259
260                         if (boost::filesystem::exists (can / "ASSETMAP") || (boost::filesystem::exists (can / "ASSETMAP.xml"))) {
261                                 content.push_back (shared_ptr<DCPContent>(new DCPContent(can)));
262                         } else {
263                                 /* I guess it's not a DCP */
264                                 content = content_factory (can);
265                         }
266
267                         BOOST_FOREACH (shared_ptr<Content> j, content) {
268                                 if (j->video) {
269                                         j->video->set_scale (VideoContentScale (content_ratio));
270                                 }
271                                 film->examine_and_add_content (j);
272                         }
273                 }
274
275                 JobManager* jm = JobManager::instance ();
276
277                 while (jm->work_to_do ()) {
278                         dcpomatic_sleep (1);
279                 }
280
281                 while (signal_manager->ui_idle() > 0) {}
282
283                 if (dcp_frame_rate) {
284                         film->set_video_frame_rate (*dcp_frame_rate);
285                 }
286
287                 ContentList content = film->content ();
288                 for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
289                         shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (*i);
290                         if (ic && ic->still()) {
291                                 ic->video->set_length (still_length * 24);
292                         }
293                 }
294
295                 if (jm->errors ()) {
296                         list<shared_ptr<Job> > jobs = jm->get ();
297                         for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
298                                 if ((*i)->finished_in_error ()) {
299                                         cerr << (*i)->error_summary () << "\n"
300                                              << (*i)->error_details () << "\n";
301                                 }
302                         }
303                         exit (EXIT_FAILURE);
304                 }
305
306                 if (output) {
307                         film->write_metadata ();
308                 } else {
309                         film->metadata()->write_to_stream_formatted (cout, "UTF-8");
310                 }
311         } catch (exception& e) {
312                 cerr << argv[0] << ": " << e.what() << "\n";
313                 exit (EXIT_FAILURE);
314         }
315
316         return 0;
317 }