From 4e730a3f4168af42593a518f3ba98b90ab6b5646 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Tue, 3 Dec 2013 00:13:25 -0600 Subject: [PATCH] Integrated more JSON methods into the library. --- include/Clip.h | 3 +++ include/KeyFrame.h | 10 ++++++--- include/ReaderBase.h | 4 ++++ include/WriterBase.h | 3 +++ src/ChunkWriter.cpp | 47 ++++++------------------------------------- src/Clip.cpp | 21 +++++++++++++++++++ src/KeyFrame.cpp | 41 +++++++++++++++++++++++++++++++++++++ src/ReaderBase.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++ src/WriterBase.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 179 insertions(+), 44 deletions(-) diff --git a/include/Clip.h b/include/Clip.h index 1a9ee132..a9a70c71 100644 --- a/include/Clip.h +++ b/include/Clip.h @@ -181,6 +181,9 @@ namespace openshot { float End() throw(ReaderClosed); ///< Get end position (in seconds) of clip (trim end of video), which can be affected by the time curve. void End(float value) { end = value; } ///< Set end position (in seconds) of clip (trim end of video) + /// Generate JSON of clip data + string Json(); + /// Waveform property bool Waveform() { return waveform; } ///< Get the waveform property of this clip void Waveform(bool value) { waveform = value; } ///< Set the waveform property of this clip diff --git a/include/KeyFrame.h b/include/KeyFrame.h index 8c57a89a..c5cf8da7 100644 --- a/include/KeyFrame.h +++ b/include/KeyFrame.h @@ -36,6 +36,7 @@ #include "Fraction.h" #include "Coordinate.h" #include "Point.h" +#include "Json.h" using namespace std; using namespace openshot; @@ -118,9 +119,6 @@ namespace openshot { /// Get the rounded INT value at a specific index int GetInt(int index); - /// Get the direction of the curve at a specific index (increasing or decreasing) - bool IsIncreasing(int index); - /// Get the fraction that represents how many times this value is repeated in the curve Fraction GetRepeatFraction(int index); @@ -133,6 +131,12 @@ namespace openshot { // Get the number of values (i.e. coordinates on the X axis) int GetLength(); + /// Get the direction of the curve at a specific index (increasing or decreasing) + bool IsIncreasing(int index); + + /// Generate JSON of keyframe data + string Json(); + /** * @brief Calculate all of the values for this keyframe. * diff --git a/include/ReaderBase.h b/include/ReaderBase.h index 593a78af..fdf6884d 100644 --- a/include/ReaderBase.h +++ b/include/ReaderBase.h @@ -33,6 +33,7 @@ #include #include "Fraction.h" #include "Frame.h" +#include "Json.h" using namespace std; @@ -103,6 +104,9 @@ namespace openshot /// this method, or the ReaderInfo struct values will not be initialized. void InitFileInfo(); + /// Generate JSON string of reader info + string Json(); + /// Open the reader (and start consuming resources, such as images or video files) virtual void Open() = 0; }; diff --git a/include/WriterBase.h b/include/WriterBase.h index f6f99377..c304298e 100644 --- a/include/WriterBase.h +++ b/include/WriterBase.h @@ -97,6 +97,9 @@ namespace openshot /// this method, or the WriterInfo struct values will not be initialized. void InitFileInfo(); + /// Generate JSON string of writer info + string Json(); + /// Display file information in the standard output stream (stdout) void DisplayInfo(); }; diff --git a/src/ChunkWriter.cpp b/src/ChunkWriter.cpp index 760c2b4b..b5a0d3e5 100644 --- a/src/ChunkWriter.cpp +++ b/src/ChunkWriter.cpp @@ -36,6 +36,10 @@ ChunkWriter::ChunkWriter(string path, ReaderBase *reader) throw (InvalidFile, In // Init FileInfo struct (clear all values) InitFileInfo(); + // Change codecs to default + info.vcodec = default_vcodec; + info.acodec = default_acodec; + // Copy info struct from the source reader CopyReaderInfo(local_reader); @@ -257,55 +261,16 @@ void ChunkWriter::Close() local_reader->Close(); } -// write json meta data +// write JSON meta data void ChunkWriter::write_json_meta_data() { - Json::Value root; - root["has_video"] = local_reader->info.has_video; - root["has_audio"] = local_reader->info.has_audio; - root["duration"] = local_reader->info.duration; - stringstream filesize_stream; - filesize_stream << local_reader->info.file_size; - root["file_size"] = filesize_stream.str(); - root["height"] = local_reader->info.height; - root["width"] = local_reader->info.width; - root["pixel_format"] = local_reader->info.pixel_format; - root["fps"] = Json::Value(Json::objectValue); - root["fps"]["num"] = local_reader->info.fps.num; - root["fps"]["den"] = local_reader->info.fps.den; - root["video_bit_rate"] = local_reader->info.video_bit_rate; - root["pixel_ratio"] = Json::Value(Json::objectValue); - root["pixel_ratio"]["num"] = local_reader->info.pixel_ratio.num; - root["pixel_ratio"]["den"] = local_reader->info.pixel_ratio.den; - root["display_ratio"] = Json::Value(Json::objectValue); - root["display_ratio"]["num"] = local_reader->info.display_ratio.num; - root["display_ratio"]["den"] = local_reader->info.display_ratio.den; - root["vcodec"] = default_vcodec; - stringstream video_length_stream; - video_length_stream << local_reader->info.video_length; - root["video_length"] = video_length_stream.str(); - root["video_stream_index"] = local_reader->info.video_stream_index; - root["video_timebase"] = Json::Value(Json::objectValue); - root["video_timebase"]["num"] = local_reader->info.video_timebase.num; - root["video_timebase"]["den"] = local_reader->info.video_timebase.den; - root["interlaced_frame"] = local_reader->info.interlaced_frame; - root["top_field_first"] = local_reader->info.top_field_first; - root["acodec"] = default_acodec; - root["audio_bit_rate"] = local_reader->info.audio_bit_rate; - root["sample_rate"] = local_reader->info.sample_rate; - root["channels"] = local_reader->info.channels; - root["audio_stream_index"] = local_reader->info.audio_stream_index; - root["audio_timebase"] = Json::Value(Json::objectValue); - root["audio_timebase"]["num"] = local_reader->info.audio_timebase.num; - root["audio_timebase"]["den"] = local_reader->info.audio_timebase.den; - // Load path of chunk folder string json_path = QDir::cleanPath(QString(path.c_str()) + QDir::separator() + "info.json").toStdString(); // Write JSON file ofstream myfile; myfile.open (json_path.c_str()); - myfile << root << endl; + myfile << local_reader->Json() << endl; myfile.close(); } diff --git a/src/Clip.cpp b/src/Clip.cpp index e9652db2..4be580f1 100644 --- a/src/Clip.cpp +++ b/src/Clip.cpp @@ -535,3 +535,24 @@ int Clip::GetSamplesPerFrame(int frame_number, Fraction rate) throw(ReaderClosed double samples_per_frame = total_samples - previous_samples; return samples_per_frame; } + +// Generate JSON of clip data +string Clip::Json() { + + // Create root json object + Json::Value root; + root["position"] = Position(); + root["layer"] = Layer(); + root["start"] = Start(); + root["end"] = End(); + root["gravity"] = gravity; + root["scale"] = scale; + root["anchor"] = anchor; + root["waveform"] = waveform; + + // return formatted json string + return root.toStyledString(); + +} + + diff --git a/src/KeyFrame.cpp b/src/KeyFrame.cpp index 6f6561d9..d51b255c 100644 --- a/src/KeyFrame.cpp +++ b/src/KeyFrame.cpp @@ -237,6 +237,47 @@ bool Keyframe::IsIncreasing(int index) return true; } +// Generate JSON of keyframe data +string Keyframe::Json() { + + // Check if it needs to be processed + if (needs_update) + Process(); + + // Create root json object + Json::Value root = Json::Value(Json::arrayValue); + + // loop through points, and find a matching coordinate + for (int x = 0; x < Points.size(); x++) { + // Get each point + Point existing_point = Points[x]; + + // Create new point object + Json::Value point = Json::Value(Json::objectValue); + point["interpolation"] = existing_point.interpolation; + point["handle_type"] = existing_point.handle_type; + point["co"] = Json::Value(Json::objectValue); + point["co"]["X"] = existing_point.co.X; + point["co"]["Y"] = existing_point.co.Y; + + point["left"] = Json::Value(Json::objectValue); + point["left"]["X"] = existing_point.handle_left.X; + point["left"]["Y"] = existing_point.handle_left.Y; + + point["right"] = Json::Value(Json::objectValue); + point["right"]["X"] = existing_point.handle_right.X; + point["right"]["Y"] = existing_point.handle_right.Y; + + // Append point to array + root.append(point); + + } + + // return formatted json string + return root.toStyledString(); + +} + // Get the fraction that represents how many times this value is repeated in the curve Fraction Keyframe::GetRepeatFraction(int index) { diff --git a/src/ReaderBase.cpp b/src/ReaderBase.cpp index fec88334..3bd6c851 100644 --- a/src/ReaderBase.cpp +++ b/src/ReaderBase.cpp @@ -95,3 +95,51 @@ void ReaderBase::DisplayInfo() { cout << "----------------------------" << endl; } +// Generate JSON string of reader info +string ReaderBase::Json() { + + // Create root json object + Json::Value root; + root["has_video"] = info.has_video; + root["has_audio"] = info.has_audio; + root["duration"] = info.duration; + stringstream filesize_stream; + filesize_stream << info.file_size; + root["file_size"] = filesize_stream.str(); + 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["video_bit_rate"] = info.video_bit_rate; + 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["vcodec"] = info.vcodec; + stringstream video_length_stream; + video_length_stream << info.video_length; + root["video_length"] = video_length_stream.str(); + root["video_stream_index"] = info.video_stream_index; + root["video_timebase"] = Json::Value(Json::objectValue); + root["video_timebase"]["num"] = info.video_timebase.num; + root["video_timebase"]["den"] = info.video_timebase.den; + root["interlaced_frame"] = info.interlaced_frame; + root["top_field_first"] = info.top_field_first; + root["acodec"] = info.acodec; + root["audio_bit_rate"] = info.audio_bit_rate; + root["sample_rate"] = info.sample_rate; + root["channels"] = info.channels; + root["audio_stream_index"] = info.audio_stream_index; + root["audio_timebase"] = Json::Value(Json::objectValue); + root["audio_timebase"]["num"] = info.audio_timebase.num; + root["audio_timebase"]["den"] = info.audio_timebase.den; + + // return formatted json string + return root.toStyledString(); +} + + diff --git a/src/WriterBase.cpp b/src/WriterBase.cpp index e1feaf2a..46326839 100644 --- a/src/WriterBase.cpp +++ b/src/WriterBase.cpp @@ -128,3 +128,49 @@ void WriterBase::DisplayInfo() { cout << "----------------------------" << endl; } +// Generate JSON string of writer info +string WriterBase::Json() { + + // Create root json object + Json::Value root; + root["has_video"] = info.has_video; + root["has_audio"] = info.has_audio; + root["duration"] = info.duration; + stringstream filesize_stream; + filesize_stream << info.file_size; + root["file_size"] = filesize_stream.str(); + 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["video_bit_rate"] = info.video_bit_rate; + 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["vcodec"] = info.vcodec; + stringstream video_length_stream; + video_length_stream << info.video_length; + root["video_length"] = video_length_stream.str(); + root["video_stream_index"] = info.video_stream_index; + root["video_timebase"] = Json::Value(Json::objectValue); + root["video_timebase"]["num"] = info.video_timebase.num; + root["video_timebase"]["den"] = info.video_timebase.den; + root["interlaced_frame"] = info.interlaced_frame; + root["top_field_first"] = info.top_field_first; + root["acodec"] = info.acodec; + root["audio_bit_rate"] = info.audio_bit_rate; + root["sample_rate"] = info.sample_rate; + root["channels"] = info.channels; + root["audio_stream_index"] = info.audio_stream_index; + root["audio_timebase"] = Json::Value(Json::objectValue); + root["audio_timebase"]["num"] = info.audio_timebase.num; + root["audio_timebase"]["den"] = info.audio_timebase.den; + + // return formatted json string + return root.toStyledString(); +}