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