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