OpenShot Library | libopenshot  0.2.4
Profiles.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for Profile class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include "../include/Profiles.h"
32 
33 using namespace openshot;
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  try
43  {
44  // Initialize info values
45  info.description = "";
46  info.height = 0;
47  info.width = 0;
48  info.pixel_format = 0;
49  info.fps.num = 0;
50  info.fps.den = 0;
51  info.pixel_ratio.num = 0;
52  info.pixel_ratio.den = 0;
55  info.interlaced_frame = false;
56 
57  QFile inputFile(path.c_str());
58  if (inputFile.open(QIODevice::ReadOnly))
59  {
60  QTextStream in(&inputFile);
61  while (!in.atEnd())
62  {
63  QString line = in.readLine();
64 
65  if (line.length() <= 0)
66  continue;
67 
68  // Split current line
69  QStringList parts = line.split( "=" );
70  std::string setting = parts[0].toStdString();
71  std::string value = parts[1].toStdString();
72  int value_int = 0;
73 
74  // update struct (based on line number)
75  if (setting == "description")
76  info.description = value;
77  else if (setting == "frame_rate_num") {
78  std::stringstream(value) >> value_int;
79  info.fps.num = value_int;
80  }
81  else if (setting == "frame_rate_den") {
82  std::stringstream(value) >> value_int;
83  info.fps.den = value_int;
84  }
85  else if (setting == "width") {
86  std::stringstream(value) >> value_int;
87  info.width = value_int;
88  }
89  else if (setting == "height") {
90  std::stringstream(value) >> value_int;
91  info.height = value_int;
92  }
93  else if (setting == "progressive") {
94  std::stringstream(value) >> value_int;
95  info.interlaced_frame = !(bool)value_int;
96  }
97  else if (setting == "sample_aspect_num") {
98  std::stringstream(value) >> value_int;
99  info.pixel_ratio.num = value_int;
100  }
101  else if (setting == "sample_aspect_den") {
102  std::stringstream(value) >> value_int;
103  info.pixel_ratio.den = value_int;
104  }
105  else if (setting == "display_aspect_num") {
106  std::stringstream(value) >> value_int;
107  info.display_ratio.num = value_int;
108  }
109  else if (setting == "display_aspect_den") {
110  std::stringstream(value) >> value_int;
111  info.display_ratio.den = value_int;
112  }
113  else if (setting == "colorspace") {
114  std::stringstream(value) >> value_int;
115  info.pixel_format = value_int;
116  }
117  }
118  read_file = true;
119  inputFile.close();
120  }
121 
122  }
123  catch (const std::exception& e)
124  {
125  // Error parsing profile file
126  throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
127  }
128 
129  // Throw error if file was not read
130  if (!read_file)
131  // Error parsing profile file
132  throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
133 }
134 
135 // Generate JSON string of this object
136 std::string Profile::Json() {
137 
138  // Return formatted string
139  return JsonValue().toStyledString();
140 }
141 
142 // Generate Json::JsonValue for this object
143 Json::Value Profile::JsonValue() {
144 
145  // Create root json object
146  Json::Value root;
147  root["height"] = info.height;
148  root["width"] = info.width;
149  root["pixel_format"] = info.pixel_format;
150  root["fps"] = Json::Value(Json::objectValue);
151  root["fps"]["num"] = info.fps.num;
152  root["fps"]["den"] = info.fps.den;
153  root["pixel_ratio"] = Json::Value(Json::objectValue);
154  root["pixel_ratio"]["num"] = info.pixel_ratio.num;
155  root["pixel_ratio"]["den"] = info.pixel_ratio.den;
156  root["display_ratio"] = Json::Value(Json::objectValue);
157  root["display_ratio"]["num"] = info.display_ratio.num;
158  root["display_ratio"]["den"] = info.display_ratio.den;
159  root["interlaced_frame"] = info.interlaced_frame;
160 
161  // return JsonValue
162  return root;
163 }
164 
165 // Load JSON string into this object
166 void Profile::SetJson(std::string value) {
167 
168  // Parse JSON string into JSON objects
169  Json::Value root;
170  Json::CharReaderBuilder rbuilder;
171  Json::CharReader* reader(rbuilder.newCharReader());
172 
173  std::string errors;
174  bool success = reader->parse( value.c_str(),
175  value.c_str() + value.size(), &root, &errors );
176  delete reader;
177 
178  if (!success)
179  // Raise exception
180  throw InvalidJSON("JSON could not be parsed (or is invalid)");
181 
182  try
183  {
184  // Set all values that match
185  SetJsonValue(root);
186  }
187  catch (const std::exception& e)
188  {
189  // Error parsing JSON (or missing keys)
190  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
191  }
192 }
193 
194 // Load Json::JsonValue into this object
195 void Profile::SetJsonValue(Json::Value root) {
196 
197  if (!root["height"].isNull())
198  info.height = root["height"].asInt();
199  if (!root["width"].isNull())
200  info.width = root["width"].asInt();
201  if (!root["pixel_format"].isNull())
202  info.pixel_format = root["pixel_format"].asInt();
203  if (!root["fps"].isNull()) {
204  info.fps.num = root["fps"]["num"].asInt();
205  info.fps.den = root["fps"]["den"].asInt();
206  }
207  if (!root["pixel_ratio"].isNull()) {
208  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
209  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
210  }
211  if (!root["display_ratio"].isNull()) {
212  info.display_ratio.num = root["display_ratio"]["num"].asInt();
213  info.display_ratio.den = root["display_ratio"]["den"].asInt();
214  }
215  if (!root["interlaced_frame"].isNull())
216  info.interlaced_frame = root["interlaced_frame"].asBool();
217 
218 }
int num
Numerator for the fraction.
Definition: Fraction.h:47
ProfileInfo info
Profile data stored here.
Definition: Profiles.h:86
std::string description
The description of this profile.
Definition: Profiles.h:59
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Profiles.cpp:143
Profile(std::string path)
Constructor for Profile.
Definition: Profiles.cpp:38
Exception for files that can not be found or opened.
Definition: Exceptions.h:173
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Profiles.cpp:195
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square) ...
Definition: Profiles.h:64
int width
The width of the video (in pixels)
Definition: Profiles.h:61
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: Profiles.h:63
std::string Json()
Get and Set JSON methods.
Definition: Profiles.cpp:136
void SetJson(std::string value)
Load JSON string into this object.
Definition: Profiles.cpp:166
int pixel_format
The pixel format (i.e. YUV420P, RGB24, etc...)
Definition: Profiles.h:62
int height
The height of the video (in pixels)
Definition: Profiles.h:60
This namespace is the default namespace for all code in the openshot library.
Exception for invalid JSON.
Definition: Exceptions.h:205
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:65
int den
Denominator for the fraction.
Definition: Fraction.h:48