OpenShot Library | libopenshot  0.2.5
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() const {
137 
138  // Return formatted string
139  return JsonValue().toStyledString();
140 }
141 
142 // Generate Json::Value for this object
143 Json::Value Profile::JsonValue() const {
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(const std::string value) {
167 
168  // Parse JSON string into JSON objects
169  try
170  {
171  const Json::Value root = openshot::stringToJson(value);
172  // Set all values that match
173  SetJsonValue(root);
174  }
175  catch (const std::exception& e)
176  {
177  // Error parsing JSON (or missing keys)
178  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
179  }
180 }
181 
182 // Load Json::Value into this object
183 void Profile::SetJsonValue(const Json::Value root) {
184 
185  if (!root["height"].isNull())
186  info.height = root["height"].asInt();
187  if (!root["width"].isNull())
188  info.width = root["width"].asInt();
189  if (!root["pixel_format"].isNull())
190  info.pixel_format = root["pixel_format"].asInt();
191  if (!root["fps"].isNull()) {
192  info.fps.num = root["fps"]["num"].asInt();
193  info.fps.den = root["fps"]["den"].asInt();
194  }
195  if (!root["pixel_ratio"].isNull()) {
196  info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
197  info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
198  }
199  if (!root["display_ratio"].isNull()) {
200  info.display_ratio.num = root["display_ratio"]["num"].asInt();
201  info.display_ratio.den = root["display_ratio"]["den"].asInt();
202  }
203  if (!root["interlaced_frame"].isNull())
204  info.interlaced_frame = root["interlaced_frame"].asBool();
205 
206 }
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
openshot::ProfileInfo::fps
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: Profiles.h:63
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:38
openshot::ProfileInfo::width
int width
The width of the video (in pixels)
Definition: Profiles.h:61
openshot::ProfileInfo::height
int height
The height of the video (in pixels)
Definition: Profiles.h:60
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:64
openshot::ProfileInfo::pixel_format
int pixel_format
The pixel format (i.e. YUV420P, RGB24, etc...)
Definition: Profiles.h:62
openshot::ProfileInfo::interlaced_frame
bool interlaced_frame
Definition: Profiles.h:66
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:47
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:48
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:65
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:205
openshot::Profile::Json
std::string Json() const
Get and Set JSON methods.
Definition: Profiles.cpp:136
openshot::Profile::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Profiles.cpp:183
openshot::ProfileInfo::description
std::string description
The description of this profile.
Definition: Profiles.h:59
path
path
Definition: FFmpegWriter.cpp:1410
openshot::Profile::info
ProfileInfo info
Profile data stored here.
Definition: Profiles.h:86
openshot::InvalidFile
Exception for files that can not be found or opened.
Definition: Exceptions.h:173
openshot::Profile::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Profiles.cpp:143
openshot::Profile::Profile
Profile(std::string path)
Constructor for Profile.
Definition: Profiles.cpp:38
openshot::Profile::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Profiles.cpp:166