Try to fix seeking with FFmpeg.
[dcpomatic.git] / src / tools / dcpomatic_cli.cc
1 /*
2     Copyright (C) 2012 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 <iomanip>
22 #include <getopt.h>
23 #include <libdcp/version.h>
24 #include "film.h"
25 #include "filter.h"
26 #include "transcode_job.h"
27 #include "job_manager.h"
28 #include "util.h"
29 #include "scaler.h"
30 #include "version.h"
31 #include "cross.h"
32 #include "config.h"
33 #include "log.h"
34
35 using std::string;
36 using std::cerr;
37 using std::cout;
38 using std::vector;
39 using std::pair;
40 using std::list;
41 using boost::shared_ptr;
42
43 static void
44 help (string n)
45 {
46         cerr << "Syntax: " << n << " [OPTION] <FILM>\n"
47              << "  -v, --version      show DCP-o-matic version\n"
48              << "  -h, --help         show this help\n"
49              << "  -d, --deps         list DCP-o-matic dependency details and quit\n"
50              << "  -n, --no-progress  do not print progress to stdout\n"
51              << "  -r, --no-remote    do not use any remote servers\n"
52              << "\n"
53              << "<FILM> is the film directory.\n";
54 }
55
56 int
57 main (int argc, char* argv[])
58 {
59         string film_dir;
60         bool progress = true;
61         bool no_remote = false;
62         int log_level = 0;
63
64         int option_index = 0;
65         while (1) {
66                 static struct option long_options[] = {
67                         { "version", no_argument, 0, 'v'},
68                         { "help", no_argument, 0, 'h'},
69                         { "deps", no_argument, 0, 'd'},
70                         { "no-progress", no_argument, 0, 'n'},
71                         { "no-remote", no_argument, 0, 'r'},
72                         { "log-level", required_argument, 0, 'l' },
73                         { 0, 0, 0, 0 }
74                 };
75
76                 int c = getopt_long (argc, argv, "vhdnrl:", long_options, &option_index);
77
78                 if (c == -1) {
79                         break;
80                 }
81
82                 switch (c) {
83                 case 'v':
84                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
85                         exit (EXIT_SUCCESS);
86                 case 'h':
87                         help (argv[0]);
88                         exit (EXIT_SUCCESS);
89                 case 'd':
90                         cout << dependency_version_summary () << "\n";
91                         exit (EXIT_SUCCESS);
92                 case 'n':
93                         progress = false;
94                         break;
95                 case 'r':
96                         no_remote = true;
97                         break;
98                 case 'l':
99                         log_level = atoi (optarg);
100                         break;
101                 }
102         }
103
104         if (optind >= argc) {
105                 help (argv[0]);
106                 exit (EXIT_FAILURE);
107         }
108
109         film_dir = argv[optind];
110                         
111         dcpomatic_setup ();
112
113         if (no_remote) {
114                 Config::instance()->set_servers (vector<ServerDescription*> ());
115         }
116
117         cout << "DCP-o-matic " << dcpomatic_version << " git " << dcpomatic_git_commit;
118         char buf[256];
119         if (gethostname (buf, 256) == 0) {
120                 cout << " on " << buf;
121         }
122         cout << "\n";
123
124         shared_ptr<Film> film;
125         try {
126                 film.reset (new Film (film_dir));
127                 film->read_metadata ();
128         } catch (std::exception& e) {
129                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
130                 exit (EXIT_FAILURE);
131         }
132
133         film->log()->set_level ((Log::Level) log_level);
134
135         cout << "\nMaking DCP for " << film->name() << "\n";
136 //      cout << "Content: " << film->content() << "\n";
137 //      pair<string, string> const f = Filter::ffmpeg_strings (film->filters ());
138 //      cout << "Filters: " << f.first << " " << f.second << "\n";
139
140         film->make_dcp ();
141
142         bool should_stop = false;
143         bool first = true;
144         bool error = false;
145         while (!should_stop) {
146
147                 dcpomatic_sleep (5);
148
149                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
150
151                 if (!first && progress) {
152                         cout << "\033[" << jobs.size() << "A";
153                         cout.flush ();
154                 }
155
156                 first = false;
157
158                 int unfinished = 0;
159                 int finished_in_error = 0;
160
161                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
162                         if (progress) {
163                                 cout << (*i)->name() << ": ";
164                                 
165                                 float const p = (*i)->overall_progress ();
166                                 
167                                 if (p >= 0) {
168                                         cout << (*i)->status() << "                         \n";
169                                 } else {
170                                         cout << ": Running           \n";
171                                 }
172                         }
173
174                         if (!(*i)->finished ()) {
175                                 ++unfinished;
176                         }
177
178                         if ((*i)->finished_in_error ()) {
179                                 ++finished_in_error;
180                                 error = true;
181                         }
182
183                         if (!progress && (*i)->finished_in_error ()) {
184                                 /* We won't see this error if we haven't been showing progress,
185                                    so show it now.
186                                 */
187                                 cout << (*i)->status() << "\n";
188                         }
189                 }
190
191                 if (unfinished == 0 || finished_in_error != 0) {
192                         should_stop = true;
193                 }
194         }
195
196         return error ? EXIT_FAILURE : EXIT_SUCCESS;
197 }
198
199