Debian Codegraph @

src/main.cpp

raw
1 /* dvbcut
2 Copyright (c) 2005 Sven Over <[email protected]>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /* $Id$ */
20
21 #include <cstring>
22 #include <cstdlib>
23 #include <cstdio>
24 #include <cerrno>
25 #include <clocale>
26 #include <string>
27 #include <list>
28
29 #include <unistd.h>
30
31 #ifdef HAVE_LIB_AO
32 #include <ao/ao.h>
33 #endif // HAVE_LIB_AO
34
35 #include <qapplication.h>
36 extern "C" {
37 #include <libavformat/avformat.h>
38 }
39 #include <qimage.h>
40 #include <qsettings.h>
41 #include <qtextcodec.h>
42 #include <QFileInfo>
43 #include <QLibraryInfo>
44 #include <QTranslator>
45
46 #include "dvbcut.h"
47 #include "mpgfile.h"
48 #include "index.h"
49
50 static char *argv0;
51
52 void
53 usage_exit(int rv=1) {
54 fprintf(stderr,
55 "Usage (" VERSION_STRING "):\n"
56 " %s -generateidx [-idx <idxfilename>] [<mpgfilename> ...]\n"
57 " %s -batch [ OPTIONS ] <prjfilename> | <mpgfilename> ...\n\n"
58 "OPTIONS: -cut 4:3|16:9|TS|TS2|<list>, -exp <expfilename>,\n"
59 " -format <num>, -automarker <num>\n\n",
60 argv0, argv0);
61 fprintf(stderr,
62 "If no input files are specified, `dvbcut -generateidx' reads from\n"
63 "standard input. By default, it also writes the index to standard\n"
64 "output, but you can specify another destination with `-idx'.\n\n");
65 fprintf(stderr,
66 "In batch mode you can use `-cut' to keep only 4:3 resp. 16:9 frames or\n"
67 "create automatically alternating START/STOP cut markers for the bookmarks\n"
68 "imported from the input transport stream (TS, TS2) or for a given list of\n"
69 "frame numbers / time stamps (you can use any of ',-|;' as separators).\n"
70 "Without any (valid) cut markers the whole file will be converted!\n\n");
71 fprintf(stderr,
72 "The -exp switch specifies the name of the exported file, with -format\n"
73 "the default export format (0=MPEG program stream/DVD) can be changed and\n"
74 "-automarker sets START/STOP markers at BOF/EOF (0=none,1=BOF,2=EOF,3=both).\n\n");
75 fprintf(stderr,
76 "Options may be abbreviated as long as they remain unambiguous.\n\n");
77 exit(rv);
78 }
79
80 int
81 main(int argc, char *argv[]) {
82 argv0=argv[0];
83 bool generateidx=false;
84 bool batchmode=false, start_bof=true, stop_eof=true;
85 int exportformat=0;
86 std::string idxfilename, expfilename;
87 std::vector<std::string> cutlist;
88 std::list<std::string> filenames;
89 int i;
90
91 /*
92 * process arguments
93 */
94 for (i = 1; i < argc; ++i) {
95 if (argv[i][0] == '-' || argv[i][0] == '+') {
96 // process switches / options
97 size_t n = strlen(argv[i]);
98 if (n == 2 && argv[i][1] == '-') { // POSIX argument separator
99 ++i;
100 break;
101 }
102 else if (strncmp(argv[i], "-batch", n) == 0)
103 batchmode = true;
104 else if (strncmp(argv[i], "-generateidx", n) == 0)
105 generateidx = true;
106 else if (strncmp(argv[i], "-voracious", n) == 0)
107 dvbcut::cache_friendly = false;
108 else if (strncmp(argv[i], "-idx", n) == 0 && (i+1) < argc)
109 idxfilename = argv[++i];
110 else if (strncmp(argv[i], "-exp", n) == 0 && (i+1) < argc)
111 expfilename = argv[++i];
112 else if (strncmp(argv[i], "-format", n) == 0 && (i+1) < argc)
113 exportformat = atoi(argv[++i]);
114 else if (strncmp(argv[i], "-automarker", n) == 0 && (i+1) < argc) {
115 int bofeof = atoi(argv[++i]);
116 start_bof = (bofeof&1)==1;
117 stop_eof = (bofeof&2)==2;
118 } else if (strncmp(argv[i], "-cut", n) == 0 && (i+1) < argc) {
119 char *pch = strtok(argv[++i],",-|;");
120 while(pch) {
121 if(strlen(pch))
122 cutlist.push_back((std::string)pch);
123 pch = strtok(NULL,",-|;");
124 }
125 } else
126 usage_exit();
127 } else
128 // process input files
129 // (that way files also can come first / options last and
130 // argument processing only happens once and only in this loop!)
131 filenames.push_back(std::string(argv[i]));
132 }
133
134 /*
135 * sanity check
136 */
137 if (batchmode && generateidx)
138 usage_exit();
139
140 /*
141 * Generate mode
142 */
143 if (generateidx) {
144 std::string mpgfilename = "<stdin>";
145 std::string errormessage;
146 inbuffer buf(8 << 20, 128 << 20);
147 bool okay = true;
148 if (filenames.empty()) {
149 // no filenames given, read from stdin
150 okay = buf.open(STDIN_FILENO, &errormessage);
151 }
152 else {
153 mpgfilename = filenames.front(); // use first one (for now)
154 if (idxfilename.empty())
155 idxfilename = mpgfilename + ".idx";
156 for(std::list<std::string>::iterator it = filenames.begin();
157 okay && it != filenames.end(); it++)
158 okay = buf.open(*it, &errormessage);
159 }
160 if (!okay) {
161 fprintf(stderr, "%s: %s\n", argv0, errormessage.c_str());
162 return 1;
163 }
164 buf.setsequential(dvbcut::cache_friendly);
165
166 mpgfile *mpg = mpgfile::open(buf, &errormessage);
167 if (mpg == 0) {
168 fprintf(stderr, "%s: %s\n", argv0, errormessage.c_str());
169 return 1;
170 }
171
172 class index idx(*mpg);
173 int pics = idx.generate();
174 if (pics <= 0) {
175 fprintf(stderr, "%s: %s: %s\n", argv0, mpgfilename.c_str(),
176 pics < 0 ? strerror(errno) : "no pictures found");
177 return 1;
178 }
179
180 int rv;
181 if (idxfilename.empty()) {
182 rv = idx.save(STDOUT_FILENO);
183 idxfilename = "<stdout>";
184 }
185 else
186 rv = idx.save(idxfilename.c_str());
187 if (rv < 0) {
188 fprintf(stderr, "%s: %s: %s\n", argv0, idxfilename.c_str(), strerror(errno));
189 return 1;
190 }
191
192 return 0;
193 }
194
195 /*
196 * GUI and batch mode
197 */
198 QApplication a(argc, argv);
199
200 QString locale = QLocale::system().name();
201 QTranslator qt_translator;
202 qt_translator.load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
203 a.installTranslator(&qt_translator);
204
205 QString appDirPath = a.applicationDirPath();
206 QTranslator app_translator;
207 // Search in development directory or system wide.
208 if (!app_translator.load("dvbcut_" + locale, appDirPath))
209 app_translator.load("dvbcut_" + locale, QFileInfo(appDirPath).absolutePath() + "/share/dvbcut");
210 a.installTranslator(&app_translator);
211
212 #ifdef HAVE_LIB_AO
213 ao_initialize();
214 #endif // HAVE_LIB_AO
215
216 int rv=1;
217 dvbcut *main=new dvbcut;
218 main->batchmode(batchmode);
219 main->exportoptions(exportformat,start_bof,stop_eof);
220
221 if (batchmode) {
222 if (filenames.empty()) // must provide at least one filename
223 usage_exit();
224 main->open(filenames,idxfilename,expfilename);
225 if(!cutlist.empty()) {
226 if(cutlist.front()=="AR") { // obsolete (use 4:3 resp. 16:9 instead)! Or just in case of another AR...
227 main->editSuggest();
228 main->editConvert(dvbcut::act_start_stop);
229 } else if(cutlist.front()=="4:3") {
230 main->editSuggest();
231 main->editConvert(dvbcut::act_4_3);
232 } else if(cutlist.front()=="16:9") {
233 main->editSuggest();
234 main->editConvert(dvbcut::act_16_9);
235 } else if(cutlist.front()=="TS" || cutlist.front()=="TS1") { // first bookmark is a START
236 main->editImport();
237 main->editConvert(dvbcut::act_start_stop);
238 } else if(cutlist.front()=="TS2") { // 2nd bookmark is a START
239 main->editImport();
240 main->editConvert(dvbcut::act_stop_start);
241 } else {
242 std::vector<int> piclist, prob_item, prob_pos;
243 unsigned int j;
244 size_t pos;
245 for (j=0; j<cutlist.size(); j++)
246 if((pos=cutlist[j].find_first_not_of("0123456789:./"))==std::string::npos) {
247 if(cutlist[j].find_first_of(":./")!=std::string::npos)
248 piclist.push_back(string2pts(cutlist[j])/main->getTimePerFrame()); // pts divided by 3600(PAL) or 3003(NTSC)
249 else
250 piclist.push_back(atoi(cutlist[j].c_str())); // integers are treated as frame numbers!
251 } else {
252 prob_item.push_back(j);
253 prob_pos.push_back(pos);
254 }
255 if(piclist.size()%2)
256 fprintf(stderr,"*** Cut list contains an odd number of entries! ***\n");
257 if(!prob_item.empty()) {
258 fprintf(stderr,"*** Problems parsing parameter provided with option `-cut'! ***\n");
259 for (j=0; j<prob_item.size(); j++) {
260 fprintf(stderr," '%s' ==> discarded!\n",cutlist[prob_item[j]].c_str());
261 for (i=0; i<5+prob_pos[j]; i++) fprintf(stderr," ");
262 fprintf(stderr,"^\n");
263 }
264 }
265 main->addStartStopItems(piclist);
266 }
267 }
268 main->fileExport();
269 rv = 0;
270 }
271 else {
272 main->show();
273
274 if (!filenames.empty())
275 main->open(filenames,idxfilename,expfilename);
276
277 if (main) {
278 a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
279 rv = a.exec();
280 }
281 }
282
283 #ifdef HAVE_LIB_AO
284 ao_shutdown();
285 #endif // HAVE_LIB_AO
286
287 return rv;
288 }