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