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 "format.h"
25 #include "film.h"
26 #include "filter.h"
27 #include "transcode_job.h"
28 #include "job_manager.h"
29 #include "ab_transcode_job.h"
30 #include "util.h"
31 #include "scaler.h"
32 #include "version.h"
33 #include "cross.h"
34 #include "config.h"
35 #include "log.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
45 static void
46 help (string n)
47 {
48         cerr << "Syntax: " << n << " [OPTION] <FILM>\n"
49              << "  -v, --version      show DCP-o-matic version\n"
50              << "  -h, --help         show this help\n"
51              << "  -d, --deps         list DCP-o-matic dependency details and quit\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                         { "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, "vhdnrl:", 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 'n':
95                         progress = false;
96                         break;
97                 case 'r':
98                         no_remote = true;
99                         break;
100                 case 'l':
101                         log_level = atoi (optarg);
102                         break;
103                 }
104         }
105
106         if (optind >= argc) {
107                 help (argv[0]);
108                 exit (EXIT_FAILURE);
109         }
110
111         film_dir = argv[optind];
112                         
113         dcpomatic_setup ();
114
115         if (no_remote) {
116                 Config::instance()->set_servers (vector<ServerDescription*> ());
117         }
118
119         cout << "DCP-o-matic " << dcpomatic_version << " git " << dcpomatic_git_commit;
120         char buf[256];
121         if (gethostname (buf, 256) == 0) {
122                 cout << " on " << buf;
123         }
124         cout << "\n";
125
126         shared_ptr<Film> film;
127         try {
128                 film.reset (new Film (film_dir, true));
129         } catch (std::exception& e) {
130                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
131                 exit (EXIT_FAILURE);
132         }
133
134         film->log()->set_level ((Log::Level) log_level);
135
136         cout << "\nMaking ";
137         if (film->ab()) {
138                 cout << "A/B ";
139         }
140         cout << "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