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