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