Use exceptions to hold errors even in the keep_going case.
[libdcp.git] / tools / dcpinfo.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <iostream>
21 #include <cstdlib>
22 #include <boost/filesystem.hpp>
23 #include <getopt.h>
24 #include "dcp.h"
25 #include "exceptions.h"
26 #include "reel.h"
27 #include "sound_mxf.h"
28 #include "picture_mxf.h"
29 #include "subtitle_content.h"
30 #include "reel_picture_asset.h"
31 #include "reel_sound_asset.h"
32 #include "reel_subtitle_asset.h"
33 #include "subtitle_string.h"
34 #include "cpl.h"
35
36 using std::string;
37 using std::cerr;
38 using std::cout;
39 using std::list;
40 using boost::shared_ptr;
41 using namespace dcp;
42
43 static void
44 help (string n)
45 {
46         cerr << "Syntax: " << n << " [options] <DCP>\n"
47              << "  -s, --subtitles   list all subtitles\n"
48              << "  -k, --keep-going  carry on in the event of errors, if possible\n";
49 }
50
51 static void
52 main_picture (shared_ptr<Reel> reel)
53 {
54         if (reel->main_picture() && reel->main_picture()->mxf()) {
55                 cout << "      Picture:  "
56                      << reel->main_picture()->mxf()->size().width
57                      << "x"
58                      << reel->main_picture()->mxf()->size().height << "\n";
59         }
60 }
61
62 static void
63 main_sound (shared_ptr<Reel> reel)
64 {
65         if (reel->main_sound() && reel->main_sound()->mxf()) {
66                 cout << "      Sound:    "
67                      << reel->main_sound()->mxf()->channels()
68                      << " channels at "
69                      << reel->main_sound()->mxf()->sampling_rate() << "Hz\n";
70         }
71 }
72
73 static void
74 main_subtitle (shared_ptr<Reel> reel, bool list_subtitles)
75 {
76         if (!reel->main_subtitle()) {
77                 return;
78         }
79         
80         list<shared_ptr<SubtitleString> > subs = reel->main_subtitle()->subtitle_content()->subtitles ();
81         cout << "      Subtitle: " << subs.size() << " subtitles in " << reel->main_subtitle()->subtitle_content()->language() << "\n";
82         if (list_subtitles) {
83                 for (list<shared_ptr<SubtitleString> >::const_iterator k = subs.begin(); k != subs.end(); ++k) {
84                         cout << "        " << (*k)->text() << "\n";
85                         cout << "          "
86                              << "font:" << (*k)->font() << "; "
87                              << "italic:" << (*k)->italic() << "; "
88                              << "color:" << (*k)->color() << "; "
89                              << "in:" << (*k)->in() << "; "
90                              << "out:" << (*k)->out() << "; "
91                              << "v_position:" << (*k)->v_position() << "; "
92                              << "v_align:" << (*k)->v_align() << "; "
93                              << "effect:" << (*k)->effect() << "; "
94                              << "effect_color:" << (*k)->effect_color() << "; "
95                              << "fade_up_time:" << (*k)->fade_up_time() << "; "
96                              << "fade_down_time:" << (*k)->fade_down_time() << "; "
97                              << "size: " << (*k)->size() << "\n";
98                 }
99         }
100 }
101
102 int
103 main (int argc, char* argv[])
104 {
105         bool subtitles = false;
106         bool keep_going = false;
107         
108         int option_index = 0;
109         while (1) {
110                 static struct option long_options[] = {
111                         { "version", no_argument, 0, 'v'},
112                         { "help", no_argument, 0, 'h'},
113                         { "subtitles", no_argument, 0, 's'},
114                         { "keep-going", no_argument, 0, 'k'},
115                         { 0, 0, 0, 0 }
116                 };
117
118                 int c = getopt_long (argc, argv, "vhsk", long_options, &option_index);
119
120                 if (c == -1) {
121                         break;
122                 }
123
124                 switch (c) {
125                 case 'v':
126                         cout << "dcpdiff version " << LIBDCP_VERSION << "\n";
127                         exit (EXIT_SUCCESS);
128                 case 'h':
129                         help (argv[0]);
130                         exit (EXIT_SUCCESS);
131                 case 's':
132                         subtitles = true;
133                         break;
134                 case 'k':
135                         keep_going = true;
136                         break;
137                 }
138         }
139
140         if (argc <= optind || argc > (optind + 1)) {
141                 help (argv[0]);
142                 exit (EXIT_FAILURE);
143         }
144
145         if (!boost::filesystem::exists (argv[optind])) {
146                 cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
147                 exit (EXIT_FAILURE);
148         }
149
150         DCP* dcp = 0;
151         list<shared_ptr<DCPReadError> > errors;
152         try {
153                 dcp = new DCP (argv[optind]);
154                 dcp->read (keep_going, &errors);
155         } catch (FileError& e) {
156                 cerr << "Could not read DCP " << argv[optind] << "; " << e.what() << "\n";
157                 exit (EXIT_FAILURE);
158         } catch (DCPReadError& e) {
159                 cerr << "Could not read DCP " << argv[optind] << "; " << e.what() << "\n";
160                 exit (EXIT_FAILURE);
161         }
162         
163         cout << "DCP: " << boost::filesystem::path(argv[optind]).filename().string() << "\n";
164
165         for (list<shared_ptr<DCPReadError> >::const_iterator i = errors.begin(); i != errors.end(); ++i) {
166                 cerr << "Error: " << (*i)->what() << "\n";
167         }
168
169         list<shared_ptr<CPL> > cpls = dcp->cpls ();
170
171         for (list<shared_ptr<CPL> >::iterator i = cpls.begin(); i != cpls.end(); ++i) {
172                 cout << "  CPL: " << (*i)->annotation_text() << "\n";
173                 
174                 list<shared_ptr<Reel> > reels = (*i)->reels ();
175
176                 int R = 1;
177                 for (list<shared_ptr<Reel> >::const_iterator j = reels.begin(); j != reels.end(); ++j) {
178                         cout << "    Reel " << R << "\n";
179
180                         try {
181                                 main_picture (*j);
182                         } catch (UnresolvedRefError& e) {
183                                 if (keep_going) {
184                                         cerr << e.what() << " (for main picture)\n";
185                                 } else {
186                                         throw;
187                                 }
188                         }
189
190                         try {
191                                 main_sound (*j);
192                         } catch (UnresolvedRefError& e) {
193                                 if (keep_going) {
194                                         cerr << e.what() << " (for main sound)\n";
195                                 } else {
196                                         throw;
197                                 }
198                         }
199
200                         try {
201                                 main_subtitle (*j, subtitles);
202                         } catch (UnresolvedRefError& e) {
203                                 if (keep_going) {
204                                         cerr << e.what() << " (for main subtitle)\n";
205                                 } else {
206                                         throw;
207                                 }
208                         }
209
210                         ++R;
211                 }
212         }
213
214         return 0;
215 }