Merge master; specify libdcp-1.0.
[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/ui_signaller.h"
32 #include "lib/job.h"
33 #include "lib/dcp_content_type.h"
34 #include "lib/ratio.h"
35
36 using std::string;
37 using std::cout;
38 using std::cerr;
39 using std::list;
40 using std::exception;
41 using boost::shared_ptr;
42 using boost::dynamic_pointer_cast;
43
44 static void
45 help (string n)
46 {
47         cerr << "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
48              << "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
49              << "will be written to stdout.\n"
50              << "Syntax: " << n << " [OPTION] <CONTENT> [<CONTENT> ...]\n"
51              << "  -v, --version                 show DCP-o-matic version\n"
52              << "  -h, --help                    show this help\n"
53              << "  -n, --name <name>             film name\n"
54              << "  -c, --dcp-content-type <type> FTR, SHR, TLR, TST, XSN, RTG, TSR, POL, PSA or ADV\n"
55              << "      --container-ratio         119, 133, 137, 138, 166, 178, 185 or 239\n"
56              << "      --content-ratio           119, 133, 137, 138, 166, 178, 185 or 239\n"
57              << "  -o, --output <dir>            output directory\n";
58 }
59
60 int
61 main (int argc, char* argv[])
62 {
63         dcpomatic_setup ();
64
65         string name;
66         DCPContentType const * dcp_content_type = DCPContentType::from_dci_name ("TST");
67         Ratio const * container_ratio = 0;
68         Ratio const * content_ratio = 0;
69         boost::filesystem::path output;
70         
71         int option_index = 0;
72         while (1) {
73                 static struct option long_options[] = {
74                         { "version", no_argument, 0, 'v'},
75                         { "help", no_argument, 0, 'h'},
76                         { "name", required_argument, 0, 'n'},
77                         { "dcp-content-type", required_argument, 0, 'c'},
78                         { "container-ratio", required_argument, 0, 'A'},
79                         { "content-ratio", required_argument, 0, 'B'},
80                         { "output", required_argument, 0, 'o'},
81                         { 0, 0, 0, 0}
82                 };
83
84                 int c = getopt_long (argc, argv, "vhn:c:A:B:o:", long_options, &option_index);
85                 if (c == -1) {
86                         break;
87                 }
88
89                 switch (c) {
90                 case 'v':
91                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
92                         exit (EXIT_SUCCESS);
93                 case 'h':
94                         help (argv[0]);
95                         exit (EXIT_SUCCESS);
96                 case 'n':
97                         name = optarg;
98                         break;
99                 case 'c':
100                         dcp_content_type = DCPContentType::from_dci_name (optarg);
101                         if (dcp_content_type == 0) {
102                                 cerr << "Bad DCP content type.\n";
103                                 help (argv[0]);
104                                 exit (EXIT_FAILURE);
105                         }
106                         break;
107                 case 'A':
108                         container_ratio = Ratio::from_id (optarg);
109                         if (container_ratio == 0) {
110                                 cerr << "Bad container ratio.\n";
111                                 help (argv[0]);
112                                 exit (EXIT_FAILURE);
113                         }
114                         break;
115                 case 'B':
116                         content_ratio = Ratio::from_id (optarg);
117                         if (content_ratio == 0) {
118                                 cerr << "Bad content ratio " << optarg << ".\n";
119                                 help (argv[0]);
120                                 exit (EXIT_FAILURE);
121                         }
122                         break;
123                 case 'o':
124                         output = optarg;
125                         break;
126                 }
127         }
128
129         if (optind > argc) {
130                 help (argv[0]);
131                 exit (EXIT_FAILURE);
132         }
133
134         if (!content_ratio) {
135                 cerr << "Missing required option --content-ratio.\n";
136                 help (argv[0]);
137                 exit (EXIT_FAILURE);
138         }
139
140         if (!container_ratio) {
141                 container_ratio = content_ratio;
142         }
143
144         ui_signaller = new UISignaller ();
145
146         try {
147                 shared_ptr<Film> film (new Film (output, false));
148                 if (!name.empty ()) {
149                         film->set_name (name);
150                 }
151
152                 film->set_container (container_ratio);
153                 
154                 for (int i = optind; i < argc; ++i) {
155                         shared_ptr<Content> c = content_factory (film, argv[i]);
156                         shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (c);
157                         if (vc) {
158                                 vc->set_ratio (content_ratio);
159                         }
160                         film->examine_and_add_content (c);
161                 }
162                 
163                 JobManager* jm = JobManager::instance ();
164                 while (jm->work_to_do ()) {
165                         ui_signaller->ui_idle ();
166                 }
167                 
168                 if (jm->errors ()) {
169                         list<shared_ptr<Job> > jobs = jm->get ();
170                         for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
171                                 if ((*i)->finished_in_error ()) {
172                                         cerr << (*i)->error_summary () << "\n"
173                                              << (*i)->error_details () << "\n";
174                                 }
175                         }
176                         exit (EXIT_FAILURE);
177                 }
178
179                 if (!output.empty ()) {
180                         film->write_metadata ();
181                 } else {
182                         film->metadata()->write_to_stream_formatted (cout);
183                 }
184         } catch (exception& e) {
185                 cerr << argv[0] << ": " << e.what() << "\n";
186                 exit (EXIT_FAILURE);
187         }
188                 
189         return 0;
190 }