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