List subtitles in test output.
[libdcp.git] / tools / dcpinfo.cc
1 #include <iostream>
2 #include <cstdlib>
3 #include <boost/filesystem.hpp>
4 #include <getopt.h>
5 #include "dcp.h"
6 #include "exceptions.h"
7 #include "reel.h"
8 #include "sound_asset.h"
9 #include "picture_asset.h"
10 #include "subtitle_asset.h"
11
12 using namespace std;
13 using namespace boost;
14 using namespace libdcp;
15
16 static void
17 help (string n)
18 {
19         cerr << "Syntax: " << n << " [options] <DCP>\n"
20              << "  -s, --subtitles  list all subtitles\n";
21 }
22
23 int
24 main (int argc, char* argv[])
25 {
26         bool subtitles = false;
27         
28         int option_index = 0;
29         while (1) {
30                 static struct option long_options[] = {
31                         { "version", no_argument, 0, 'v'},
32                         { "help", no_argument, 0, 'h'},
33                         { "subtitles", no_argument, 0, 's'},
34                         { 0, 0, 0, 0 }
35                 };
36
37                 int c = getopt_long (argc, argv, "vhs", long_options, &option_index);
38
39                 if (c == -1) {
40                         break;
41                 }
42
43                 switch (c) {
44                 case 'v':
45                         cout << "dcpdiff version " << LIBDCP_VERSION << "\n";
46                         exit (EXIT_SUCCESS);
47                 case 'h':
48                         help (argv[0]);
49                         exit (EXIT_SUCCESS);
50                 case 's':
51                         subtitles = true;
52                         break;
53                 }
54         }
55
56         if (argc <= optind || argc > (optind + 1)) {
57                 help (argv[0]);
58                 exit (EXIT_FAILURE);
59         }
60
61         if (!filesystem::exists (argv[optind])) {
62                 cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
63                 exit (EXIT_FAILURE);
64         }
65
66         DCP* dcp = 0;
67         try {
68                 dcp = new DCP (argv[optind]);
69                 dcp->read (false);
70         } catch (FileError& e) {
71                 cerr << "Could not read DCP " << argv[optind] << "; " << e.what() << " " << e.filename() << "\n";
72                 exit (EXIT_FAILURE);
73         }
74
75         cout << "DCP: " << argv[optind] << "\n";
76
77         list<shared_ptr<const CPL> > cpls = dcp->cpls ();
78
79         for (list<shared_ptr<const CPL> >::iterator i = cpls.begin(); i != cpls.end(); ++i) {
80                 cout << "  CPL: " << (*i)->name() << "\n"
81                      << "    Length: " << (*i)->length() << "\n"
82                      << "    Frames per second: " << (*i)->frames_per_second() << "\n";
83                 
84                 list<shared_ptr<const Reel> > reels = (*i)->reels ();
85
86                 int R = 1;
87                 for (list<shared_ptr<const Reel> >::const_iterator j = reels.begin(); j != reels.end(); ++j) {
88                         cout << "    Reel " << R << "\n";
89                         
90                         if ((*j)->main_picture()) {
91                                 cout << "      Picture:  " << (*j)->main_picture()->width() << "x" << (*j)->main_picture()->height() << "\n";
92                         }
93                         if ((*j)->main_sound()) {
94                                 cout << "      Sound:    " << (*j)->main_sound()->channels() << " channels at " << (*j)->main_sound()->sampling_rate() << "Hz\n";
95                         }
96                         if ((*j)->main_subtitle()) {
97                                 list<shared_ptr<Subtitle> > subs = (*j)->main_subtitle()->subtitles ();
98                                 cout << "      Subtitle: " << subs.size() << " subtitles in " << (*j)->main_subtitle()->language() << "\n";
99                                 if (subtitles) {
100                                         for (list<shared_ptr<Subtitle> >::const_iterator k = subs.begin(); k != subs.end(); ++k) {
101                                                 cout << "        " << (*k)->text() << "\n";
102                                         }
103                                 }
104                         }
105                         ++R;
106                 }
107         }
108
109         return 0;
110 }