f4296f41c428016f784349a4b1a2c8b286e94ef0
[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
37 using namespace std;
38 using namespace boost;
39
40 static void
41 help (string n)
42 {
43         cerr << "Syntax: " << n << " [OPTION] <FILM>\n"
44              << "  -h, --help         show this help\n"
45              << "  -d, --deps         list DVD-o-matic dependency details and quit\n"
46              << "  -t, --test         run in test mode (repeatable UUID generation, timestamps etc.)\n"
47              << "  -n, --no-progress  do not print progress to stdout\n"
48              << "\n"
49              << "<FILM> is the film directory.\n";
50 }
51
52 int
53 main (int argc, char* argv[])
54 {
55         string film_dir;
56         bool test_mode = false;
57         bool progress = true;
58
59         int option_index = 0;
60         while (1) {
61                 static struct option long_options[] = {
62                         { "help", no_argument, 0, 'h'},
63                         { "deps", no_argument, 0, 'd'},
64                         { "test", no_argument, 0, 't'},
65                         { "no-progress", no_argument, 0, 'n'},
66                         { 0, 0, 0, 0 }
67                 };
68
69                 int c = getopt_long (argc, argv, "hdtn", long_options, &option_index);
70
71                 if (c == -1) {
72                         break;
73                 }
74
75                 switch (c) {
76                 case 'h':
77                         help (argv[0]);
78                         exit (EXIT_SUCCESS);
79                 case 'd':
80                         cout << dependency_version_summary () << "\n";
81                         exit (EXIT_SUCCESS);
82                 case 't':
83                         test_mode = true;
84                         break;
85                 case 'n':
86                         progress = false;
87                         break;
88                 }
89         }
90
91         if (optind >= argc) {
92                 help (argv[0]);
93                 exit (EXIT_FAILURE);
94         }
95
96         film_dir = argv[optind];
97                         
98         dvdomatic_setup ();
99
100         cout << "DVD-o-matic " << dvdomatic_version << " git " << dvdomatic_git_commit;
101         char buf[256];
102         if (gethostname (buf, 256) == 0) {
103                 cout << " on " << buf;
104         }
105         cout << "\n";
106
107         if (test_mode) {
108                 libdcp::enable_test_mode ();
109                 cout << dependency_version_summary() << "\n";
110         }
111
112         Film* film = 0;
113         try {
114                 film = new Film (film_dir, true);
115         } catch (std::exception& e) {
116                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
117                 exit (EXIT_FAILURE);
118         }
119
120         cout << "\nMaking ";
121         if (film->dcp_ab ()) {
122                 cout << "A/B ";
123         }
124         cout << "DCP for " << film->name() << "\n";
125         cout << "Test mode: " << (test_mode ? "yes" : "no") << "\n";
126         cout << "Content: " << film->content() << "\n";
127         pair<string, string> const f = Filter::ffmpeg_strings (film->filters ());
128         cout << "Filters: " << f.first << " " << f.second << "\n";
129
130         film->make_dcp (true);
131
132         list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
133
134         bool all_done = false;
135         bool first = true;
136         while (!all_done) {
137
138                 dvdomatic_sleep (5);
139
140                 if (!first && progress) {
141                         cout << "\033[" << jobs.size() << "A";
142                         cout.flush ();
143                 }
144
145                 first = false;
146                 
147                 all_done = true;
148                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
149                         if (progress) {
150                                 cout << (*i)->name() << ": ";
151                                 
152                                 float const p = (*i)->overall_progress ();
153                                 
154                                 if (p >= 0) {
155                                         cout << (*i)->status() << "                         \n";
156                                 } else {
157                                         cout << ": Running           \n";
158                                 }
159                         }
160                         
161                         if (!(*i)->finished ()) {
162                                 all_done = false;
163                         }
164
165                         if (!progress && (*i)->finished_in_error ()) {
166                                 /* We won't see this error if we haven't been showing progress,
167                                    so show it now.
168                                 */
169                                 cout << (*i)->status() << "\n";
170                         }
171                 }
172         }
173
174         return 0;
175 }
176
177