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/>.
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"
29 #include "lib/filter.h"
30 #include "lib/job_manager.h"
31 #include "lib/json_server.h"
33 #include "lib/ratio.h"
34 #include "lib/signal_manager.h"
35 #include "lib/transcode_job.h"
37 #include "lib/version.h"
38 #include "lib/video_content.h"
39 #include <dcp/version.h>
47 using std::dynamic_pointer_cast;
50 using std::runtime_error;
52 using std::shared_ptr;
55 using boost::optional;
61 cerr << "Syntax: " << n << " [OPTION] [<FILM>]\n"
62 << " -v, --version show DCP-o-matic version\n"
63 << " -h, --help show this help\n"
64 << " -f, --flags show flags passed to C++ compiler on build\n"
65 << " -n, --no-progress do not print progress to stdout\n"
66 << " -r, --no-remote do not use any remote servers\n"
67 << " -t, --threads specify number of local encoding threads (overriding configuration)\n"
68 << " -j, --json <port> run a JSON server on the specified port\n"
69 << " -k, --keep-going keep running even when the job is complete\n"
70 << " -s, --servers <file> specify servers to use in a text file\n"
71 << " -l, --list-servers just display a list of encoding servers that DCP-o-matic is configured to use; don't encode\n"
72 << " -d, --dcp-path echo DCP's path to stdout on successful completion (implies -n)\n"
73 << " -c, --config <dir> directory containing config.xml and cinemas.xml\n"
74 << " --dump just dump a summary of the film's settings; don't encode\n"
75 << " --no-check don't check project's content files for changes before making the DCP\n"
76 << " --export-format <format> export project to a file, rather than making a DCP: specify mov or mp4\n"
77 << " --export-filename filename to export to with --export\n"
79 << "<FILM> is the film directory.\n";
84 print_dump (shared_ptr<Film> film)
86 cout << film->dcp_name (true) << "\n"
87 << film->container()->container_nickname() << " at " << ((film->resolution() == Resolution::TWO_K) ? "2K" : "4K") << "\n"
88 << (film->j2k_bandwidth() / 1000000) << "Mbit/s" << "\n"
89 << "Output " << film->video_frame_rate() << "fps " << (film->three_d() ? "3D" : "2D") << " " << (film->audio_frame_rate() / 1000) << "kHz\n"
90 << (film->interop() ? "Inter-Op" : "SMPTE") << " " << (film->encrypted() ? "encrypted" : "unencrypted") << "\n";
92 for (auto c: film->content()) {
95 << "\tat " << c->position().seconds ()
96 << " length " << c->full_length(film).seconds ()
97 << " start trim " << c->trim_start().seconds ()
98 << " end trim " << c->trim_end().seconds () << "\n";
101 cout << "\t" << c->video->size().width << "x" << c->video->size().height << "\n"
102 << "\t" << c->active_video_frame_rate(film) << "fps\n"
103 << "\tcrop left " << c->video->requested_left_crop()
104 << " right " << c->video->requested_right_crop()
105 << " top " << c->video->requested_top_crop()
106 << " bottom " << c->video->requested_bottom_crop() << "\n";
107 if (c->video->custom_ratio()) {
108 cout << "\tscale to custom ratio " << *c->video->custom_ratio() << ":1\n";
110 if (c->video->colour_conversion()) {
111 if (c->video->colour_conversion().get().preset()) {
112 cout << "\tcolour conversion "
113 << PresetColourConversion::all()[c->video->colour_conversion().get().preset().get()].name
116 cout << "\tcustom colour conversion\n";
119 cout << "\tno colour conversion\n";
125 cout << "\t" << c->audio->delay() << " delay\n"
126 << "\t" << c->audio->gain() << " gain\n";
137 auto servers = EncodeServerFinder::instance()->servers();
139 /* This is a bit fiddly because we want to list configured servers that are down as well
140 as all those (configured and found by broadcast) that are up.
143 if (servers.empty() && Config::instance()->servers().empty()) {
144 cout << "No encoding servers found or configured.\n";
147 cout << std::left << setw(24) << "Host" << " Status Threads\n";
150 /* Report the state of configured servers */
151 for (auto i: Config::instance()->servers()) {
152 cout << std::left << setw(24) << i << " ";
154 /* See if this server is on the active list; if so, remove it and note
155 the number of threads it is offering.
157 optional<int> threads;
158 auto j = servers.begin ();
159 while (j != servers.end ()) {
160 if (i == j->host_name() && j->current_link_version()) {
161 threads = j->threads();
170 if (static_cast<bool>(threads)) {
171 cout << "UP " << threads.get() << "\n";
178 /* Now report any left that have been found by broadcast */
179 for (auto const& i: servers) {
180 if (i.current_link_version()) {
181 cout << std::left << setw(24) << i.host_name() << " UP " << i.threads() << "\n";
183 cout << std::left << setw(24) << i.host_name() << " bad version\n";
189 dcpomatic_sleep_seconds (1);
191 for (int i = 0; i < N; ++i) {
192 cout << "\033[1A\033[2K";
199 main (int argc, char* argv[])
201 boost::filesystem::path film_dir;
202 bool progress = true;
203 bool no_remote = false;
204 optional<int> threads;
205 optional<int> json_port;
206 bool keep_going = false;
208 optional<boost::filesystem::path> servers;
209 bool list_servers_ = false;
210 bool dcp_path = false;
211 optional<boost::filesystem::path> config;
213 optional<string> export_format;
214 optional<boost::filesystem::path> export_filename;
216 int option_index = 0;
218 static struct option long_options[] = {
219 { "version", no_argument, 0, 'v'},
220 { "help", no_argument, 0, 'h'},
221 { "flags", no_argument, 0, 'f'},
222 { "no-progress", no_argument, 0, 'n'},
223 { "no-remote", no_argument, 0, 'r'},
224 { "threads", required_argument, 0, 't'},
225 { "json", required_argument, 0, 'j'},
226 { "keep-going", no_argument, 0, 'k' },
227 { "servers", required_argument, 0, 's' },
228 { "list-servers", no_argument, 0, 'l' },
229 { "dcp-path", no_argument, 0, 'd' },
230 { "config", required_argument, 0, 'c' },
231 /* Just using A, B, C ... from here on */
232 { "dump", no_argument, 0, 'A' },
233 { "no-check", no_argument, 0, 'B' },
234 { "export-format", required_argument, 0, 'C' },
235 { "export-filename", required_argument, 0, 'D' },
239 int c = getopt_long (argc, argv, "vhfnrt:j:kAs:ldc:BC:D:", long_options, &option_index);
247 cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
253 cout << dcpomatic_cxx_flags << "\n";
262 threads = atoi (optarg);
265 json_port = atoi (optarg);
277 list_servers_ = true;
290 export_format = optarg;
293 export_filename = optarg;
299 State::override_path = *config;
303 auto f = fopen_boost (*servers, "r");
305 cerr << "Could not open servers list file " << *servers << "\n";
308 vector<string> servers;
311 if (fscanf (f, "%s.127", buffer) == 1) {
312 servers.push_back (buffer);
316 Config::instance()->set_servers (servers);
324 if (optind >= argc) {
329 if (export_format && !export_filename) {
330 cerr << "Argument --export-filename is required with --export-format\n";
334 if (!export_format && export_filename) {
335 cerr << "Argument --export-format is required with --export-filename\n";
339 if (export_format && *export_format != "mp4" && *export_format != "mov") {
340 cerr << "Unrecognised export format: must be mp4 or mov\n";
344 film_dir = argv[optind];
346 dcpomatic_setup_path_encoding ();
348 signal_manager = new SignalManager ();
350 if (no_remote || export_format) {
351 EncodeServerFinder::instance()->stop ();
355 new JSONServer (json_port.get ());
359 Config::instance()->set_master_encoding_threads (threads.get ());
362 shared_ptr<Film> film;
364 film.reset (new Film (film_dir));
365 film->read_metadata ();
366 } catch (std::exception& e) {
367 cerr << argv[0] << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
376 dcpomatic_log = film->log ();
378 for (auto i: film->content()) {
379 auto paths = i->paths();
380 for (auto j: paths) {
381 if (!boost::filesystem::exists(j)) {
382 cerr << argv[0] << ": content file " << j << " not found.\n";
390 cout << "\nExporting " << film->name() << "\n";
392 cout << "\nMaking DCP for " << film->name() << "\n";
396 TranscodeJob::ChangedBehaviour behaviour = check ? TranscodeJob::ChangedBehaviour::STOP : TranscodeJob::ChangedBehaviour::IGNORE;
399 auto job = std::make_shared<TranscodeJob>(film, behaviour);
401 std::make_shared<FFmpegEncoder> (
402 film, job, *export_filename, *export_format == "mp4" ? ExportFormat::H264_AAC : ExportFormat::PRORES, false, false, false, 23
405 JobManager::instance()->add (job);
408 film->make_dcp (behaviour);
409 } catch (runtime_error& e) {
410 std::cerr << "Could not make DCP: " << e.what() << "\n";
415 bool const error = show_jobs_on_console (progress);
419 dcpomatic_sleep_seconds (3600);
423 /* This is just to stop valgrind reporting leaks due to JobManager
424 indirectly holding onto codecs.
428 EncodeServerFinder::drop ();
430 if (dcp_path && !error) {
431 cout << film->dir (film->dcp_name (false)).string() << "\n";
434 return error ? EXIT_FAILURE : EXIT_SUCCESS;