-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptions.cpp
More file actions
189 lines (161 loc) · 6.28 KB
/
options.cpp
File metadata and controls
189 lines (161 loc) · 6.28 KB
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
#include <iterator>
#include <primo/avblocks/avb.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <filesystem>
#include "options.h"
#include "program_options.h"
#include "util.h"
namespace fs = std::filesystem;
using namespace std;
using namespace primo::avblocks;
using namespace primo::codecs;
PresetDescriptor avb_presets[] = {
// video presets
{ Preset::Video::DVD::PAL_4x3_MP2, "mpg" },
{ Preset::Video::DVD::PAL_16x9_MP2, "mpg" },
{ Preset::Video::DVD::NTSC_4x3_MP2, "mpg" },
{ Preset::Video::DVD::NTSC_16x9_MP2, "mpg" },
{ Preset::Video::AppleTV::H264_480p, "mp4" },
{ Preset::Video::AppleTV::H264_720p, "mp4" },
{ Preset::Video::AppleTV::MPEG4_480p, "mp4" },
{ Preset::Video::AppleLiveStreaming::WiFi_H264_640x480_30p_1200K_AAC_96K, "ts" },
{ Preset::Video::AppleLiveStreaming::WiFi_Wide_H264_1280x720_30p_4500K_AAC_128K, "ts" },
{ Preset::Video::Generic::MP4::Base_H264_AAC, "mp4" },
{ Preset::Video::iPad::H264_576p, "mp4" },
{ Preset::Video::iPad::H264_720p, "mp4" },
{ Preset::Video::iPad::MPEG4_480p, "mp4" },
{ Preset::Video::iPhone::H264_480p, "mp4" },
{ Preset::Video::iPhone::MPEG4_480p, "mp4" },
{ Preset::Video::iPod::H264_240p, "mp4" },
{ Preset::Video::iPod::MPEG4_240p, "mp4" },
{ Preset::Video::AndroidPhone::H264_360p, "mp4" },
{ Preset::Video::AndroidPhone::H264_720p, "mp4" },
{ Preset::Video::AndroidTablet::H264_720p, "mp4" },
{ Preset::Video::AndroidTablet::WebM_VP8_720p, "webm" },
{ Preset::Video::VCD::PAL, "mpg" },
{ Preset::Video::VCD::NTSC, "mpg" },
{ Preset::Video::Generic::WebM::Base_VP8_Vorbis, "webm" }
};
const int avb_presets_len = sizeof(avb_presets) / sizeof(PresetDescriptor);
void listPresets()
{
cout << "\nPRESETS" << endl;
cout << "-------" << endl;
for (int i=0; i < avb_presets_len; ++i)
{
const PresetDescriptor& preset = avb_presets[i];
cout << left << setw(45) << preset.name << " ." << preset.extension << endl;
}
}
PresetDescriptor* getPresetByName(const char* presetName)
{
for (int i=0; i< avb_presets_len; ++i)
{
PresetDescriptor* preset = &avb_presets[i];
if (0 == strcasecmp(preset->name, presetName))
return preset;
}
return NULL;
}
void help(primo::program_options::OptionsConfig<char>& optcfg)
{
cout << "\nUsage: slideshow --input <directory> --output <file> [--preset <PRESET>]";
cout << " [--presets]";
cout << endl;
primo::program_options::doHelp(cout, optcfg);
}
void setDefaultOptions(Options& opt)
{
opt.input_dir = getExeDir() + "/../../assets/img";
fs::path output(getExeDir() + "/../../output/slideshow");
fs::create_directories(output);
ostringstream s;
s << output.c_str() << "/cube.mp4";
opt.output_file = s.str();
opt.preset = *getPresetByName(Preset::Video::Generic::MP4::Base_H264_AAC);
}
bool validateOptions(Options& opt)
{
if (!opt.preset.name)
{
opt.preset = *getPresetByName(Preset::Video::Generic::MP4::Base_H264_AAC);
}
// fix output: append file extension
if (!opt.output_file.empty() && opt.preset.extension)
{
string::size_type pos = opt.output_file.rfind('.');
if (0 == pos || string::npos == pos)
{
opt.output_file.append(".");
}
else
{
opt.output_file = opt.output_file.substr(0, pos+1);
}
opt.output_file.append(opt.preset.extension);
}
if (opt.output_file.empty())
{
return false;
}
return true;
}
ErrorCodes prepareOptions(Options &opt, int argc, char* argv[])
{
if (argc < 2)
{
setDefaultOptions(opt);
cout << "Using defaults:\n";
cout << " --input " << opt.input_dir;
cout << " --output " << opt.output_file;
cout << " --preset " << opt.preset.name;
cout << endl;
return Parsed;
}
primo::program_options::OptionsConfig<char> optcfg;
optcfg.addOptions()
("help,h", opt.help, "")
("input,i", opt.input_dir, string(), "input directory containing images for the slideshow.")
("output,o", opt.output_file, string(), "output filename (without extension). The extension is added based on the preset.")
("preset,p", opt.preset, PresetDescriptor(), "output preset id. Use --presets to list presets.")
("presets", opt.list_presets,"list presets");
try
{
primo::program_options::scanArgv(optcfg, argc, argv);
}
catch (primo::program_options::ParseFailure<char> &ex)
{
cout << ex.message() << endl;
help(optcfg);
return Error;
}
if (opt.help)
{
help(optcfg);
return Command;
}
if (opt.list_presets)
{
listPresets();
return Command;
}
if (!validateOptions(opt))
{
help(optcfg);
return Error;
}
return Parsed;
}
std::istringstream &operator>>(std::istringstream &in, PresetDescriptor &preset)
{
std::string presetName;
in >> presetName;
PresetDescriptor* presetDesc = getPresetByName(presetName.c_str());
if(!presetDesc)
throw primo::program_options::ParseFailure<char>("", presetName, "Parse error");
preset = *presetDesc;
return in;
}