25 #include <unordered_map>
33 is_open(false), auto_map_clips(true), managed_cache(true),
path(
""), max_time(0.0)
79 info.width, info.height, info.fps, info.sample_rate,
80 info.channels, info.channel_layout) {}
84 is_open(false), auto_map_clips(true), managed_cache(true),
path(projectPath), max_time(0.0) {
103 QFileInfo filePath(QString::fromStdString(path));
104 if (!filePath.exists()) {
105 throw InvalidFile(
"File could not be opened.", path);
111 if (!openshotPath.exists()) {
114 QDir openshotTransPath(openshotPath.filePath(
"transitions"));
115 if (!openshotTransPath.exists()) {
116 throw InvalidFile(
"PATH_OPENSHOT_INSTALL/transitions could not be found.", openshotTransPath.path().toStdString());
120 QString asset_name = filePath.baseName().left(30) +
"_assets";
121 QDir asset_folder(filePath.dir().filePath(asset_name));
122 if (!asset_folder.exists()) {
124 asset_folder.mkpath(
".");
128 QFile projectFile(QString::fromStdString(path));
129 projectFile.open(QFile::ReadOnly);
130 QString projectContents = QString::fromUtf8(projectFile.readAll());
133 if (convert_absolute_paths) {
137 QRegularExpression allPathsRegex(QStringLiteral(
"\"(image|path)\":.*?\"(.*?)\""));
138 std::vector<QRegularExpressionMatch> matchedPositions;
139 QRegularExpressionMatchIterator i = allPathsRegex.globalMatch(projectContents);
140 while (i.hasNext()) {
141 QRegularExpressionMatch match = i.next();
142 if (match.hasMatch()) {
144 matchedPositions.push_back(match);
149 std::vector<QRegularExpressionMatch>::reverse_iterator itr;
150 for (itr = matchedPositions.rbegin(); itr != matchedPositions.rend(); itr++) {
151 QRegularExpressionMatch match = *itr;
152 QString relativeKey = match.captured(1);
153 QString relativePath = match.captured(2);
154 QString absolutePath =
"";
157 if (relativePath.startsWith(
"@assets")) {
158 absolutePath = QFileInfo(asset_folder.absoluteFilePath(relativePath.replace(
"@assets",
"."))).canonicalFilePath();
159 }
else if (relativePath.startsWith(
"@transitions")) {
160 absolutePath = QFileInfo(openshotTransPath.absoluteFilePath(relativePath.replace(
"@transitions",
"."))).canonicalFilePath();
162 absolutePath = QFileInfo(filePath.absoluteDir().absoluteFilePath(relativePath)).canonicalFilePath();
166 if (!absolutePath.isEmpty()) {
167 projectContents.replace(match.capturedStart(0), match.capturedLength(0),
"\"" + relativeKey +
"\": \"" + absolutePath +
"\"");
171 matchedPositions.clear();
175 SetJson(projectContents.toStdString());
179 float calculated_duration = 0.0;
180 for (
auto clip : clips)
183 if (clip_last_frame > calculated_duration)
184 calculated_duration = clip_last_frame;
185 if (
clip->Reader() &&
clip->Reader()->info.has_audio)
187 if (
clip->Reader() &&
clip->Reader()->info.has_video)
220 if (managed_cache && final_cache) {
230 auto iterator = tracked_objects.find(trackedObject->Id());
232 if (iterator != tracked_objects.end()){
234 iterator->second = trackedObject;
238 tracked_objects[trackedObject->Id()] = trackedObject;
248 auto iterator = tracked_objects.find(
id);
250 if (iterator != tracked_objects.end()){
252 std::shared_ptr<openshot::TrackedObjectBase> trackedObject = iterator->second;
253 return trackedObject;
265 std::list<std::string> trackedObjects_ids;
268 for (
auto const& it: tracked_objects){
270 trackedObjects_ids.push_back(it.first);
273 return trackedObjects_ids;
281 Json::Value trackedObjectJson;
284 auto iterator = tracked_objects.find(
id);
286 if (iterator != tracked_objects.end())
289 std::shared_ptr<TrackedObjectBBox> trackedObject = std::static_pointer_cast<TrackedObjectBBox>(iterator->second);
292 if (trackedObject->ExactlyContains(frame_number)){
293 BBox box = trackedObject->GetBox(frame_number);
294 float x1 = box.
cx - (box.
width/2);
296 float x2 = box.
cx + (box.
width/2);
298 float rotation = box.
angle;
300 trackedObjectJson[
"x1"] = x1;
301 trackedObjectJson[
"y1"] = y1;
302 trackedObjectJson[
"x2"] = x2;
303 trackedObjectJson[
"y2"] = y2;
304 trackedObjectJson[
"rotation"] = rotation;
307 BBox box = trackedObject->BoxVec.begin()->second;
308 float x1 = box.
cx - (box.
width/2);
310 float x2 = box.
cx + (box.
width/2);
312 float rotation = box.
angle;
314 trackedObjectJson[
"x1"] = x1;
315 trackedObjectJson[
"y1"] = y1;
316 trackedObjectJson[
"x2"] = x2;
317 trackedObjectJson[
"y2"] = y2;
318 trackedObjectJson[
"rotation"] = rotation;
324 trackedObjectJson[
"x1"] = 0;
325 trackedObjectJson[
"y1"] = 0;
326 trackedObjectJson[
"x2"] = 0;
327 trackedObjectJson[
"y2"] = 0;
328 trackedObjectJson[
"rotation"] = 0;
331 return trackedObjectJson.toStyledString();
339 const std::lock_guard<std::recursive_mutex> guard(
getFrameMutex);
342 clip->ParentTimeline(
this);
345 if (
clip->Reader() &&
clip->Reader()->GetCache())
346 clip->Reader()->GetCache()->Clear();
349 if (auto_map_clips) {
351 apply_mapper_to_clip(
clip);
355 clips.push_back(
clip);
365 const std::lock_guard<std::recursive_mutex> guard(
getFrameMutex);
368 effect->ParentTimeline(
this);
371 effects.push_back(effect);
381 const std::lock_guard<std::recursive_mutex> guard(
getFrameMutex);
383 effects.remove(effect);
386 if (allocated_effects.count(effect)) {
387 allocated_effects.erase(effect);
400 const std::lock_guard<std::recursive_mutex> guard(
getFrameMutex);
405 if (allocated_clips.count(
clip)) {
406 allocated_clips.erase(
clip);
419 for (
const auto&
clip : clips) {
431 for (
const auto& effect : effects) {
432 if (effect->Id() ==
id) {
442 for (
const auto&
clip : clips) {
443 const auto e =
clip->GetEffect(
id);
455 std::list<EffectBase*> timelineEffectsList;
458 for (
const auto&
clip : clips) {
461 std::list<EffectBase*> clipEffectsList =
clip->Effects();
464 timelineEffectsList.insert(timelineEffectsList.end(), clipEffectsList.begin(), clipEffectsList.end());
467 return timelineEffectsList;
481 return static_cast<int64_t
>(std::ceil(t * fps));
489 return static_cast<int64_t
>(std::floor(t * fps)) + 1;
499 void Timeline::apply_mapper_to_clip(
Clip* clip)
503 if (
clip->Reader()->Name() ==
"FrameMapper")
516 allocated_frame_mappers.insert(mapper);
521 clip->Reader(clip_reader);
531 for (
auto clip : clips)
534 apply_mapper_to_clip(
clip);
539 double Timeline::calculate_time(int64_t number,
Fraction rate)
542 double raw_fps = rate.
ToFloat();
545 return double(number - 1) / raw_fps;
553 "Timeline::apply_effects",
554 "frame->number", frame->number,
555 "timeline_frame_number", timeline_frame_number,
559 for (
auto effect : effects)
563 int64_t effect_start_position =
static_cast<int64_t
>(std::llround(effect->Position() * fpsD)) + 1;
564 int64_t effect_end_position =
static_cast<int64_t
>(std::llround((effect->Position() + effect->Duration()) * fpsD));
566 bool does_effect_intersect = (effect_start_position <= timeline_frame_number && effect_end_position >= timeline_frame_number && effect->Layer() == layer);
569 if (does_effect_intersect)
572 int64_t effect_start_frame =
static_cast<int64_t
>(std::llround(effect->Start() * fpsD)) + 1;
573 int64_t effect_frame_number = timeline_frame_number - effect_start_position + effect_start_frame;
583 "Timeline::apply_effects (Process Effect)",
584 "effect_frame_number", effect_frame_number,
585 "does_effect_intersect", does_effect_intersect);
588 frame = effect->GetFrame(frame, effect_frame_number);
598 std::shared_ptr<Frame> Timeline::GetOrCreateFrame(std::shared_ptr<Frame> background_frame,
Clip* clip, int64_t number,
openshot::TimelineInfoStruct* options)
600 std::shared_ptr<Frame> new_frame;
608 "Timeline::GetOrCreateFrame (from reader)",
610 "samples_in_frame", samples_in_frame);
613 new_frame = std::shared_ptr<Frame>(
clip->
GetFrame(background_frame, number, options));
626 "Timeline::GetOrCreateFrame (create blank)",
628 "samples_in_frame", samples_in_frame);
635 void Timeline::add_layer(std::shared_ptr<Frame> new_frame,
Clip* source_clip, int64_t clip_frame_number,
bool is_top_clip,
float max_volume)
643 std::shared_ptr<Frame> source_frame;
644 source_frame = GetOrCreateFrame(new_frame, source_clip, clip_frame_number, &options);
652 "Timeline::add_layer",
653 "new_frame->number", new_frame->number,
654 "clip_frame_number", clip_frame_number);
657 if (source_clip->
Reader()->info.has_audio) {
660 "Timeline::add_layer (Copy Audio)",
661 "source_clip->Reader()->info.has_audio", source_clip->
Reader()->info.has_audio,
662 "source_frame->GetAudioChannelsCount()", source_frame->GetAudioChannelsCount(),
664 "clip_frame_number", clip_frame_number);
669 if (new_frame->GetAudioSamplesCount() != source_frame->GetAudioSamplesCount()){
673 for (
int channel = 0; channel < source_frame->GetAudioChannelsCount(); channel++)
676 float previous_volume = source_clip->
volume.
GetValue(clip_frame_number - 1);
684 previous_volume = previous_volume / max_volume;
685 volume = volume / max_volume;
689 previous_volume = previous_volume * 0.77;
690 volume = volume * 0.77;
694 if (channel_filter != -1 && channel_filter != channel)
698 if (previous_volume == 0.0 && volume == 0.0)
702 if (channel_mapping == -1)
703 channel_mapping = channel;
706 if (!isEqual(previous_volume, 1.0) || !isEqual(volume, 1.0))
707 source_frame->ApplyGainRamp(channel_mapping, 0, source_frame->GetAudioSamplesCount(), previous_volume, volume);
711 new_frame->AddAudio(
false, channel_mapping, 0, source_frame->GetAudioSamples(channel), source_frame->GetAudioSamplesCount(), 1.0);
717 "Timeline::add_layer (No Audio Copied - Wrong # of Channels)",
718 "source_clip->Reader()->info.has_audio",
719 source_clip->
Reader()->info.has_audio,
720 "source_frame->GetAudioChannelsCount()",
721 source_frame->GetAudioChannelsCount(),
723 "clip_frame_number", clip_frame_number);
728 "Timeline::add_layer (Transform: Composite Image Layer: Completed)",
729 "source_frame->number", source_frame->number,
730 "new_frame->GetImage()->width()", new_frame->GetWidth(),
731 "new_frame->GetImage()->height()", new_frame->GetHeight());
735 void Timeline::update_open_clips(
Clip *clip,
bool does_clip_intersect)
738 const std::lock_guard<std::recursive_mutex> guard(
getFrameMutex);
741 "Timeline::update_open_clips (before)",
742 "does_clip_intersect", does_clip_intersect,
743 "closing_clips.size()", closing_clips.size(),
744 "open_clips.size()", open_clips.size());
747 bool clip_found = open_clips.count(
clip);
749 if (clip_found && !does_clip_intersect)
752 open_clips.erase(
clip);
757 else if (!clip_found && does_clip_intersect)
773 "Timeline::update_open_clips (after)",
774 "does_clip_intersect", does_clip_intersect,
775 "clip_found", clip_found,
776 "closing_clips.size()", closing_clips.size(),
777 "open_clips.size()", open_clips.size());
781 void Timeline::calculate_max_duration() {
782 double last_clip = 0.0;
783 double last_effect = 0.0;
784 double first_clip = std::numeric_limits<double>::max();
785 double first_effect = std::numeric_limits<double>::max();
788 if (!clips.empty()) {
790 const auto max_clip = std::max_element(
792 last_clip = (*max_clip)->Position() + (*max_clip)->Duration();
795 const auto min_clip = std::min_element(
797 return lhs->Position() < rhs->Position();
799 first_clip = (*min_clip)->Position();
803 if (!effects.empty()) {
805 const auto max_effect = std::max_element(
807 last_effect = (*max_effect)->Position() + (*max_effect)->Duration();
810 const auto min_effect = std::min_element(
812 return lhs->Position() < rhs->Position();
814 first_effect = (*min_effect)->Position();
818 max_time = std::max(last_clip, last_effect);
819 min_time = std::min(first_clip, first_effect);
822 if (clips.empty() && effects.empty()) {
829 void Timeline::sort_clips()
832 const std::lock_guard<std::recursive_mutex> guard(
getFrameMutex);
836 "Timeline::SortClips",
837 "clips.size()", clips.size());
843 calculate_max_duration();
847 void Timeline::sort_effects()
850 const std::lock_guard<std::recursive_mutex> guard(
getFrameMutex);
856 calculate_max_duration();
865 const std::lock_guard<std::recursive_mutex> guard(
getFrameMutex);
868 for (
auto clip : clips)
870 update_open_clips(
clip,
false);
873 bool allocated = allocated_clips.count(
clip);
880 allocated_clips.clear();
883 for (
auto effect : effects)
886 bool allocated = allocated_effects.count(effect);
893 allocated_effects.clear();
896 for (
auto mapper : allocated_frame_mappers)
902 allocated_frame_mappers.clear();
911 const std::lock_guard<std::recursive_mutex> guard(
getFrameMutex);
914 for (
auto clip : clips)
917 update_open_clips(
clip,
false);
934 bool Timeline::isEqual(
double a,
double b)
936 return fabs(a - b) < 0.000001;
943 if (requested_frame < 1)
947 std::shared_ptr<Frame> frame;
948 frame = final_cache->
GetFrame(requested_frame);
952 "Timeline::GetFrame (Cached frame found)",
953 "requested_frame", requested_frame);
961 const std::lock_guard<std::recursive_mutex> lock(
getFrameMutex);
964 std::shared_ptr<Frame> frame;
965 frame = final_cache->
GetFrame(requested_frame);
969 "Timeline::GetFrame (Cached frame found on 2nd check)",
970 "requested_frame", requested_frame);
977 std::vector<Clip *> nearby_clips;
978 nearby_clips = find_intersecting_clips(requested_frame, 1,
true);
982 "Timeline::GetFrame (processing frame)",
983 "requested_frame", requested_frame,
984 "omp_get_thread_num()", omp_get_thread_num());
991 new_frame->AddAudioSilence(samples_in_frame);
997 "Timeline::GetFrame (Adding solid color)",
998 "requested_frame", requested_frame,
1010 "Timeline::GetFrame (Loop through clips)",
1011 "requested_frame", requested_frame,
1012 "clips.size()", clips.size(),
1013 "nearby_clips.size()", nearby_clips.size());
1020 int64_t start_frame;
1021 int64_t frame_number;
1024 std::vector<ClipInfo> clip_infos;
1025 clip_infos.reserve(nearby_clips.size());
1028 for (
auto clip : nearby_clips) {
1029 int64_t start_pos =
static_cast<int64_t
>(std::llround(
clip->
Position() * fpsD)) + 1;
1030 int64_t end_pos =
static_cast<int64_t
>(std::llround((
clip->
Position() +
clip->Duration()) * fpsD));
1031 bool intersects = (start_pos <= requested_frame && end_pos >= requested_frame);
1032 int64_t start_frame =
static_cast<int64_t
>(std::llround(
clip->
Start() * fpsD)) + 1;
1033 int64_t frame_number = requested_frame - start_pos + start_frame;
1034 clip_infos.push_back({
clip, start_pos, end_pos, start_frame, frame_number, intersects});
1038 std::unordered_map<int, int64_t> top_start_for_layer;
1039 std::unordered_map<int, Clip*> top_clip_for_layer;
1040 for (
const auto& ci : clip_infos) {
1041 if (!ci.intersects)
continue;
1042 const int layer = ci.clip->Layer();
1043 auto it = top_start_for_layer.find(layer);
1044 if (it == top_start_for_layer.end() || ci.start_pos > it->second) {
1045 top_start_for_layer[layer] = ci.start_pos;
1046 top_clip_for_layer[layer] = ci.clip;
1051 float max_volume_sum = 0.0f;
1052 for (
const auto& ci : clip_infos) {
1053 if (!ci.intersects)
continue;
1054 if (ci.clip->Reader() && ci.clip->Reader()->info.has_audio &&
1055 ci.clip->has_audio.GetInt(ci.frame_number) != 0) {
1056 max_volume_sum +=
static_cast<float>(ci.clip->volume.GetValue(ci.frame_number));
1061 for (
const auto& ci : clip_infos) {
1064 "Timeline::GetFrame (Does clip intersect)",
1065 "requested_frame", requested_frame,
1066 "clip->Position()", ci.clip->Position(),
1067 "clip->Duration()", ci.clip->Duration(),
1068 "does_clip_intersect", ci.intersects);
1071 if (ci.intersects) {
1073 bool is_top_clip =
false;
1074 const int layer = ci.clip->Layer();
1075 auto top_it = top_clip_for_layer.find(layer);
1076 if (top_it != top_clip_for_layer.end())
1077 is_top_clip = (top_it->second == ci.clip);
1080 int64_t clip_frame_number = ci.frame_number;
1084 "Timeline::GetFrame (Calculate clip's frame #)",
1085 "clip->Position()", ci.clip->Position(),
1086 "clip->Start()", ci.clip->Start(),
1088 "clip_frame_number", clip_frame_number);
1091 add_layer(new_frame, ci.clip, clip_frame_number, is_top_clip, max_volume_sum);
1096 "Timeline::GetFrame (clip does not intersect)",
1097 "requested_frame", requested_frame,
1098 "does_clip_intersect", ci.intersects);
1105 "Timeline::GetFrame (Add frame to cache)",
1106 "requested_frame", requested_frame,
1111 new_frame->SetFrameNumber(requested_frame);
1114 final_cache->
Add(new_frame);
1124 std::vector<Clip*> Timeline::find_intersecting_clips(int64_t requested_frame,
int number_of_frames,
bool include)
1127 std::vector<Clip*> matching_clips;
1130 const int64_t min_requested_frame = requested_frame;
1131 const int64_t max_requested_frame = requested_frame + (number_of_frames - 1);
1134 matching_clips.reserve(clips.size());
1136 for (
auto clip : clips)
1139 int64_t clip_start_position =
static_cast<int64_t
>(std::llround(
clip->
Position() * fpsD)) + 1;
1140 int64_t clip_end_position =
static_cast<int64_t
>(std::llround((
clip->
Position() +
clip->Duration()) * fpsD)) + 1;
1142 bool does_clip_intersect =
1143 (clip_start_position <= min_requested_frame || clip_start_position <= max_requested_frame) &&
1144 (clip_end_position >= min_requested_frame || clip_end_position >= max_requested_frame);
1148 "Timeline::find_intersecting_clips (Is clip near or intersecting)",
1149 "requested_frame", requested_frame,
1150 "min_requested_frame", min_requested_frame,
1151 "max_requested_frame", max_requested_frame,
1153 "does_clip_intersect", does_clip_intersect);
1156 update_open_clips(
clip, does_clip_intersect);
1159 if (does_clip_intersect && include)
1161 matching_clips.push_back(
clip);
1163 else if (!does_clip_intersect && !include)
1165 matching_clips.push_back(
clip);
1170 return matching_clips;
1176 const std::lock_guard<std::recursive_mutex> lock(
getFrameMutex);
1179 if (managed_cache && final_cache) {
1182 managed_cache =
false;
1186 final_cache = new_cache;
1201 root[
"type"] =
"Timeline";
1206 root[
"path"] = path;
1209 root[
"clips"] = Json::Value(Json::arrayValue);
1212 for (
const auto existing_clip : clips)
1214 root[
"clips"].append(existing_clip->JsonValue());
1218 root[
"effects"] = Json::Value(Json::arrayValue);
1221 for (
const auto existing_effect: effects)
1223 root[
"effects"].append(existing_effect->JsonValue());
1234 const std::lock_guard<std::recursive_mutex> lock(
getFrameMutex);
1243 catch (
const std::exception& e)
1246 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
1254 const std::lock_guard<std::recursive_mutex> lock(
getFrameMutex);
1257 bool was_open = is_open;
1264 if (!root[
"path"].isNull())
1265 path = root[
"path"].asString();
1267 if (!root[
"clips"].isNull()) {
1272 for (
const Json::Value existing_clip : root[
"clips"]) {
1274 if (existing_clip.isNull()) {
1282 allocated_clips.insert(c);
1299 if (!root[
"effects"].isNull()) {
1304 for (
const Json::Value existing_effect :root[
"effects"]) {
1306 if (existing_effect.isNull()) {
1313 if (!existing_effect[
"type"].isNull()) {
1315 if ( (e =
EffectInfo().CreateEffect(existing_effect[
"type"].asString())) ) {
1318 allocated_effects.insert(e);
1330 if (!root[
"duration"].isNull()) {
1353 const std::lock_guard<std::recursive_mutex> lock(
getFrameMutex);
1360 for (
const Json::Value change : root) {
1361 std::string change_key = change[
"key"][(uint)0].asString();
1364 if (change_key ==
"clips")
1366 apply_json_to_clips(change);
1368 else if (change_key ==
"effects")
1370 apply_json_to_effects(change);
1374 apply_json_to_timeline(change);
1378 catch (
const std::exception& e)
1381 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
1386 void Timeline::apply_json_to_clips(Json::Value change) {
1389 std::string change_type = change[
"type"].asString();
1390 std::string clip_id =
"";
1391 Clip *existing_clip = NULL;
1394 for (
auto key_part : change[
"key"]) {
1396 if (key_part.isObject()) {
1398 if (!key_part[
"id"].isNull()) {
1400 clip_id = key_part[
"id"].asString();
1403 for (
auto c : clips)
1405 if (c->Id() == clip_id) {
1417 if (existing_clip && change[
"key"].size() == 4 && change[
"key"][2] ==
"effects")
1420 Json::Value key_part = change[
"key"][3];
1422 if (key_part.isObject()) {
1424 if (!key_part[
"id"].isNull())
1427 std::string effect_id = key_part[
"id"].asString();
1430 std::list<EffectBase*> effect_list = existing_clip->
Effects();
1431 for (
auto e : effect_list)
1433 if (e->Id() == effect_id) {
1435 apply_json_to_effects(change, e);
1439 int64_t new_ending_frame = ((existing_clip->
Position() + existing_clip->Duration()) *
info.
fps.
ToDouble()) + 1;
1440 final_cache->
Remove(new_starting_frame - 8, new_ending_frame + 8);
1450 if (change_type ==
"insert") {
1456 allocated_clips.insert(
clip);
1464 }
else if (change_type ==
"update") {
1467 if (existing_clip) {
1470 int64_t old_ending_frame = ((existing_clip->
Position() + existing_clip->Duration()) *
info.
fps.
ToDouble()) + 1;
1477 int64_t new_ending_frame = ((existing_clip->
Position() + existing_clip->Duration()) *
info.
fps.
ToDouble()) + 1;
1480 final_cache->
Remove(old_starting_frame - 8, old_ending_frame + 8);
1481 final_cache->
Remove(new_starting_frame - 8, new_ending_frame + 8);
1484 if (existing_clip->
Reader() && existing_clip->
Reader()->GetCache()) {
1485 existing_clip->
Reader()->GetCache()->Remove(old_starting_frame - 8, old_ending_frame + 8);
1486 existing_clip->
Reader()->GetCache()->Remove(new_starting_frame - 8, new_ending_frame + 8);
1490 if (auto_map_clips) {
1491 apply_mapper_to_clip(existing_clip);
1495 }
else if (change_type ==
"delete") {
1498 if (existing_clip) {
1504 int64_t old_ending_frame = ((existing_clip->
Position() + existing_clip->Duration()) *
info.
fps.
ToDouble()) + 1;
1505 final_cache->
Remove(old_starting_frame - 8, old_ending_frame + 8);
1515 void Timeline::apply_json_to_effects(Json::Value change) {
1518 std::string change_type = change[
"type"].asString();
1522 for (
auto key_part : change[
"key"]) {
1524 if (key_part.isObject()) {
1526 if (!key_part[
"id"].isNull())
1529 std::string effect_id = key_part[
"id"].asString();
1532 for (
auto e : effects)
1534 if (e->Id() == effect_id) {
1535 existing_effect = e;
1545 if (existing_effect || change_type ==
"insert") {
1547 apply_json_to_effects(change, existing_effect);
1552 void Timeline::apply_json_to_effects(Json::Value change,
EffectBase* existing_effect) {
1555 std::string change_type = change[
"type"].asString();
1558 if (!change[
"value"].isArray() && !change[
"value"][
"position"].isNull()) {
1559 int64_t new_starting_frame = (change[
"value"][
"position"].asDouble() *
info.
fps.
ToDouble()) + 1;
1560 int64_t new_ending_frame = ((change[
"value"][
"position"].asDouble() + change[
"value"][
"end"].asDouble() - change[
"value"][
"start"].asDouble()) *
info.
fps.
ToDouble()) + 1;
1561 final_cache->
Remove(new_starting_frame - 8, new_ending_frame + 8);
1565 if (change_type ==
"insert") {
1568 std::string effect_type = change[
"value"][
"type"].asString();
1577 allocated_effects.insert(e);
1586 }
else if (change_type ==
"update") {
1589 if (existing_effect) {
1593 int64_t old_ending_frame = ((existing_effect->
Position() + existing_effect->Duration()) *
info.
fps.
ToDouble()) + 1;
1594 final_cache->
Remove(old_starting_frame - 8, old_ending_frame + 8);
1600 }
else if (change_type ==
"delete") {
1603 if (existing_effect) {
1607 int64_t old_ending_frame = ((existing_effect->
Position() + existing_effect->Duration()) *
info.
fps.
ToDouble()) + 1;
1608 final_cache->
Remove(old_starting_frame - 8, old_ending_frame + 8);
1621 void Timeline::apply_json_to_timeline(Json::Value change) {
1622 bool cache_dirty =
true;
1625 std::string change_type = change[
"type"].asString();
1626 std::string root_key = change[
"key"][(uint)0].asString();
1627 std::string sub_key =
"";
1628 if (change[
"key"].size() >= 2)
1629 sub_key = change[
"key"][(uint)1].asString();
1632 if (change_type ==
"insert" || change_type ==
"update") {
1636 if (root_key ==
"color")
1639 else if (root_key ==
"viewport_scale")
1642 else if (root_key ==
"viewport_x")
1645 else if (root_key ==
"viewport_y")
1648 else if (root_key ==
"duration") {
1654 cache_dirty =
false;
1656 else if (root_key ==
"width") {
1661 else if (root_key ==
"height") {
1666 else if (root_key ==
"fps" && sub_key ==
"" && change[
"value"].isObject()) {
1668 if (!change[
"value"][
"num"].isNull())
1669 info.
fps.
num = change[
"value"][
"num"].asInt();
1670 if (!change[
"value"][
"den"].isNull())
1671 info.
fps.
den = change[
"value"][
"den"].asInt();
1673 else if (root_key ==
"fps" && sub_key ==
"num")
1676 else if (root_key ==
"fps" && sub_key ==
"den")
1679 else if (root_key ==
"display_ratio" && sub_key ==
"" && change[
"value"].isObject()) {
1681 if (!change[
"value"][
"num"].isNull())
1683 if (!change[
"value"][
"den"].isNull())
1686 else if (root_key ==
"display_ratio" && sub_key ==
"num")
1689 else if (root_key ==
"display_ratio" && sub_key ==
"den")
1692 else if (root_key ==
"pixel_ratio" && sub_key ==
"" && change[
"value"].isObject()) {
1694 if (!change[
"value"][
"num"].isNull())
1696 if (!change[
"value"][
"den"].isNull())
1699 else if (root_key ==
"pixel_ratio" && sub_key ==
"num")
1702 else if (root_key ==
"pixel_ratio" && sub_key ==
"den")
1706 else if (root_key ==
"sample_rate")
1709 else if (root_key ==
"channels")
1712 else if (root_key ==
"channel_layout")
1717 throw InvalidJSONKey(
"JSON change key is invalid", change.toStyledString());
1720 }
else if (change[
"type"].asString() ==
"delete") {
1724 if (root_key ==
"color") {
1730 else if (root_key ==
"viewport_scale")
1732 else if (root_key ==
"viewport_x")
1734 else if (root_key ==
"viewport_y")
1738 throw InvalidJSONKey(
"JSON change key is invalid", change.toStyledString());
1753 final_cache->
Clear();
1758 for (
const auto clip : clips) {
1760 if (
clip->Reader()) {
1761 if (
auto rc =
clip->Reader()->GetCache())
1765 if (deep &&
clip->Reader()->Name() ==
"FrameMapper") {
1767 if (nested_reader->
Reader()) {
1775 if (
auto cc =
clip->GetCache())
1791 display_ratio_size.scale(proposed_size, Qt::KeepAspectRatio);