Set up for the correct handling of UTF-8 with Windows
[dcpomatic.git] / src / tools / dcpomatic_create.cc
1 /*
2     Copyright (C) 2013-2015 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 "lib/version.h"
21 #include "lib/film.h"
22 #include "lib/util.h"
23 #include "lib/content_factory.h"
24 #include "lib/job_manager.h"
25 #include "lib/signal_manager.h"
26 #include "lib/job.h"
27 #include "lib/dcp_content_type.h"
28 #include "lib/ratio.h"
29 #include "lib/image_content.h"
30 #include <boost/filesystem.hpp>
31 #include <getopt.h>
32 #include <string>
33 #include <iostream>
34 #include <cstdlib>
35 #include <stdexcept>
36
37 using std::string;
38 using std::cout;
39 using std::cerr;
40 using std::list;
41 using std::exception;
42 using boost::shared_ptr;
43 using boost::dynamic_pointer_cast;
44
45 static void
46 syntax (string n)
47 {
48         cerr << "Syntax: " << n << " [OPTION] <CONTENT> [<CONTENT> ...]\n"
49              << "  -v, --version                 show DCP-o-matic version\n"
50              << "  -h, --help                    show this help\n"
51              << "  -n, --name <name>             film name\n"
52              << "  -c, --dcp-content-type <type> FTR, SHR, TLR, TST, XSN, RTG, TSR, POL, PSA or ADV\n"
53              << "      --container-ratio <ratio> 119, 133, 137, 138, 166, 178, 185 or 239\n"
54              << "      --content-ratio <ratio>   119, 133, 137, 138, 166, 178, 185 or 239\n"
55              << "  -s, --still-length <n>        number of seconds that still content should last\n"
56              << "      --standard <standard>     SMPTE or interop (default SMPTE)\n"
57              << "      --no-use-isdcf-name       do not use an ISDCF name; use the specified name unmodified\n"
58              << "      --no-sign                 do not sign the DCP\n"
59              << "  -o, --output <dir>            output directory\n";
60 }
61
62 static void
63 help (string n)
64 {
65         cerr << "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
66              << "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
67              << "will be written to stdout.\n";
68
69         syntax (n);
70 }
71
72 class SimpleSignalManager : public SignalManager
73 {
74 public:
75         /* Do nothing in this method so that UI events happen in our thread
76            when we call SignalManager::ui_idle().
77         */
78         void wake_ui () {}
79 };
80
81 int
82 main (int argc, char* argv[])
83 {
84         dcpomatic_setup_path_encoding ();
85         dcpomatic_setup ();
86
87         string name;
88         DCPContentType const * dcp_content_type = DCPContentType::from_isdcf_name ("TST");
89         Ratio const * container_ratio = 0;
90         Ratio const * content_ratio = 0;
91         int still_length = 10;
92         dcp::Standard standard = dcp::SMPTE;
93         boost::filesystem::path output;
94         bool sign = true;
95         bool use_isdcf_name = true;
96
97         int option_index = 0;
98         while (true) {
99                 static struct option long_options[] = {
100                         { "version", no_argument, 0, 'v'},
101                         { "help", no_argument, 0, 'h'},
102                         { "name", required_argument, 0, 'n'},
103                         { "dcp-content-type", required_argument, 0, 'c'},
104                         { "container-ratio", required_argument, 0, 'A'},
105                         { "content-ratio", required_argument, 0, 'B'},
106                         { "still-length", required_argument, 0, 's'},
107                         { "standard", required_argument, 0, 'C'},
108                         { "no-use-isdcf-name", no_argument, 0, 'D'},
109                         { "no-sign", no_argument, 0, 'E'},
110                         { "output", required_argument, 0, 'o'},
111                         { 0, 0, 0, 0}
112                 };
113
114                 int c = getopt_long (argc, argv, "vhn:c:A:B:C:s:o:DE", long_options, &option_index);
115                 if (c == -1) {
116                         break;
117                 }
118
119                 switch (c) {
120                 case 'v':
121                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
122                         exit (EXIT_SUCCESS);
123                 case 'h':
124                         help (argv[0]);
125                         exit (EXIT_SUCCESS);
126                 case 'n':
127                         name = optarg;
128                         break;
129                 case 'c':
130                         dcp_content_type = DCPContentType::from_isdcf_name (optarg);
131                         if (dcp_content_type == 0) {
132                                 cerr << "Bad DCP content type.\n";
133                                 syntax (argv[0]);
134                                 exit (EXIT_FAILURE);
135                         }
136                         break;
137                 case 'A':
138                         container_ratio = Ratio::from_id (optarg);
139                         if (container_ratio == 0) {
140                                 cerr << "Bad container ratio.\n";
141                                 syntax (argv[0]);
142                                 exit (EXIT_FAILURE);
143                         }
144                         break;
145                 case 'B':
146                         content_ratio = Ratio::from_id (optarg);
147                         if (content_ratio == 0) {
148                                 cerr << "Bad content ratio " << optarg << ".\n";
149                                 syntax (argv[0]);
150                                 exit (EXIT_FAILURE);
151                         }
152                         break;
153                 case 'C':
154                         if (strcmp (optarg, "interop") == 0) {
155                                 standard = dcp::INTEROP;
156                         } else if (strcmp (optarg, "SMPTE") != 0) {
157                                 cerr << "Bad standard " << optarg << ".\n";
158                                 syntax (argv[0]);
159                                 exit (EXIT_FAILURE);
160                         }
161                         break;
162                 case 'D':
163                         use_isdcf_name = false;
164                         break;
165                 case 'E':
166                         sign = false;
167                         break;
168                 case 's':
169                         still_length = atoi (optarg);
170                         break;
171                 case 'o':
172                         output = optarg;
173                         break;
174                 case '?':
175                         syntax (argv[0]);
176                         exit (EXIT_FAILURE);
177                 }
178         }
179
180         if (optind > argc) {
181                 help (argv[0]);
182                 exit (EXIT_FAILURE);
183         }
184
185         if (!content_ratio) {
186                 cerr << argv[0] << ": missing required option --content-ratio.\n";
187                 exit (EXIT_FAILURE);
188         }
189
190         if (!container_ratio) {
191                 container_ratio = content_ratio;
192         }
193
194         if (optind == argc) {
195                 cerr << argv[0] << ": no content specified.\n";
196                 exit (EXIT_FAILURE);
197         }
198
199         signal_manager = new SimpleSignalManager ();
200
201         if (name.empty ()) {
202                 name = boost::filesystem::path (argv[optind]).leaf().string ();
203         }
204
205         try {
206                 shared_ptr<Film> film (new Film (output, false));
207                 film->set_name (name);
208
209                 film->set_container (container_ratio);
210                 film->set_dcp_content_type (dcp_content_type);
211                 film->set_interop (standard == dcp::INTEROP);
212                 film->set_use_isdcf_name (use_isdcf_name);
213                 film->set_signed (sign);
214
215                 for (int i = optind; i < argc; ++i) {
216                         shared_ptr<Content> c = content_factory (film, argv[i]);
217                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c);
218                         if (vc) {
219                                 vc->set_scale (VideoContentScale (content_ratio));
220                         }
221                         film->examine_and_add_content (c);
222                 }
223
224                 JobManager* jm = JobManager::instance ();
225
226                 while (jm->work_to_do ()) {}
227                 while (signal_manager->ui_idle() > 0) {}
228
229                 ContentList content = film->content ();
230                 for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
231                         shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (*i);
232                         if (ic) {
233                                 ic->set_video_length (still_length * 24);
234                         }
235                 }
236
237                 if (jm->errors ()) {
238                         list<shared_ptr<Job> > jobs = jm->get ();
239                         for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
240                                 if ((*i)->finished_in_error ()) {
241                                         cerr << (*i)->error_summary () << "\n"
242                                              << (*i)->error_details () << "\n";
243                                 }
244                         }
245                         exit (EXIT_FAILURE);
246                 }
247
248                 if (!output.empty ()) {
249                         film->write_metadata ();
250                 } else {
251                         film->metadata()->write_to_stream_formatted (cout, "UTF-8");
252                 }
253         } catch (exception& e) {
254                 cerr << argv[0] << ": " << e.what() << "\n";
255                 exit (EXIT_FAILURE);
256         }
257
258         return 0;
259 }