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