Profiles.cpp
Go to the documentation of this file.
/**
* @file
* @brief Source file for Profile class
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @section LICENSE
*
* Copyright (c) 2008-2014 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Library (libopenshot), an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Library (libopenshot) is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenShot Library (libopenshot) is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../include/Profiles.h"
using namespace openshot;
// @brief Constructor for Profile.
// @param path The folder path / location of a profile file
Profile::Profile(string path) {
bool read_file = false;
try
{
// Initialize info values
info.description = "";
info.height = 0;
info.width = 0;
info.pixel_format = 0;
info.fps.num = 0;
info.fps.den = 0;
info.pixel_ratio.num = 0;
info.pixel_ratio.den = 0;
info.display_ratio.num = 0;
info.display_ratio.den = 0;
info.interlaced_frame = false;
QFile inputFile(path.c_str());
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
while (!in.atEnd())
{
QString line = in.readLine();
if (line.length() <= 0)
continue;
// Split current line
QStringList parts = line.split( "=" );
string setting = parts[0].toStdString();
string value = parts[1].toStdString();
int value_int = 0;
// update struct (based on line number)
if (setting == "description")
info.description = value;
else if (setting == "frame_rate_num") {
stringstream(value) >> value_int;
info.fps.num = value_int;
}
else if (setting == "frame_rate_den") {
stringstream(value) >> value_int;
info.fps.den = value_int;
}
else if (setting == "width") {
stringstream(value) >> value_int;
info.width = value_int;
}
else if (setting == "height") {
stringstream(value) >> value_int;
info.height = value_int;
}
else if (setting == "progressive") {
stringstream(value) >> value_int;
info.interlaced_frame = !(bool)value_int;
}
else if (setting == "sample_aspect_num") {
stringstream(value) >> value_int;
info.pixel_ratio.num = value_int;
}
else if (setting == "sample_aspect_den") {
stringstream(value) >> value_int;
info.pixel_ratio.den = value_int;
}
else if (setting == "display_aspect_num") {
stringstream(value) >> value_int;
info.display_ratio.num = value_int;
}
else if (setting == "display_aspect_den") {
stringstream(value) >> value_int;
info.display_ratio.den = value_int;
}
else if (setting == "colorspace") {
stringstream(value) >> value_int;
info.pixel_format = value_int;
}
}
read_file = true;
inputFile.close();
}
}
catch (exception e)
{
// Error parsing profile file
throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
}
// Throw error if file was not read
if (!read_file)
// Error parsing profile file
throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
}
// Generate JSON string of this object
string Profile::Json() {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::JsonValue for this object
Json::Value Profile::JsonValue() {
// Create root json object
Json::Value root;
root["height"] = info.height;
root["width"] = info.width;
root["pixel_format"] = info.pixel_format;
root["fps"] = Json::Value(Json::objectValue);
root["fps"]["num"] = info.fps.num;
root["fps"]["den"] = info.fps.den;
root["pixel_ratio"] = Json::Value(Json::objectValue);
root["pixel_ratio"]["num"] = info.pixel_ratio.num;
root["pixel_ratio"]["den"] = info.pixel_ratio.den;
root["display_ratio"] = Json::Value(Json::objectValue);
root["display_ratio"]["num"] = info.display_ratio.num;
root["display_ratio"]["den"] = info.display_ratio.den;
root["interlaced_frame"] = info.interlaced_frame;
// return JsonValue
return root;
}
// Load JSON string into this object
void Profile::SetJson(string value) {
// Parse JSON string into JSON objects
Json::Value root;
Json::Reader reader;
bool success = reader.parse( value, root );
if (!success)
// Raise exception
throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
try
{
// Set all values that match
SetJsonValue(root);
}
catch (exception e)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
}
}
// Load Json::JsonValue into this object
void Profile::SetJsonValue(Json::Value root) {
if (!root["height"].isNull())
info.height = root["height"].asInt();
if (!root["width"].isNull())
info.width = root["width"].asInt();
if (!root["pixel_format"].isNull())
info.pixel_format = root["pixel_format"].asInt();
if (!root["fps"].isNull()) {
info.fps.num = root["fps"]["num"].asInt();
info.fps.den = root["fps"]["den"].asInt();
}
if (!root["pixel_ratio"].isNull()) {
info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
}
if (!root["display_ratio"].isNull()) {
info.display_ratio.num = root["display_ratio"]["num"].asInt();
info.display_ratio.den = root["display_ratio"]["den"].asInt();
}
if (!root["interlaced_frame"].isNull())
info.interlaced_frame = root["interlaced_frame"].asBool();
}
openshot::Profile::Profile
Profile(string path)
Constructor for Profile.
Definition: Profiles.cpp:35
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:44
openshot::Profile::info
ProfileInfo info
Profile data stored here.
Definition: Profiles.h:85
openshot::ProfileInfo::interlaced_frame
bool interlaced_frame
Definition: Profiles.h:65
openshot::ProfileInfo::description
string description
The description of this profile.
Definition: Profiles.h:58
OpenShot Wipe Tests.e
e
Definition: OpenShot Wipe Tests.py:28
openshot::Profile::JsonValue
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Profiles.cpp:140
openshot::InvalidFile
Exception for files that can not be found or opened.
Definition: Exceptions.h:132
openshot::Profile::SetJsonValue
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Profiles.cpp:186
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:63
openshot::ProfileInfo::width
int width
The width of the video (in pixels)
Definition: Profiles.h:60
openshot::Profile::SetJson
void SetJson(string value)
Load JSON string into this object.
Definition: Profiles.cpp:163
openshot::ProfileInfo::fps
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: Profiles.h:62
openshot::ProfileInfo::pixel_format
int pixel_format
The pixel format (i.e. YUV420P, RGB24, etc...)
Definition: Profiles.h:61
openshot::ProfileInfo::height
int height
The height of the video (in pixels)
Definition: Profiles.h:59
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:45
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:152
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:64
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:45
openshot::Profile::Json
string Json()
Get and Set JSON methods.
Definition: Profiles.cpp:133