Add --no-sign and --no-use-isdcf-name to dcpomatic_create.
[dcpomatic.git] / src / tools / dcpomatic_create.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/version.h"
21 #include "lib/film.h"
22 #include "lib/util.h"
23 #include "lib/content_factory.h"
24 #include "lib/job_manager.h"
25 #include "lib/signal_manager.h"
26 #include "lib/job.h"
27 #include "lib/dcp_content_type.h"
28 #include "lib/ratio.h"
29 #include "lib/image_content.h"
30 #include <boost/filesystem.hpp>
31 #include <getopt.h>
32 #include <string>
33 #include <iostream>
34 #include <cstdlib>
35 #include <stdexcept>
36
37 using std::string;
38 using std::cout;
39 using std::cerr;
40 using std::list;
41 using std::exception;
42 using boost::shared_ptr;
43 using boost::dynamic_pointer_cast;
44
45 static void
46 syntax (string n)
47 {
48         cerr << "Syntax: " << n << " [OPTION] <CONTENT> [<CONTENT> ...]\n"
49              << "  -v, --version                 show DCP-o-matic version\n"
50              << "  -h, --help                    show this help\n"
51              << "  -n, --name <name>             film name\n"
52              << "  -c, --dcp-content-type <type> FTR, SHR, TLR, TST, XSN, RTG, TSR, POL, PSA or ADV\n"
53              << "      --container-ratio <ratio> 119, 133, 137, 138, 166, 178, 185 or 239\n"
54              << "      --content-ratio <ratio>   119, 133, 137, 138, 166, 178, 185 or 239\n"
55              << "  -s, --still-length <n>        number of seconds that still content should last\n"
56              << "      --standard <standard>     SMPTE or interop (default SMPTE)\n"
57              << "      --no-use-isdcf-name       do not use an ISDCF name; use the specified name unmodified\n"
58              << "      --no-sign                 do not sign the DCP\n"
59              << "  -o, --output <dir>            output directory\n";
60 }
61
62 static void
63 help (string n)
64 {
65         cerr << "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
66              << "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
67              << "will be written to stdout.\n";
68
69         syntax (n);
70 }
71
72 class SimpleSignalManager : public SignalManager
73 {
74 public:
75         /* Do nothing in this method so that UI events happen in our thread
76            when we call SignalManager::ui_idle().
77         */
78         void wake_ui () {}
79 };
80
81 int
82 main (int argc, char* argv[])
83 {
84         dcpomatic_setup ();
85
86         string name;
87         DCPContentType const * dcp_content_type = DCPContentType::from_isdcf_name ("TST");
88         Ratio const * container_ratio = 0;
89         Ratio const * content_ratio = 0;
90         int still_length = 10;
91         dcp::Standard standard = dcp::SMPTE;
92         boost::filesystem::path output;
93         bool sign = true;
94         bool use_isdcf_name = true;
95
96         int option_index = 0;
97         while (true) {
98                 static struct option long_options[] = {
99                         { "version", no_argument, 0, 'v'},
100                         { "help", no_argument, 0, 'h'},
101                         { "name", required_argument, 0, 'n'},
102                         { "dcp-content-type", required_argument, 0, 'c'},
103                         { "container-ratio", required_argument, 0, 'A'},
104                         { "content-ratio", required_argument, 0, 'B'},
105                         { "still-length", required_argument, 0, 's'},
106                         { "standard", required_argument, 0, 'C'},
107                         { "no-use-isdcf-name", no_argument, 0, 'D'},
108                         { "no-sign", no_argument, 0, 'E'},
109                         { "output", required_argument, 0, 'o'},
110                         { 0, 0, 0, 0}
111                 };
112
113                 int c = getopt_long (argc, argv, "vhn:c:A:B:C:s:o:DE", long_options, &option_index);
114                 if (c == -1) {
115                         break;
116                 }
117
118                 switch (c) {
119                 case 'v':
120                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
121                         exit (EXIT_SUCCESS);
122                 case 'h':
123                         help (argv[0]);
124                         exit (EXIT_SUCCESS);
125                 case 'n':
126                         name = optarg;
127                         break;
128                 case 'c':
129                         dcp_content_type = DCPContentType::from_isdcf_name (optarg);
130                         if (dcp_content_type == 0) {
131                                 cerr << "Bad DCP content type.\n";
132                                 syntax (argv[0]);
133                                 exit (EXIT_FAILURE);
134                         }
135                         break;
136                 case 'A':
137                         container_ratio = Ratio::from_id (optarg);
138                         if (container_ratio == 0) {
139                                 cerr << "Bad container ratio.\n";
140                                 syntax (argv[0]);
141                                 exit (EXIT_FAILURE);
142                         }
143                         break;
144                 case 'B':
145                         content_ratio = Ratio::from_id (optarg);
146                         if (content_ratio == 0) {
147                                 cerr << "Bad content ratio " << optarg << ".\n";
148                                 syntax (argv[0]);
149                                 exit (EXIT_FAILURE);
150                         }
151                         break;
152                 case 'C':
153                         if (strcmp (optarg, "interop") == 0) {
154                                 standard = dcp::INTEROP;
155                         } else if (strcmp (optarg, "SMPTE") != 0) {
156                                 cerr << "Bad standard " << optarg << ".\n";
157                                 syntax (argv[0]);
158                                 exit (EXIT_FAILURE);
159                         }
160                         break;
161                 case 'D':
162                         use_isdcf_name = false;
163                         break;
164                 case 'E':
165                         sign = false;
166                         break;
167                 case 's':
168                         still_length = atoi (optarg);
169                         break;
170                 case 'o':
171                         output = optarg;
172                         break;
173                 case '?':
174                         syntax (argv[0]);
175                         exit (EXIT_FAILURE);
176                 }
177         }
178
179         if (optind > argc) {
180                 help (argv[0]);
181                 exit (EXIT_FAILURE);
182         }
183
184         if (!content_ratio) {
185                 cerr << argv[0] << ": missing required option --content-ratio.\n";
186                 exit (EXIT_FAILURE);
187         }
188
189         if (!container_ratio) {
190                 container_ratio = content_ratio;
191         }
192
193         if (optind == argc) {
194                 cerr << argv[0] << ": no content specified.\n";
195                 exit (EXIT_FAILURE);
196         }
197
198         signal_manager = new SimpleSignalManager ();
199
200         if (name.empty ()) {
201                 name = boost::filesystem::path (argv[optind]).leaf().string ();
202         }
203
204         try {
205                 shared_ptr<Film> film (new Film (output, false));
206                 film->set_name (name);
207
208                 film->set_container (container_ratio);
209                 film->set_dcp_content_type (dcp_content_type);
210                 film->set_interop (standard == dcp::INTEROP);
211                 film->set_use_isdcf_name (use_isdcf_name);
212                 film->set_signed (sign);
213
214                 for (int i = optind; i < argc; ++i) {
215                         shared_ptr<Content> c = content_factory (film, argv[i]);
216                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c);
217                         if (vc) {
218                                 vc->set_scale (VideoContentScale (content_ratio));
219                         }
220                         film->examine_and_add_content (c);
221                 }
222
223                 JobManager* jm = JobManager::instance ();
224
225                 while (jm->work_to_do ()) {}
226                 while (signal_manager->ui_idle() > 0) {}
227
228                 ContentList content = film->content ();
229                 for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
230                         shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (*i);
231                         if (ic) {
232                                 ic->set_video_length (still_length * 24);
233                         }
234                 }
235
236                 if (jm->errors ()) {
237                         list<shared_ptr<Job> > jobs = jm->get ();
238                         for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
239                                 if ((*i)->finished_in_error ()) {
240                                         cerr << (*i)->error_summary () << "\n"
241                                              << (*i)->error_details () << "\n";
242                                 }
243                         }
244                         exit (EXIT_FAILURE);
245                 }
246
247                 if (!output.empty ()) {
248                         film->write_metadata ();
249                 } else {
250                         film->metadata()->write_to_stream_formatted (cout, "UTF-8");
251                 }
252         } catch (exception& e) {
253                 cerr << argv[0] << ": " << e.what() << "\n";
254                 exit (EXIT_FAILURE);
255         }
256
257         return 0;
258 }