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