Fix dcp::LocalTime constructor to cope with longer fractional second parts (DoM ...
[libdcp.git] / tools / dcprecover.cc
1 /*
2     Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #include "asset_factory.h"
36 #include "cpl.h"
37 #include "dcp.h"
38 #include "exceptions.h"
39 #include "reel_asset.h"
40 #include "warnings.h"
41 #include <getopt.h>
42 LIBDCP_DISABLE_WARNINGS
43 #include <libxml++/libxml++.h>
44 LIBDCP_ENABLE_WARNINGS
45 #include <boost/filesystem.hpp>
46 #include <iostream>
47
48
49 using std::cerr;
50 using std::cout;
51 using std::make_shared;
52 using std::shared_ptr;
53 using std::string;
54 using std::vector;
55 using boost::optional;
56
57
58 static void
59 help (string n)
60 {
61         cerr << "Syntax: " << n << " [OPTION] <DCP>]\n"
62              << "  -h, --help         show this help\n"
63              << "  -o, --output       output DCP directory\n";
64 }
65
66
67 void progress (float f)
68 {
69         cout << (f * 100) << "%               \r";
70 }
71
72
73 int
74 main (int argc, char* argv[])
75 {
76         dcp::init ();
77
78         int option_index = 0;
79         optional<boost::filesystem::path> output;
80         while (true) {
81                 struct option long_options[] = {
82                         { "help", no_argument, 0, 'h' },
83                         { "output", required_argument, 0, 'o' },
84                         { 0, 0, 0, 0 }
85                 };
86
87                 int c = getopt_long (argc, argv, "ho:", long_options, &option_index);
88
89                 if (c == -1) {
90                         break;
91                 }
92
93                 switch (c) {
94                 case 'h':
95                         help (argv[0]);
96                         exit (EXIT_SUCCESS);
97                 case 'o':
98                         output = optarg;
99                         break;
100                 }
101         }
102
103         if (optind >= argc) {
104                 help (argv[0]);
105                 exit (EXIT_FAILURE);
106         }
107
108         boost::filesystem::path dcp_dir = argv[optind];
109
110         /* Try to read it and report errors */
111
112         dcp::DCP dcp (dcp_dir);
113         vector<dcp::VerificationNote> notes;
114         try {
115                 dcp.read (&notes, true);
116         } catch (dcp::ReadError& e) {
117                 cout << "Error:" <<  e.what() << "\n";
118         }
119
120         for (auto i: notes) {
121                 cout << "Error: " << dcp::note_to_string(i) << "\n";
122         }
123
124         /* Look for a CPL */
125
126         shared_ptr<dcp::CPL> cpl;
127         for (auto i: boost::filesystem::directory_iterator(dcp_dir)) {
128                 if (i.path().extension() == ".xml") {
129                         try {
130                                 cpl = make_shared<dcp::CPL>(i.path());
131                         } catch (dcp::ReadError& e) {
132                                 cout << "Error: " << e.what() << "\n";
133                         } catch (xmlpp::parse_error& e) {
134                                 cout << "Error: " << e.what() << "\n";
135                         }
136                 }
137         }
138
139         if (cpl) {
140                 cout << "Got a CPL!\n";
141
142                 if (!output) {
143                         cerr << "No output directory specified.\n";
144                         exit(1);
145                 }
146
147                 /* Read all MXF assets */
148                 vector<shared_ptr<dcp::Asset>> assets;
149                 for (auto i: boost::filesystem::directory_iterator(dcp_dir)) {
150                         if (i.path().extension() == ".mxf") {
151                                 try {
152                                         auto asset = dcp::asset_factory(i.path(), true);
153                                         asset->set_file (*output / i.path().filename());
154                                         cout << "Hashing " << i.path().filename() << "\n";
155                                         asset->hash (&progress);
156                                         cout << "100%                     \n";
157                                         assets.push_back (asset);
158                                 } catch (dcp::ReadError& e) {
159                                         cout << "Error: " << e.what() << "\n";
160                                 }
161                         }
162                 }
163
164                 dcp::DCP fixed (*output);
165                 fixed.add (cpl);
166                 fixed.resolve_refs (assets);
167                 fixed.write_xml ();
168                 cout << "Fixed XML files written to " << output->string() << "\n";
169         }
170
171         return 0;
172 }