22 info.description =
"";
25 info.pixel_format = 0;
28 info.pixel_ratio.num = 0;
29 info.pixel_ratio.den = 0;
30 info.display_ratio.num = 0;
31 info.display_ratio.den = 0;
32 info.interlaced_frame =
false;
33 info.spherical =
false;
40 bool read_file =
false;
43 info.description =
"";
46 info.pixel_format = 0;
49 info.pixel_ratio.num = 0;
50 info.pixel_ratio.den = 0;
51 info.display_ratio.num = 0;
52 info.display_ratio.den = 0;
53 info.interlaced_frame =
false;
54 info.spherical =
false;
58 QFile inputFile(
path.c_str());
59 if (inputFile.open(QIODevice::ReadOnly))
61 QTextStream in(&inputFile);
64 QString line = in.readLine();
66 if (line.length() <= 0)
70 QStringList parts = line.split(
"=" );
71 std::string setting = parts[0].toStdString();
72 std::string value = parts[1].toStdString();
76 if (setting ==
"description") {
77 info.description = value;
79 else if (setting ==
"frame_rate_num") {
80 value_int = std::stoi(value);
81 info.fps.num = value_int;
83 else if (setting ==
"frame_rate_den") {
84 value_int = std::stoi(value);
85 info.fps.den = value_int;
87 else if (setting ==
"width") {
88 value_int = std::stoi(value);
89 info.width = value_int;
91 else if (setting ==
"height") {
92 value_int = std::stoi(value);
93 info.height = value_int;
95 else if (setting ==
"progressive") {
96 value_int = std::stoi(value);
97 info.interlaced_frame = !(bool)value_int;
99 else if (setting ==
"sample_aspect_num") {
100 value_int = std::stoi(value);
101 info.pixel_ratio.num = value_int;
103 else if (setting ==
"sample_aspect_den") {
104 value_int = std::stoi(value);
105 info.pixel_ratio.den = value_int;
107 else if (setting ==
"display_aspect_num") {
108 value_int = std::stoi(value);
109 info.display_ratio.num = value_int;
111 else if (setting ==
"display_aspect_den") {
112 value_int = std::stoi(value);
113 info.display_ratio.den = value_int;
115 else if (setting ==
"colorspace") {
116 value_int = std::stoi(value);
117 info.pixel_format = value_int;
119 else if (setting ==
"spherical") {
120 value_int = std::stoi(value);
121 info.spherical = (bool)value_int;
129 catch (
const std::exception& e)
132 throw InvalidFile(
"Profile could not be found or loaded (or is invalid).",
path);
138 throw InvalidFile(
"Profile could not be found or loaded (or is invalid).",
path);
142std::string Profile::formattedFPS(
bool include_decimal) {
144 if (!include_decimal) {
156 std::snprintf(buffer,
sizeof(buffer),
"%04d", fps_code);
157 return std::string(buffer);
161 float fps =
info.fps.ToFloat();
163 if (std::fabs(fps - std::round(fps)) < 0.01) {
164 return std::to_string(
static_cast<int>(std::round(fps)));
168 std::snprintf(buffer,
sizeof(buffer),
"%.2f", fps);
169 return std::string(buffer);
174 std::string raw_fps = formattedFPS(
false);
177 std::string fps_padded = std::string(4 - raw_fps.length(),
'0') + raw_fps;
180 std::snprintf(buffer,
sizeof(buffer),
"%05dx%04d%s%s_%02d-%02d",
183 info.interlaced_frame ?
"i" :
"p",
185 info.display_ratio.num,
186 info.display_ratio.den
189 std::string result(buffer);
197 std::string progressive_str =
info.interlaced_frame ?
"i" :
"p";
198 std::string fps_string = formattedFPS(
true);
199 std::string result = std::to_string(
info.width) +
"x" + std::to_string(
info.height) + progressive_str + fps_string;
208 std::string progressive_str =
info.interlaced_frame ?
"i" :
"p";
209 std::string fps_string = formattedFPS(
true);
210 std::string result = std::to_string(
info.width) +
"x" + std::to_string(
info.height) +
211 progressive_str +
" @ " + fps_string +
212 " fps (" + std::to_string(
info.display_ratio.num) +
":" +
213 std::to_string(
info.display_ratio.den) +
")";
222 std::string progressive_str =
info.interlaced_frame ?
"i" :
"p";
223 std::string fps_string = formattedFPS(
true);
225 std::string result = std::to_string(
info.width) +
"x" + std::to_string(
info.height) +
226 progressive_str +
" @ " + fps_string +
227 " fps (" + std::to_string(
info.display_ratio.num) +
":" +
228 std::to_string(
info.display_ratio.den) +
")";
233 if (!
info.description.empty())
234 result +=
" " +
info.description;
241 std::ofstream file(file_path);
242 if (!file.is_open()) {
243 throw std::ios_base::failure(
"Failed to save profile.");
246 file <<
"description=" <<
info.description <<
"\n";
247 file <<
"frame_rate_num=" <<
info.fps.num <<
"\n";
248 file <<
"frame_rate_den=" <<
info.fps.den <<
"\n";
249 file <<
"width=" <<
info.width <<
"\n";
250 file <<
"height=" <<
info.height <<
"\n";
251 file <<
"progressive=" << !
info.interlaced_frame <<
"\n";
252 file <<
"sample_aspect_num=" <<
info.pixel_ratio.num <<
"\n";
253 file <<
"sample_aspect_den=" <<
info.pixel_ratio.den <<
"\n";
254 file <<
"display_aspect_num=" <<
info.display_ratio.num <<
"\n";
255 file <<
"display_aspect_den=" <<
info.display_ratio.den <<
"\n";
256 file <<
"pixel_format=" <<
info.pixel_format <<
"\n";
257 file <<
"spherical=" <<
info.spherical;
274 root[
"description"] =
info.description;
275 root[
"height"] =
info.height;
276 root[
"width"] =
info.width;
277 root[
"pixel_format"] =
info.pixel_format;
278 root[
"fps"] = Json::Value(Json::objectValue);
279 root[
"fps"][
"num"] =
info.fps.num;
280 root[
"fps"][
"den"] =
info.fps.den;
281 root[
"pixel_ratio"] = Json::Value(Json::objectValue);
282 root[
"pixel_ratio"][
"num"] =
info.pixel_ratio.num;
283 root[
"pixel_ratio"][
"den"] =
info.pixel_ratio.den;
284 root[
"display_ratio"] = Json::Value(Json::objectValue);
285 root[
"display_ratio"][
"num"] =
info.display_ratio.num;
286 root[
"display_ratio"][
"den"] =
info.display_ratio.den;
287 root[
"progressive"] = !
info.interlaced_frame;
288 root[
"spherical"] =
info.spherical;
304 catch (
const std::exception& e)
307 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
314 if (!root[
"description"].isNull())
315 info.description = root[
"description"].asString();
316 if (!root[
"height"].isNull())
317 info.height = root[
"height"].asInt();
318 if (!root[
"width"].isNull())
319 info.width = root[
"width"].asInt();
320 if (!root[
"pixel_format"].isNull())
321 info.pixel_format = root[
"pixel_format"].asInt();
322 if (!root[
"fps"].isNull()) {
323 info.fps.num = root[
"fps"][
"num"].asInt();
324 info.fps.den = root[
"fps"][
"den"].asInt();
326 if (!root[
"pixel_ratio"].isNull()) {
327 info.pixel_ratio.num = root[
"pixel_ratio"][
"num"].asInt();
328 info.pixel_ratio.den = root[
"pixel_ratio"][
"den"].asInt();
329 info.pixel_ratio.Reduce();
331 if (!root[
"display_ratio"].isNull()) {
332 info.display_ratio.num = root[
"display_ratio"][
"num"].asInt();
333 info.display_ratio.den = root[
"display_ratio"][
"den"].asInt();
334 info.display_ratio.Reduce();
336 if (!root[
"progressive"].isNull())
337 info.interlaced_frame = !root[
"progressive"].asBool();
338 if (!root[
"spherical"].isNull())
339 info.spherical = root[
"spherical"].asBool();
Header file for all Exception classes.
Header file for Profile class.
int num
Numerator for the fraction.
int den
Denominator for the fraction.
Exception for files that can not be found or opened.
Exception for invalid JSON.
std::string LongName()
Return a longer format name (1920x1080p @ 29.97 fps (16:9)).
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
std::string ShortName()
Return the name of this profile (1920x1080p29.97).
Profile()
Default Constructor for Profile.
void Save(const std::string &file_path) const
Save profile to a text file (label=value, one per line format).
std::string LongNameWithDesc()
Return a longer format name with description (1920x1080p @ 29.97 fps (16:9) HD 1080i 29....
std::string Json() const
Generate JSON string of this object.
ProfileInfo info
Profile data stored here.
void SetJson(const std::string value)
Load JSON string into this object.
std::string Key()
Return a unique key of this profile with padding (01920x1080i2997_16:09).
Json::Value JsonValue() const
Generate Json::Value for this object.
This namespace is the default namespace for all code in the openshot library.
const Json::Value stringToJson(const std::string value)
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps).