forked from CutePoisonX/PoisonConvert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·440 lines (403 loc) · 16.4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//
// Copyright 2015 CutePoisonX ([email protected])
//
// This file is part of PoisonConvert.
//
// PoisonConvert is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// PoisonConvert is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with PoisonConvert. If not, see <http://www.gnu.org/licenses/>.
//
#include <cstdlib>
#include <string>
#include <tclap/CmdLine.h>
#include "VectorSourceManager.h"
#include "UserInterface.h"
#include "ConfigMode.h"
#include "FileManager.h"
#include "FileReadException.h"
#include "SettingsMode.h"
#include "StartMode.h"
#include "AnalyzeMedia.h"
#include "Settings.h"
#include "YesNoSetting.h"
using namespace std;
string const version = "1.4.1";
bool readOutSettingFile(UserInterface& ui, FileManager& filemanager, std::string& settings_error_msg)
{
try
{
if (filemanager.readSettings() == false)
{
settings_error_msg = "There occured an error at reading out the \"Settings\"-file (some settings were corrupt). Please check settings-menu and focus on the red settings.";
return false;
}
} catch (OpenFileException)
{
settings_error_msg = "Created default settings-file \"PoisonConvert_Settings\" in /usr/syno/etc/poisonconvert/. Please check settings-menu.";
try
{
filemanager.saveSettingsToFile();
}
catch (exception)
{
return false;
}
return false;
} catch (exception)
{
settings_error_msg = "There occured an error at reading out the \"Settings\"-file. Please check settings-menu.";
return false;
}
return true;
}
void setCommandLineSetting(UserInterface& ui, SettingsMode& settingsmode, std::string const& settings_value, SettingsMode::SETTING_SPECIFIER set_spec)
{
if (settingsmode.getSettingsParam(set_spec) != settings_value)
{
if (settingsmode.writeParam(settings_value, set_spec, true, false) != Settings::PARAM_CHANGE_SUCCESS)
{
ui.writeString("Warning: Ignoring invalid value for option: " + settingsmode.getSettingsName(set_spec) + ".", true, "yellow");
}
}
}
bool parseCmdLineOptions(int argc, char** argv, UserInterface& ui, FileManager& filemanager, SettingsMode& settingsmode, std::vector<std::string>& config_files)
{
std::string settings_error_msg;
bool delete_file_default;
bool optimize_file_default;
bool reading_out_settings_succeeded = readOutSettingFile(ui, filemanager, settings_error_msg);
try
{
TCLAP::CmdLine cmd("PoisonConvert version " + version + " is part of the MediaWare Factory collection (http://www.mediaware-factory.com).\n"
"If you experience any bugs, please file an issue on github. For any other feedback, requests, etc. please use the contact form at MediaWare Factories homepage.\n"
"Developed by Christoph Ebner and licensed under the GPLv3 (see: https://www.gnu.org/licenses/gpl)."
, ' ', version);
TCLAP::SwitchArg start_arg("s", "start", "Start converting.", false);
TCLAP::MultiArg<std::string> conf_file_arg("c", settingsmode.getSettingsName(SettingsMode::CONFIGNAME), "Config file you want to use.", false, "filename");
TCLAP::ValueArg<std::string> conf_path_arg("p", settingsmode.getSettingsName(SettingsMode::CONFIGLOC), "Path to config files.", false, settingsmode.getSettingsParam(SettingsMode::CONFIGLOC), "path");
TCLAP::ValueArg<std::string> ffmpeg_cmd_arg("F", settingsmode.getSettingsName(SettingsMode::FFMPEG_CMD), "ffmpeg command.", false, settingsmode.getSettingsParam(SettingsMode::FFMPEG_CMD), "path");
TCLAP::ValueArg<std::string> ffprobe_cmd_arg("f", settingsmode.getSettingsName(SettingsMode::FFPROBE_CMD), "ffprobe command.", false, settingsmode.getSettingsParam(SettingsMode::FFPROBE_CMD), "path");
TCLAP::ValueArg<std::string> qt_cmd_arg("q", settingsmode.getSettingsName(SettingsMode::QT_CMD), "qt-faststart command.", false, settingsmode.getSettingsParam(SettingsMode::QT_CMD), "path");
if (settingsmode.getSettingsParam(SettingsMode::DELETESET) == "Yes")
{
delete_file_default = true;
}
else
{
delete_file_default = false;
}
TCLAP::SwitchArg delete_arg("D",settingsmode.getSettingsName(SettingsMode::DELETESET), "Delete original file after conversion.", delete_file_default);
if (settingsmode.getSettingsParam(SettingsMode::OPTIMIZESET) == "Yes")
{
optimize_file_default = true;
}
else
{
optimize_file_default = false;
}
TCLAP::SwitchArg optimize_arg("o", settingsmode.getSettingsName(SettingsMode::OPTIMIZESET), "Optimize file for streaming.", optimize_file_default);
TCLAP::ValueArg<std::string> log_path_arg("l", settingsmode.getSettingsName(SettingsMode::LOGPATH), "Location of logfiles.", false, settingsmode.getSettingsParam(SettingsMode::LOGPATH), "path");
TCLAP::ValueArg<std::string> movie_path_arg("m", settingsmode.getSettingsName(SettingsMode::MOVIEPATH), "Where to look for movies.", false, settingsmode.getSettingsParam(SettingsMode::MOVIEPATH), "path");
TCLAP::ValueArg<std::string> dest_path_arg("d", settingsmode.getSettingsName(SettingsMode::DESTINATION), "Where to save processed movies.", false, settingsmode.getSettingsParam(SettingsMode::DESTINATION), "path");
cmd.add(start_arg);
cmd.add(conf_file_arg);
cmd.add(conf_path_arg);
cmd.add(ffmpeg_cmd_arg);
cmd.add(ffprobe_cmd_arg);
cmd.add(qt_cmd_arg);
cmd.add(delete_arg);
cmd.add(optimize_arg);
cmd.add(log_path_arg);
cmd.add(movie_path_arg);
cmd.add(dest_path_arg);
cmd.parse(argc, argv);
if (reading_out_settings_succeeded)
{
setCommandLineSetting(ui, settingsmode, conf_path_arg.getValue(), SettingsMode::CONFIGLOC);
if (delete_arg.getValue() == true)
{
setCommandLineSetting(ui, settingsmode, "Yes", SettingsMode::DELETESET);
}
if (optimize_arg.getValue() == true)
{
setCommandLineSetting(ui, settingsmode, "Yes", SettingsMode::OPTIMIZESET);
}
setCommandLineSetting(ui, settingsmode, ffmpeg_cmd_arg.getValue(), SettingsMode::FFMPEG_CMD);
setCommandLineSetting(ui, settingsmode, ffprobe_cmd_arg.getValue(), SettingsMode::FFPROBE_CMD);
setCommandLineSetting(ui, settingsmode, qt_cmd_arg.getValue(), SettingsMode::QT_CMD);
setCommandLineSetting(ui, settingsmode, log_path_arg.getValue(), SettingsMode::LOGPATH);
setCommandLineSetting(ui, settingsmode, movie_path_arg.getValue(), SettingsMode::MOVIEPATH);
setCommandLineSetting(ui, settingsmode, dest_path_arg.getValue(), SettingsMode::DESTINATION);
}
else
{
ui.writeString(settings_error_msg, true, "red");
ui.writeString("Ignored any command line options.", true, "yellow");
return false;
}
config_files = conf_file_arg.getValue(); //the config files get checked later if they exist, etc ...
if (start_arg.getValue())
{
return true;
}
else
{
return false;
}
}
catch(std::exception const& except)
{
ui.writeString(except.what(), true, "red");
exit(-1);
}
}
bool readOutPreferenceFile(UserInterface& ui, FileManager& filemanager, VectorSourceManager& vecman)
{
try
{
filemanager.readPreferences();
} catch (OpenFileException)
{
ui.writeString("Can not open config-file. Please check if file is present and you entered filename and path correctly.", true, "red");
ui.writeString("Please go to settings-menu and enter the correct configuration-settings.", true, "red");
vecman.clearAllInstances();
return false;
} catch (exception)
{
ui.writeString("There occured an error at reading out the config-file. It seems that it is corrupt.", true, "red");
ui.writeString("Loaded without any configurations.", true, "red");
vecman.clearAllInstances();
return false;
}
return true;
}
bool executeStartMode(UserInterface& ui, SettingsMode& settingsmode, StartMode& startmode,
VectorSourceManager& vecman, AnalyzeMedia& analyze, bool automatic_mode)
{
bool settings_fail = false;
std::string warning_output_color;
if (automatic_mode)
{
warning_output_color = "red";
}
else
{
warning_output_color = "green";
}
for (int i = 0; i < settingsmode.getVectorLen(); i++)
{
if (settingsmode.checkParam(static_cast<SettingsMode::SETTING_SPECIFIER>(i), false) == Settings::PARAM_CHANGE_ERROR)
{
ui.writeString("Invalid setting: ", false, "red");
ui.writeString(settingsmode.getSettingsName(i), true, "red");
settings_fail = true;
}
}
if (settings_fail == true)
{
ui.writeString("Please enter listed setting(s). Go to 'settings - menu' to do so.", true, warning_output_color);
if (automatic_mode == false)
{
ui.readString(false);
}
return false;
}
if (vecman.getVectorLen(SRCVIDEO) != 0 || vecman.getVectorLen(SRCAUDIO) != 0 ||
vecman.getVectorLen(SRCSUB) != 0)
{
if (settings_fail == false)
{
try
{
startmode.executeCommand();
} catch (exception)
{
ui.writeString("It seems that there occured an unknown error. Please report it, so we can fix this bug together.", true, "red");
analyze.clearAllInstances();
vecman.clearAllInstances();
return false;
}
return true;
}
}
else
{
ui.writeString("You have to specify rules first. Go to 'config - menu' to do so.", true, warning_output_color);
if (automatic_mode == false)
{
ui.readString(false);
}
return false;
}
}
bool startInNormalMode(UserInterface& ui, FileManager& filemanager, VectorSourceManager& vecman,
SettingsMode& settingsmode, StartMode& startmode, AnalyzeMedia& analyze,
ConfigMode& configmode)
{
string input;
bool going_back = true;
if (readOutPreferenceFile(ui, filemanager, vecman) == false)
{
ui.readString(false);
}
ui.writeString("", true);
ui.writeString("**************************************************************", true);
ui.writeString("****************** ");
ui.writeString("WELCOME TO POISONCONVERT", false, "green");
ui.writeString(" ******************", true);
ui.writeString("**************************************************************", true);
ui.writeString("", true);
while (input != "exit")
{
if (going_back == true)
{
ui.writeString("Please select one of the following options:", true, "red");
ui.writeString(" [");
ui.writeString("start", false, "blue");
ui.writeString("] - start conversion with custom settings.", true);
ui.writeString(" [");
ui.writeString("config", false, "blue");
ui.writeString("] - videostream management.", true);
ui.writeString(" [");
ui.writeString("settings", false, "blue");
ui.writeString("] - enter some settings.", true);
ui.writeString(" [");
ui.writeString("exit", false, "blue");
ui.writeString("] - quit program.", true);
ui.writeString("", true);
going_back = false;
}
input = ui.readStringNoCapitalize();
if (input == "start")
{
going_back = executeStartMode(ui, settingsmode, startmode, vecman, analyze, false);
} else if (input == "config")
{
try
{
if( filemanager.checkPathToConfig() == "" )
{
ui.writeString("Warning: You did not specify a path to the config-file in settings. Changes will not be saved.", true, "red");
ui.readString(false);
}
configmode.executeCommand();
if(filemanager.savePreferencesToFile() == -1)
{
ui.writeString("Did not save changes because you did not specify a path to a config-file in settings.", true, "red");
ui.readString(false);
}
} catch (FileWriteException)
{
ui.writeString("Can not write config-file. Please check if poisonconvert has enough rights to do so.", true, "red");
} catch (exception)
{
ui.writeString("It seems that there occured an unknown error. Please report it, so we can fix this bug together.", true, "red");
analyze.clearAllInstances();
vecman.clearAllInstances();
return -1;
}
going_back = true;
} else if (input == "settings")
{
try
{
if (settingsmode.executeCommand() == SettingsMode::SAVE_SETTINGS)
{
filemanager.saveSettingsToFile();
}
} catch (FileWriteException)
{
ui.writeString("Can not write settings-file. Please check if poisonconvert has enough rights to do so.", true, "red");
ui.readString(false);
} catch (exception)
{
ui.writeString("It seems that there occured an unknown error. Please report it, so we can fix this bug together.", true, "red");
analyze.clearAllInstances();
vecman.clearAllInstances();
return -1;
}
if (filemanager.checkPathToConfig() != "")
{
try
{
vecman.clearAllInstances();
filemanager.readPreferences();
} catch (OpenFileException)
{
ui.writeString("Attention: Config file empty or not created (this is normal if you specified a new config-file. Just create some configurations).", true, "red");
ui.readString(false);
} catch (exception)
{
ui.writeString("There occured an error at reading out the config-file. It seems, that it is corrupt.", true, "red");
ui.writeString("Loaded without any configurations.", true, "red");
vecman.clearAllInstances();
ui.readString(false);
}
} else
{
ui.writeString("Warning: You did not specify a path to the config-file in settings. Cannot read/create config-file.", true, "red");
ui.readString(false);
}
going_back = true;
} else if (input != "exit")
{
ui.writeString("Please enter one of the listed options!", true);
}
}
}
int main(int argc, char** argv)
{
bool immediate_start;
std::vector<std::string> config_files;
UserInterface ui;
VectorSourceManager vecman;
AnalyzeMedia analyze;
ConfigMode configmode(ui, vecman);
SettingsMode settingsmode(ui);
FileManager filemanager(settingsmode, vecman, analyze);
StartMode startmode(ui, vecman, filemanager, analyze, settingsmode);
immediate_start = parseCmdLineOptions(argc, argv, ui, filemanager, settingsmode, config_files);
if (immediate_start)
{
if (config_files.empty())
{
config_files.push_back(settingsmode.getSettingsParam(SettingsMode::CONFIGNAME)); //using default config file
}
for (unsigned int config_file_nr = 0; config_file_nr < config_files.size(); ++config_file_nr)
{
std::string config_file = config_files[config_file_nr];
settingsmode.writeParam(config_file, SettingsMode::CONFIGNAME, true, false);
ui.writeString("Processing config-file: " + config_file, true);
if (readOutPreferenceFile(ui, filemanager, vecman) == true)
{
if (executeStartMode(ui, settingsmode, startmode, vecman, analyze, true) == false)
{
return -1;
}
}
ui.writeString("", true);
}
return 0;
}
else if (config_files.size() > 1)
{
ui.writeString("Illegal option: " + settingsmode.getSettingsName(SettingsMode::CONFIGNAME) + ". Multiple config files are only allowed in start-mode (invoke with -s or --start).", true, "red");
return -1;
}
else if (config_files.empty() == false)
{
settingsmode.writeParam(config_files[0], SettingsMode::CONFIGNAME, true, false);
}
analyze.clearAllInstances();
vecman.clearAllInstances();
return startInNormalMode(ui, filemanager, vecman, settingsmode, startmode, analyze, configmode);
}