OpenShot Library | libopenshot  0.4.0
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 
17 using namespace openshot;
18 
19 // default constructor
21  // Initialize info values
22  info.description = "";
23  info.height = 0;
24  info.width = 0;
25  info.pixel_format = 0;
26  info.fps.num = 0;
27  info.fps.den = 0;
28  info.pixel_ratio.num = 0;
29  info.pixel_ratio.den = 0;
32  info.interlaced_frame = false;
33  info.spherical = false; // Default to non-spherical (regular) video
34 }
35 
36 // @brief Constructor for Profile.
37 // @param path The folder path / location of a profile file
38 Profile::Profile(std::string path) {
39 
40  bool read_file = false;
41 
42  // Initialize all values to defaults (same as default constructor)
43  info.description = "";
44  info.height = 0;
45  info.width = 0;
46  info.pixel_format = 0;
47  info.fps.num = 0;
48  info.fps.den = 0;
49  info.pixel_ratio.num = 0;
50  info.pixel_ratio.den = 0;
53  info.interlaced_frame = false;
54  info.spherical = false; // Default to non-spherical (regular) video
55 
56  try
57  {
58  QFile inputFile(path.c_str());
59  if (inputFile.open(QIODevice::ReadOnly))
60  {
61  QTextStream in(&inputFile);
62  while (!in.atEnd())
63  {
64  QString line = in.readLine();
65 
66  if (line.length() <= 0)
67  continue;
68 
69  // Split current line
70  QStringList parts = line.split( "=" );
71  std::string setting = parts[0].toStdString();
72  std::string value = parts[1].toStdString();
73  int value_int = 0;
74 
75  // update struct (based on line number)
76  if (setting == "description") {
77  info.description = value;
78  }
79  else if (setting == "frame_rate_num") {
80  std::stringstream(value) >> value_int;
81  info.fps.num = value_int;
82  }
83  else if (setting == "frame_rate_den") {
84  std::stringstream(value) >> value_int;
85  info.fps.den = value_int;
86  }
87  else if (setting == "width") {
88  std::stringstream(value) >> value_int;
89  info.width = value_int;
90  }
91  else if (setting == "height") {
92  std::stringstream(value) >> value_int;
93  info.height = value_int;
94  }
95  else if (setting == "progressive") {
96  std::stringstream(value) >> value_int;
97  info.interlaced_frame = !(bool)value_int;
98  }
99  else if (setting == "sample_aspect_num") {
100  std::stringstream(value) >> value_int;
101  info.pixel_ratio.num = value_int;
102  }
103  else if (setting == "sample_aspect_den") {
104  std::stringstream(value) >> value_int;
105  info.pixel_ratio.den = value_int;
106  }
107  else if (setting == "display_aspect_num") {
108  std::stringstream(value) >> value_int;
109  info.display_ratio.num = value_int;
110  }
111  else if (setting == "display_aspect_den") {
112  std::stringstream(value) >> value_int;
113  info.display_ratio.den = value_int;
114  }
115  else if (setting == "colorspace") {
116  std::stringstream(value) >> value_int;
117  info.pixel_format = value_int;
118  }
119  else if (setting == "spherical") {
120  std::stringstream(value) >> value_int;
121  info.spherical = (bool)value_int;
122  }
123  }
124  read_file = true;
125  inputFile.close();
126  }
127 
128  }
129  catch (const std::exception& e)
130  {
131  // Error parsing profile file
132  throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
133  }
134 
135  // Throw error if file was not read
136  if (!read_file)
137  // Error parsing profile file
138  throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
139 }
140 
141 // Return a formatted FPS
142 std::string Profile::formattedFPS(bool include_decimal) {
143  // Format FPS to use 2 decimals (if needed)
144  float fps = info.fps.ToFloat();
145  std::stringstream fps_string;
146  if (info.fps.den == 1) {
147  // For example: 24.0 will become 24
148  fps_string << std::fixed << std::setprecision(0) << fps;
149  } else {
150  // For example: 29.97002997 will become 29.97
151  fps_string << std::fixed << std::setprecision(2) << fps;
152  // Remove decimal place using QString (for convenience)
153  if (!include_decimal) {
154  QString fps_qstring = QString::fromStdString(fps_string.str());
155  fps_qstring.replace(".", "");
156  fps_string.str(fps_qstring.toStdString());
157  }
158  }
159  return fps_string.str();
160 }
161 
162 // Return a unique key of this profile (01920x1080i2997_16-09)
163 std::string Profile::Key() {
164  std::stringstream output;
165  std::string progressive_str = "p";
166  if (info.interlaced_frame) {
167  progressive_str = "i";
168  }
169  std::string fps_string = formattedFPS(false);
170  output << std::setfill('0') << std::setw(5) << info.width << std::setfill('\0') << "x";
171  output << std::setfill('0') << std::setw(4) << info.height << std::setfill('\0') << progressive_str;
172  output << std::setfill('0') << std::setw(4) << fps_string << std::setfill('\0') << "_";
173  output << std::setfill('0') << std::setw(2) << info.display_ratio.num << std::setfill('\0') << "-";
174  output << std::setfill('0') << std::setw(2) << info.display_ratio.den << std::setfill('\0');
175 
176  // Add spherical indicator
177  if (info.spherical) {
178  output << "_360";
179  }
180 
181  return output.str();
182 }
183 
184 // Return the name of this profile (1920x1080p29.97)
185 std::string Profile::ShortName() {
186  std::stringstream output;
187  std::string progressive_str = "p";
188  if (info.interlaced_frame) {
189  progressive_str = "i";
190  }
191  std::string fps_string = formattedFPS(true);
192  output << info.width << "x" << info.height << progressive_str << fps_string;
193 
194  // Add 360° indicator for spherical videos
195  if (info.spherical) {
196  output << " 360°";
197  }
198 
199  return output.str();
200 }
201 
202 // Return a longer format name (1920x1080p @ 29.97 fps (16:9))
203 std::string Profile::LongName() {
204  std::stringstream output;
205  std::string progressive_str = "p";
206  if (info.interlaced_frame) {
207  progressive_str = "i";
208  }
209  std::string fps_string = formattedFPS(true);
210  output << info.width << "x" << info.height << progressive_str << " @ " << fps_string
211  << " fps (" << info.display_ratio.num << ":" << info.display_ratio.den << ")";
212 
213  // Add 360° indicator for spherical videos
214  if (info.spherical) {
215  output << " 360°";
216  }
217 
218  return output.str();
219 }
220 
221 // Return a longer format name (1920x1080p @ 29.97 fps (16:9) HD 1080i 29.97 fps)
223  std::stringstream output;
224  std::string progressive_str = "p";
225  if (info.interlaced_frame) {
226  progressive_str = "i";
227  }
228  std::string fps_string = formattedFPS(true);
229  output << info.width << "x" << info.height << progressive_str << " @ " << fps_string
230  << " fps (" << info.display_ratio.num << ":" << info.display_ratio.den << ")";
231 
232  // Add 360° indicator for spherical videos
233  if (info.spherical) {
234  output << " 360°";
235  }
236 
237  output << " " << info.description;
238  return output.str();
239 }
240 
241 // Save profile to file system
242 void Profile::Save(const std::string& file_path) const {
243  std::ofstream file(file_path);
244  if (!file.is_open()) {
245  throw std::ios_base::failure("Failed to save profile.");
246  }
247 
248  file << "description=" << info.description << "\n";
249  file << "frame_rate_num=" << info.fps.num << "\n";
250  file << "frame_rate_den=" << info.fps.den << "\n";
251  file << "width=" << info.width << "\n";
252  file << "height=" << info.height << "\n";
253  file << "progressive=" << !info.interlaced_frame << "\n"; // Correct the boolean value for progressive/interlaced
254  file << "sample_aspect_num=" << info.pixel_ratio.num << "\n";
255  file << "sample_aspect_den=" << info.pixel_ratio.den << "\n";
256  file << "display_aspect_num=" << info.display_ratio.num << "\n";
257  file << "display_aspect_den=" << info.display_ratio.den << "\n";
258  file << "pixel_format=" << info.pixel_format << "\n";
259  file << "spherical=" << info.spherical;
260 
261  file.close();
262 }
263 
264 // Generate JSON string of this object
265 std::string Profile::Json() const {
266 
267  // Return formatted string
268  return JsonValue().toStyledString();
269 }
270 
271 // Generate Json::Value for this object
272 Json::Value Profile::JsonValue() const {
273 
274  // Create root json object
275  Json::Value root;
276  root["description"] = info.description;
277  root["height"] = info.height;
278  root["width"] = info.width;
279  root["pixel_format"] = info.pixel_format;
280  root["fps"] = Json::Value(Json::objectValue);
281  root["fps"]["num"] = info.fps.num;
282  root["fps"]["den"] = info.fps.den;
283  root["pixel_ratio"] = Json::Value(Json::objectValue);
284  root["pixel_ratio"]["num"] = info.pixel_ratio.num;
285  root["pixel_ratio"]["den"] = info.pixel_ratio.den;
286  root["display_ratio"] = Json::Value(Json::objectValue);
287  root["display_ratio"]["num"] = info.display_ratio.num;
288  root["display_ratio"]["den"] = info.display_ratio.den;
289  root["progressive"] = !info.interlaced_frame;
290  root["spherical"] = info.spherical;
291 
292  // return JsonValue
293  return root;
294 }
295 
296 // Load JSON string into this object
297 void Profile::SetJson(const std::string value) {
298 
299  // Parse JSON string into JSON objects
300  try
301  {
302  const Json::Value root = openshot::stringToJson(value);
303  // Set all values that match
304  SetJsonValue(root);
305  }
306  catch (const std::exception& e)
307  {
308  // Error parsing JSON (or missing keys)
309  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
310  }
311 }
312 
313 // Load Json::Value into this object
314 void Profile::SetJsonValue(const Json::Value root) {
315 
316  if (!root["description"].isNull())
317  info.description = root["description"].asString();
318  if (!root["height"].isNull())
319  info.height = root["height"].asInt();
320  if (!root["width"].isNull())
321  info.width = root["width"].asInt();
322  if (!root["pixel_format"].isNull())
323  info.pixel_format = root["pixel_format"].asInt();
324  if (!root["fps"].isNull()) {
325  info.fps.num = root["fps"]["num"].asInt();
326  info.fps.den = root["fps"]["den"].asInt();
327  }
328  if (!root["pixel_ratio"].isNull()) {
329  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
330  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
332  }
333  if (!root["display_ratio"].isNull()) {
334  info.display_ratio.num = root["display_ratio"]["num"].asInt();
335  info.display_ratio.den = root["display_ratio"]["den"].asInt();
337  }
338  if (!root["progressive"].isNull())
339  info.interlaced_frame = !root["progressive"].asBool();
340  if (!root["spherical"].isNull())
341  info.spherical = root["spherical"].asBool();
342 
343 }
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::Profile::LongNameWithDesc
std::string LongNameWithDesc()
Return a longer format name with description (1920x1080p @ 29.97 fps (16:9) HD 1080i 29....
Definition: Profiles.cpp:222
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:35
openshot::ProfileInfo::spherical
bool spherical
Is this video a spherical/360° video.
Definition: Profiles.h:49
openshot::ProfileInfo::fps
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: Profiles.h:45
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::ProfileInfo::width
int width
The width of the video (in pixels)
Definition: Profiles.h:43
openshot::ProfileInfo::height
int height
The height of the video (in pixels)
Definition: Profiles.h:42
openshot::ProfileInfo::pixel_ratio
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
Definition: Profiles.h:46
openshot::ProfileInfo::pixel_format
int pixel_format
The pixel format (i.e. YUV420P, RGB24, etc...)
Definition: Profiles.h:44
openshot::Profile::LongName
std::string LongName()
Return a longer format name (1920x1080p @ 29.97 fps (16:9))
Definition: Profiles.cpp:203
openshot::ProfileInfo::interlaced_frame
bool interlaced_frame
Are the contents of this frame interlaced.
Definition: Profiles.h:48
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:32
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:33
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:65
openshot::ProfileInfo::display_ratio
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
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::Profile::Json
std::string Json() const
Generate JSON string of this object.
Definition: Profiles.cpp:265
openshot::Profile::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Profiles.cpp:314
openshot::ProfileInfo::description
std::string description
The description of this profile.
Definition: Profiles.h:41
path
path
Definition: FFmpegWriter.cpp:1476
openshot::Profile::info
ProfileInfo info
Profile data stored here.
Definition: Profiles.h:137
openshot::InvalidFile
Exception for files that can not be found or opened.
Definition: Exceptions.h:187
openshot::Profile::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Profiles.cpp:272
openshot::Profile::Save
void Save(const std::string &file_path) const
Save profile to a text file (label=value, one per line format)
Definition: Profiles.cpp:242
openshot::Profile::ShortName
std::string ShortName()
Return the name of this profile (1920x1080p29.97)
Definition: Profiles.cpp:185
openshot::Profile::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Profiles.cpp:297
Profiles.h
Header file for Profile class.
openshot::Profile::Key
std::string Key()
Return a unique key of this profile with padding (01920x1080i2997_16:09)
Definition: Profiles.cpp:163
openshot::Profile::Profile
Profile()
Default Constructor for Profile.
Definition: Profiles.cpp:20
Exceptions.h
Header file for all Exception classes.