计算时间跨度的刻度

本文关键字:时间跨度 计算 | 更新日期: 2023-09-27 18:10:27

我有一个Keyframe s列表,这只是对象与TimeSpans和一个字段(类型为long),它有自己的timeSpan的刻度,称为tempTicks。完整的列表从关键帧1到7000。

几乎每个关键帧都有一个比之前更大的时间戳。我想从300-800中抓取这些关键帧,我想给他们

List<Keyframe> region = new List<Keyframe>();
long highestTicks = 0;
long durationTicks = 0; //Stores the whole duration of this new region
//beginFrame and endFrame are 300 and 800
for (int i = beginFrame; i < endFrame; i += 1)
{
    //Clip is the full list of keyframes
    Keyframe k = clip.Keyframes[i];
    if (region.Count < 1)
    {
        k.Time = TimeSpan.FromTicks(0);
    }
    else
    {
        //This is the trouble-part
        if (k.Time.Ticks > highestTicks)
        {
           highestTicks = k.Time.Ticks;
           k.Time = 
           TimeSpan.FromTicks(highestTicks - region[region.Count -1].tempTicks);
        }
     }
     durationTicks += k.Time.Ticks;
     region.Add(k);
}

我不能正确地得到它们。你知道为什么吗?

示例:选取最喜欢的电影场景。您希望以一种方式导出它,即场景从媒体播放器中的0:00开始,而不是从您最初拍摄它的87:00开始。

计算时间跨度的刻度

试着这样做:

var tickOffset = clip.Keyframes[beginFrame].Time.Ticks;
// this is your 'region' variable
var adjustedFrames = clip.Keyframes
    .Skip(beginFrame)
    .Take(endFrame - beginFrame)
    .Select(kf => new Keyframe { 
        Time = TimeSpan.FromTicks(kf.Time.Ticks - tickOffset),
        OtherProperty = kf.OtherProperty            
    })
    .ToList();
var durationTicks = adjustedFrames.Max(k => k.Time.Ticks);

修改这些帧的时间有点奇怪。人们期望将它们提取到一个新列表中,而不修改原始值。然而,这样做的方法是使用第一个字段作为"基数",然后从所有其他字段中减去该值。因此,如果您的时间是[..., 27, 28, 32, 33, 35, 37, 39, ...],并且您想将值从27更改为39,则它们变成:[0, 1, 5, 6, 8, 10, 12]:

List<Keyframe> region = new List<Keyframe>();
long highestTicks = 0;
long durationTicks = 0; //Stores the whole duration of this new region
long baseTicks = clip.Keyframes[beginFrame].Time.Ticks;
//beginFrame and endFrame are 300 and 800
for (int i = beginFrame; i <= endFrame; i += 1)
{
    //Clip is the full list of keyframes
    Keyframe k = clip.Keyframes[i];
    k.Time = TimeSpan.FromTicks(k.Time.Ticks - baseTicks);
    highestTicks = Math.Max(highestTicks, k.Time.Ticks);
     region.Add(k);
}
durationTicks = highestTicks;

虽然我真的不明白你为什么担心虱子。你可以直接计算TimeSpan

看起来"i"的值从300到799。您需要<=操作符吗?