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