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 "ab_transcode_job.h"
29 #include "util.h"
30 #include "scaler.h"
31 #include "version.h"
32 #include "cross.h"
33 #include "config.h"
34 #include "log.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              << "  -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                         { "no-progress", no_argument, 0, 'n'},
72                         { "no-remote", no_argument, 0, 'r'},
73                         { "log-level", required_argument, 0, 'l' },
74                         { 0, 0, 0, 0 }
75                 };
76
77                 int c = getopt_long (argc, argv, "vhdnrl:", long_options, &option_index);
78
79                 if (c == -1) {
80                         break;
81                 }
82
83                 switch (c) {
84                 case 'v':
85                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
86                         exit (EXIT_SUCCESS);
87                 case 'h':
88                         help (argv[0]);
89                         exit (EXIT_SUCCESS);
90                 case 'd':
91                         cout << dependency_version_summary () << "\n";
92                         exit (EXIT_SUCCESS);
93                 case 'n':
94                         progress = false;
95                         break;
96                 case 'r':
97                         no_remote = true;
98                         break;
99                 case 'l':
100                         log_level = atoi (optarg);
101                         break;
102                 }
103         }
104
105         if (optind >= argc) {
106                 help (argv[0]);
107                 exit (EXIT_FAILURE);
108         }
109
110         film_dir = argv[optind];
111                         
112         dcpomatic_setup ();
113
114         if (no_remote) {
115                 Config::instance()->set_servers (vector<ServerDescription*> ());
116         }
117
118         cout << "DCP-o-matic " << dcpomatic_version << " git " << dcpomatic_git_commit;
119         char buf[256];
120         if (gethostname (buf, 256) == 0) {
121                 cout << " on " << buf;
122         }
123         cout << "\n";
124
125         shared_ptr<Film> film;
126         try {
127                 film.reset (new Film (film_dir));
128                 film->read_metadata ();
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