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