Skip to content

Commit

Permalink
Timeline: Add id-lookup API and GetMaxFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdnyc committed Sep 2, 2020
1 parent 36dab0b commit f49a795
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
27 changes: 27 additions & 0 deletions include/Timeline.h
Expand Up @@ -78,6 +78,21 @@ namespace openshot {
return false;
}};

/// Comparison method for finding the far end of the timeline, by locating
/// the Clip with the highest end-frame number using std::max_element
struct CompareClipEndFrames {
bool operator()(const openshot::Clip* lhs, const openshot::Clip* rhs) {
return (lhs->Position() + lhs->Duration())
<= (rhs->Position() + rhs->Duration());
}};

/// Like CompareClipEndFrames, but for effects
struct CompareEffectEndFrames {
bool operator()(const openshot::EffectBase* lhs, const openshot::EffectBase* rhs) {
return (lhs->Position() + lhs->Duration())
<= (rhs->Position() + rhs->Duration());
}};

/**
* @brief This class represents a timeline
*
Expand Down Expand Up @@ -241,6 +256,18 @@ namespace openshot {
/// Return a list of clips on the timeline
std::list<openshot::Clip*> Clips() { return clips; };

/// Look up a single clip by ID
openshot::ClipBase* GetClip(const std::string& id);

/// Look up a clip effect by ID
openshot::EffectBase* GetClipEffect(const std::string& id);

/// Look up a timeline effect by ID
openshot::EffectBase* GetEffect(const std::string& id);

/// Look up the end frame number of the latest element on the timeline
int64_t GetMaxFrame();

/// Close the timeline reader (and any resources it was consuming)
void Close() override;

Expand Down
53 changes: 53 additions & 0 deletions src/Timeline.cpp
Expand Up @@ -263,6 +263,59 @@ void Timeline::RemoveClip(Clip* clip)
clips.remove(clip);
}

// Look up a clip
openshot::ClipBase* Timeline::GetClip(const std::string& id)
{
// Find the matching clip (if any)
for (const auto& clip : clips) {
if (clip->Id() == id) {
return clip;
}
}
return nullptr;
}

// Look up a timeline effect
openshot::EffectBase* Timeline::GetEffect(const std::string& id)
{
// Find the matching effect (if any)
for (const auto& effect : effects) {
if (effect->Id() == id) {
return effect;
}
}
return nullptr;
}

openshot::EffectBase* Timeline::GetClipEffect(const std::string& id)
{
// Search all clips for matching effect ID
for (const auto& clip : clips) {
auto e = clip->GetEffect(id);
if (e != nullptr) {
return e;
}
}
return nullptr;
}

int64_t Timeline::GetMaxFrame() {
int64_t last_clip = 1;
int64_t last_effect = 1;

if (!clips.empty()) {
const auto max_clip = std::max_element(
clips.begin(), clips.end(), CompareClipEndFrames());
last_clip = (*max_clip)->Position() + (*max_clip)->Duration();
}
if (!effects.empty()) {
const auto max_effect = std::max_element(
effects.begin(), effects.end(), CompareEffectEndFrames());
last_effect = (*max_effect)->Position() + (*max_effect)->Duration();
}
return std::max(last_clip, last_effect);
}

// Apply a FrameMapper to a clip which matches the settings of this timeline
void Timeline::apply_mapper_to_clip(Clip* clip)
{
Expand Down

0 comments on commit f49a795

Please sign in to comment.