63889a5977f7f9ff6bccdb4662b3926cfc031d2b
[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/audio_content.h"
22 #include "lib/config.h"
23 #include "lib/content_factory.h"
24 #include "lib/create_cli.h"
25 #include "lib/cross.h"
26 #include "lib/dcp_content.h"
27 #include "lib/dcp_content_type.h"
28 #include "lib/film.h"
29 #include "lib/image_content.h"
30 #include "lib/job.h"
31 #include "lib/job_manager.h"
32 #include "lib/ratio.h"
33 #include "lib/signal_manager.h"
34 #include "lib/util.h"
35 #include "lib/version.h"
36 #include "lib/version.h"
37 #include "lib/video_content.h"
38 #include <dcp/exceptions.h>
39 #include <libxml++/libxml++.h>
40 #include <boost/filesystem.hpp>
41 #include <getopt.h>
42 #include <cstdlib>
43 #include <iostream>
44 #include <stdexcept>
45 #include <string>
46
47
48 using std::cerr;
49 using std::cout;
50 using std::dynamic_pointer_cast;
51 using std::exception;
52 using std::list;
53 using std::make_shared;
54 using std::shared_ptr;
55 using std::string;
56 using std::vector;
57 using boost::optional;
58
59
60 class SimpleSignalManager : public SignalManager
61 {
62 public:
63         /* Do nothing in this method so that UI events happen in our thread
64            when we call SignalManager::ui_idle().
65         */
66         void wake_ui () override {}
67 };
68
69 int
70 main (int argc, char* argv[])
71 {
72         dcpomatic_setup_path_encoding ();
73         dcpomatic_setup ();
74
75         CreateCLI cc (argc, argv);
76         if (cc.error) {
77                 cerr << *cc.error << "\n";
78                 exit (1);
79         }
80
81         if (cc.version) {
82                 cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
83                 exit (EXIT_SUCCESS);
84         }
85
86         if (cc.config_dir) {
87                 State::override_path = *cc.config_dir;
88         }
89
90         signal_manager = new SimpleSignalManager ();
91         auto jm = JobManager::instance ();
92
93         try {
94                 auto film = cc.make_film();
95
96                 for (auto cli_content: cc.content) {
97                         auto const can = boost::filesystem::canonical (cli_content.path);
98                         vector<shared_ptr<Content>> film_content_list;
99
100                         if (boost::filesystem::exists (can / "ASSETMAP") || (boost::filesystem::exists (can / "ASSETMAP.xml"))) {
101                                 auto dcp = make_shared<DCPContent>(can);
102                                 film_content_list.push_back (dcp);
103                                 if (cli_content.kdm) {
104                                         dcp->add_kdm (dcp::EncryptedKDM(dcp::file_to_string(*cli_content.kdm)));
105                                 }
106                                 if (cli_content.cpl) {
107                                         dcp->set_cpl(*cli_content.cpl);
108                                 }
109                         } else {
110                                 /* I guess it's not a DCP */
111                                 film_content_list = content_factory (can);
112                         }
113
114                         for (auto film_content: film_content_list) {
115                                 film->examine_and_add_content (film_content);
116                         }
117
118                         while (jm->work_to_do ()) {
119                                 dcpomatic_sleep_seconds (1);
120                         }
121
122                         while (signal_manager->ui_idle() > 0) {}
123
124                         for (auto film_content: film_content_list) {
125                                 if (film_content->video) {
126                                         film_content->video->set_frame_type (cli_content.frame_type);
127                                 }
128                                 if (film_content->audio && cli_content.channel) {
129                                         for (auto stream: film_content->audio->streams()) {
130                                                 AudioMapping mapping(stream->channels(), film->audio_channels());
131                                                 for (int channel = 0; channel < stream->channels(); ++channel) {
132                                                         mapping.set(channel, *cli_content.channel, 1.0f);
133                                                 }
134                                                 stream->set_mapping (mapping);
135                                         }
136                                 }
137                                 if (film_content->audio && cli_content.gain) {
138                                         film_content->audio->set_gain (*cli_content.gain);
139                                 }
140                         }
141                 }
142
143                 if (cc.dcp_frame_rate) {
144                         film->set_video_frame_rate (*cc.dcp_frame_rate);
145                 }
146
147                 for (auto i: film->content()) {
148                         auto ic = dynamic_pointer_cast<ImageContent> (i);
149                         if (ic && ic->still()) {
150                                 ic->video->set_length(cc.still_length.get_value_or(10) * 24);
151                         }
152                 }
153
154                 if (jm->errors ()) {
155                         for (auto i: jm->get()) {
156                                 if (i->finished_in_error()) {
157                                         cerr << i->error_summary() << "\n";
158                                         if (!i->error_details().empty()) {
159                                              cout << i->error_details() << "\n";
160                                         }
161                                 }
162                         }
163                         exit (EXIT_FAILURE);
164                 }
165
166                 if (cc.output_dir) {
167                         film->write_metadata ();
168                 } else {
169                         film->metadata()->write_to_stream_formatted(cout, "UTF-8");
170                 }
171         } catch (exception& e) {
172                 cerr << argv[0] << ": " << e.what() << "\n";
173                 exit (EXIT_FAILURE);
174         }
175
176         return 0;
177 }