No-op: remove all trailing whitespace.
[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/audio_content.h"
34 #include <dcp/version.h>
35 #include <boost/foreach.hpp>
36 #include <getopt.h>
37 #include <iostream>
38 #include <iomanip>
39
40 using std::string;
41 using std::cerr;
42 using std::cout;
43 using std::vector;
44 using std::pair;
45 using std::list;
46 using boost::shared_ptr;
47 using boost::optional;
48 using boost::dynamic_pointer_cast;
49
50 static void
51 help (string n)
52 {
53         cerr << "Syntax: " << n << " [OPTION] <FILM>\n"
54              << "  -v, --version      show DCP-o-matic version\n"
55              << "  -h, --help         show this help\n"
56              << "  -f, --flags        show flags passed to C++ compiler on build\n"
57              << "  -n, --no-progress  do not print progress to stdout\n"
58              << "  -r, --no-remote    do not use any remote servers\n"
59              << "  -j, --json <port>  run a JSON server on the specified port\n"
60              << "  -k, --keep-going   keep running even when the job is complete\n"
61              << "      --dump         just dump a summary of the film's settings; don't encode\n"
62              << "\n"
63              << "<FILM> is the film directory.\n";
64 }
65
66 static void
67 print_dump (shared_ptr<Film> film)
68 {
69         cout << film->dcp_name (true) << "\n"
70              << film->container()->nickname() << " at " << ((film->resolution() == RESOLUTION_2K) ? "2K" : "4K") << "\n"
71              << (film->j2k_bandwidth() / 1000000) << "Mbit/s" << "\n"
72              << "Output " << film->video_frame_rate() << "fps " << (film->three_d() ? "3D" : "2D") << " " << (film->audio_frame_rate() / 1000) << "kHz\n"
73              << (film->interop() ? "Inter-Op" : "SMPTE") << " " << (film->encrypted() ? "encrypted" : "unencrypted") << "\n";
74
75         BOOST_FOREACH (shared_ptr<Content> c, film->content ()) {
76                 cout << "\n"
77                      << c->path(0) << "\n"
78                      << "\tat " << c->position().seconds ()
79                      << " length " << c->full_length().seconds ()
80                      << " start trim " << c->trim_start().seconds ()
81                      << " end trim " << c->trim_end().seconds () << "\n";
82
83                 shared_ptr<VideoContent> video = dynamic_pointer_cast<VideoContent> (c);
84                 if (video) {
85                         cout << "\t" << video->video_size().width << "x" << video->video_size().height << "\n"
86                              << "\t" << video->video_frame_rate() << "fps\n"
87                              << "\tcrop left " << video->left_crop()
88                              << " right " << video->right_crop()
89                              << " top " << video->top_crop()
90                              << " bottom " << video->bottom_crop() << "\n"
91                              << "\tscale " << video->scale().name() << "\n";
92                         if (video->colour_conversion()) {
93                                 if (video->colour_conversion().get().preset()) {
94                                         cout << "\tcolour conversion "
95                                              << PresetColourConversion::all()[video->colour_conversion().get().preset().get()].name
96                                              << "\n";
97                                 } else {
98                                         cout << "\tcustom colour conversion\n";
99                                 }
100                         } else {
101                                 cout << "\tno colour conversion\n";
102                         }
103
104                 }
105
106                 shared_ptr<AudioContent> audio = dynamic_pointer_cast<AudioContent> (c);
107                 if (audio) {
108                         cout << "\t" << audio->audio_delay() << " delay\n"
109                              << "\t" << audio->audio_gain() << " gain\n";
110                 }
111         }
112 }
113
114 int
115 main (int argc, char* argv[])
116 {
117         string film_dir;
118         bool progress = true;
119         bool no_remote = false;
120         optional<int> json_port;
121         bool keep_going = false;
122         bool dump = false;
123
124         int option_index = 0;
125         while (true) {
126                 static struct option long_options[] = {
127                         { "version", no_argument, 0, 'v'},
128                         { "help", no_argument, 0, 'h'},
129                         { "flags", no_argument, 0, 'f'},
130                         { "no-progress", no_argument, 0, 'n'},
131                         { "no-remote", no_argument, 0, 'r'},
132                         { "json", required_argument, 0, 'j'},
133                         { "keep-going", no_argument, 0, 'k' },
134                         /* Just using A, B, C ... from here on */
135                         { "dump", no_argument, 0, 'A' },
136                         { 0, 0, 0, 0 }
137                 };
138
139                 int c = getopt_long (argc, argv, "vhfnrj:kA", long_options, &option_index);
140
141                 if (c == -1) {
142                         break;
143                 }
144
145                 switch (c) {
146                 case 'v':
147                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
148                         exit (EXIT_SUCCESS);
149                 case 'h':
150                         help (argv[0]);
151                         exit (EXIT_SUCCESS);
152                 case 'f':
153                         cout << dcpomatic_cxx_flags << "\n";
154                         exit (EXIT_SUCCESS);
155                 case 'n':
156                         progress = false;
157                         break;
158                 case 'r':
159                         no_remote = true;
160                         break;
161                 case 'j':
162                         json_port = atoi (optarg);
163                         break;
164                 case 'k':
165                         keep_going = true;
166                         break;
167                 case 'A':
168                         dump = true;
169                         break;
170                 }
171         }
172
173         if (optind >= argc) {
174                 help (argv[0]);
175                 exit (EXIT_FAILURE);
176         }
177
178         film_dir = argv[optind];
179
180         dcpomatic_setup ();
181         signal_manager = new SignalManager ();
182
183         if (no_remote) {
184                 ServerFinder::instance()->disable ();
185         }
186
187         if (json_port) {
188                 new JSONServer (json_port.get ());
189         }
190
191         shared_ptr<Film> film;
192         try {
193                 film.reset (new Film (film_dir));
194                 film->read_metadata ();
195         } catch (std::exception& e) {
196                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
197                 exit (EXIT_FAILURE);
198         }
199
200         if (dump) {
201                 print_dump (film);
202                 exit (EXIT_SUCCESS);
203         }
204
205         ContentList content = film->content ();
206         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
207                 vector<boost::filesystem::path> paths = (*i)->paths ();
208                 for (vector<boost::filesystem::path>::const_iterator j = paths.begin(); j != paths.end(); ++j) {
209                         if (!boost::filesystem::exists (*j)) {
210                                 cerr << argv[0] << ": content file " << *j << " not found.\n";
211                                 exit (EXIT_FAILURE);
212                         }
213                 }
214         }
215
216         if (progress) {
217                 cout << "\nMaking DCP for " << film->name() << "\n";
218         }
219
220         film->make_dcp ();
221
222         bool should_stop = false;
223         bool first = true;
224         bool error = false;
225         while (!should_stop) {
226
227                 dcpomatic_sleep (5);
228
229                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
230
231                 if (!first && progress) {
232                         cout << "\033[" << jobs.size() << "A";
233                         cout.flush ();
234                 }
235
236                 first = false;
237
238                 int unfinished = 0;
239                 int finished_in_error = 0;
240
241                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
242                         if (progress) {
243                                 cout << (*i)->name() << ": ";
244
245                                 if ((*i)->progress ()) {
246                                         cout << (*i)->status() << "                         \n";
247                                 } else {
248                                         cout << ": Running           \n";
249                                 }
250                         }
251
252                         if (!(*i)->finished ()) {
253                                 ++unfinished;
254                         }
255
256                         if ((*i)->finished_in_error ()) {
257                                 ++finished_in_error;
258                                 error = true;
259                         }
260
261                         if (!progress && (*i)->finished_in_error ()) {
262                                 /* We won't see this error if we haven't been showing progress,
263                                    so show it now.
264                                 */
265                                 cout << (*i)->status() << "\n";
266                         }
267                 }
268
269                 if (unfinished == 0 || finished_in_error != 0) {
270                         should_stop = true;
271                 }
272         }
273
274         if (keep_going) {
275                 while (true) {
276                         dcpomatic_sleep (3600);
277                 }
278         }
279
280         /* This is just to stop valgrind reporting leaks due to JobManager
281            indirectly holding onto codecs.
282         */
283         JobManager::drop ();
284
285         ServerFinder::drop ();
286
287         return error ? EXIT_FAILURE : EXIT_SUCCESS;
288 }
289
290