Merge master.
[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 #include "cpl.h"
12
13 using std::string;
14 using std::cerr;
15 using std::cout;
16 using std::list;
17 using boost::shared_ptr;
18 using namespace libdcp;
19
20 static void
21 help (string n)
22 {
23         cerr << "Syntax: " << n << " [options] <DCP>\n"
24              << "  -s, --subtitles  list all subtitles\n";
25 }
26
27 int
28 main (int argc, char* argv[])
29 {
30         bool subtitles = false;
31         
32         int option_index = 0;
33         while (1) {
34                 static struct option long_options[] = {
35                         { "version", no_argument, 0, 'v'},
36                         { "help", no_argument, 0, 'h'},
37                         { "subtitles", no_argument, 0, 's'},
38                         { 0, 0, 0, 0 }
39                 };
40
41                 int c = getopt_long (argc, argv, "vhs", long_options, &option_index);
42
43                 if (c == -1) {
44                         break;
45                 }
46
47                 switch (c) {
48                 case 'v':
49                         cout << "dcpdiff version " << LIBDCP_VERSION << "\n";
50                         exit (EXIT_SUCCESS);
51                 case 'h':
52                         help (argv[0]);
53                         exit (EXIT_SUCCESS);
54                 case 's':
55                         subtitles = true;
56                         break;
57                 }
58         }
59
60         if (argc <= optind || argc > (optind + 1)) {
61                 help (argv[0]);
62                 exit (EXIT_FAILURE);
63         }
64
65         if (!boost::filesystem::exists (argv[optind])) {
66                 cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
67                 exit (EXIT_FAILURE);
68         }
69
70         DCP* dcp = 0;
71         try {
72                 dcp = new DCP (argv[optind]);
73                 dcp->read (false);
74         } catch (FileError& e) {
75                 cerr << "Could not read DCP " << argv[optind] << "; " << e.what() << " " << e.filename() << "\n";
76                 exit (EXIT_FAILURE);
77         }
78
79         cout << "DCP: " << argv[optind] << "\n";
80
81         list<shared_ptr<const CPL> > cpls = dcp->cpls ();
82
83         for (list<shared_ptr<const CPL> >::iterator i = cpls.begin(); i != cpls.end(); ++i) {
84                 cout << "  CPL: " << (*i)->name() << "\n"
85                      << "    Length: " << (*i)->length() << "\n"
86                      << "    Frames per second: " << (*i)->frames_per_second() << "\n";
87                 
88                 list<shared_ptr<const Reel> > reels = (*i)->reels ();
89
90                 int R = 1;
91                 for (list<shared_ptr<const Reel> >::const_iterator j = reels.begin(); j != reels.end(); ++j) {
92                         cout << "    Reel " << R << "\n";
93                         
94                         if ((*j)->main_picture()) {
95                                 cout << "      Picture:  " << (*j)->main_picture()->size().width << "x" << (*j)->main_picture()->size().height << "\n";
96                         }
97                         if ((*j)->main_sound()) {
98                                 cout << "      Sound:    " << (*j)->main_sound()->channels() << " channels at " << (*j)->main_sound()->sampling_rate() << "Hz\n";
99                         }
100                         if ((*j)->main_subtitle()) {
101                                 list<shared_ptr<Subtitle> > subs = (*j)->main_subtitle()->subtitles ();
102                                 cout << "      Subtitle: " << subs.size() << " subtitles in " << (*j)->main_subtitle()->language() << "\n";
103                                 if (subtitles) {
104                                         for (list<shared_ptr<Subtitle> >::const_iterator k = subs.begin(); k != subs.end(); ++k) {
105                                                 cout << "        " << (*k)->text() << "\n";
106                                                 cout << "          "
107                                                      << "font:" << (*k)->font() << "; "
108                                                      << "italic:" << (*k)->italic() << "; "
109                                                      << "color:" << (*k)->color() << "; "
110                                                      << "in:" << (*k)->in() << "; "
111                                                      << "out:" << (*k)->out() << "; "
112                                                      << "v_position:" << (*k)->v_position() << "; "
113                                                      << "v_align:" << (*k)->v_align() << "; "
114                                                      << "effect:" << (*k)->effect() << "; "
115                                                      << "effect_color:" << (*k)->effect_color() << "; "
116                                                      << "fade_up_time:" << (*k)->fade_up_time() << "; "
117                                                      << "fade_down_time:" << (*k)->fade_down_time() << "; "
118                                                      << "size: " << (*k)->size() << "\n";
119                                         }
120                                 }
121                         }
122                         ++R;
123                 }
124         }
125
126         return 0;
127 }