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