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