Don't overlap simultaneous video content in the timeline. Fix keep-aligned for separ...
[dcpomatic.git] / src / tools / dcpomatic_kdm.cc
1 /*
2     Copyright (C) 2013 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 <getopt.h>
21 #include <libdcp/certificates.h>
22 #include "lib/film.h"
23 #include "lib/cinema.h"
24 #include "lib/kdm.h"
25 #include "lib/config.h"
26 #include "lib/exceptions.h"
27
28 using std::string;
29 using std::stringstream;
30 using std::cout;
31 using std::cerr;
32 using std::list;
33 using boost::shared_ptr;
34
35 static string program_name;
36
37 static void
38 help ()
39 {
40         cerr << "Syntax: " << program_name << " [OPTION] [<FILM>]\n"
41                 "  -h, --help             show this help\n"
42                 "  -o, --output           output file or directory\n"
43                 "  -f, --valid-from       valid from time (e.g. \"2013-09-28 01:41:51\") or \"now\"\n"
44                 "  -t, --valid-to         valid to time (e.g. \"2014-09-28 01:41:51\")\n"
45                 "  -d, --valid-duration   valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")\n"
46                 "  -z, --zip              ZIP each cinema's KDMs into its own file\n"
47                 "  -v, --verbose          be verbose\n"
48                 "  -c, --cinema           specify a cinema, either by name or email address\n"
49                 "      --cinemas          list known cinemas from the DCP-o-matic settings\n"
50                 "      --certificate file containing projector certificate\n\n"
51                 "For example:\n\n"
52                 "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"
53                 "(Fred's Cinema must have been set up in DCP-o-matic's KDM window)\n\n"
54                 "\tdcpomatic_kdm -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie\n\n";
55 }
56
57 static void
58 error (string m)
59 {
60         cerr << program_name << ": " << m << "\n";
61         exit (EXIT_FAILURE);
62 }
63
64 static boost::posix_time::ptime
65 time_from_string (string t)
66 {
67         if (t == "now") {
68                 return boost::posix_time::second_clock::local_time ();
69         }
70
71         return boost::posix_time::time_from_string (t);
72 }
73
74 static boost::posix_time::time_duration
75 duration_from_string (string d)
76 {
77         stringstream s (d);
78         int N;
79         string unit;
80         s >> N >> unit;
81
82         if (N == 0) {
83                 cerr << "Could not understand duration \"" << d << "\"\n";
84                 exit (EXIT_FAILURE);
85         }
86
87         if (unit == "year" || unit == "years") {
88                 return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
89         } else if (unit == "week" || unit == "weeks") {
90                 return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
91         } else if (unit == "day" || unit == "days") {
92                 return boost::posix_time::time_duration (N * 24, 0, 0, 0);
93         } else if (unit == "hour" || unit == "hours") {
94                 return boost::posix_time::time_duration (N, 0, 0, 0);
95         }
96
97         cerr << "Could not understand duration \"" << d << "\"\n";
98         exit (EXIT_FAILURE);
99 }
100
101 int main (int argc, char* argv[])
102 {
103         boost::filesystem::path output;
104         boost::optional<boost::posix_time::ptime> valid_from;
105         boost::optional<boost::posix_time::ptime> valid_to;
106         string certificate_file;
107         bool zip = false;
108         string cinema_name;
109         bool cinemas = false;
110         string duration_string;
111         bool verbose = false;
112
113         program_name = argv[0];
114         
115         int option_index = 0;
116         while (1) {
117                 static struct option long_options[] = {
118                         { "help", no_argument, 0, 'h'},
119                         { "output", required_argument, 0, 'o'},
120                         { "valid-from", required_argument, 0, 'f'},
121                         { "valid-to", required_argument, 0, 't'},
122                         { "certificate", required_argument, 0, 'A' },
123                         { "cinema", required_argument, 0, 'c' },
124                         { "cinemas", no_argument, 0, 'B' },
125                         { "zip", no_argument, 0, 'z' },
126                         { "duration", required_argument, 0, 'd' },
127                         { "verbose", no_argument, 0, 'v' },
128                         { 0, 0, 0, 0 }
129                 };
130
131                 int c = getopt_long (argc, argv, "ho:f:t:c:A:Bzd:v", long_options, &option_index);
132
133                 if (c == -1) {
134                         break;
135                 }
136
137                 switch (c) {
138                 case 'h':
139                         help ();
140                         exit (EXIT_SUCCESS);
141                 case 'o':
142                         output = optarg;
143                         break;
144                 case 'f':
145                         valid_from = time_from_string (optarg);
146                         break;
147                 case 't':
148                         valid_to = time_from_string (optarg);
149                         break;
150                 case 'A':
151                         certificate_file = optarg;
152                         break;
153                 case 'c':
154                         cinema_name = optarg;
155                         break;
156                 case 'B':
157                         cinemas = true;
158                         break;
159                 case 'z':
160                         zip = true;
161                         break;
162                 case 'd':
163                         duration_string = optarg;
164                         break;
165                 case 'v':
166                         verbose = true;
167                         break;
168                 }
169         }
170
171         if (cinemas) {
172                 list<boost::shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
173                 for (list<boost::shared_ptr<Cinema> >::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
174                         cout << (*i)->name << " (" << (*i)->email << ")\n";
175                 }
176                 exit (EXIT_SUCCESS);
177         }
178
179         if (duration_string.empty() && !valid_to) {
180                 error ("you must specify a --valid-duration or --valid-to");
181         }
182
183         if (!valid_from) {
184                 error ("you must specify --valid-from");
185                 exit (EXIT_FAILURE);
186         }
187
188         if (optind >= argc) {
189                 help ();
190                 exit (EXIT_FAILURE);
191         }
192
193         if (cinema_name.empty() && certificate_file.empty()) {
194                 error ("you must specify either a cinema, a screen or a certificate file");
195         }
196
197         if (!duration_string.empty ()) {
198                 valid_to = valid_from.get() + duration_from_string (duration_string);
199         }
200
201         string const film_dir = argv[optind];
202                         
203         dcpomatic_setup ();
204
205         shared_ptr<Film> film;
206         try {
207                 film.reset (new Film (film_dir));
208                 film->read_metadata ();
209                 if (verbose) {
210                         cout << "Read film " << film->name () << "\n";
211                 }
212         } catch (std::exception& e) {
213                 cerr << program_name << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
214                 exit (EXIT_FAILURE);
215         }
216
217         if (verbose) {
218                 cout << "Making KDMs valid from " << valid_from.get() << " to " << valid_to.get() << "\n";
219         }
220
221         /* XXX: allow specification of this */
222         list<boost::filesystem::path> dcps = film->dcps ();
223         if (dcps.empty ()) {
224                 error ("no DCPs found in film");
225         } else if (dcps.size() > 1) {
226                 error ("more than one DCP found in film");
227         }
228
229         boost::filesystem::path dcp = dcps.front ();
230
231         if (cinema_name.empty ()) {
232
233                 if (output.empty ()) {
234                         error ("you must specify --output");
235                 }
236                 
237                 shared_ptr<libdcp::Certificate> certificate (new libdcp::Certificate (boost::filesystem::path (certificate_file)));
238                 libdcp::KDM kdm = film->make_kdm (certificate, dcp, valid_from.get(), valid_to.get());
239                 kdm.as_xml (output);
240                 if (verbose) {
241                         cout << "Generated KDM " << output << " for certificate.\n";
242                 }
243         } else {
244
245                 list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
246                 list<shared_ptr<Cinema> >::const_iterator i = cinemas.begin();
247                 while (i != cinemas.end() && (*i)->name != cinema_name && (*i)->email != cinema_name) {
248                         ++i;
249                 }
250
251                 if (i == cinemas.end ()) {
252                         cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
253                         exit (EXIT_FAILURE);
254                 }
255
256                 if (output.empty ()) {
257                         output = ".";
258                 }
259
260                 try {
261                         if (zip) {
262                                 write_kdm_zip_files (film, (*i)->screens(), dcp, valid_from.get(), valid_to.get(), output);
263                                 if (verbose) {
264                                         cout << "Wrote ZIP files to " << output << "\n";
265                                 }
266                         } else {
267                                 write_kdm_files (film, (*i)->screens(), dcp, valid_from.get(), valid_to.get(), output);
268                                 if (verbose) {
269                                         cout << "Wrote KDM files to " << output << "\n";
270                                 }
271                         }
272                 } catch (FileError& e) {
273                         cerr << argv[0] << ": " << e.what() << " (" << e.file().string() << ")\n";
274                         exit (EXIT_FAILURE);
275                 } catch (KDMError& e) {
276                         cerr << argv[0] << ": " << e.what() << "\n";
277                         exit (EXIT_FAILURE);
278                 }
279         }
280
281         return 0;
282 }