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