2 Copyright (C) 2012-2022 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
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"
30 #include "lib/filter.h"
31 #include "lib/hints.h"
32 #include "lib/job_manager.h"
33 #include "lib/json_server.h"
35 #include "lib/make_dcp.h"
36 #include "lib/ratio.h"
37 #include "lib/signal_manager.h"
38 #include "lib/transcode_job.h"
40 #include "lib/version.h"
41 #include "lib/video_content.h"
42 #include <dcp/filesystem.h>
43 #include <dcp/version.h>
51 using std::dynamic_pointer_cast;
54 using std::runtime_error;
56 using std::shared_ptr;
59 using boost::optional;
65 cerr << "Syntax: " << n << " [OPTION] [<FILM>]\n"
66 << " -v, --version show DCP-o-matic version\n"
67 << " -h, --help show this help\n"
68 << " -f, --flags show flags passed to C++ compiler on build\n"
69 << " -n, --no-progress do not print progress to stdout\n"
70 << " -r, --no-remote do not use any remote servers\n"
71 << " -t, --threads specify number of local encoding threads (overriding configuration)\n"
72 << " -j, --json <port> run a JSON server on the specified port\n"
73 << " -k, --keep-going keep running even when the job is complete\n"
74 << " -s, --servers <file> specify servers to use in a text file\n"
75 << " -l, --list-servers just display a list of encoding servers that DCP-o-matic is configured to use; don't encode\n"
76 << " -d, --dcp-path echo DCP's path to stdout on successful completion (implies -n)\n"
77 << " -c, --config <dir> directory containing config.xml and cinemas.xml\n"
78 << " --dump just dump a summary of the film's settings; don't encode\n"
79 << " --no-check don't check project's content files for changes before making the DCP\n"
80 << " --export-format <format> export project to a file, rather than making a DCP: specify mov or mp4\n"
81 << " --export-filename <filename> filename to export to with --export-format\n"
82 << " --hints analyze film for hints before encoding and abort if any are found\n"
84 << "<FILM> is the film directory.\n";
89 print_dump (shared_ptr<Film> film)
91 cout << film->dcp_name (true) << "\n"
92 << film->container()->container_nickname() << " at " << ((film->resolution() == Resolution::TWO_K) ? "2K" : "4K") << "\n"
93 << (film->j2k_bandwidth() / 1000000) << "Mbit/s" << "\n"
94 << "Output " << film->video_frame_rate() << "fps " << (film->three_d() ? "3D" : "2D") << " " << (film->audio_frame_rate() / 1000) << "kHz\n"
95 << (film->interop() ? "Inter-Op" : "SMPTE") << " " << (film->encrypted() ? "encrypted" : "unencrypted") << "\n";
97 for (auto c: film->content()) {
100 << "\tat " << c->position().seconds ()
101 << " length " << c->full_length(film).seconds ()
102 << " start trim " << c->trim_start().seconds ()
103 << " end trim " << c->trim_end().seconds () << "\n";
106 cout << "\t" << c->video->size().width << "x" << c->video->size().height << "\n"
107 << "\t" << c->active_video_frame_rate(film) << "fps\n"
108 << "\tcrop left " << c->video->requested_left_crop()
109 << " right " << c->video->requested_right_crop()
110 << " top " << c->video->requested_top_crop()
111 << " bottom " << c->video->requested_bottom_crop() << "\n";
112 if (c->video->custom_ratio()) {
113 cout << "\tscale to custom ratio " << *c->video->custom_ratio() << ":1\n";
115 if (c->video->colour_conversion()) {
116 if (c->video->colour_conversion().get().preset()) {
117 cout << "\tcolour conversion "
118 << PresetColourConversion::all()[c->video->colour_conversion().get().preset().get()].name
121 cout << "\tcustom colour conversion\n";
124 cout << "\tno colour conversion\n";
130 cout << "\t" << c->audio->delay() << " delay\n"
131 << "\t" << c->audio->gain() << " gain\n";
142 auto servers = EncodeServerFinder::instance()->servers();
144 /* This is a bit fiddly because we want to list configured servers that are down as well
145 as all those (configured and found by broadcast) that are up.
148 if (servers.empty() && Config::instance()->servers().empty()) {
149 cout << "No encoding servers found or configured.\n";
152 cout << std::left << setw(24) << "Host" << " Status Threads\n";
155 /* Report the state of configured servers */
156 for (auto i: Config::instance()->servers()) {
157 cout << std::left << setw(24) << i << " ";
159 /* See if this server is on the active list; if so, remove it and note
160 the number of threads it is offering.
162 optional<int> threads;
163 auto j = servers.begin ();
164 while (j != servers.end ()) {
165 if (i == j->host_name() && j->current_link_version()) {
166 threads = j->threads();
175 if (static_cast<bool>(threads)) {
176 cout << "UP " << threads.get() << "\n";
183 /* Now report any left that have been found by broadcast */
184 for (auto const& i: servers) {
185 if (i.current_link_version()) {
186 cout << std::left << setw(24) << i.host_name() << " UP " << i.threads() << "\n";
188 cout << std::left << setw(24) << i.host_name() << " bad version\n";
194 dcpomatic_sleep_seconds (1);
196 for (int i = 0; i < N; ++i) {
197 cout << "\033[1A\033[2K";
204 show_jobs_on_console (bool progress)
210 dcpomatic_sleep_seconds (5);
212 auto jobs = JobManager::instance()->get();
214 if (!first && progress) {
215 for (size_t i = 0; i < jobs.size(); ++i) {
216 cout << UP_ONE_LINE_AND_ERASE;
226 if (!i->sub_name().empty()) {
227 cout << "; " << i->sub_name();
231 if (i->progress ()) {
232 cout << i->status() << " \n";
234 cout << ": Running \n";
238 if (!progress && i->finished_in_error()) {
239 /* We won't see this error if we haven't been showing progress,
242 cout << i->status() << "\n";
245 if (i->finished_in_error()) {
250 if (!JobManager::instance()->work_to_do()) {
260 main (int argc, char* argv[])
262 boost::filesystem::path film_dir;
263 bool progress = true;
264 bool no_remote = false;
265 optional<int> threads;
266 optional<int> json_port;
267 bool keep_going = false;
269 optional<boost::filesystem::path> servers;
270 bool list_servers_ = false;
271 bool dcp_path = false;
272 optional<boost::filesystem::path> config;
274 optional<string> export_format;
275 optional<boost::filesystem::path> export_filename;
278 int option_index = 0;
280 static struct option long_options[] = {
281 { "version", no_argument, 0, 'v'},
282 { "help", no_argument, 0, 'h'},
283 { "flags", no_argument, 0, 'f'},
284 { "no-progress", no_argument, 0, 'n'},
285 { "no-remote", no_argument, 0, 'r'},
286 { "threads", required_argument, 0, 't'},
287 { "json", required_argument, 0, 'j'},
288 { "keep-going", no_argument, 0, 'k' },
289 { "servers", required_argument, 0, 's' },
290 { "list-servers", no_argument, 0, 'l' },
291 { "dcp-path", no_argument, 0, 'd' },
292 { "config", required_argument, 0, 'c' },
293 /* Just using A, B, C ... from here on */
294 { "dump", no_argument, 0, 'A' },
295 { "no-check", no_argument, 0, 'B' },
296 { "export-format", required_argument, 0, 'C' },
297 { "export-filename", required_argument, 0, 'D' },
298 { "hints", no_argument, 0, 'E' },
302 int c = getopt_long (argc, argv, "vhfnrt:j:kAs:ldc:BC:D:E", long_options, &option_index);
310 cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
316 cout << dcpomatic_cxx_flags << "\n";
325 threads = atoi (optarg);
328 json_port = atoi (optarg);
340 list_servers_ = true;
353 export_format = optarg;
356 export_filename = optarg;
365 State::override_path = *config;
369 dcp::File f(*servers, "r");
371 cerr << "Could not open servers list file " << *servers << "\n";
374 vector<string> servers;
377 if (fscanf(f.get(), "%s.127", buffer) == 1) {
378 servers.push_back (buffer);
381 Config::instance()->set_servers (servers);
389 if (optind >= argc) {
394 if (export_format && !export_filename) {
395 cerr << "Argument --export-filename is required with --export-format\n";
399 if (!export_format && export_filename) {
400 cerr << "Argument --export-format is required with --export-filename\n";
404 if (export_format && *export_format != "mp4" && *export_format != "mov") {
405 cerr << "Unrecognised export format: must be mp4 or mov\n";
409 film_dir = argv[optind];
411 dcpomatic_setup_path_encoding ();
413 signal_manager = new SignalManager ();
415 if (no_remote || export_format) {
416 EncodeServerFinder::drop();
420 new JSONServer (json_port.get ());
424 Config::instance()->set_master_encoding_threads (threads.get ());
427 shared_ptr<Film> film;
429 film.reset (new Film (film_dir));
430 film->read_metadata ();
431 } catch (std::exception& e) {
432 cerr << argv[0] << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
441 dcpomatic_log = film->log ();
443 for (auto i: film->content()) {
444 auto paths = i->paths();
445 for (auto j: paths) {
446 if (!dcp::filesystem::exists(j)) {
447 cerr << argv[0] << ": content file " << j << " not found.\n";
453 if (!export_format && hints) {
454 string const prefix = "Checking project for hints";
455 bool pulse_phase = false;
456 vector<string> hints;
457 bool finished = false;
459 Hints hint_finder(film);
460 hint_finder.Progress.connect([prefix](string progress) {
461 std::cout << UP_ONE_LINE_AND_ERASE << prefix << ": " << progress << "\n";
464 hint_finder.Pulse.connect([prefix, &pulse_phase]() {
465 std::cout << UP_ONE_LINE_AND_ERASE << prefix << ": " << (pulse_phase ? "X" : "x") << "\n";
467 pulse_phase = !pulse_phase;
469 hint_finder.Hint.connect([&hints](string hint) {
470 hints.push_back(hint);
472 hint_finder.Finished.connect([&finished]() {
476 std::cout << prefix << ":\n";
481 signal_manager->ui_idle();
482 dcpomatic_sleep_milliseconds(200);
485 std::cout << UP_ONE_LINE_AND_ERASE;
487 if (!hints.empty()) {
488 std::cerr << "Hints:\n\n";
489 for (auto hint: hints) {
490 std::cerr << word_wrap("* " + hint, 70) << "\n";
492 std::cerr << "*** Encoding aborted because hints were found ***\n\n";
493 std::cerr << "Modify your settings and run the command again, or run without\n";
494 std::cerr << "the `--hints' option to ignore these hints and encode anyway.\n";
501 cout << "\nExporting " << film->name() << "\n";
503 cout << "\nMaking DCP for " << film->name() << "\n";
507 TranscodeJob::ChangedBehaviour const behaviour = check ? TranscodeJob::ChangedBehaviour::STOP : TranscodeJob::ChangedBehaviour::IGNORE;
510 auto job = std::make_shared<TranscodeJob>(film, behaviour);
512 std::make_shared<FFmpegEncoder> (
513 film, job, *export_filename, *export_format == "mp4" ? ExportFormat::H264_AAC : ExportFormat::PRORES_HQ, false, false, false, 23
516 JobManager::instance()->add (job);
519 make_dcp (film, behaviour);
520 } catch (runtime_error& e) {
521 std::cerr << "Could not make DCP: " << e.what() << "\n";
526 bool const error = show_jobs_on_console (progress);
530 dcpomatic_sleep_seconds (3600);
534 /* This is just to stop valgrind reporting leaks due to JobManager
535 indirectly holding onto codecs.
539 EncodeServerFinder::drop ();
541 if (dcp_path && !error) {
542 cout << film->dir (film->dcp_name (false)).string() << "\n";
545 return error ? EXIT_FAILURE : EXIT_SUCCESS;