Added Json methods to readers and readerBase classes.

This commit is contained in:
Jonathan Thomas
2013-12-07 16:52:09 -06:00
parent 109e88dddc
commit e3d41808b9
10 changed files with 312 additions and 10 deletions

View File

@@ -238,6 +238,10 @@ namespace openshot
/// @param requested_frame The frame number that is requested.
tr1::shared_ptr<Frame> GetFrame(int requested_frame) throw(ReaderClosed, TooManySeeks);
/// Get and Set JSON methods
Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
void Json(Json::Value root) throw(InvalidFile); ///< Load Json::JsonValue into this object
/// Open File - which is called by the constructor automatically
void Open() throw(InvalidFile, NoStreamsFound, InvalidCodec);
};

View File

@@ -87,6 +87,10 @@ namespace openshot
/// @param requested_frame The frame number that is requested.
tr1::shared_ptr<Frame> GetFrame(int requested_frame) throw(ReaderClosed);
/// Get and Set JSON methods
Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
void Json(Json::Value root) throw(InvalidFile); ///< Load Json::JsonValue into this object
/// Open File - which is called by the constructor automatically
void Open() throw(InvalidFile);
};

View File

@@ -104,8 +104,11 @@ namespace openshot
/// this method, or the ReaderInfo struct values will not be initialized.
void InitFileInfo();
/// Generate JSON string of reader info
string Json();
/// Get and Set JSON methods
string Json(); ///< Generate JSON string of this object
Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
void Json(string value) throw(InvalidJSON); ///< Load JSON string into this object
void Json(Json::Value root); ///< Load Json::JsonValue into this object
/// Open the reader (and start consuming resources, such as images or video files)
virtual void Open() = 0;

View File

@@ -120,6 +120,10 @@ namespace openshot
/// @param requested_frame The frame number that is requested.
tr1::shared_ptr<Frame> GetFrame(int requested_frame) throw(ReaderClosed);
/// Get and Set JSON methods
Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
void Json(Json::Value root) throw(InvalidFile); ///< Load Json::JsonValue into this object
/// Open Reader - which is called by the constructor automatically
void Open();
};

View File

@@ -97,8 +97,11 @@ namespace openshot
/// this method, or the WriterInfo struct values will not be initialized.
void InitFileInfo();
/// Generate JSON string of writer info
string Json();
/// Get and Set JSON methods
string Json(); ///< Generate JSON string of this object
Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
void Json(string value) throw(InvalidJSON); ///< Load JSON string into this object
void Json(Json::Value root); ///< Load Json::JsonValue into this object
/// Display file information in the standard output stream (stdout)
void DisplayInfo();

View File

@@ -1554,4 +1554,28 @@ int FFmpegReader::GetSmallestAudioFrame()
return smallest_frame;
}
// Generate Json::JsonValue for this object
Json::Value FFmpegReader::JsonValue() {
// Create root json object
Json::Value root = ReaderBase::JsonValue(); // get parent properties
root["path"] = path;
// return JsonValue
return root;
}
// Load Json::JsonValue into this object
void FFmpegReader::Json(Json::Value root) throw(InvalidFile) {
// Set parent data
ReaderBase::Json(root);
// Set data from Json (if key is found)
if (root["path"] != Json::nullValue)
path = root["path"].asString();
// Open path, and re-init everything
Close();
Open();
}

View File

@@ -123,3 +123,28 @@ tr1::shared_ptr<Frame> ImageReader::GetFrame(int requested_frame) throw(ReaderCl
}
// Generate Json::JsonValue for this object
Json::Value ImageReader::JsonValue() {
// Create root json object
Json::Value root = ReaderBase::JsonValue(); // get parent properties
root["path"] = path;
// return JsonValue
return root;
}
// Load Json::JsonValue into this object
void ImageReader::Json(Json::Value root) throw(InvalidFile) {
// Set parent data
ReaderBase::Json(root);
// Set data from Json (if key is found)
if (root["path"] != Json::nullValue)
path = root["path"].asString();
// Open path, and re-init everything
Close();
Open();
}

View File

