Bv2.1 8.5: Features must have FFEC/FFMC markers.
[libdcp.git] / tools / dcprecover.cc
1 /*
2     Copyright (C) 2018 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 #include "dcp.h"
35 #include "cpl.h"
36 #include "exceptions.h"
37 #include "asset_factory.h"
38 #include "reel_asset.h"
39 #include <getopt.h>
40 #include <libxml++/libxml++.h>
41 #include <boost/filesystem.hpp>
42 #include <boost/foreach.hpp>
43 #include <iostream>
44
45 using std::cerr;
46 using std::cout;
47 using std::string;
48 using std::shared_ptr;
49 using std::vector;
50 using boost::optional;
51
52 static void
53 help (string n)
54 {
55         cerr << "Syntax: " << n << " [OPTION] <DCP>]\n"
56              << "  -h, --help         show this help\n"
57              << "  -o, --output       output DCP directory\n";
58 }
59
60 void progress (float f)
61 {
62         cout << (f * 100) << "%               \r";
63 }
64
65 int
66 main (int argc, char* argv[])
67 {
68         dcp::init ();
69
70         int option_index = 0;
71         optional<boost::filesystem::path> output;
72         while (true) {
73                 struct option long_options[] = {
74                         { "help", no_argument, 0, 'h' },
75                         { "output", required_argument, 0, 'o' },
76                         { 0, 0, 0, 0 }
77                 };
78
79                 int c = getopt_long (argc, argv, "ho:", long_options, &option_index);
80
81                 if (c == -1) {
82                         break;
83                 }
84
85                 switch (c) {
86                 case 'h':
87                         help (argv[0]);
88                         exit (EXIT_SUCCESS);
89                 case 'o':
90                         output = optarg;
91                         break;
92                 }
93         }
94
95         if (optind >= argc) {
96                 help (argv[0]);
97                 exit (EXIT_FAILURE);
98         }
99
100         boost::filesystem::path dcp_dir = argv[optind];
101
102         /* Try to read it and report errors */
103
104         dcp::DCP dcp (dcp_dir);
105         vector<dcp::VerificationNote> notes;
106         try {
107                 dcp.read (&notes, true);
108         } catch (dcp::ReadError& e) {
109                 cout << "Error:" <<  e.what() << "\n";
110         }
111
112         BOOST_FOREACH (dcp::VerificationNote i, notes) {
113                 cout << "Error: " << dcp::note_to_string(i) << "\n";
114         }
115
116         /* Look for a CPL */
117
118         shared_ptr<dcp::CPL> cpl;
119         for (boost::filesystem::directory_iterator i(dcp_dir); i != boost::filesystem::directory_iterator(); ++i) {
120                 if (i->path().extension() == ".xml") {
121                         try {
122                                 cpl.reset(new dcp::CPL(i->path()));
123                         } catch (dcp::ReadError& e) {
124                                 cout << "Error: " << e.what() << "\n";
125                         } catch (xmlpp::parse_error& e) {
126                                 cout << "Error: " << e.what() << "\n";
127                         }
128                 }
129         }
130
131         if (cpl) {
132                 cout << "Got a CPL!\n";
133
134                 if (!output) {
135                         cerr << "No output directory specified.\n";
136                         exit(1);
137                 }
138
139                 /* Read all MXF assets */
140                 vector<shared_ptr<dcp::Asset>> assets;
141                 for (boost::filesystem::directory_iterator i(dcp_dir); i != boost::filesystem::directory_iterator(); ++i) {
142                         if (i->path().extension() == ".mxf") {
143                                 try {
144                                         shared_ptr<dcp::Asset> asset = dcp::asset_factory(i->path(), true);
145                                         asset->set_file (*output / i->path().filename());
146                                         cout << "Hashing " << i->path().filename() << "\n";
147                                         asset->hash (&progress);
148                                         cout << "100%                     \n";
149                                         assets.push_back (asset);
150                                 } catch (dcp::ReadError& e) {
151                                         cout << "Error: " << e.what() << "\n";
152                                 }
153                         }
154                 }
155
156                 dcp::DCP fixed (*output);
157                 fixed.add (cpl);
158                 fixed.resolve_refs (assets);
159                 fixed.write_xml (dcp::INTEROP);
160                 cout << "Fixed XML files written to " << output->string() << "\n";
161         }
162
163         return 0;
164 }