OpenShot Library | libopenshot 0.3.3
Loading...
Searching...
No Matches
Profiles.cpp
Go to the documentation of this file.
1
9// Copyright (c) 2008-2019 OpenShot Studios, LLC
10//
11// SPDX-License-Identifier: LGPL-3.0-or-later
12
13#include <iomanip>
14#include "Profiles.h"
15#include "Exceptions.h"
16
17using namespace openshot;
18
19// default constructor
21 // Initialize info values
22 info.description = "";
23 info.height = 0;
24 info.width = 0;
26 info.fps.num = 0;
27 info.fps.den = 0;
32 info.interlaced_frame = false;
33}
34
35// @brief Constructor for Profile.
36// @param path The folder path / location of a profile file
37Profile::Profile(std::string path) {
38
39 bool read_file = false;
40
41 // Call default constructor
42 Profile();
43
44 try
45 {
46 QFile inputFile(path.c_str());
47 if (inputFile.open(QIODevice::ReadOnly))
48 {
49 QTextStream in(&inputFile);
50 while (!in.atEnd())
51 {
52 QString line = in.readLine();
53
54 if (line.length() <= 0)
55 continue;
56
57 // Split current line
58 QStringList parts = line.split( "=" );
59 std::string setting = parts[0].toStdString();
60 std::string value = parts[1].toStdString();
61 int value_int = 0;
62
63 // update struct (based on line number)
64 if (setting == "description") {
65 info.description = value;
66 }
67 else if (setting == "frame_rate_num") {
68 std::stringstream(value) >> value_int;
69 info.fps.num = value_int;
70 }
71 else if (setting == "frame_rate_den") {
72 std::stringstream(value) >> value_int;
73 info.fps.den = value_int;
74 }
75 else if (setting == "width") {
76 std::stringstream(value) >> value_int;
77 info.width = value_int;
78 }
79 else if (setting == "height") {
80 std::stringstream(value) >> value_int;
81 info.height = value_int;
82 }
83 else if (setting == "progressive") {
84 std::stringstream(value) >> value_int;
85 info.interlaced_frame = !(bool)value_int;
86 }
87 else if (setting == "sample_aspect_num") {
88 std::stringstream(value) >> value_int;
89 info.pixel_ratio.num = value_int;
90 }
91 else if (setting == "sample_aspect_den") {
92 std::stringstream(value) >> value_int;
93 info.pixel_ratio.den = value_int;
94 }
95 else if (setting == "display_aspect_num") {
96 std::stringstream(value) >> value_int;
97 info.display_ratio.num = value_int;
98 }
99 else if (setting == "display_aspect_den") {
100 std::stringstream(value) >> value_int;
101 info.display_ratio.den = value_int;
102 }
103 else if (setting == "colorspace") {
104 std::stringstream(value) >> value_int;
105 info.pixel_format = value_int;
106 }
107 }
108 read_file = true;
109 inputFile.close();
110 }
111
112 }
113 catch (const std::exception& e)
114 {
115 // Error parsing profile file
116 throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
117 }
118
119 // Throw error if file was not read
120 if (!read_file)
121 // Error parsing profile file
122 throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
123}
124
125// Return a formatted FPS
126std::string Profile::formattedFPS(bool include_decimal) {
127 // Format FPS to use 2 decimals (if needed)
128 float fps = info.fps.ToFloat();
129 std::stringstream fps_string;
130 if (info.fps.den == 1) {
131 // For example: 24.0 will become 24
132 fps_string << std::fixed << std::setprecision(0) << fps;
133 } else {
134 // For example: 29.97002997 will become 29.97
135 fps_string << std::fixed << std::setprecision(2) << fps;
136 // Remove decimal place using QString (for convenience)
137 if (!include_decimal) {
138 QString fps_qstring = QString::fromStdString(fps_string.str());
139 fps_qstring.replace(".", "");
140 fps_string.str(fps_qstring.toStdString());
141 }
142 }
143 return fps_string.str();
144}
145
146// Return a unique key of this profile (01920x1080i2997_16-09)
147std::string Profile::Key() {
148 std::stringstream output;
149 std::string progressive_str = "p";
151 progressive_str = "i";
152 }
153 std::string fps_string = formattedFPS(false);
154 output << std::setfill('0') << std::setw(5) << info.width << std::setfill('\0') << "x";
155 output << std::setfill('0') << std::setw(4) << info.height << std::setfill('\0') << progressive_str;
156 output << std::setfill('0') << std::setw(4) << fps_string << std::setfill('\0') << "_";
157 output << std::setfill('0') << std::setw(2) << info.display_ratio.num << std::setfill('\0') << "-";
158 output << std::setfill('0') << std::setw(2) << info.display_ratio.den << std::setfill('\0');
159 return output.str();
160}
161
162// Return the name of this profile (1920x1080p29.97)
163std::string Profile::ShortName() {
164 std::stringstream output;
165 std::string progressive_str = "p";
167 progressive_str = "i";
168 }
169 std::string fps_string = formattedFPS(true);
170 output << info.width << "x" << info.height << progressive_str << fps_string;
171 return output.str();
172}
173
174// Return a longer format name (1920x1080p @ 29.97 fps (16:9))
175std::string Profile::LongName() {
176 std::stringstream output;
177 std::string progressive_str = "p";
179 progressive_str = "i";
180 }
181 std::string fps_string = formattedFPS(true);
182 output << info.width << "x" << info.height << progressive_str << " @ " << fps_string
183 << " fps (" << info.display_ratio.num << ":" << info.display_ratio.den << ")";
184 return output.str();
185}
186
187// Return a longer format name (1920x1080p @ 29.97 fps (16:9) HD 1080i 29.97 fps)
189 std::stringstream output;
190 std::string progressive_str = "p";
192 progressive_str = "i";
193 }
194 std::string fps_string = formattedFPS(true);
195 output << info.width << "x" << info.height << progressive_str << " @ " << fps_string
196 << " fps (" << info.display_ratio.num << ":" << info.display_ratio.den << ") " << info.description;
197 return output.str();
198}
199
200// Generate JSON string of this object
201std::string Profile::Json() const {
202
203 // Return formatted string
204 return JsonValue().toStyledString();
205}
206
207// Generate Json::Value for this object
208Json::Value Profile::JsonValue() const {
209
210 // Create root json object
211 Json::Value root;
212 root["height"] = info.height;
213 root["width"] = info.width;
214 root["pixel_format"] = info.pixel_format;
215 root["fps"] = Json::Value(Json::objectValue);
216 root["fps"]["num"] = info.fps.num;
217 root["fps"]["den"] = info.fps.den;
218 root["pixel_ratio"] = Json::Value(Json::objectValue);
219 root["pixel_ratio"]["num"] = info.pixel_ratio.num;
220 root["pixel_ratio"]["den"] = info.pixel_ratio.den;
221 root["display_ratio"] = Json::Value(Json::objectValue);
222 root["display_ratio"]["num"] = info.display_ratio.num;
223 root["display_ratio"]["den"] = info.display_ratio.den;
224 root["interlaced_frame"] = info.interlaced_frame;
225
226 // return JsonValue
227 return root;
228}
229
230// Load JSON string into this object
231void Profile::SetJson(const std::string value) {
232
233 // Parse JSON string into JSON objects
234 try
235 {
236 const Json::Value root = openshot::stringToJson(value);
237 // Set all values that match
238 SetJsonValue(root);
239 }
240 catch (const std::exception& e)
241 {
242 // Error parsing JSON (or missing keys)
243 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
244 }
245}
246
247// Load Json::Value into this object
248void Profile::SetJsonValue(const Json::Value root) {
249
250 if (!root["height"].isNull())
251 info.height = root["height"].asInt();
252 if (!root["width"].isNull())
253 info.width = root["width"].asInt();
254 if (!root["pixel_format"].isNull())
255 info.pixel_format = root["pixel_format"].asInt();
256 if (!root["fps"].isNull()) {
257 info.fps.num = root["fps"]["num"].asInt();
258 info.fps.den = root["fps"]["den"].asInt();
259 }
260 if (!root["pixel_ratio"].isNull()) {
261 info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
262 info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
263 }
264 if (!root["display_ratio"].isNull()) {
265 info.display_ratio.num = root["display_ratio"]["num"].asInt();
266 info.display_ratio.den = root["display_ratio"]["den"].asInt();
267 }
268 if (!root["interlaced_frame"].isNull())
269 info.interlaced_frame = root["interlaced_frame"].asBool();
270
271}
Header file for all Exception classes.
Header file for Profile class.
int num
Numerator for the fraction.
Definition Fraction.h:32
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition Fraction.cpp:35
int den
Denominator for the fraction.
Definition Fraction.h:33
Exception for files that can not be found or opened.
Definition Exceptions.h:188
Exception for invalid JSON.
Definition Exceptions.h:218
std::string LongName()
Return a longer format name (1920x1080p @ 29.97 fps (16:9))
Definition Profiles.cpp:175
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition Profiles.cpp:248
std::string ShortName()
Return the name of this profile (1920x1080p29.97)
Definition Profiles.cpp:163
Profile()
Default Constructor for Profile.
Definition Profiles.cpp:20
std::string LongNameWithDesc()
Return a longer format name with description (1920x1080p @ 29.97 fps (16:9) HD 1080i 29....
Definition Profiles.cpp:188
std::string Json() const
Generate JSON string of this object.
Definition Profiles.cpp:201
ProfileInfo info
Profile data stored here.
Definition Profiles.h:136
void SetJson(const std::string value)
Load JSON string into this object.
Definition Profiles.cpp:231
std::string Key()
Return a unique key of this profile with padding (01920x1080i2997_16:09)
Definition Profiles.cpp:147
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition Profiles.cpp:208
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
std::string description
The description of this profile.
Definition Profiles.h:41
int width
The width of the video (in pixels)
Definition Profiles.h:43
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
Definition Profiles.h:46
Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3)
Definition Profiles.h:47
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition Profiles.h:45
int height
The height of the video (in pixels)
Definition Profiles.h:42
int pixel_format
The pixel format (i.e. YUV420P, RGB24, etc...)
Definition Profiles.h:44