OpenShot Library | libopenshot  0.3.3
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 }
34 
35 // @brief Constructor for Profile.
36 // @param path The folder path / location of a profile file
37 Profile::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
126 std::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)
147 std::string Profile::Key() {
148  std::stringstream output;
149  std::string progressive_str = "p";
150  if (info.interlaced_frame) {
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)
163 std::string Profile::ShortName() {
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(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))
175 std::string Profile::LongName() {
176  std::stringstream output;
177  std::string progressive_str = "p";
178  if (info.interlaced_frame) {
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";
191  if (info.interlaced_frame) {
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 // Save profile to file system
201 void Profile::Save(const std::string& file_path) const {
202  std::ofstream file(file_path);
203  if (!file.is_open()) {
204  throw std::ios_base::failure("Failed to save profile.");
205  }
206 
207  file << "description=" << info.description << "\n";
208  file << "frame_rate_num=" << info.fps.num << "\n";
209  file << "frame_rate_den=" << info.fps.den << "\n";
210  file << "width=" << info.width << "\n";
211  file << "height=" << info.height << "\n";
212  file << "progressive=" << !info.interlaced_frame << "\n"; // Correct the boolean value for progressive/interlaced
213  file << "sample_aspect_num=" << info.pixel_ratio.num << "\n";
214  file << "sample_aspect_den=" << info.pixel_ratio.den << "\n";
215  file << "display_aspect_num=" << info.display_ratio.num << "\n";
216  file << "display_aspect_den=" << info.display_ratio.den << "\n";
217  file << "pixel_format=" << info.pixel_format;
218 
219  file.close();
220 }
221 
222 // Generate JSON string of this object
223 std::string Profile::Json() const {
224 
225  // Return formatted string
226  return JsonValue().toStyledString();
227 }
228 
229 // Generate Json::Value for this object
230 Json::Value Profile::JsonValue() const {
231 
232  // Create root json object
233  Json::Value root;
234  root["description"] = info.description;
235  root["height"] = info.height;
236  root["width"] = info.width;
237  root["pixel_format"] = info.pixel_format;
238  root["fps"] = Json::Value(Json::objectValue);
239  root["fps"]["num"] = info.fps.num;
240  root["fps"]["den"] = info.fps.den;
241  root["pixel_ratio"] = Json::Value(Json::objectValue);
242  root["pixel_ratio"]["num"] = info.pixel_ratio.num;
243  root["pixel_ratio"]["den"] = info.pixel_ratio.den;
244  root["display_ratio"] = Json::Value(Json::objectValue);
245  root["display_ratio"]["num"] = info.display_ratio.num;
246  root["display_ratio"]["den"] = info.display_ratio.den;
247  root["progressive"] = !info.interlaced_frame;
248 
249  // return JsonValue
250  return root;
251 }
252 
253 // Load JSON string into this object
254 void Profile::SetJson(const std::string value) {
255 
256  // Parse JSON string into JSON objects
257  try
258  {
259  const Json::Value root = openshot::stringToJson(value);
260  // Set all values that match
261  SetJsonValue(root);
262  }
263  catch (const std::exception& e)
264  {
265  // Error parsing JSON (or missing keys)
266  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
267  }
268 }
269 
270 // Load Json::Value into this object
271 void Profile::SetJsonValue(const Json::Value root) {
272 
273  if (!root["description"].isNull())
274  info.description = root["description"].asString();
275  if (!root["height"].isNull())
276  info.height = root["height"].asInt();
277  if (!root["width"].isNull())
278  info.width = root["width"].asInt();
279  if (!root["pixel_format"].isNull())
280  info.pixel_format = root["pixel_format"].asInt();
281  if (!root["fps"].isNull()) {
282  info.fps.num = root["fps"]["num"].asInt();
283  info.fps.den = root["fps"]["den"].asInt();
284  }
285  if (!root["pixel_ratio"].isNull()) {
286  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
287  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
289  }
290  if (!root["display_ratio"].isNull()) {
291  info.display_ratio.num = root["display_ratio"]["num"].asInt();
292  info.display_ratio.den = root["display_ratio"]["den"].asInt();
294  }
295  if (!root["progressive"].isNull())
296  info.interlaced_frame = !root["progressive"].asBool();
297 
298 }
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:188
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:35
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:175
openshot::ProfileInfo::interlaced_frame
bool interlaced_frame
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:223
openshot::Profile::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Profiles.cpp:271
openshot::ProfileInfo::description
std::string description
The description of this profile.
Definition: Profiles.h:41
path
path
Definition: FFmpegWriter.cpp:1479
openshot::Profile::info
ProfileInfo info
Profile data stored here.
Definition: Profiles.h:136
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:230
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:201
openshot::Profile::ShortName
std::string ShortName()
Return the name of this profile (1920x1080p29.97)
Definition: Profiles.cpp:163
openshot::Profile::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Profiles.cpp:254
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:147
openshot::Profile::Profile
Profile()
Default Constructor for Profile.
Definition: Profiles.cpp:20
Exceptions.h
Header file for all Exception classes.