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