Rename video/audio/subtitle part methods.
[dcpomatic.git] / src / tools / dcpomatic_create.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/version.h"
21 #include "lib/film.h"
22 #include "lib/util.h"
23 #include "lib/content_factory.h"
24 #include "lib/job_manager.h"
25 #include "lib/signal_manager.h"
26 #include "lib/job.h"
27 #include "lib/dcp_content_type.h"
28 #include "lib/ratio.h"
29 #include "lib/image_content.h"
30 #include "lib/video_content.h"
31 #include <libxml++/libxml++.h>
32 #include <boost/filesystem.hpp>
33 #include <getopt.h>
34 #include <string>
35 #include <iostream>
36 #include <cstdlib>
37 #include <stdexcept>
38
39 using std::string;
40 using std::cout;
41 using std::cerr;
42 using std::list;
43 using std::exception;
44 using boost::shared_ptr;
45 using boost::dynamic_pointer_cast;
46
47 static void
48 syntax (string n)
49 {
50         cerr << "Syntax: " << n << " [OPTION] <CONTENT> [<CONTENT> ...]\n"
51              << "  -v, --version                 show DCP-o-matic version\n"
52              << "  -h, --help                    show this help\n"
53              << "  -n, --name <name>             film name\n"
54              << "  -c, --dcp-content-type <type> FTR, SHR, TLR, TST, XSN, RTG, TSR, POL, PSA or ADV\n"
55              << "      --container-ratio <ratio> 119, 133, 137, 138, 166, 178, 185 or 239\n"
56              << "      --content-ratio <ratio>   119, 133, 137, 138, 166, 178, 185 or 239\n"
57              << "  -s, --still-length <n>        number of seconds that still content should last\n"
58              << "      --standard <standard>     SMPTE or interop (default SMPTE)\n"
59              << "      --no-use-isdcf-name       do not use an ISDCF name; use the specified name unmodified\n"
60              << "      --no-sign                 do not sign the DCP\n"
61              << "  -o, --output <dir>            output directory\n";
62 }
63
64 static void
65 help (string n)
66 {
67         cerr << "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
68              << "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
69              << "will be written to stdout.\n";
70
71         syntax (n);
72 }
73
74 class SimpleSignalManager : public SignalManager
75 {
76 public:
77         /* Do nothing in this method so that UI events happen in our thread
78            when we call SignalManager::ui_idle().
79         */
80         void wake_ui () {}
81 };
82
83 int
84 main (int argc, char* argv[])
85 {
86         dcpomatic_setup_path_encoding ();
87         dcpomatic_setup ();
88
89         string name;
90         DCPContentType const * dcp_content_type = DCPContentType::from_isdcf_name ("TST");
91         Ratio const * container_ratio = 0;
92         Ratio const * content_ratio = 0;
93         int still_length = 10;
94         dcp::Standard standard = dcp::SMPTE;
95         boost::filesystem::path output;
96         bool sign = true;
97         bool use_isdcf_name = true;
98
99         int option_index = 0;
100         while (true) {
101                 static struct option long_options[] = {
102                         { "version", no_argument, 0, 'v'},
103                         { "help", no_argument, 0, 'h'},
104                         { "name", required_argument, 0, 'n'},
105                         { "dcp-content-type", required_argument, 0, 'c'},
106                         { "container-ratio", required_argument, 0, 'A'},
107                         { "content-ratio", required_argument, 0, 'B'},
108                         { "still-length", required_argument, 0, 's'},
109                         { "standard", required_argument, 0, 'C'},
110                         { "no-use-isdcf-name", no_argument, 0, 'D'},
111                         { "no-sign", no_argument, 0, 'E'},
112                         { "output", required_argument, 0, 'o'},
113                         { 0, 0, 0, 0}
114                 };
115
116                 int c = getopt_long (argc, argv, "vhn:c:A:B:C:s:o:DE", long_options, &option_index);
117                 if (c == -1) {
118                         break;
119                 }
120
121                 switch (c) {
122                 case 'v':
123                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
124                         exit (EXIT_SUCCESS);
125                 case 'h':
126                         help (argv[0]);
127                         exit (EXIT_SUCCESS);
128                 case 'n':
129                         name = optarg;
130                         break;
131                 case 'c':
132                         dcp_content_type = DCPContentType::from_isdcf_name (optarg);
133                         if (dcp_content_type == 0) {
134                                 cerr << "Bad DCP content type.\n";
135                                 syntax (argv[0]);
136                                 exit (EXIT_FAILURE);
137                         }
138                         break;
139                 case 'A':
140                         container_ratio = Ratio::from_id (optarg);
141                         if (container_ratio == 0) {
142                                 cerr << "Bad container ratio.\n";
143                                 syntax (argv[0]);
144                                 exit (EXIT_FAILURE);
145                         }
146                         break;
147                 case 'B':
148                         content_ratio = Ratio::from_id (optarg);
149                         if (content_ratio == 0) {
150                                 cerr << "Bad content ratio " << optarg << ".\n";
151                                 syntax (argv[0]);
152                                 exit (EXIT_FAILURE);
153                         }
154                         break;
155                 case 'C':
156                         if (strcmp (optarg, "interop") == 0) {
157                                 standard = dcp::INTEROP;
158                         } else if (strcmp (optarg, "SMPTE") != 0) {
159                                 cerr << "Bad standard " << optarg << ".\n";
160                                 syntax (argv[0]);
161                                 exit (EXIT_FAILURE);
162                         }
163                         break;
164                 case 'D':
165                         use_isdcf_name = false;
166                         break;
167                 case 'E':
168                         sign = false;
169                         break;
170                 case 's':
171                         still_length = atoi (optarg);
172                         break;
173                 case 'o':
174                         output = optarg;
175                         break;
176                 case '?':
177                         syntax (argv[0]);
178                         exit (EXIT_FAILURE);
179                 }
180         }
181
182         if (optind > argc) {
183                 help (argv[0]);
184                 exit (EXIT_FAILURE);
185         }
186
187         if (!content_ratio) {
188                 cerr << argv[0] << ": missing required option --content-ratio.\n";
189                 exit (EXIT_FAILURE);
190         }
191
192         if (!container_ratio) {
193                 container_ratio = content_ratio;
194         }
195
196         if (optind == argc) {
197                 cerr << argv[0] << ": no content specified.\n";
198                 exit (EXIT_FAILURE);
199         }
200
201         signal_manager = new SimpleSignalManager ();
202
203         if (name.empty ()) {
204                 name = boost::filesystem::path (argv[optind]).leaf().string ();
205         }
206
207         try {
208                 shared_ptr<Film> film (new Film (output, false));
209                 film->set_name (name);
210
211                 film->set_container (container_ratio);
212                 film->set_dcp_content_type (dcp_content_type);
213                 film->set_interop (standard == dcp::INTEROP);
214                 film->set_use_isdcf_name (use_isdcf_name);
215                 film->set_signed (sign);
216
217                 for (int i = optind; i < argc; ++i) {
218                         shared_ptr<Content> c = content_factory (film, boost::filesystem::canonical (argv[i]));
219                         if (c->video) {
220                                 c->video->set_scale (VideoContentScale (content_ratio));
221                         }
222                         film->examine_and_add_content (c);
223                 }
224
225                 JobManager* jm = JobManager::instance ();
226
227                 while (jm->work_to_do ()) {}
228                 while (signal_manager->ui_idle() > 0) {}
229
230                 ContentList content = film->content ();
231                 for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
232                         shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (*i);
233                         if (ic) {
234                                 ic->video->set_length (still_length * 24);
235                         }
236                 }
237
238                 if (jm->errors ()) {
239                         list<shared_ptr<Job> > jobs = jm->get ();
240                         for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
241                                 if ((*i)->finished_in_error ()) {
242                                         cerr << (*i)->error_summary () << "\n"
243                                              << (*i)->error_details () << "\n";
244                                 }
245                         }
246                         exit (EXIT_FAILURE);
247                 }
248
249                 if (!output.empty ()) {
250                         film->write_metadata ();
251                 } else {
252                         film->metadata()->write_to_stream_formatted (cout, "UTF-8");
253                 }
254         } catch (exception& e) {
255                 cerr << argv[0] << ": " << e.what() << "\n";
256                 exit (EXIT_FAILURE);
257         }
258
259         return 0;
260 }