2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/tools/dcpomatic_kdm_cli.cc
21 * @brief Command-line program to generate KDMs.
25 #include "lib/cinema.h"
26 #include "lib/screen_kdm.h"
27 #include "lib/cinema_kdms.h"
28 #include "lib/config.h"
29 #include "lib/exceptions.h"
30 #include "lib/safe_stringstream.h"
31 #include <dcp/certificate.h>
40 using boost::shared_ptr;
45 cerr << "Syntax: " << program_name << " [OPTION] [<FILM>]\n"
46 " -h, --help show this help\n"
47 " -o, --output output file or directory\n"
48 " -f, --valid-from valid from time (in local time zone) (e.g. \"2013-09-28 01:41:51\") or \"now\"\n"
49 " -t, --valid-to valid to time (in local time zone) (e.g. \"2014-09-28 01:41:51\")\n"
50 " -d, --valid-duration valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")\n"
51 " --formulation modified-transitional-1, dci-any or dci-specific [default modified-transitional-1]\n"
52 " -z, --zip ZIP each cinema's KDMs into its own file\n"
53 " -v, --verbose be verbose\n"
54 " -c, --cinema specify a cinema, either by name or email address\n"
55 " --cinemas list known cinemas from the DCP-o-matic settings\n"
56 " --certificate file containing projector certificate\n\n"
58 "Create KDMs for my_great_movie to play in all of Fred's Cinema's screens for the next two weeks and zip them up.\n"
59 "(Fred's Cinema must have been set up in DCP-o-matic's KDM window)\n\n"
60 "\tdcpomatic_kdm -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie\n\n";
66 cerr << program_name << ": " << m << "\n";
70 static boost::posix_time::ptime
71 time_from_string (string t)
74 return boost::posix_time::second_clock::local_time ();
77 return boost::posix_time::time_from_string (t);
80 static boost::posix_time::time_duration
81 duration_from_string (string d)
83 SafeStringStream s (d);
89 cerr << "Could not understand duration \"" << d << "\"\n";
93 if (unit == "year" || unit == "years") {
94 return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
95 } else if (unit == "week" || unit == "weeks") {
96 return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
97 } else if (unit == "day" || unit == "days") {
98 return boost::posix_time::time_duration (N * 24, 0, 0, 0);
99 } else if (unit == "hour" || unit == "hours") {
100 return boost::posix_time::time_duration (N, 0, 0, 0);
103 cerr << "Could not understand duration \"" << d << "\"\n";
107 int main (int argc, char* argv[])
109 boost::filesystem::path output;
110 boost::optional<boost::posix_time::ptime> valid_from;
111 boost::optional<boost::posix_time::ptime> valid_to;
112 string certificate_file;
115 bool cinemas = false;
116 string duration_string;
117 bool verbose = false;
118 dcp::Formulation formulation = dcp::MODIFIED_TRANSITIONAL_1;
120 program_name = argv[0];
122 int option_index = 0;
124 static struct option long_options[] = {
125 { "help", no_argument, 0, 'h'},
126 { "output", required_argument, 0, 'o'},
127 { "valid-from", required_argument, 0, 'f'},
128 { "valid-to", required_argument, 0, 't'},
129 { "certificate", required_argument, 0, 'A' },
130 { "cinema", required_argument, 0, 'c' },
131 { "cinemas", no_argument, 0, 'B' },
132 { "zip", no_argument, 0, 'z' },
133 { "duration", required_argument, 0, 'd' },
134 { "verbose", no_argument, 0, 'v' },
135 { "formulation", required_argument, 0, 'C' },
139 int c = getopt_long (argc, argv, "ho:f:t:c:A:Bzd:vC:", long_options, &option_index);
153 valid_from = time_from_string (optarg);
156 valid_to = time_from_string (optarg);
159 certificate_file = optarg;
162 cinema_name = optarg;
171 duration_string = optarg;
177 if (string (optarg) == "modified-transitional-1") {
178 formulation = dcp::MODIFIED_TRANSITIONAL_1;
179 } else if (string (optarg) == "dci-any") {
180 formulation = dcp::DCI_ANY;
181 } else if (string (optarg) == "dci-specific") {
182 formulation = dcp::DCI_SPECIFIC;
184 error ("unrecognised KDM formulation " + string (optarg));
190 list<boost::shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
191 for (list<boost::shared_ptr<Cinema> >::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
192 cout << (*i)->name << " (" << (*i)->email << ")\n";
197 if (duration_string.empty() && !valid_to) {
198 error ("you must specify a --valid-duration or --valid-to");
202 error ("you must specify --valid-from");
206 if (optind >= argc) {
211 if (cinema_name.empty() && certificate_file.empty()) {
212 error ("you must specify either a cinema, a screen or a certificate file");
215 if (!duration_string.empty ()) {
216 valid_to = valid_from.get() + duration_from_string (duration_string);
219 string const film_dir = argv[optind];
221 dcpomatic_setup_path_encoding ();
224 shared_ptr<Film> film;
226 film.reset (new Film (film_dir));
227 film->read_metadata ();
229 cout << "Read film " << film->name () << "\n";
231 } catch (std::exception& e) {
232 cerr << program_name << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
237 cout << "Making KDMs valid from " << valid_from.get() << " to " << valid_to.get() << "\n";
240 /* XXX: allow specification of this */
241 vector<CPLSummary> cpls = film->cpls ();
243 error ("no CPLs found in film");
244 } else if (cpls.size() > 1) {
245 error ("more than one CPL found in film");
248 boost::filesystem::path cpl = cpls.front().cpl_file;
250 if (cinema_name.empty ()) {
252 if (output.empty ()) {
253 error ("you must specify --output");
256 dcp::Certificate certificate (dcp::file_to_string (certificate_file));
257 dcp::EncryptedKDM kdm = film->make_kdm (certificate, cpl, valid_from.get(), valid_to.get(), formulation);
260 cout << "Generated KDM " << output << " for certificate.\n";
264 list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
265 list<shared_ptr<Cinema> >::const_iterator i = cinemas.begin();
266 while (i != cinemas.end() && (*i)->name != cinema_name && (*i)->email != cinema_name) {
270 if (i == cinemas.end ()) {
271 cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
275 if (output.empty ()) {
280 list<ScreenKDM> screen_kdms = film->make_kdms (
281 (*i)->screens(), cpl, dcp::LocalTime (valid_from.get()), dcp::LocalTime (valid_to.get()), formulation
285 CinemaKDMs::write_zip_files (film, CinemaKDMs::collect (screen_kdms), output);
288 cout << "Wrote ZIP files to " << output << "\n";
291 ScreenKDM::write_files (film, screen_kdms, output);
294 cout << "Wrote KDM files to " << output << "\n";
297 } catch (FileError& e) {
298 cerr << argv[0] << ": " << e.what() << " (" << e.file().string() << ")\n";
300 } catch (KDMError& e) {
301 cerr << argv[0] << ": " << e.what() << "\n";