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