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