Rename video/audio/subtitle part methods.
[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/encode_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              << "  -d, --dcp-path     echo DCP's path to stdout on successful completion (implies -n)\n"
65              << "      --dump         just dump a summary of the film's settings; don't encode\n"
66              << "\n"
67              << "<FILM> is the film directory.\n";
68 }
69
70 static void
71 print_dump (shared_ptr<Film> film)
72 {
73         cout << film->dcp_name (true) << "\n"
74              << film->container()->nickname() << " at " << ((film->resolution() == RESOLUTION_2K) ? "2K" : "4K") << "\n"
75              << (film->j2k_bandwidth() / 1000000) << "Mbit/s" << "\n"
76              << "Output " << film->video_frame_rate() << "fps " << (film->three_d() ? "3D" : "2D") << " " << (film->audio_frame_rate() / 1000) << "kHz\n"
77              << (film->interop() ? "Inter-Op" : "SMPTE") << " " << (film->encrypted() ? "encrypted" : "unencrypted") << "\n";
78
79         BOOST_FOREACH (shared_ptr<Content> c, film->content ()) {
80                 cout << "\n"
81                      << c->path(0) << "\n"
82                      << "\tat " << c->position().seconds ()
83                      << " length " << c->full_length().seconds ()
84                      << " start trim " << c->trim_start().seconds ()
85                      << " end trim " << c->trim_end().seconds () << "\n";
86
87                 if (c->video) {
88                         cout << "\t" << c->video->size().width << "x" << c->video->size().height << "\n"
89                              << "\t" << c->video->frame_rate() << "fps\n"
90                              << "\tcrop left " << c->video->left_crop()
91                              << " right " << c->video->right_crop()
92                              << " top " << c->video->top_crop()
93                              << " bottom " << c->video->bottom_crop() << "\n"
94                              << "\tscale " << c->video->scale().name() << "\n";
95                         if (c->video->colour_conversion()) {
96                                 if (c->video->colour_conversion().get().preset()) {
97                                         cout << "\tcolour conversion "
98                                              << PresetColourConversion::all()[c->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                 if (c->audio) {
110                         cout << "\t" << c->audio->delay() << " delay\n"
111                              << "\t" << c->audio->gain() << " gain\n";
112                 }
113         }
114 }
115
116 static void
117 show_servers ()
118 {
119         while (true) {
120                 int N = 0;
121                 list<EncodeServerDescription> servers = EncodeServerFinder::instance()->servers ();
122
123                 if (Config::instance()->use_any_servers ()) {
124                         if (servers.empty ()) {
125                                 cout << "No encoding servers found.\n";
126                                 ++N;
127                         } else {
128                                 cout << std::left << setw(24) << "Host" << " Threads\n";
129                                 ++N;
130                                 BOOST_FOREACH (EncodeServerDescription const & i, servers) {
131                                         cout << std::left << setw(24) << i.host_name() << " " << i.threads() << "\n";
132                                         ++N;
133                                 }
134                         }
135                 } else {
136                         vector<string> configured_servers = Config::instance()->servers();
137                         if (configured_servers.empty()) {
138                                 cout << "No configured servers, and DCP-o-matic is not set to search for any server.\n";
139                                 ++N;
140                         } else {
141                                 cout << std::left << setw(24) << "Host" << " Status Threads\n";
142                                 ++N;
143                                 BOOST_FOREACH (string const & i, Config::instance()->servers()) {
144                                         cout << std::left << setw(24) << i << " ";
145                                         optional<int> threads;
146                                         BOOST_FOREACH (EncodeServerDescription const & j, servers) {
147                                                 if (i == j.host_name()) {
148                                                         threads = j.threads();
149                                                 }
150                                         }
151                                         if (static_cast<bool>(threads)) {
152                                                 cout << "UP     " << threads.get() << "\n";
153                                         } else {
154                                                 cout << "DOWN\n";
155                                         }
156                                         ++N;
157                                 }
158                         }
159                 }
160
161
162                 dcpomatic_sleep (1);
163
164                 for (int i = 0; i < N; ++i) {
165                         cout << "\033[1A\033[2K";
166                 }
167         }
168 }
169
170
171 int
172 main (int argc, char* argv[])
173 {
174         string film_dir;
175         bool progress = true;
176         bool no_remote = false;
177         optional<int> json_port;
178         bool keep_going = false;
179         bool dump = false;
180         bool servers = false;
181         bool dcp_path = 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                         { "dcp-path", no_argument, 0, 'd' },
195                         /* Just using A, B, C ... from here on */
196                         { "dump", no_argument, 0, 'A' },
197                         { 0, 0, 0, 0 }
198                 };
199
200                 int c = getopt_long (argc, argv, "vhfnrj:kAsd", long_options, &option_index);
201
202                 if (c == -1) {
203                         break;
204                 }
205
206                 switch (c) {
207                 case 'v':
208                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
209                         exit (EXIT_SUCCESS);
210                 case 'h':
211                         help (argv[0]);
212                         exit (EXIT_SUCCESS);
213                 case 'f':
214                         cout << dcpomatic_cxx_flags << "\n";
215                         exit (EXIT_SUCCESS);
216                 case 'n':
217                         progress = false;
218                         break;
219                 case 'r':
220                         no_remote = true;
221                         break;
222                 case 'j':
223                         json_port = atoi (optarg);
224                         break;
225                 case 'k':
226                         keep_going = true;
227                         break;
228                 case 'A':
229                         dump = true;
230                         break;
231                 case 's':
232                         servers = true;
233                         break;
234                 case 'd':
235                         dcp_path = true;
236                         progress = false;
237                         break;
238                 }
239         }
240
241         if (servers) {
242                 show_servers ();
243                 exit (EXIT_SUCCESS);
244         }
245
246         if (optind >= argc) {
247                 help (argv[0]);
248                 exit (EXIT_FAILURE);
249         }
250
251         film_dir = argv[optind];
252
253         dcpomatic_setup_path_encoding ();
254         dcpomatic_setup ();
255         signal_manager = new SignalManager ();
256
257         if (no_remote) {
258                 EncodeServerFinder::instance()->disable ();
259         }
260
261         if (json_port) {
262                 new JSONServer (json_port.get ());
263         }
264
265         shared_ptr<Film> film;
266         try {
267                 film.reset (new Film (film_dir));
268                 film->read_metadata ();
269         } catch (std::exception& e) {
270                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
271                 exit (EXIT_FAILURE);
272         }
273
274         if (dump) {
275                 print_dump (film);
276                 exit (EXIT_SUCCESS);
277         }
278
279         ContentList content = film->content ();
280         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
281                 vector<boost::filesystem::path> paths = (*i)->paths ();
282                 for (vector<boost::filesystem::path>::const_iterator j = paths.begin(); j != paths.end(); ++j) {
283                         if (!boost::filesystem::exists (*j)) {
284                                 cerr << argv[0] << ": content file " << *j << " not found.\n";
285                                 exit (EXIT_FAILURE);
286                         }
287                 }
288         }
289
290         if (progress) {
291                 cout << "\nMaking DCP for " << film->name() << "\n";
292         }
293
294         film->make_dcp ();
295
296         bool should_stop = false;
297         bool first = true;
298         bool error = false;
299         while (!should_stop) {
300
301                 dcpomatic_sleep (5);
302
303                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
304
305                 if (!first && progress) {
306                         for (size_t i = 0; i < jobs.size(); ++i) {
307                                 cout << "\033[1A\033[2K";
308                         }
309                         cout.flush ();
310                 }
311
312                 first = false;
313
314                 int unfinished = 0;
315                 int finished_in_error = 0;
316
317                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
318                         if (progress) {
319                                 cout << (*i)->name() << ": ";
320
321                                 if ((*i)->progress ()) {
322                                         cout << (*i)->status() << "                         \n";
323                                 } else {
324                                         cout << ": Running           \n";
325                                 }
326                         }
327
328                         if (!(*i)->finished ()) {
329                                 ++unfinished;
330                         }
331
332                         if ((*i)->finished_in_error ()) {
333                                 ++finished_in_error;
334                                 error = true;
335                         }
336
337                         if (!progress && (*i)->finished_in_error ()) {
338                                 /* We won't see this error if we haven't been showing progress,
339                                    so show it now.
340                                 */
341                                 cout << (*i)->status() << "\n";
342                         }
343                 }
344
345                 if (unfinished == 0 || finished_in_error != 0) {
346                         should_stop = true;
347                 }
348         }
349
350         if (keep_going) {
351                 while (true) {
352                         dcpomatic_sleep (3600);
353                 }
354         }
355
356         /* This is just to stop valgrind reporting leaks due to JobManager
357            indirectly holding onto codecs.
358         */
359         JobManager::drop ();
360
361         EncodeServerFinder::drop ();
362
363         if (dcp_path && !error) {
364                 cout << film->dir (film->dcp_name (false)).string() << "\n";
365         }
366
367         return error ? EXIT_FAILURE : EXIT_SUCCESS;
368 }