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