Simpole DCP recovery utility (dcprecover) added.
[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::list;
49 using boost::shared_ptr;
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         int option_index = 0;
69         optional<boost::filesystem::path> output;
70         while (true) {
71                 struct option long_options[] = {
72                         { "help", no_argument, 0, 'h' },
73                         { "output", required_argument, 0, 'o' },
74                         { 0, 0, 0, 0 }
75                 };
76
77                 int c = getopt_long (argc, argv, "ho:", long_options, &option_index);
78
79                 if (c == -1) {
80                         break;
81                 }
82
83                 switch (c) {
84                 case 'h':
85                         help (argv[0]);
86                         exit (EXIT_SUCCESS);
87                 case 'o':
88                         output = optarg;
89                         break;
90                 }
91         }
92
93         if (optind >= argc) {
94                 help (argv[0]);
95                 exit (EXIT_FAILURE);
96         }
97
98         boost::filesystem::path dcp_dir = argv[optind];
99
100         /* Try to read it and report errors */
101
102         dcp::DCP dcp (dcp_dir);
103         dcp::DCP::ReadErrors errors;
104         try {
105                 dcp.read (true, &errors, true);
106         } catch (dcp::DCPReadError& e) {
107                 cout << "Error:" <<  e.what() << "\n";
108         }
109
110         BOOST_FOREACH (shared_ptr<dcp::DCPReadError> i, errors) {
111                 cout << "Error: " << i->what() << "\n";
112         }
113
114         /* Look for a CPL */
115
116         shared_ptr<dcp::CPL> cpl;
117         for (boost::filesystem::directory_iterator i(dcp_dir); i != boost::filesystem::directory_iterator(); ++i) {
118                 if (i->path().extension() == ".xml") {
119                         try {
120                                 cpl.reset(new dcp::CPL(i->path()));
121                         } catch (dcp::DCPReadError& e) {
122                                 cout << "Error: " << e.what() << "\n";
123                         } catch (xmlpp::parse_error& e) {
124                                 cout << "Error: " << e.what() << "\n";
125                         }
126                 }
127         }
128
129         if (cpl) {
130                 cout << "Got a CPL!\n";
131
132                 if (!output) {
133                         cerr << "No output directory specified.\n";
134                         exit(1);
135                 }
136
137                 /* Read all MXF assets */
138                 list<shared_ptr<dcp::Asset> > assets;
139                 for (boost::filesystem::directory_iterator i(dcp_dir); i != boost::filesystem::directory_iterator(); ++i) {
140                         if (i->path().extension() == ".mxf") {
141                                 try {
142                                         shared_ptr<dcp::Asset> asset = dcp::asset_factory(i->path(), true);
143                                         asset->set_file (*output / i->path().filename());
144                                         cout << "Hashing " << i->path().filename() << "\n";
145                                         asset->hash (&progress);
146                                         cout << "100%                     \n";
147                                         assets.push_back (asset);
148                                 } catch (dcp::DCPReadError& e) {
149                                         cout << "Error: " << e.what() << "\n";
150                                 }
151                         }
152                 }
153
154                 dcp::DCP fixed (*output);
155                 fixed.add (cpl);
156                 fixed.resolve_refs (assets);
157                 fixed.write_xml (dcp::INTEROP);
158                 cout << "Fixed XML files written to " << output->string() << "\n";
159         }
160
161         return 0;
162 }