14b08d299621921cf93961e66ae6cfd68759e8ec
[dcpomatic.git] / src / tools / dcpomatic_cli.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/film.h"
21 #include "lib/filter.h"
22 #include "lib/transcode_job.h"
23 #include "lib/job_manager.h"
24 #include "lib/util.h"
25 #include "lib/version.h"
26 #include "lib/cross.h"
27 #include "lib/config.h"
28 #include "lib/log.h"
29 #include "lib/signal_manager.h"
30 #include "lib/server_finder.h"
31 #include "lib/json_server.h"
32 #include "lib/ratio.h"
33 #include "lib/video_content.h"
34 #include "lib/audio_content.h"
35 #include <dcp/version.h>
36 #include <boost/foreach.hpp>
37 #include <getopt.h>
38 #include <iostream>
39 #include <iomanip>
40
41 using std::string;
42 using std::cerr;
43 using std::cout;
44 using std::vector;
45 using std::pair;
46 using std::setw;
47 using std::list;
48 using boost::shared_ptr;
49 using boost::optional;
50 using boost::dynamic_pointer_cast;
51
52 static void
53 help (string n)
54 {
55         cerr << "Syntax: " << n << " [OPTION] [<FILM>]\n"
56              << "  -v, --version      show DCP-o-matic version\n"
57              << "  -h, --help         show this help\n"
58              << "  -f, --flags        show flags passed to C++ compiler on build\n"
59              << "  -n, --no-progress  do not print progress to stdout\n"
60              << "  -r, --no-remote    do not use any remote servers\n"
61              << "  -j, --json <port>  run a JSON server on the specified port\n"
62              << "  -k, --keep-going   keep running even when the job is complete\n"
63              << "  -s, --servers      just display a list of encoding servers that DCP-o-matic is configured to use; don't encode\n"
64              << "      --dump         just dump a summary of the film's settings; don't encode\n"
65              << "\n"
66              << "<FILM> is the film directory.\n";
67 }
68
69 static void
70 print_dump (shared_ptr<Film> film)
71 {
72         cout << film->dcp_name (true) << "\n"
73              << film->container()->nickname() << " at " << ((film->resolution() == RESOLUTION_2K) ? "2K" : "4K") << "\n"
74              << (film->j2k_bandwidth() / 1000000) << "Mbit/s" << "\n"
75              << "Output " << film->video_frame_rate() << "fps " << (film->three_d() ? "3D" : "2D") << " " << (film->audio_frame_rate() / 1000) << "kHz\n"
76              << (film->interop() ? "Inter-Op" : "SMPTE") << " " << (film->encrypted() ? "encrypted" : "unencrypted") << "\n";
77
78         BOOST_FOREACH (shared_ptr<Content> c, film->content ()) {
79                 cout << "\n"
80                      << c->path(0) << "\n"
81                      << "\tat " << c->position().seconds ()
82                      << " length " << c->full_length().seconds ()
83                      << " start trim " << c->trim_start().seconds ()
84                      << " end trim " << c->trim_end().seconds () << "\n";
85
86                 shared_ptr<VideoContent> video = dynamic_pointer_cast<VideoContent> (c);
87                 if (video) {
88                         cout << "\t" << video->video_size().width << "x" << video->video_size().height << "\n"
89                              << "\t" << video->video_frame_rate() << "fps\n"
90                              << "\tcrop left " << video->left_crop()
91                              << " right " << video->right_crop()
92                              << " top " << video->top_crop()
93                              << " bottom " << video->bottom_crop() << "\n"
94                              << "\tscale " << video->scale().name() << "\n";
95                         if (video->colour_conversion()) {
96                                 if (video->colour_conversion().get().preset()) {
97                                         cout << "\tcolour conversion "
98                                              << PresetColourConversion::all()[video->colour_conversion().get().preset().get()].name
99                                              << "\n";
100                                 } else {
101                                         cout << "\tcustom colour conversion\n";
102                                 }
103                         } else {
104                                 cout << "\tno colour conversion\n";
105                         }
106
107                 }
108
109                 shared_ptr<AudioContent> audio = dynamic_pointer_cast<AudioContent> (c);
110                 if (audio) {
111                         cout << "\t" << audio->audio_delay() << " delay\n"
112                              << "\t" << audio->audio_gain() << " gain\n";
113                 }
114         }
115 }
116
117 static void
118 show_servers ()
119 {
120         while (true) {
121                 int N = 0;
122                 list<ServerDescription> servers = ServerFinder::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 (ServerDescription 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 (ServerDescription 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
183         int option_index = 0;
184         while (true) {
185                 static struct option long_options[] = {
186                         { "version", no_argument, 0, 'v'},
187                         { "help", no_argument, 0, 'h'},
188                         { "flags", no_argument, 0, 'f'},
189                         { "no-progress", no_argument, 0, 'n'},
190                         { "no-remote", no_argument, 0, 'r'},
191                         { "json", required_argument, 0, 'j'},
192                         { "keep-going", no_argument, 0, 'k' },
193                         { "servers", no_argument, 0, 's' },
194                         /* Just using A, B, C ... from here on */
195                         { "dump", no_argument, 0, 'A' },
196                         { 0, 0, 0, 0 }
197                 };
198
199                 int c = getopt_long (argc, argv, "vhfnrj:kAs", long_options, &option_index);
200
201                 if (c == -1) {
202                         break;
203                 }
204
205                 switch (c) {
206                 case 'v':
207                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
208                         exit (EXIT_SUCCESS);
209                 case 'h':
210                         help (argv[0]);
211                         exit (EXIT_SUCCESS);
212                 case 'f':
213                         cout << dcpomatic_cxx_flags << "\n";
214                         exit (EXIT_SUCCESS);
215                 case 'n':
216                         progress = false;
217                         break;
218                 case 'r':
219                         no_remote = true;
220                         break;
221                 case 'j':
222                         json_port = atoi (optarg);
223                         break;
224                 case 'k':
225                         keep_going = true;
226                         break;
227                 case 'A':
228                         dump = true;
229                         break;
230                 case 's':
231                         servers = true;
232                         break;
233                 }
234         }
235
236         if (servers) {
237                 show_servers ();
238                 exit (EXIT_SUCCESS);
239         }
240
241         if (optind >= argc) {
242                 help (argv[0]);
243                 exit (EXIT_FAILURE);
244         }
245
246         film_dir = argv[optind];
247
248         dcpomatic_setup_path_encoding ();
249         dcpomatic_setup ();
250         signal_manager = new SignalManager ();
251
252         if (no_remote) {
253                 ServerFinder::instance()->disable ();
254         }
255
256         if (json_port) {
257                 new JSONServer (json_port.get ());
258         }
259
260         shared_ptr<Film> film;
261         try {
262                 film.reset (new Film (film_dir));
263                 film->read_metadata ();
264         } catch (std::exception& e) {
265                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
266                 exit (EXIT_FAILURE);
267         }
268
269         if (dump) {
270                 print_dump (film);
271                 exit (EXIT_SUCCESS);
272         }
273
274         ContentList content = film->content ();
275         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
276                 vector<boost::filesystem::path> paths = (*i)->paths ();
277                 for (vector<boost::filesystem::path>::const_iterator j = paths.begin(); j != paths.end(); ++j) {
278                         if (!boost::filesystem::exists (*j)) {
279                                 cerr << argv[0] << ": content file " << *j << " not found.\n";
280                                 exit (EXIT_FAILURE);
281                         }
282                 }
283         }
284
285         if (progress) {
286                 cout << "\nMaking DCP for " << film->name() << "\n";
287         }
288
289         film->make_dcp ();
290
291         bool should_stop = false;
292         bool first = true;
293         bool error = false;
294         while (!should_stop) {
295
296                 dcpomatic_sleep (5);
297
298                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
299
300                 if (!first && progress) {
301                         for (size_t i = 0; i < jobs.size(); ++i) {
302                                 cout << "\033[1A\033[2K";
303                         }
304                         cout.flush ();
305                 }
306
307                 first = false;
308
309                 int unfinished = 0;
310                 int finished_in_error = 0;
311
312                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
313                         if (progress) {
314                                 cout << (*i)->name() << ": ";
315
316                                 if ((*i)->progress ()) {
317                                         cout << (*i)->status() << "                         \n";
318                                 } else {
319                                         cout << ": Running           \n";
320                                 }
321                         }
322
323                         if (!(*i)->finished ()) {
324                                 ++unfinished;
325                         }
326
327                         if ((*i)->finished_in_error ()) {
328                                 ++finished_in_error;
329                                 error = true;
330                         }
331
332                         if (!progress && (*i)->finished_in_error ()) {
333                                 /* We won't see this error if we haven't been showing progress,
334                                    so show it now.
335                                 */
336                                 cout << (*i)->status() << "\n";
337                         }
338                 }
339
340                 if (unfinished == 0 || finished_in_error != 0) {
341                         should_stop = true;
342                 }
343         }
344
345         if (keep_going) {
346                 while (true) {
347                         dcpomatic_sleep (3600);
348                 }
349         }
350
351         /* This is just to stop valgrind reporting leaks due to JobManager
352            indirectly holding onto codecs.
353         */
354         JobManager::drop ();
355
356         ServerFinder::drop ();
357
358         return error ? EXIT_FAILURE : EXIT_SUCCESS;
359 }