Assorted test fixes.
[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 #include "common.h"
36
37 using std::string;
38 using std::cerr;
39 using std::cout;
40 using std::list;
41 using boost::shared_ptr;
42 using namespace dcp;
43
44 static void
45 help (string n)
46 {
47         cerr << "Syntax: " << n << " [options] <DCP>\n"
48              << "  -s, --subtitles              list all subtitles\n"
49              << "  -k, --keep-going             carry on in the event of errors, if possible\n"
50              << "      --ignore-missing-assets  ignore missing asset files\n";
51 }
52
53 static void
54 main_picture (shared_ptr<Reel> reel)
55 {
56         if (reel->main_picture() && reel->main_picture()->mxf()) {
57                 cout << "      Picture:  "
58                      << reel->main_picture()->mxf()->size().width
59                      << "x"
60                      << reel->main_picture()->mxf()->size().height << "\n";
61         }
62 }
63
64 static void
65 main_sound (shared_ptr<Reel> reel)
66 {
67         if (reel->main_sound() && reel->main_sound()->mxf()) {
68                 cout << "      Sound:    "
69                      << reel->main_sound()->mxf()->channels()
70                      << " channels at "
71                      << reel->main_sound()->mxf()->sampling_rate() << "Hz\n";
72         }
73 }
74
75 static void
76 main_subtitle (shared_ptr<Reel> reel, bool list_subtitles)
77 {
78         if (!reel->main_subtitle()) {
79                 return;
80         }
81         
82         list<shared_ptr<SubtitleString> > subs = reel->main_subtitle()->subtitle_content()->subtitles ();
83         cout << "      Subtitle: " << subs.size() << " subtitles in " << reel->main_subtitle()->subtitle_content()->language() << "\n";
84         if (list_subtitles) {
85                 for (list<shared_ptr<SubtitleString> >::const_iterator k = subs.begin(); k != subs.end(); ++k) {
86                         cout << "        " << (*k)->text() << "\n";
87                         cout << "          "
88                              << "font:" << (*k)->font() << "; "
89                              << "italic:" << (*k)->italic() << "; "
90                              << "color:" << (*k)->color() << "; "
91                              << "in:" << (*k)->in() << "; "
92                              << "out:" << (*k)->out() << "; "
93                              << "v_position:" << (*k)->v_position() << "; "
94                              << "v_align:" << (*k)->v_align() << "; "
95                              << "effect:" << (*k)->effect() << "; "
96                              << "effect_color:" << (*k)->effect_color() << "; "
97                              << "fade_up_time:" << (*k)->fade_up_time() << "; "
98                              << "fade_down_time:" << (*k)->fade_down_time() << "; "
99                              << "size: " << (*k)->size() << "\n";
100                 }
101         }
102 }
103
104 int
105 main (int argc, char* argv[])
106 {
107         bool subtitles = false;
108         bool keep_going = false;
109         bool ignore_missing_assets = false;
110         
111         int option_index = 0;
112         while (1) {
113                 static struct option long_options[] = {
114                         { "version", no_argument, 0, 'v' },
115                         { "help", no_argument, 0, 'h' },
116                         { "subtitles", no_argument, 0, 's' },
117                         { "keep-going", no_argument, 0, 'k' },
118                         { "ignore-missing-assets", no_argument, 0, 'A' },
119                         { 0, 0, 0, 0 }
120                 };
121
122                 int c = getopt_long (argc, argv, "vhskA", long_options, &option_index);
123
124                 if (c == -1) {
125                         break;
126                 }
127
128                 switch (c) {
129                 case 'v':
130                         cout << "dcpdiff version " << LIBDCP_VERSION << "\n";
131                         exit (EXIT_SUCCESS);
132                 case 'h':
133                         help (argv[0]);
134                         exit (EXIT_SUCCESS);
135                 case 's':
136                         subtitles = true;
137                         break;
138                 case 'k':
139                         keep_going = true;
140                         break;
141                 case 'A':
142                         ignore_missing_assets = true;
143                         break;
144                 }
145         }
146
147         if (argc <= optind || argc > (optind + 1)) {
148                 help (argv[0]);
149                 exit (EXIT_FAILURE);
150         }
151
152         if (!boost::filesystem::exists (argv[optind])) {
153                 cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
154                 exit (EXIT_FAILURE);
155         }
156
157         DCP* dcp = 0;
158         DCP::ReadErrors errors;
159         try {
160                 dcp = new DCP (argv[optind]);
161                 dcp->read (keep_going, &errors);
162         } catch (FileError& e) {
163                 cerr << "Could not read DCP " << argv[optind] << "; " << e.what() << "\n";
164                 exit (EXIT_FAILURE);
165         } catch (DCPReadError& e) {
166                 cerr << "Could not read DCP " << argv[optind] << "; " << e.what() << "\n";
167                 exit (EXIT_FAILURE);
168         }
169         
170         cout << "DCP: " << boost::filesystem::path(argv[optind]).filename().string() << "\n";
171
172         dcp::filter_errors (errors, ignore_missing_assets);
173         for (DCP::ReadErrors::const_iterator i = errors.begin(); i != errors.end(); ++i) {
174                 cerr << "Error: " << (*i)->what() << "\n";
175         }
176
177         list<shared_ptr<CPL> > cpls = dcp->cpls ();
178
179         for (list<shared_ptr<CPL> >::iterator i = cpls.begin(); i != cpls.end(); ++i) {
180                 cout << "  CPL: " << (*i)->annotation_text() << "\n";
181                 
182                 list<shared_ptr<Reel> > reels = (*i)->reels ();
183
184                 int R = 1;
185                 for (list<shared_ptr<Reel> >::const_iterator j = reels.begin(); j != reels.end(); ++j) {
186                         cout << "    Reel " << R << "\n";
187
188                         try {
189                                 main_picture (*j);
190                         } catch (UnresolvedRefError& e) {
191                                 if (keep_going) {
192                                         if (!ignore_missing_assets) {
193                                                 cerr << e.what() << " (for main picture)\n";
194                                         }
195                                 } else {
196                                         throw;
197                                 }
198                         }
199
200                         try {
201                                 main_sound (*j);
202                         } catch (UnresolvedRefError& e) {
203                                 if (keep_going) {
204                                         if (!ignore_missing_assets) {
205                                                 cerr << e.what() << " (for main sound)\n";
206                                         }
207                                 } else {
208                                         throw;
209                                 }
210                         }
211
212                         try {
213                                 main_subtitle (*j, subtitles);
214                         } catch (UnresolvedRefError& e) {
215                                 if (keep_going) {
216                                         if (!ignore_missing_assets) {
217                                                 cerr << e.what() << " (for main subtitle)\n";
218                                         }
219                                 } else {
220                                         throw;
221                                 }
222                         }
223
224                         ++R;
225                 }
226         }
227
228         return 0;
229 }