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