No-op; fix GPL address and use the explicit-program-name version.
[dcpomatic.git] / src / tools / dcpomatic_cli.cc
1 /*
2     Copyright (C) 2012-2015 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 <dcp/version.h>
37 #include <boost/foreach.hpp>
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 boost::shared_ptr;
50 using boost::optional;
51 using boost::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              << "  -j, --json <port>  run a JSON server on the specified port\n"
63              << "  -k, --keep-going   keep running even when the job is complete\n"
64              << "  -s, --servers      just display a list of encoding servers that DCP-o-matic is configured to use; don't encode\n"
65              << "  -d, --dcp-path     echo DCP's path to stdout on successful completion (implies -n)\n"
66              << "      --dump         just dump a summary of the film's settings; don't encode\n"
67              << "\n"
68              << "<FILM> is the film directory.\n";
69 }
70
71 static void
72 print_dump (shared_ptr<Film> film)
73 {
74         cout << film->dcp_name (true) << "\n"
75              << film->container()->nickname() << " at " << ((film->resolution() == RESOLUTION_2K) ? "2K" : "4K") << "\n"
76              << (film->j2k_bandwidth() / 1000000) << "Mbit/s" << "\n"
77              << "Output " << film->video_frame_rate() << "fps " << (film->three_d() ? "3D" : "2D") << " " << (film->audio_frame_rate() / 1000) << "kHz\n"
78              << (film->interop() ? "Inter-Op" : "SMPTE") << " " << (film->encrypted() ? "encrypted" : "unencrypted") << "\n";
79
80         BOOST_FOREACH (shared_ptr<Content> c, film->content ()) {
81                 cout << "\n"
82                      << c->path(0) << "\n"
83                      << "\tat " << c->position().seconds ()
84                      << " length " << c->full_length().seconds ()
85                      << " start trim " << c->trim_start().seconds ()
86                      << " end trim " << c->trim_end().seconds () << "\n";
87
88                 if (c->video) {
89                         cout << "\t" << c->video->size().width << "x" << c->video->size().height << "\n"
90                              << "\t" << c->active_video_frame_rate() << "fps\n"
91                              << "\tcrop left " << c->video->left_crop()
92                              << " right " << c->video->right_crop()
93                              << " top " << c->video->top_crop()
94                              << " bottom " << c->video->bottom_crop() << "\n"
95                              << "\tscale " << c->video->scale().name() << "\n";
96                         if (c->video->colour_conversion()) {
97                                 if (c->video->colour_conversion().get().preset()) {
98                                         cout << "\tcolour conversion "
99                                              << PresetColourConversion::all()[c->video->colour_conversion().get().preset().get()].name
100                                              << "\n";
101                                 } else {
102                                         cout << "\tcustom colour conversion\n";
103                                 }
104                         } else {
105                                 cout << "\tno colour conversion\n";
106                         }
107
108                 }
109
110                 if (c->audio) {
111                         cout << "\t" << c->audio->delay() << " delay\n"
112                              << "\t" << c->audio->gain() << " gain\n";
113                 }
114         }
115 }
116
117 static void
118 show_servers ()
119 {
120         while (true) {
121                 int N = 0;
122                 list<EncodeServerDescription> servers = EncodeServerFinder::instance()->servers ();
123
124                 if (Config::instance()->use_any_servers ()) {
125                         if (servers.empty ()) {
126                                 cout << "No encoding servers found.\n";
127                                 ++N;
128                         } else {
129                                 cout << std::left << setw(24) << "Host" << " Threads\n";
130                                 ++N;
131                                 BOOST_FOREACH (EncodeServerDescription const & i, servers) {
132                                         cout << std::left << setw(24) << i.host_name() << " " << i.threads() << "\n";
133                                         ++N;
134                                 }
135                         }
136                 } else {
137                         vector<string> configured_servers = Config::instance()->servers();
138                         if (configured_servers.empty()) {
139                                 cout << "No configured servers, and DCP-o-matic is not set to search for any server.\n";
140                                 ++N;
141                         } else {
142                                 cout << std::left << setw(24) << "Host" << " Status Threads\n";
143                                 ++N;
144                                 BOOST_FOREACH (string const & i, Config::instance()->servers()) {
145                                         cout << std::left << setw(24) << i << " ";
146                                         optional<int> threads;
147                                         BOOST_FOREACH (EncodeServerDescription const & j, servers) {
148                                                 if (i == j.host_name()) {
149                                                         threads = j.threads();
150                                                 }
151                                         }
152                                         if (static_cast<bool>(threads)) {
153                                                 cout << "UP     " << threads.get() << "\n";
154                                         } else {
155                                                 cout << "DOWN\n";
156                                         }
157                                         ++N;
158                                 }
159                         }
160                 }
161
162
163                 dcpomatic_sleep (1);
164
165                 for (int i = 0; i < N; ++i) {
166                         cout << "\033[1A\033[2K";
167                 }
168         }
169 }
170
171
172 int
173 main (int argc, char* argv[])
174 {
175         string film_dir;
176         bool progress = true;
177         bool no_remote = false;
178         optional<int> json_port;
179         bool keep_going = false;
180         bool dump = false;
181         bool servers = false;
182         bool dcp_path = false;
183
184         int option_index = 0;
185         while (true) {
186                 static struct option long_options[] = {
187                         { "version", no_argument, 0, 'v'},
188                         { "help", no_argument, 0, 'h'},
189                         { "flags", no_argument, 0, 'f'},
190                         { "no-progress", no_argument, 0, 'n'},
191                         { "no-remote", no_argument, 0, 'r'},
192                         { "json", required_argument, 0, 'j'},
193                         { "keep-going", no_argument, 0, 'k' },
194                         { "servers", no_argument, 0, 's' },
195                         { "dcp-path", no_argument, 0, 'd' },
196                         /* Just using A, B, C ... from here on */
197                         { "dump", no_argument, 0, 'A' },
198                         { 0, 0, 0, 0 }
199                 };
200
201                 int c = getopt_long (argc, argv, "vhfnrj:kAsd", long_options, &option_index);
202
203                 if (c == -1) {
204                         break;
205                 }
206
207                 switch (c) {
208                 case 'v':
209                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
210                         exit (EXIT_SUCCESS);
211                 case 'h':
212                         help (argv[0]);
213                         exit (EXIT_SUCCESS);
214                 case 'f':
215                         cout << dcpomatic_cxx_flags << "\n";
216                         exit (EXIT_SUCCESS);
217                 case 'n':
218                         progress = false;
219                         break;
220                 case 'r':
221                         no_remote = true;
222                         break;
223                 case 'j':
224                         json_port = atoi (optarg);
225                         break;
226                 case 'k':
227                         keep_going = true;
228                         break;
229                 case 'A':
230                         dump = true;
231                         break;
232                 case 's':
233                         servers = true;
234                         break;
235                 case 'd':
236                         dcp_path = true;
237                         progress = false;
238                         break;
239                 }
240         }
241
242         if (servers) {
243                 show_servers ();
244                 exit (EXIT_SUCCESS);
245         }
246
247         if (optind >= argc) {
248                 help (argv[0]);
249                 exit (EXIT_FAILURE);
250         }
251
252         film_dir = argv[optind];
253
254         dcpomatic_setup_path_encoding ();
255         dcpomatic_setup ();
256         signal_manager = new SignalManager ();
257
258         if (no_remote) {
259                 EncodeServerFinder::instance()->disable ();
260         }
261
262         if (json_port) {
263                 new JSONServer (json_port.get ());
264         }
265
266         shared_ptr<Film> film;
267         try {
268                 film.reset (new Film (film_dir));
269                 film->read_metadata ();
270         } catch (std::exception& e) {
271                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
272                 exit (EXIT_FAILURE);
273         }
274
275         if (dump) {
276                 print_dump (film);
277                 exit (EXIT_SUCCESS);
278         }
279
280         ContentList content = film->content ();
281         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
282                 vector<boost::filesystem::path> paths = (*i)->paths ();
283                 for (vector<boost::filesystem::path>::const_iterator j = paths.begin(); j != paths.end(); ++j) {
284                         if (!boost::filesystem::exists (*j)) {
285                                 cerr << argv[0] << ": content file " << *j << " not found.\n";
286                                 exit (EXIT_FAILURE);
287                         }
288                 }
289         }
290
291         if (progress) {
292                 cout << "\nMaking DCP for " << film->name() << "\n";
293         }
294
295         film->make_dcp ();
296
297         bool should_stop = false;
298         bool first = true;
299         bool error = false;
300         while (!should_stop) {
301
302                 dcpomatic_sleep (5);
303
304                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
305
306                 if (!first && progress) {
307                         for (size_t i = 0; i < jobs.size(); ++i) {
308                                 cout << "\033[1A\033[2K";
309                         }
310                         cout.flush ();
311                 }
312
313                 first = false;
314
315                 int unfinished = 0;
316                 int finished_in_error = 0;
317
318                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
319                         if (progress) {
320                                 cout << (*i)->name() << ": ";
321
322                                 if ((*i)->progress ()) {
323                                         cout << (*i)->status() << "                         \n";
324                                 } else {
325                                         cout << ": Running           \n";
326                                 }
327                         }
328
329                         if (!(*i)->finished ()) {
330                                 ++unfinished;
331                         }
332
333                         if ((*i)->finished_in_error ()) {
334                                 ++finished_in_error;
335                                 error = true;
336                         }
337
338                         if (!progress && (*i)->finished_in_error ()) {
339                                 /* We won't see this error if we haven't been showing progress,
340                                    so show it now.
341                                 */
342                                 cout << (*i)->status() << "\n";
343                         }
344                 }
345
346                 if (unfinished == 0 || finished_in_error != 0) {
347                         should_stop = true;
348                 }
349         }
350
351         if (keep_going) {
352                 while (true) {
353                         dcpomatic_sleep (3600);
354                 }
355         }
356
357         /* This is just to stop valgrind reporting leaks due to JobManager
358            indirectly holding onto codecs.
359         */
360         JobManager::drop ();
361
362         EncodeServerFinder::drop ();
363
364         if (dcp_path && !error) {
365                 cout << film->dir (film->dcp_name (false)).string() << "\n";
366         }
367
368         return error ? EXIT_FAILURE : EXIT_SUCCESS;
369 }