38 #include <google/protobuf/util/time_util.h> 42 using google::protobuf::util::TimeUtil;
45 Stabilizer::Stabilizer(std::string clipStabilizedDataPath):protobuf_data_path(clipStabilizedDataPath)
48 init_effect_details();
57 init_effect_details();
61 void Stabilizer::init_effect_details()
70 info.
description =
"Stabilize video clip to remove undesired shaking and jitter.";
73 protobuf_data_path =
"";
83 cv::Mat frame_image = frame->GetImageCV();
86 if(!frame_image.empty()){
91 float zoom_value = zoom.
GetValue(frame_number);
94 cv::Mat T(2,3,CV_64F);
106 cv::Mat frame_stabilized;
107 cv::warpAffine(frame_image, frame_stabilized, T, frame_image.size());
110 cv::Mat T_scale = cv::getRotationMatrix2D(cv::Point2f(frame_stabilized.cols/2, frame_stabilized.rows/2), 0, zoom_value);
111 cv::warpAffine(frame_stabilized, frame_stabilized, T_scale, frame_stabilized.size());
112 frame_image = frame_stabilized;
117 frame->SetImageCV(frame_image);
125 pb_stabilize::Stabilization stabilizationMessage;
128 std::fstream input(inputFilePath, ios::in | ios::binary);
129 if (!stabilizationMessage.ParseFromIstream(&input)) {
130 std::cerr <<
"Failed to parse protobuf message." << std::endl;
139 for (
size_t i = 0; i < stabilizationMessage.frame_size(); i++) {
142 const pb_stabilize::Frame& pbFrameData = stabilizationMessage.frame(i);
145 size_t id = pbFrameData.id();
148 float x = pbFrameData.x();
149 float y = pbFrameData.y();
150 float a = pbFrameData.a();
156 float dx = pbFrameData.dx();
157 float dy = pbFrameData.dy();
158 float da = pbFrameData.da();
165 google::protobuf::ShutdownProtobufLibrary();
185 root[
"protobuf_data_path"] = protobuf_data_path;
202 catch (
const std::exception& e)
205 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
216 if (!root[
"protobuf_data_path"].isNull()){
217 protobuf_data_path = (root[
"protobuf_data_path"].asString());
220 std::cout<<
"Invalid protobuf data path";
221 protobuf_data_path =
"";
224 if(!root[
"zoom"].isNull())
233 root[
"id"] =
add_property_json(
"ID", 0.0,
"string",
Id(), NULL, -1, -1,
true, requested_frame);
234 root[
"position"] =
add_property_json(
"Position",
Position(),
"float",
"", NULL, 0, 1000 * 60 * 30,
false, requested_frame);
236 root[
"start"] =
add_property_json(
"Start",
Start(),
"float",
"", NULL, 0, 1000 * 60 * 30,
false, requested_frame);
237 root[
"end"] =
add_property_json(
"End",
End(),
"float",
"", NULL, 0, 1000 * 60 * 30,
false, requested_frame);
238 root[
"duration"] =
add_property_json(
"Duration",
Duration(),
"float",
"", NULL, 0, 1000 * 60 * 30,
true, requested_frame);
240 root[
"zoom"] =
add_property_json(
"Zoom", zoom.
GetValue(requested_frame),
"float",
"", &zoom, 0.0, 2.0,
false, requested_frame);
246 return root.toStyledString();
std::string Json() const override
Generate JSON string of this object.
std::string Id() const
Get the Id of this clip object.
float Start() const
Get start position (in seconds) of clip (trim start of video)
virtual void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
std::string id
ID Property for all derived Clip and Effect classes.
void SetJson(const std::string value) override
Load JSON string into this object.
std::map< size_t, EffectTransformParam > transformationData
const Json::Value stringToJson(const std::string value)
virtual Json::Value JsonValue() const
Generate Json::Value for this object.
bool has_audio
Determines if this effect manipulates the audio of a frame.
Header file for all Exception classes.
std::map< size_t, EffectCamTrajectory > trajectoryData
bool LoadStabilizedData(std::string inputFilePath)
Load protobuf data file.
std::string class_name
The class name of the effect.
std::string name
The name of the effect.
std::string PropertiesJSON(int64_t requested_frame) const override
float Duration() const
Get the length of this clip (in seconds)
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
This namespace is the default namespace for all code in the openshot library.
Json::Value JsonValue() const
Generate Json::Value for this object.
std::string description
The description of this effect and what it does.
Json::Value JsonValue() const override
Generate Json::Value for this object.
bool has_video
Determines if this effect manipulates the image of a frame.
std::shared_ptr< Frame > GetFrame(std::shared_ptr< Frame > frame, int64_t frame_number) override
This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame...
Exception for invalid JSON.
double GetValue(int64_t index) const
Get the value at a specific index.
Header file for Stabilizer effect class.
std::string parent_effect_id
Id of the parent effect (if there is one)
float Position() const
Get position on timeline (in seconds)
float End() const
Get end position (in seconds) of clip (trim end of video)
Json::Value add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe *keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const
Generate JSON for a property.
int Layer() const
Get layer of clip on timeline (lower number is covered by higher numbers)
EffectInfoStruct info
Information about the current effect.