OpenShot Library | libopenshot  0.3.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 "Profiles.h"
14 #include "Exceptions.h"
15 
16 using namespace openshot;
17 
18 
19 // @brief Constructor for Profile.
20 // @param path The folder path / location of a profile file
21 Profile::Profile(std::string path) {
22 
23  bool read_file = false;
24 
25  try
26  {
27  // Initialize info values
28  info.description = "";
29  info.height = 0;
30  info.width = 0;
31  info.pixel_format = 0;
32  info.fps.num = 0;
33  info.fps.den = 0;
34  info.pixel_ratio.num = 0;
35  info.pixel_ratio.den = 0;
38  info.interlaced_frame = false;
39 
40  QFile inputFile(path.c_str());
41  if (inputFile.open(QIODevice::ReadOnly))
42  {
43  QTextStream in(&inputFile);
44  while (!in.atEnd())
45  {
46  QString line = in.readLine();
47 
48  if (line.length() <= 0)
49  continue;
50 
51  // Split current line
52  QStringList parts = line.split( "=" );
53  std::string setting = parts[0].toStdString();
54  std::string value = parts[1].toStdString();
55  int value_int = 0;
56 
57  // update struct (based on line number)
58  if (setting == "description")
59  info.description = value;
60  else if (setting == "frame_rate_num") {
61  std::stringstream(value) >> value_int;
62  info.fps.num = value_int;
63  }
64  else if (setting == "frame_rate_den") {
65  std::stringstream(value) >> value_int;
66  info.fps.den = value_int;
67  }
68  else if (setting == "width") {
69  std::stringstream(value) >> value_int;
70  info.width = value_int;
71  }
72  else if (setting == "height") {
73  std::stringstream(value) >> value_int;
74  info.height = value_int;
75  }
76  else if (setting == "progressive") {
77  std::stringstream(value) >> value_int;
78  info.interlaced_frame = !(bool)value_int;
79  }
80  else if (setting == "sample_aspect_num") {
81  std::stringstream(value) >> value_int;
82  info.pixel_ratio.num = value_int;
83  }
84  else if (setting == "sample_aspect_den") {
85  std::stringstream(value) >> value_int;
86  info.pixel_ratio.den = value_int;
87  }
88  else if (setting == "display_aspect_num") {
89  std::stringstream(value) >> value_int;
90  info.display_ratio.num = value_int;
91  }
92  else if (setting == "display_aspect_den") {
93  std::stringstream(value) >> value_int;
94  info.display_ratio.den = value_int;
95  }
96  else if (setting == "colorspace") {
97  std::stringstream(value) >> value_int;
98  info.pixel_format = value_int;
99  }
100  }
101  read_file = true;
102  inputFile.close();
103  }
104 
105  }
106  catch (const std::exception& e)
107  {
108  // Error parsing profile file
109  throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
110  }
111 
112  // Throw error if file was not read
113  if (!read_file)
114  // Error parsing profile file
115  throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
116 }
117 
118 // Generate JSON string of this object
119 std::string Profile::Json() const {
120 
121  // Return formatted string
122  return JsonValue().toStyledString();
123 }
124 
125 // Generate Json::Value for this object
126 Json::Value Profile::JsonValue() const {
127 
128  // Create root json object
129  Json::Value root;
130  root["height"] = info.height;
131  root["width"] = info.width;
132  root["pixel_format"] = info.pixel_format;
133  root["fps"] = Json::Value(Json::objectValue);
134  root["fps"]["num"] = info.fps.num;
135  root["fps"]["den"] = info.fps.den;
136  root["pixel_ratio"] = Json::Value(Json::objectValue);
137  root["pixel_ratio"]["num"] = info.pixel_ratio.num;
138  root["pixel_ratio"]["den"] = info.pixel_ratio.den;
139  root["display_ratio"] = Json::Value(Json::objectValue);
140  root["display_ratio"]["num"] = info.display_ratio.num;
141  root["display_ratio"]["den"] = info.display_ratio.den;
142  root["interlaced_frame"] = info.interlaced_frame;
143 
144  // return JsonValue
145  return root;
146 }
147 
148 // Load JSON string into this object
149 void Profile::SetJson(const std::string value) {
150 
151  // Parse JSON string into JSON objects
152  try
153  {
154  const Json::Value root = openshot::stringToJson(value);
155  // Set all values that match
156  SetJsonValue(root);
157  }
158  catch (const std::exception& e)
159  {
160  // Error parsing JSON (or missing keys)
161  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
162  }
163 }
164 
165 // Load Json::Value into this object
166 void Profile::SetJsonValue(const Json::Value root) {
167 
168  if (!root["height"].isNull())
169  info.height = root["height"].asInt();
170  if (!root["width"].isNull())
171  info.width = root["width"].asInt();
172  if (!root["pixel_format"].isNull())
173  info.pixel_format = root["pixel_format"].asInt();
174  if (!root["fps"].isNull()) {
175  info.fps.num = root["fps"]["num"].asInt();
176  info.fps.den = root["fps"]["den"].asInt();
177  }
178  if (!root["pixel_ratio"].isNull()) {
179  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
180  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
181  }
182  if (!root["display_ratio"].isNull()) {
183  info.display_ratio.num = root["display_ratio"]["num"].asInt();
184  info.display_ratio.den = root["display_ratio"]["den"].asInt();
185  }
186  if (!root["interlaced_frame"].isNull())
187  info.interlaced_frame = root["interlaced_frame"].asBool();
188 
189 }
int num
Numerator for the fraction.
Definition: Fraction.h:32
ProfileInfo info
Profile data stored here.
Definition: Profiles.h:67
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Profiles.cpp:149
std::string description
The description of this profile.
Definition: Profiles.h:40
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
Profile(std::string path)
Constructor for Profile.
Definition: Profiles.cpp:21
std::string Json() const
Generate JSON string of this object.
Definition: Profiles.cpp:119
Header file for all Exception classes.
Exception for files that can not be found or opened.
Definition: Exceptions.h:187
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: Profiles.h:45
Header file for Profile class.
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Profiles.cpp:126
int width
The width of the video (in pixels)
Definition: Profiles.h:42
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: Profiles.h:44
int pixel_format
The pixel format (i.e. YUV420P, RGB24, etc...)
Definition: Profiles.h:43
int height
The height of the video (in pixels)
Definition: Profiles.h:41
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
Exception for invalid JSON.
Definition: Exceptions.h:217
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Profiles.cpp:166
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:46
int den
Denominator for the fraction.
Definition: Fraction.h:33