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