40d764c2b64e430e8c6fb6521d3fa760fd39571f
[dcpomatic.git] / src / tools / dcpomatic_create.cc
1 /*
2     Copyright (C) 2013-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "lib/version.h"
22 #include "lib/film.h"
23 #include "lib/util.h"
24 #include "lib/content_factory.h"
25 #include "lib/job_manager.h"
26 #include "lib/signal_manager.h"
27 #include "lib/job.h"
28 #include "lib/dcp_content_type.h"
29 #include "lib/ratio.h"
30 #include "lib/image_content.h"
31 #include "lib/video_content.h"
32 #include "lib/cross.h"
33 #include "lib/config.h"
34 #include "lib/dcp_content.h"
35 #include "lib/create_cli.h"
36 #include "lib/version.h"
37 #include <dcp/exceptions.h>
38 #include <libxml++/libxml++.h>
39 #include <boost/filesystem.hpp>
40 #include <boost/foreach.hpp>
41 #include <getopt.h>
42 #include <string>
43 #include <iostream>
44 #include <cstdlib>
45 #include <stdexcept>
46
47 using std::string;
48 using std::cout;
49 using std::cerr;
50 using std::list;
51 using std::exception;
52 using boost::shared_ptr;
53 using boost::dynamic_pointer_cast;
54 using boost::optional;
55
56 class SimpleSignalManager : public SignalManager
57 {
58 public:
59         /* Do nothing in this method so that UI events happen in our thread
60            when we call SignalManager::ui_idle().
61         */
62         void wake_ui () {}
63 };
64
65 int
66 main (int argc, char* argv[])
67 {
68         dcpomatic_setup_path_encoding ();
69         dcpomatic_setup ();
70
71         CreateCLI cc (argc, argv);
72         if (cc.error) {
73                 cerr << *cc.error << "\n";
74                 exit (1);
75         }
76
77         if (cc.version) {
78                 cerr << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
79                 exit (1);
80         }
81
82         if (cc.config_dir) {
83                 State::override_path = *cc.config_dir;
84         }
85
86         signal_manager = new SimpleSignalManager ();
87         JobManager* jm = JobManager::instance ();
88
89         try {
90                 shared_ptr<Film> film (new Film(cc.output_dir));
91                 if (cc.template_name) {
92                         film->use_template (cc.template_name.get());
93                 }
94                 film->set_name (cc.name);
95
96                 film->set_container (cc.container_ratio);
97                 film->set_dcp_content_type (cc.dcp_content_type);
98                 film->set_interop (cc.standard == dcp::INTEROP);
99                 film->set_use_isdcf_name (!cc.no_use_isdcf_name);
100                 film->set_signed (!cc.no_sign);
101                 film->set_encrypted (cc.encrypt);
102                 film->set_three_d (cc.threed);
103                 film->set_resolution (cc.fourk ? RESOLUTION_4K : RESOLUTION_2K);
104                 if (cc.j2k_bandwidth) {
105                         film->set_j2k_bandwidth (*cc.j2k_bandwidth);
106                 }
107
108                 BOOST_FOREACH (CreateCLI::Content i, cc.content) {
109                         boost::filesystem::path const can = boost::filesystem::canonical (i.path);
110                         list<shared_ptr<Content> > content;
111
112                         if (boost::filesystem::exists (can / "ASSETMAP") || (boost::filesystem::exists (can / "ASSETMAP.xml"))) {
113                                 content.push_back (shared_ptr<DCPContent>(new DCPContent(can)));
114                         } else {
115                                 /* I guess it's not a DCP */
116                                 content = content_factory (can);
117                         }
118
119                         BOOST_FOREACH (shared_ptr<Content> j, content) {
120                                 film->examine_and_add_content (j);
121                         }
122
123                         while (jm->work_to_do ()) {
124                                 dcpomatic_sleep (1);
125                         }
126
127                         while (signal_manager->ui_idle() > 0) {}
128
129                         BOOST_FOREACH (shared_ptr<Content> j, content) {
130                                 if (j->video) {
131                                         j->video->set_scale (VideoContentScale(cc.content_ratio));
132                                         j->video->set_frame_type (i.frame_type);
133                                 }
134                         }
135                 }
136
137                 if (cc.dcp_frame_rate) {
138                         film->set_video_frame_rate (*cc.dcp_frame_rate);
139                 }
140
141                 BOOST_FOREACH (shared_ptr<Content> i, film->content()) {
142                         shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (i);
143                         if (ic && ic->still()) {
144                                 ic->video->set_length (cc.still_length * 24);
145                         }
146                 }
147
148                 if (jm->errors ()) {
149                         BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
150                                 if (i->finished_in_error()) {
151                                         cerr << i->error_summary() << "\n"
152                                              << i->error_details() << "\n";
153                                 }
154                         }
155                         exit (EXIT_FAILURE);
156                 }
157
158                 if (cc.output_dir) {
159                         film->write_metadata ();
160                 } else {
161                         film->metadata()->write_to_stream_formatted(cout, "UTF-8");
162                 }
163         } catch (exception& e) {
164                 cerr << argv[0] << ": " << e.what() << "\n";
165                 exit (EXIT_FAILURE);
166         }
167
168         return 0;
169 }