Extract all uses of DCP-o-matic name to allow branding.
[dcpomatic.git] / src / tools / dcpomatic_cli.cc
1 /*
2     Copyright (C) 2012-2022 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
22 #include "lib/ansi.h"
23 #include "lib/audio_content.h"
24 #include "lib/config.h"
25 #include "lib/cross.h"
26 #include "lib/dcpomatic_log.h"
27 #include "lib/encode_server_finder.h"
28 #include "lib/ffmpeg_encoder.h"
29 #include "lib/film.h"
30 #include "lib/filter.h"
31 #ifdef DCPOMATIC_GROK
32 #include "lib/grok/context.h"
33 #endif
34 #include "lib/hints.h"
35 #include "lib/job_manager.h"
36 #include "lib/json_server.h"
37 #include "lib/log.h"
38 #include "lib/make_dcp.h"
39 #include "lib/ratio.h"
40 #include "lib/signal_manager.h"
41 #include "lib/transcode_job.h"
42 #include "lib/util.h"
43 #include "lib/variant.h"
44 #include "lib/version.h"
45 #include "lib/video_content.h"
46 #include <dcp/filesystem.h>
47 #include <dcp/version.h>
48 #include <getopt.h>
49 #include <iostream>
50 #include <iomanip>
51
52
53 using std::cerr;
54 using std::cout;
55 using std::dynamic_pointer_cast;
56 using std::list;
57 using std::pair;
58 using std::runtime_error;
59 using std::setw;
60 using std::shared_ptr;
61 using std::string;
62 using std::vector;
63 using boost::optional;
64
65
66 static void
67 help (string n)
68 {
69         cerr << "Syntax: " << n << " [OPTION] [<FILM>]\n"
70              << variant::insert_dcpomatic("  -v, --version                     show %1 version\n")
71              << "  -h, --help                        show this help\n"
72              << "  -f, --flags                       show flags passed to C++ compiler on build\n"
73              << "  -n, --no-progress                 do not print progress to stdout\n"
74              << "  -r, --no-remote                   do not use any remote servers\n"
75              << "  -t, --threads                     specify number of local encoding threads (overriding configuration)\n"
76              << "  -j, --json <port>                 run a JSON server on the specified port\n"
77              << "  -k, --keep-going                  keep running even when the job is complete\n"
78              << "  -s, --servers <file>              specify servers to use in a text file\n"
79              << variant::insert_dcpomatic("  -l, --list-servers                just display a list of encoding servers that %1 is configured to use; don't encode\n")
80              << "  -d, --dcp-path                    echo DCP's path to stdout on successful completion (implies -n)\n"
81              << "  -c, --config <dir>                directory containing config.xml and cinemas.xml\n"
82              << "      --dump                        just dump a summary of the film's settings; don't encode\n"
83              << "      --no-check                    don't check project's content files for changes before making the DCP\n"
84              << "      --export-format <format>      export project to a file, rather than making a DCP: specify mov or mp4\n"
85              << "      --export-filename <filename>  filename to export to with --export-format\n"
86              << "      --hints                       analyze film for hints before encoding and abort if any are found\n"
87              << "\n"
88              << "<FILM> is the film directory.\n";
89 }
90
91
92 static void
93 print_dump (shared_ptr<Film> film)
94 {
95         cout << film->dcp_name (true) << "\n"
96              << film->container()->container_nickname() << " at " << ((film->resolution() == Resolution::TWO_K) ? "2K" : "4K") << "\n"
97              << (film->j2k_bandwidth() / 1000000) << "Mbit/s" << "\n"
98              << "Duration " << (film->length().timecode(film->video_frame_rate())) << "\n"
99              << "Output " << film->video_frame_rate() << "fps " << (film->three_d() ? "3D" : "2D") << " " << (film->audio_frame_rate() / 1000) << "kHz\n"
100              << (film->interop() ? "Inter-Op" : "SMPTE") << " " << (film->encrypted() ? "encrypted" : "unencrypted") << "\n";
101
102         for (auto c: film->content()) {
103                 cout << "\n"
104                      << c->path(0).string() << "\n"
105                      << "\tat " << c->position().seconds ()
106                      << " length " << c->full_length(film).seconds ()
107                      << " start trim " << c->trim_start().seconds ()
108                      << " end trim " << c->trim_end().seconds () << "\n";
109
110                 if (c->video && c->video->size()) {
111                         cout << "\t" << c->video->size()->width << "x" << c->video->size()->height << "\n"
112                              << "\t" << c->active_video_frame_rate(film) << "fps\n"
113                              << "\tcrop left " << c->video->requested_left_crop()
114                              << " right " << c->video->requested_right_crop()
115                              << " top " << c->video->requested_top_crop()
116                              << " bottom " << c->video->requested_bottom_crop() << "\n";
117                         if (c->video->custom_ratio()) {
118                                 cout << "\tscale to custom ratio " << *c->video->custom_ratio() << ":1\n";
119                         }
120                         if (c->video->colour_conversion()) {
121                                 if (c->video->colour_conversion().get().preset()) {
122                                         cout << "\tcolour conversion "
123                                              << PresetColourConversion::all()[c->video->colour_conversion().get().preset().get()].name
124                                              << "\n";
125                                 } else {
126                                         cout << "\tcustom colour conversion\n";
127                                 }
128                         } else {
129                                 cout << "\tno colour conversion\n";
130                         }
131
132                 }
133
134                 if (c->audio) {
135                         cout << "\t" << c->audio->delay() << " delay\n"
136                              << "\t" << c->audio->gain() << " gain\n";
137                 }
138         }
139 }
140
141
142 static void
143 list_servers ()
144 {
145         while (true) {
146                 int N = 0;
147                 auto servers = EncodeServerFinder::instance()->servers();
148
149                 /* This is a bit fiddly because we want to list configured servers that are down as well
150                    as all those (configured and found by broadcast) that are up.
151                 */
152
153                 if (servers.empty() && Config::instance()->servers().empty()) {
154                         cout << "No encoding servers found or configured.\n";
155                         ++N;
156                 } else {
157                         cout << std::left << setw(24) << "Host" << " Status Threads\n";
158                         ++N;
159
160                         /* Report the state of configured servers */
161                         for (auto i: Config::instance()->servers()) {
162                                 cout << std::left << setw(24) << i << " ";
163
164                                 /* See if this server is on the active list; if so, remove it and note
165                                    the number of threads it is offering.
166                                 */
167                                 optional<int> threads;
168                                 auto j = servers.begin ();
169                                 while (j != servers.end ()) {
170                                         if (i == j->host_name() && j->current_link_version()) {
171                                                 threads = j->threads();
172                                                 auto tmp = j;
173                                                 ++tmp;
174                                                 servers.erase (j);
175                                                 j = tmp;
176                                         } else {
177                                                 ++j;
178                                         }
179                                 }
180                                 if (static_cast<bool>(threads)) {
181                                         cout << "UP     " << threads.get() << "\n";
182                                 } else {
183                                         cout << "DOWN\n";
184                                 }
185                                 ++N;
186                         }
187
188                         /* Now report any left that have been found by broadcast */
189                         for (auto const& i: servers) {
190                                 if (i.current_link_version()) {
191                                         cout << std::left << setw(24) << i.host_name() << " UP     " << i.threads() << "\n";
192                                 } else {
193                                         cout << std::left << setw(24) << i.host_name() << " bad version\n";
194                                 }
195                                 ++N;
196                         }
197                 }
198
199                 dcpomatic_sleep_seconds (1);
200
201                 for (int i = 0; i < N; ++i) {
202                         cout << "\033[1A\033[2K";
203                 }
204         }
205 }
206
207
208 bool
209 show_jobs_on_console (bool progress)
210 {
211         bool first = true;
212         bool error = false;
213         while (true) {
214
215                 dcpomatic_sleep_seconds (5);
216
217                 auto jobs = JobManager::instance()->get();
218
219                 if (!first && progress) {
220                         for (size_t i = 0; i < jobs.size(); ++i) {
221                                 cout << UP_ONE_LINE_AND_ERASE;
222                         }
223                         cout.flush ();
224                 }
225
226                 first = false;
227
228                 for (auto i: jobs) {
229                         if (progress) {
230                                 cout << i->name();
231                                 if (!i->sub_name().empty()) {
232                                         cout << "; " << i->sub_name();
233                                 }
234                                 cout << ": ";
235
236                                 if (i->progress ()) {
237                                         cout << i->status() << "                            \n";
238                                 } else {
239                                         cout << ": Running           \n";
240                                 }
241                         }
242
243                         if (!progress && i->finished_in_error()) {
244                                 /* We won't see this error if we haven't been showing progress,
245                                    so show it now.
246                                 */
247                                 cout << i->status() << "\n";
248                         }
249
250                         if (i->finished_in_error()) {
251                                 error = true;
252                         }
253                 }
254
255                 if (!JobManager::instance()->work_to_do()) {
256                         break;
257                 }
258         }
259
260         return error;
261 }
262
263
264 int
265 main (int argc, char* argv[])
266 {
267         boost::filesystem::path film_dir;
268         bool progress = true;
269         bool no_remote = false;
270         optional<int> threads;
271         optional<int> json_port;
272         bool keep_going = false;
273         bool dump = false;
274         optional<boost::filesystem::path> servers;
275         bool list_servers_ = false;
276         bool dcp_path = false;
277         optional<boost::filesystem::path> config;
278         bool check = true;
279         optional<string> export_format;
280         optional<boost::filesystem::path> export_filename;
281         bool hints = false;
282
283         int option_index = 0;
284         while (true) {
285                 static struct option long_options[] = {
286                         { "version", no_argument, 0, 'v'},
287                         { "help", no_argument, 0, 'h'},
288                         { "flags", no_argument, 0, 'f'},
289                         { "no-progress", no_argument, 0, 'n'},
290                         { "no-remote", no_argument, 0, 'r'},
291                         { "threads", required_argument, 0, 't'},
292                         { "json", required_argument, 0, 'j'},
293                         { "keep-going", no_argument, 0, 'k' },
294                         { "servers", required_argument, 0, 's' },
295                         { "list-servers", no_argument, 0, 'l' },
296                         { "dcp-path", no_argument, 0, 'd' },
297                         { "config", required_argument, 0, 'c' },
298                         /* Just using A, B, C ... from here on */
299                         { "dump", no_argument, 0, 'A' },
300                         { "no-check", no_argument, 0, 'B' },
301                         { "export-format", required_argument, 0, 'C' },
302                         { "export-filename", required_argument, 0, 'D' },
303                         { "hints", no_argument, 0, 'E' },
304                         { 0, 0, 0, 0 }
305                 };
306
307                 int c = getopt_long (argc, argv, "vhfnrt:j:kAs:ldc:BC:D:E", long_options, &option_index);
308
309                 if (c == -1) {
310                         break;
311                 }
312
313                 switch (c) {
314                 case 'v':
315                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
316                         exit (EXIT_SUCCESS);
317                 case 'h':
318                         help (argv[0]);
319                         exit (EXIT_SUCCESS);
320                 case 'f':
321                         cout << dcpomatic_cxx_flags << "\n";
322                         exit (EXIT_SUCCESS);
323                 case 'n':
324                         progress = false;
325                         break;
326                 case 'r':
327                         no_remote = true;
328                         break;
329                 case 't':
330                         threads = atoi (optarg);
331                         break;
332                 case 'j':
333                         json_port = atoi (optarg);
334                         break;
335                 case 'k':
336                         keep_going = true;
337                         break;
338                 case 'A':
339                         dump = true;
340                         break;
341                 case 's':
342                         servers = optarg;
343                         break;
344                 case 'l':
345                         list_servers_ = true;
346                         break;
347                 case 'd':
348                         dcp_path = true;
349                         progress = false;
350                         break;
351                 case 'c':
352                         config = optarg;
353                         break;
354                 case 'B':
355                         check = false;
356                         break;
357                 case 'C':
358                         export_format = optarg;
359                         break;
360                 case 'D':
361                         export_filename = optarg;
362                         break;
363                 case 'E':
364                         hints = true;
365                         break;
366                 }
367         }
368
369         if (config) {
370                 State::override_path = *config;
371         }
372
373         if (servers) {
374                 dcp::File f(*servers, "r");
375                 if (!f) {
376                         cerr << "Could not open servers list file " << *servers << "\n";
377                         exit (EXIT_FAILURE);
378                 }
379                 vector<string> servers;
380                 while (!f.eof()) {
381                         char buffer[128];
382                         if (fscanf(f.get(), "%s.127", buffer) == 1) {
383                                 servers.push_back (buffer);
384                         }
385                 }
386                 Config::instance()->set_servers (servers);
387         }
388
389         if (list_servers_) {
390                 list_servers ();
391                 exit (EXIT_SUCCESS);
392         }
393
394         if (optind >= argc) {
395                 help (argv[0]);
396                 exit (EXIT_FAILURE);
397         }
398
399         if (export_format && !export_filename) {
400                 cerr << "Argument --export-filename is required with --export-format\n";
401                 exit (EXIT_FAILURE);
402         }
403
404         if (!export_format && export_filename) {
405                 cerr << "Argument --export-format is required with --export-filename\n";
406                 exit (EXIT_FAILURE);
407         }
408
409         if (export_format && *export_format != "mp4" && *export_format != "mov") {
410                 cerr << "Unrecognised export format: must be mp4 or mov\n";
411                 exit (EXIT_FAILURE);
412         }
413
414         film_dir = argv[optind];
415
416         dcpomatic_setup_path_encoding ();
417         dcpomatic_setup ();
418         signal_manager = new SignalManager ();
419
420         if (no_remote || export_format) {
421                 EncodeServerFinder::drop();
422         }
423
424         if (json_port) {
425                 new JSONServer (json_port.get ());
426         }
427
428         if (threads) {
429                 Config::instance()->set_master_encoding_threads (threads.get ());
430         }
431
432         shared_ptr<Film> film;
433         try {
434                 film.reset (new Film (film_dir));
435                 film->read_metadata ();
436         } catch (std::exception& e) {
437                 cerr << argv[0] << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
438                 exit (EXIT_FAILURE);
439         }
440
441         if (dump) {
442                 print_dump (film);
443                 exit (EXIT_SUCCESS);
444         }
445
446         dcpomatic_log = film->log ();
447
448         for (auto i: film->content()) {
449                 auto paths = i->paths();
450                 for (auto j: paths) {
451                         if (!dcp::filesystem::exists(j)) {
452                                 cerr << argv[0] << ": content file " << j << " not found.\n";
453                                 exit (EXIT_FAILURE);
454                         }
455                 }
456         }
457
458         if (!export_format && hints) {
459                 string const prefix = "Checking project for hints";
460                 bool pulse_phase = false;
461                 vector<string> hints;
462                 bool finished = false;
463
464                 Hints hint_finder(film);
465                 hint_finder.Progress.connect([prefix](string progress) {
466                                              std::cout << UP_ONE_LINE_AND_ERASE << prefix << ": " << progress << "\n";
467                                              std::cout.flush();
468                                              });
469                 hint_finder.Pulse.connect([prefix, &pulse_phase]() {
470                                           std::cout << UP_ONE_LINE_AND_ERASE << prefix << ": " << (pulse_phase ? "X" : "x") << "\n";
471                                           std::cout.flush();
472                                           pulse_phase = !pulse_phase;
473                                           });
474                 hint_finder.Hint.connect([&hints](string hint) {
475                                          hints.push_back(hint);
476                                          });
477                 hint_finder.Finished.connect([&finished]() {
478                                              finished = true;
479                                              });
480
481                 std::cout << prefix << ":\n";
482                 std::cout.flush();
483
484                 hint_finder.start();
485                 while (!finished) {
486                         signal_manager->ui_idle();
487                         dcpomatic_sleep_milliseconds(200);
488                 }
489
490                 std::cout << UP_ONE_LINE_AND_ERASE;
491
492                 if (!hints.empty()) {
493                         std::cerr << "Hints:\n\n";
494                         for (auto hint: hints) {
495                                 std::cerr << word_wrap("* " + hint, 70) << "\n";
496                         }
497                         std::cerr << "*** Encoding aborted because hints were found ***\n\n";
498                         std::cerr << "Modify your settings and run the command again, or run without\n";
499                         std::cerr << "the `--hints' option to ignore these hints and encode anyway.\n";
500                         exit(EXIT_FAILURE);
501                 }
502         }
503
504 #ifdef DCPOMATIC_GROK
505         grk_plugin::setMessengerLogger(new grk_plugin::GrokLogger("[GROK] "));
506         setup_grok_library_path();
507 #endif
508
509         if (progress) {
510                 if (export_format) {
511                         cout << "\nExporting " << film->name() << "\n";
512                 } else {
513                         cout << "\nMaking DCP for " << film->name() << "\n";
514                 }
515         }
516
517         TranscodeJob::ChangedBehaviour const behaviour = check ? TranscodeJob::ChangedBehaviour::STOP : TranscodeJob::ChangedBehaviour::IGNORE;
518
519         if (export_format) {
520                 auto job = std::make_shared<TranscodeJob>(film, behaviour);
521                 job->set_encoder (
522                         std::make_shared<FFmpegEncoder> (
523                                 film, job, *export_filename, *export_format == "mp4" ? ExportFormat::H264_AAC : ExportFormat::PRORES_HQ, false, false, false, 23
524                                 )
525                         );
526                 JobManager::instance()->add (job);
527         } else {
528                 try {
529                         make_dcp (film, behaviour);
530                 } catch (runtime_error& e) {
531                         std::cerr << "Could not make DCP: " << e.what() << "\n";
532                         exit(EXIT_FAILURE);
533                 }
534         }
535
536         bool const error = show_jobs_on_console (progress);
537
538         if (keep_going) {
539                 while (true) {
540                         dcpomatic_sleep_seconds (3600);
541                 }
542         }
543
544         /* This is just to stop valgrind reporting leaks due to JobManager
545            indirectly holding onto codecs.
546         */
547         JobManager::drop ();
548
549         EncodeServerFinder::drop ();
550
551         if (dcp_path && !error) {
552                 cout << film->dir (film->dcp_name (false)).string() << "\n";
553         }
554
555         return error ? EXIT_FAILURE : EXIT_SUCCESS;
556 }