Remove Scaler config and use SWS_BICUBIC everywhere.
[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 <iostream>
21 #include <iomanip>
22 #include <getopt.h>
23 #include <dcp/version.h>
24 #include "lib/film.h"
25 #include "lib/filter.h"
26 #include "lib/transcode_job.h"
27 #include "lib/job_manager.h"
28 #include "lib/util.h"
29 #include "lib/version.h"
30 #include "lib/cross.h"
31 #include "lib/config.h"
32 #include "lib/log.h"
33 #include "lib/ui_signaller.h"
34 #include "lib/server_finder.h"
35 #include "lib/json_server.h"
36
37 using std::string;
38 using std::cerr;
39 using std::cout;
40 using std::vector;
41 using std::pair;
42 using std::list;
43 using boost::shared_ptr;
44 using boost::optional;
45
46 static void
47 help (string n)
48 {
49         cerr << "Syntax: " << n << " [OPTION] <FILM>\n"
50              << "  -v, --version      show DCP-o-matic version\n"
51              << "  -h, --help         show this help\n"
52              << "  -f, --flags        show flags passed to C++ compiler on build\n"
53              << "  -n, --no-progress  do not print progress to stdout\n"
54              << "  -r, --no-remote    do not use any remote servers\n"
55              << "  -j, --json <port>  run a JSON server on the specified port\n"
56              << "  -k, --keep-going   keep running even when the job is complete\n"
57              << "\n"
58              << "<FILM> is the film directory.\n";
59 }
60
61 int
62 main (int argc, char* argv[])
63 {
64         string film_dir;
65         bool progress = true;
66         bool no_remote = false;
67         optional<int> json_port;
68         bool keep_going = false;
69
70         int option_index = 0;
71         while (true) {
72                 static struct option long_options[] = {
73                         { "version", no_argument, 0, 'v'},
74                         { "help", no_argument, 0, 'h'},
75                         { "flags", no_argument, 0, 'f'},
76                         { "no-progress", no_argument, 0, 'n'},
77                         { "no-remote", no_argument, 0, 'r'},
78                         { "json", required_argument, 0, 'j'},
79                         { "keep-going", no_argument, 0, 'k' },
80                         { 0, 0, 0, 0 }
81                 };
82
83                 int c = getopt_long (argc, argv, "vhfnrj:k", long_options, &option_index);
84
85                 if (c == -1) {
86                         break;
87                 }
88
89                 switch (c) {
90                 case 'v':
91                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
92                         exit (EXIT_SUCCESS);
93                 case 'h':
94                         help (argv[0]);
95                         exit (EXIT_SUCCESS);
96                 case 'f':
97                         cout << dcpomatic_cxx_flags << "\n";
98                         exit (EXIT_SUCCESS);
99                 case 'n':
100                         progress = false;
101                         break;
102                 case 'r':
103                         no_remote = true;
104                         break;
105                 case 'j':
106                         json_port = atoi (optarg);
107                         break;
108                 case 'k':
109                         keep_going = true;
110                         break;
111                 }
112         }
113
114         if (optind >= argc) {
115                 help (argv[0]);
116                 exit (EXIT_FAILURE);
117         }
118
119         film_dir = argv[optind];
120                         
121         dcpomatic_setup ();
122         ui_signaller = new UISignaller ();
123         
124         if (no_remote) {
125                 ServerFinder::instance()->disable ();
126         }
127
128         if (json_port) {
129                 new JSONServer (json_port.get ());
130         }
131
132         cout << "DCP-o-matic " << dcpomatic_version << " git " << dcpomatic_git_commit;
133         char buf[256];
134         if (gethostname (buf, 256) == 0) {
135                 cout << " on " << buf;
136         }
137         cout << "\n";
138
139         shared_ptr<Film> film;
140         try {
141                 film.reset (new Film (film_dir));
142                 film->read_metadata ();
143         } catch (std::exception& e) {
144                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
145                 exit (EXIT_FAILURE);
146         }
147
148         ContentList content = film->content ();
149         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
150                 vector<boost::filesystem::path> paths = (*i)->paths ();
151                 for (vector<boost::filesystem::path>::const_iterator j = paths.begin(); j != paths.end(); ++j) {
152                         if (!boost::filesystem::exists (*j)) {
153                                 cerr << argv[0] << ": content file " << *j << " not found.\n";
154                                 exit (EXIT_FAILURE);
155                         }
156                 }
157         }
158                 
159         cout << "\nMaking DCP for " << film->name() << "\n";
160
161         film->make_dcp ();
162
163         bool should_stop = false;
164         bool first = true;
165         bool error = false;
166         while (!should_stop) {
167
168                 dcpomatic_sleep (5);
169
170                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
171
172                 if (!first && progress) {
173                         cout << "\033[" << jobs.size() << "A";
174                         cout.flush ();
175                 }
176
177                 first = false;
178
179                 int unfinished = 0;
180                 int finished_in_error = 0;
181
182                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
183                         if (progress) {
184                                 cout << (*i)->name() << ": ";
185                                 
186                                 if ((*i)->progress ()) {
187                                         cout << (*i)->status() << "                         \n";
188                                 } else {
189                                         cout << ": Running           \n";
190                                 }
191                         }
192
193                         if (!(*i)->finished ()) {
194                                 ++unfinished;
195                         }
196
197                         if ((*i)->finished_in_error ()) {
198                                 ++finished_in_error;
199                                 error = true;
200                         }
201
202                         if (!progress && (*i)->finished_in_error ()) {
203                                 /* We won't see this error if we haven't been showing progress,
204                                    so show it now.
205                                 */
206                                 cout << (*i)->status() << "\n";
207                         }
208                 }
209
210                 if (unfinished == 0 || finished_in_error != 0) {
211                         should_stop = true;
212                 }
213         }
214
215         if (keep_going) {
216                 while (true) {
217                         dcpomatic_sleep (3600);
218                 }
219         }
220
221         /* This is just to stop valgrind reporting leaks due to JobManager
222            indirectly holding onto codecs.
223         */
224         JobManager::drop ();
225
226         ServerFinder::drop ();
227         
228         return error ? EXIT_FAILURE : EXIT_SUCCESS;
229 }
230
231