@@ -95,9 +95,16 @@ void ReaderBase::DisplayInfo() {
cout << "----------------------------" << endl;
}
// Generate JSON string of reader info
// Generate JSON string of this object
string ReaderBase::Json() {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::JsonValue for this object
Json::Value ReaderBase::JsonValue() {
// Create root json object
Json::Value root;
root["has_video"] = info.has_video;
@@ -138,8 +145,92 @@ string ReaderBase::Json() {
root["audio_timebase"]["num"] = info.audio_timebase.num;
root["audio_timebase"]["den"] = info.audio_timebase.den;
// return formatted json string
return root.toStyledString();
// return JsonValue
return root;
}
// Load JSON string into this object
void ReaderBase::Json(string value) throw(InvalidJSON) {
// 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
Json(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 ReaderBase::Json(Json::Value root) {
// Set data from Json (if key is found)
if (root["has_video"] != Json::nullValue)
info.has_video = root["has_video"].asBool();
if (root["has_audio"] != Json::nullValue)
info.has_audio = root["has_audio"].asBool();
if (root["duration"] != Json::nullValue)
info.duration = root["duration"].asDouble();
if (root["file_size"] != Json::nullValue)
info.file_size = (long long) root["file_size"].asUInt();
if (root["height"] != Json::nullValue)
info.height = root["height"].asInt();
if (root["width"] != Json::nullValue)
info.width = root["width"].asInt();
if (root["pixel_format"] != Json::nullValue)
info.pixel_format = root["pixel_format"].asInt();
if (root["fps"] != Json::nullValue) {
info.fps.num = root["fps"]["num"].asInt();
info.fps.den = root["fps"]["den"].asInt();
}
if (root["video_bit_rate"] != Json::nullValue)
info.video_bit_rate = root["video_bit_rate"].asInt();
if (root["pixel_ratio"] != Json::nullValue) {
info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
}
if (root["display_ratio"] != Json::nullValue) {
info.display_ratio.num = root["display_ratio"]["num"].asInt();
info.display_ratio.den = root["display_ratio"]["den"].asInt();
}
if (root["vcodec"] != Json::nullValue)
info.vcodec = root["vcodec"].asString();
if (root["video_length"] != Json::nullValue)
info.video_length = (long int) root["video_length"].asUInt();
if (root["video_stream_index"] != Json::nullValue)
info.video_stream_index = root["video_stream_index"].asInt();
if (root["video_timebase"] != Json::nullValue) {
info.video_timebase.num = root["video_timebase"]["num"].asInt();
info.video_timebase.den = root["video_timebase"]["den"].asInt();
}
if (root["interlaced_frame"] != Json::nullValue)
info.interlaced_frame = root["interlaced_frame"].asBool();
if (root["top_field_first"] != Json::nullValue)
info.top_field_first = root["top_field_first"].asBool();
if (root["acodec"] != Json::nullValue)
info.acodec = root["acodec"].asString();
if (root["audio_bit_rate"] != Json::nullValue)
info.audio_bit_rate = root["audio_bit_rate"].asInt();
if (root["sample_rate"] != Json::nullValue)
info.sample_rate = root["sample_rate"].asInt();
if (root["channels"] != Json::nullValue)
info.channels = root["channels"].asInt();
if (root["audio_stream_index"] != Json::nullValue)
info.audio_stream_index = root["audio_stream_index"].asInt();
if (root["audio_timebase"] != Json::nullValue) {
info.audio_timebase.num = root["audio_timebase"]["num"].asInt();
info.audio_timebase.den = root["audio_timebase"]["den"].asInt();
}
}

View File

@@ -157,4 +157,55 @@ tr1::shared_ptr<Frame> TextReader::GetFrame(int requested_frame) throw(ReaderClo
}
// Generate Json::JsonValue for this object
Json::Value TextReader::JsonValue() {
// Create root json object
Json::Value root = ReaderBase::JsonValue(); // get parent properties
root["width"] = width;
root["height"] = height;
root["x_offset"] = x_offset;
root["y_offset"] = y_offset;
root["text"] = text;
root["font"] = font;
root["size"] = size;
root["text_color"] = text_color;
root["background_color"] = background_color;
root["gravity"] = gravity;
// return JsonValue
return root;
}
// Load Json::JsonValue into this object
void TextReader::Json(Json::Value root) throw(InvalidFile) {
// Set parent data
ReaderBase::Json(root);
// Set data from Json (if key is found)
if (root["width"] != Json::nullValue)
width = root["width"].asInt();
if (root["height"] != Json::nullValue)
height = root["height"].asInt();
if (root["x_offset"] != Json::nullValue)
x_offset = root["x_offset"].asInt();
if (root["y_offset"] != Json::nullValue)
y_offset = root["y_offset"].asInt();
if (root["text"] != Json::nullValue)
text = root["text"].asString();
if (root["font"] != Json::nullValue)
font = root["font"].asString();
if (root["size"] != Json::nullValue)
size = root["size"].asDouble();
if (root["text_color"] != Json::nullValue)
text_color = root["text_color"].asString();
if (root["background_color"] != Json::nullValue)
background_color = root["background_color"].asString();
if (root["gravity"] != Json::nullValue)
gravity = (GravityType) root["gravity"].asInt();
// Open path, and re-init everything
Close();
Open();
}

View File

@@ -128,9 +128,16 @@ void WriterBase::DisplayInfo() {
cout << "----------------------------" << endl;
}
// Generate JSON string of writer info
// Generate JSON string of this object
string WriterBase::Json() {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::JsonValue for this object
Json::Value WriterBase::JsonValue() {
// Create root json object
Json::Value root;
root["has_video"] = info.has_video;
@@ -171,6 +178,92 @@ string WriterBase::Json() {
root["audio_timebase"]["num"] = info.audio_timebase.num;
root["audio_timebase"]["den"] = info.audio_timebase.den;
// return formatted json string
return root.toStyledString();
// return JsonValue
return root;
}
// Load JSON string into this object
void WriterBase::Json(string value) throw(InvalidJSON) {
// 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
Json(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 WriterBase::Json(Json::Value root) {
// Set data from Json (if key is found)
if (root["has_video"] != Json::nullValue)
info.has_video = root["has_video"].asBool();
if (root["has_audio"] != Json::nullValue)
info.has_audio = root["has_audio"].asBool();
if (root["duration"] != Json::nullValue)
info.duration = root["duration"].asDouble();
if (root["file_size"] != Json::nullValue)
info.file_size = (long long) root["file_size"].asUInt();
if (root["height"] != Json::nullValue)
info.height = root["height"].asInt();
if (root["width"] != Json::nullValue)
info.width = root["width"].asInt();
if (root["pixel_format"] != Json::nullValue)
info.pixel_format = root["pixel_format"].asInt();
if (root["fps"] != Json::nullValue) {
info.fps.num = root["fps"]["num"].asInt();
info.fps.den = root["fps"]["den"].asInt();
}
if (root["video_bit_rate"] != Json::nullValue)
info.video_bit_rate = root["video_bit_rate"].asInt();
if (root["pixel_ratio"] != Json::nullValue) {
info.pixel_ratio.num = root["pixel_ratio"]["num"].asInt();
info.pixel_ratio.den = root["pixel_ratio"]["den"].asInt();
}
if (root["display_ratio"] != Json::nullValue) {
info.display_ratio.num = root["display_ratio"]["num"].asInt();
info.display_ratio.den = root["display_ratio"]["den"].asInt();
}
if (root["vcodec"] != Json::nullValue)
info.vcodec = root["vcodec"].asString();
if (root["video_length"] != Json::nullValue)
info.video_length = (long int) root["video_length"].asUInt();
if (root["video_stream_index"] != Json::nullValue)
info.video_stream_index = root["video_stream_index"].asInt();
if (root["video_timebase"] != Json::nullValue) {
info.video_timebase.num = root["video_timebase"]["num"].asInt();
info.video_timebase.den = root["video_timebase"]["den"].asInt();
}
if (root["interlaced_frame"] != Json::nullValue)
info.interlaced_frame = root["interlaced_frame"].asBool();
if (root["top_field_first"] != Json::nullValue)
info.top_field_first = root["top_field_first"].asBool();
if (root["acodec"] != Json::nullValue)
info.acodec = root["acodec"].asString();
if (root["audio_bit_rate"] != Json::nullValue)
info.audio_bit_rate = root["audio_bit_rate"].asInt();
if (root["sample_rate"] != Json::nullValue)
info.sample_rate = root["sample_rate"].asInt();
if (root["channels"] != Json::nullValue)
info.channels = root["channels"].asInt();
if (root["audio_stream_index"] != Json::nullValue)
info.audio_stream_index = root["audio_stream_index"].asInt();
if (root["audio_timebase"] != Json::nullValue) {
info.audio_timebase.num = root["audio_timebase"]["num"].asInt();
info.audio_timebase.den = root["audio_timebase"]["den"].asInt();
}
}