Merge branch '1.0' of /home/carl/git/dvdomatic into 1.0
[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
36 using std::string;
37 using std::cerr;
38 using std::cout;
39 using std::vector;
40 using std::pair;
41 using std::list;
42 using boost::shared_ptr;
43
44 static void
45 help (string n)
46 {
47         cerr << "Syntax: " << n << " [OPTION] <FILM>\n"
48              << "  -v, --version      show DCP-o-matic version\n"
49              << "  -h, --help         show this help\n"
50              << "  -d, --deps         list DCP-o-matic dependency details and quit\n"
51              << "  -f, --flags        show flags passed to C++ compiler on build\n"
52              << "  -n, --no-progress  do not print progress to stdout\n"
53              << "  -r, --no-remote    do not use any remote servers\n"
54              << "\n"
55              << "<FILM> is the film directory.\n";
56 }
57
58 int
59 main (int argc, char* argv[])
60 {
61         string film_dir;
62         bool progress = true;
63         bool no_remote = false;
64         int log_level = 0;
65
66         int option_index = 0;
67         while (1) {
68                 static struct option long_options[] = {
69                         { "version", no_argument, 0, 'v'},
70                         { "help", no_argument, 0, 'h'},
71                         { "deps", no_argument, 0, 'd'},
72                         { "flags", no_argument, 0, 'f'},
73                         { "no-progress", no_argument, 0, 'n'},
74                         { "no-remote", no_argument, 0, 'r'},
75                         { "log-level", required_argument, 0, 'l' },
76                         { 0, 0, 0, 0 }
77                 };
78
79                 int c = getopt_long (argc, argv, "vhdfnrl:", long_options, &option_index);
80
81                 if (c == -1) {
82                         break;
83                 }
84
85                 switch (c) {
86                 case 'v':
87                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
88                         exit (EXIT_SUCCESS);
89                 case 'h':
90                         help (argv[0]);
91                         exit (EXIT_SUCCESS);
92                 case 'd':
93                         cout << dependency_version_summary () << "\n";
94                         exit (EXIT_SUCCESS);
95                 case 'f':
96                         cout << dcpomatic_cxx_flags << "\n";
97                         exit (EXIT_SUCCESS);
98                 case 'n':
99                         progress = false;
100                         break;
101                 case 'r':
102                         no_remote = true;
103                         break;
104                 case 'l':
105                         log_level = atoi (optarg);
106                         break;
107                 }
108         }
109
110         if (optind >= argc) {
111                 help (argv[0]);
112                 exit (EXIT_FAILURE);
113         }
114
115         film_dir = argv[optind];
116                         
117         dcpomatic_setup ();
118         ui_signaller = new UISignaller ();
119
120         if (no_remote) {
121                 Config::instance()->set_servers (vector<ServerDescription> ());
122         }
123
124         cout << "DCP-o-matic " << dcpomatic_version << " git " << dcpomatic_git_commit;
125         char buf[256];
126         if (gethostname (buf, 256) == 0) {
127                 cout << " on " << buf;
128         }
129         cout << "\n";
130
131         shared_ptr<Film> film;
132         try {
133                 film.reset (new Film (film_dir));
134                 film->read_metadata ();
135         } catch (std::exception& e) {
136                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
137                 exit (EXIT_FAILURE);
138         }
139
140         film->log()->set_level ((Log::Level) log_level);
141
142         cout << "\nMaking DCP for " << film->name() << "\n";
143 //      cout << "Content: " << film->content() << "\n";
144 //      pair<string, string> const f = Filter::ffmpeg_strings (film->filters ());
145 //      cout << "Filters: " << f.first << " " << f.second << "\n";
146
147         film->make_dcp ();
148
149         bool should_stop = false;
150         bool first = true;
151         bool error = false;
152         while (!should_stop) {
153
154                 dcpomatic_sleep (5);
155
156                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
157
158                 if (!first && progress) {
159                         cout << "\033[" << jobs.size() << "A";
160                         cout.flush ();
161                 }
162
163                 first = false;
164
165                 int unfinished = 0;
166                 int finished_in_error = 0;
167
168                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
169                         if (progress) {
170                                 cout << (*i)->name() << ": ";
171                                 
172                                 float const p = (*i)->progress ();
173                                 
174                                 if (p >= 0) {
175                                         cout << (*i)->status() << "                         \n";
176                                 } else {
177                                         cout << ": Running           \n";
178                                 }
179                         }
180
181                         if (!(*i)->finished ()) {
182                                 ++unfinished;
183                         }
184
185                         if ((*i)->finished_in_error ()) {
186                                 ++finished_in_error;
187                                 error = true;
188                         }
189
190                         if (!progress && (*i)->finished_in_error ()) {
191                                 /* We won't see this error if we haven't been showing progress,
192                                    so show it now.
193                                 */
194                                 cout << (*i)->status() << "\n";
195                         }
196                 }
197
198                 if (unfinished == 0 || finished_in_error != 0) {
199                         should_stop = true;
200                 }
201         }
202
203         return error ? EXIT_FAILURE : EXIT_SUCCESS;
204 }
205
206