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