75f57fa89ce4b659a50346c79df05b6f6acab978
[dcpomatic.git] / src / tools / dcpomatic_cli.cc
1 /*
2     Copyright (C) 2012-2017 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/film.h"
22 #include "lib/filter.h"
23 #include "lib/transcode_job.h"
24 #include "lib/job_manager.h"
25 #include "lib/util.h"
26 #include "lib/version.h"
27 #include "lib/cross.h"
28 #include "lib/config.h"
29 #include "lib/log.h"
30 #include "lib/signal_manager.h"
31 #include "lib/encode_server_finder.h"
32 #include "lib/json_server.h"
33 #include "lib/ratio.h"
34 #include "lib/video_content.h"
35 #include "lib/audio_content.h"
36 #include "lib/dcpomatic_log.h"
37 #include <dcp/version.h>
38 #include <getopt.h>
39 #include <iostream>
40 #include <iomanip>
41
42 using std::string;
43 using std::cerr;
44 using std::cout;
45 using std::vector;
46 using std::pair;
47 using std::setw;
48 using std::list;
49 using std::shared_ptr;
50 using boost::optional;
51 using std::dynamic_pointer_cast;
52
53 static void
54 help (string n)
55 {
56         cerr << "Syntax: " << n << " [OPTION] [<FILM>]\n"
57              << "  -v, --version        show DCP-o-matic version\n"
58              << "  -h, --help           show this help\n"
59              << "  -f, --flags          show flags passed to C++ compiler on build\n"
60              << "  -n, --no-progress    do not print progress to stdout\n"
61              << "  -r, --no-remote      do not use any remote servers\n"
62              << "  -t, --threads        specify number of local encoding threads (overriding configuration)\n"
63              << "  -j, --json <port>    run a JSON server on the specified port\n"
64              << "  -k, --keep-going     keep running even when the job is complete\n"
65              << "  -s, --servers <file> specify servers to use in a text file\n"
66              << "  -l, --list-servers   just display a list of encoding servers that DCP-o-matic is configured to use; don't encode\n"
67              << "  -d, --dcp-path       echo DCP's path to stdout on successful completion (implies -n)\n"
68              << "  -c, --config <dir>   directory containing config.xml and cinemas.xml\n"
69              << "      --dump           just dump a summary of the film's settings; don't encode\n"
70              << "      --no-check       don't check project's content files for changes before making the DCP\n"
71              << "\n"
72              << "<FILM> is the film directory.\n";
73 }
74
75 static void
76 print_dump (shared_ptr<Film> film)
77 {
78         cout << film->dcp_name (true) << "\n"
79              << film->container()->container_nickname() << " at " << ((film->resolution() == Resolution::TWO_K) ? "2K" : "4K") << "\n"
80              << (film->j2k_bandwidth() / 1000000) << "Mbit/s" << "\n"
81              << "Output " << film->video_frame_rate() << "fps " << (film->three_d() ? "3D" : "2D") << " " << (film->audio_frame_rate() / 1000) << "kHz\n"
82              << (film->interop() ? "Inter-Op" : "SMPTE") << " " << (film->encrypted() ? "encrypted" : "unencrypted") << "\n";
83
84         for (auto c: film->content()) {
85                 cout << "\n"
86                      << c->path(0) << "\n"
87                      << "\tat " << c->position().seconds ()
88                      << " length " << c->full_length(film).seconds ()
89                      << " start trim " << c->trim_start().seconds ()
90                      << " end trim " << c->trim_end().seconds () << "\n";
91
92                 if (c->video) {
93                         cout << "\t" << c->video->size().width << "x" << c->video->size().height << "\n"
94                              << "\t" << c->active_video_frame_rate(film) << "fps\n"
95                              << "\tcrop left " << c->video->requested_left_crop()
96                              << " right " << c->video->requested_right_crop()
97                              << " top " << c->video->requested_top_crop()
98                              << " bottom " << c->video->requested_bottom_crop() << "\n";
99                         if (c->video->custom_ratio()) {
100                                 cout << "\tscale to custom ratio " << *c->video->custom_ratio() << ":1\n";
101                         }
102                         if (c->video->colour_conversion()) {
103                                 if (c->video->colour_conversion().get().preset()) {
104                                         cout << "\tcolour conversion "
105                                              << PresetColourConversion::all()[c->video->colour_conversion().get().preset().get()].name
106                                              << "\n";
107                                 } else {
108                                         cout << "\tcustom colour conversion\n";
109                                 }
110                         } else {
111                                 cout << "\tno colour conversion\n";
112                         }
113
114                 }
115
116                 if (c->audio) {
117                         cout << "\t" << c->audio->delay() << " delay\n"
118                              << "\t" << c->audio->gain() << " gain\n";
119                 }
120         }
121 }
122
123 static void
124 list_servers ()
125 {
126         while (true) {
127                 int N = 0;
128                 auto servers = EncodeServerFinder::instance()->servers();
129
130                 /* This is a bit fiddly because we want to list configured servers that are down as well
131                    as all those (configured and found by broadcast) that are up.
132                 */
133
134                 if (servers.empty() && Config::instance()->servers().empty()) {
135                         cout << "No encoding servers found or configured.\n";
136                         ++N;
137                 } else {
138                         cout << std::left << setw(24) << "Host" << " Status Threads\n";
139                         ++N;
140
141                         /* Report the state of configured servers */
142                         for (auto i: Config::instance()->servers()) {
143                                 cout << std::left << setw(24) << i << " ";
144
145                                 /* See if this server is on the active list; if so, remove it and note
146                                    the number of threads it is offering.
147                                 */
148                                 optional<int> threads;
149                                 auto j = servers.begin ();
150                                 while (j != servers.end ()) {
151                                         if (i == j->host_name() && j->current_link_version()) {
152                                                 threads = j->threads();
153                                                 auto tmp = j;
154                                                 ++tmp;
155                                                 servers.erase (j);
156                                                 j = tmp;
157                                         } else {
158                                                 ++j;
159                                         }
160                                 }
161                                 if (static_cast<bool>(threads)) {
162                                         cout << "UP     " << threads.get() << "\n";
163                                 } else {
164                                         cout << "DOWN\n";
165                                 }
166                                 ++N;
167                         }
168
169                         /* Now report any left that have been found by broadcast */
170                         for (auto const& i: servers) {
171                                 if (i.current_link_version()) {
172                                         cout << std::left << setw(24) << i.host_name() << " UP     " << i.threads() << "\n";
173                                 } else {
174                                         cout << std::left << setw(24) << i.host_name() << " bad version\n";
175                                 }
176                                 ++N;
177                         }
178                 }
179
180                 dcpomatic_sleep_seconds (1);
181
182                 for (int i = 0; i < N; ++i) {
183                         cout << "\033[1A\033[2K";
184                 }
185         }
186 }
187
188
189 int
190 main (int argc, char* argv[])
191 {
192         boost::filesystem::path film_dir;
193         bool progress = true;
194         bool no_remote = false;
195         optional<int> threads;
196         optional<int> json_port;
197         bool keep_going = false;
198         bool dump = false;
199         optional<boost::filesystem::path> servers;
200         bool list_servers_ = false;
201         bool dcp_path = false;
202         optional<boost::filesystem::path> config;
203         bool check = true;
204
205         int option_index = 0;
206         while (true) {
207                 static struct option long_options[] = {
208                         { "version", no_argument, 0, 'v'},
209                         { "help", no_argument, 0, 'h'},
210                         { "flags", no_argument, 0, 'f'},
211                         { "no-progress", no_argument, 0, 'n'},
212                         { "no-remote", no_argument, 0, 'r'},
213                         { "threads", required_argument, 0, 't'},
214                         { "json", required_argument, 0, 'j'},
215                         { "keep-going", no_argument, 0, 'k' },
216                         { "servers", required_argument, 0, 's' },
217                         { "list-servers", no_argument, 0, 'l' },
218                         { "dcp-path", no_argument, 0, 'd' },
219                         { "config", required_argument, 0, 'c' },
220                         /* Just using A, B, C ... from here on */
221                         { "dump", no_argument, 0, 'A' },
222                         { "no-check", no_argument, 0, 'B' },
223                         { 0, 0, 0, 0 }
224                 };
225
226                 int c = getopt_long (argc, argv, "vhfnrt:j:kAs:ldc:B", long_options, &option_index);
227
228                 if (c == -1) {
229                         break;
230                 }
231
232                 switch (c) {
233                 case 'v':
234                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
235                         exit (EXIT_SUCCESS);
236                 case 'h':
237                         help (argv[0]);
238                         exit (EXIT_SUCCESS);
239                 case 'f':
240                         cout << dcpomatic_cxx_flags << "\n";
241                         exit (EXIT_SUCCESS);
242                 case 'n':
243                         progress = false;
244                         break;
245                 case 'r':
246                         no_remote = true;
247                         break;
248                 case 't':
249                         threads = atoi (optarg);
250                         break;
251                 case 'j':
252                         json_port = atoi (optarg);
253                         break;
254                 case 'k':
255                         keep_going = true;
256                         break;
257                 case 'A':
258                         dump = true;
259                         break;
260                 case 's':
261                         servers = optarg;
262                         break;
263                 case 'l':
264                         list_servers_ = true;
265                         break;
266                 case 'd':
267                         dcp_path = true;
268                         progress = false;
269                         break;
270                 case 'c':
271                         config = optarg;
272                         break;
273                 case 'B':
274                         check = false;
275                         break;
276                 }
277         }
278
279         if (config) {
280                 State::override_path = *config;
281         }
282
283         if (servers) {
284                 auto f = fopen_boost (*servers, "r");
285                 if (!f) {
286                         cerr << "Could not open servers list file " << *servers << "\n";
287                         exit (EXIT_FAILURE);
288                 }
289                 vector<string> servers;
290                 while (!feof (f)) {
291                         char buffer[128];
292                         if (fscanf (f, "%s.127", buffer) == 1) {
293                                 servers.push_back (buffer);
294                         }
295                 }
296                 fclose (f);
297                 Config::instance()->set_servers (servers);
298         }
299
300         if (list_servers_) {
301                 list_servers ();
302                 exit (EXIT_SUCCESS);
303         }
304
305         if (optind >= argc) {
306                 help (argv[0]);
307                 exit (EXIT_FAILURE);
308         }
309
310         film_dir = argv[optind];
311
312         dcpomatic_setup_path_encoding ();
313         dcpomatic_setup ();
314         signal_manager = new SignalManager ();
315
316         if (no_remote) {
317                 EncodeServerFinder::instance()->stop ();
318         }
319
320         if (json_port) {
321                 new JSONServer (json_port.get ());
322         }
323
324         if (threads) {
325                 Config::instance()->set_master_encoding_threads (threads.get ());
326         }
327
328         shared_ptr<Film> film;
329         try {
330                 film.reset (new Film (film_dir));
331                 film->read_metadata ();
332         } catch (std::exception& e) {
333                 cerr << argv[0] << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
334                 exit (EXIT_FAILURE);
335         }
336
337         if (dump) {
338                 print_dump (film);
339                 exit (EXIT_SUCCESS);
340         }
341
342         dcpomatic_log = film->log ();
343
344         for (auto i: film->content()) {
345                 auto paths = i->paths();
346                 for (auto j: paths) {
347                         if (!boost::filesystem::exists(j)) {
348                                 cerr << argv[0] << ": content file " << j << " not found.\n";
349                                 exit (EXIT_FAILURE);
350                         }
351                 }
352         }
353
354         if (progress) {
355                 cout << "\nMaking DCP for " << film->name() << "\n";
356         }
357
358         film->make_dcp (false, check);
359         bool const error = show_jobs_on_console (progress);
360
361         if (keep_going) {
362                 while (true) {
363                         dcpomatic_sleep_seconds (3600);
364                 }
365         }
366
367         /* This is just to stop valgrind reporting leaks due to JobManager
368            indirectly holding onto codecs.
369         */
370         JobManager::drop ();
371
372         EncodeServerFinder::drop ();
373
374         if (dcp_path && !error) {
375                 cout << film->dir (film->dcp_name (false)).string() << "\n";
376         }
377
378         return error ? EXIT_FAILURE : EXIT_SUCCESS;
379 }