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