simplify check for credits reaching end

This commit is contained in:
rlauu 2024-10-21 08:52:43 +02:00
parent b02403da84
commit 4b725aaaad
2 changed files with 13 additions and 25 deletions

View File

@ -37,8 +37,7 @@ namespace IntroSkipper.Manager
try
{
var existingSegments = await _mediaSegmentManager.GetSegmentsAsync(episode.EpisodeId, null).ConfigureAwait(false);
var deleteTasks = existingSegments.Select(s => _mediaSegmentManager.DeleteSegmentAsync(s.Id));
await Task.WhenAll(deleteTasks).ConfigureAwait(false);
await Task.WhenAll(existingSegments.Select(s => _mediaSegmentManager.DeleteSegmentAsync(s.Id))).ConfigureAwait(false);
var newSegments = await _segmentProvider.GetMediaSegments(new MediaSegmentGenerationRequest { ItemId = episode.EpisodeId }, cancellationToken).ConfigureAwait(false);
@ -48,8 +47,7 @@ namespace IntroSkipper.Manager
continue;
}
var createTasks = newSegments.Select(s => _mediaSegmentManager.CreateSegmentAsync(s, _name));
await Task.WhenAll(createTasks).ConfigureAwait(false);
await Task.WhenAll(newSegments.Select(s => _mediaSegmentManager.CreateSegmentAsync(s, _name))).ConfigureAwait(false);
_logger.LogDebug("Updated {SegmentCount} segments for episode {EpisodeId}", newSegments.Count, episode.EpisodeId);
}

View File

@ -23,40 +23,30 @@ namespace IntroSkipper.Providers
/// <inheritdoc/>
public Task<IReadOnlyList<MediaSegmentDto>> GetMediaSegments(MediaSegmentGenerationRequest request, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(request);
var segments = new List<MediaSegmentDto>();
var remainingTicks = TimeSpan.FromSeconds(Plugin.Instance?.Configuration.RemainingSecondsOfIntro ?? 2).Ticks;
var remainingTicks = (Plugin.Instance?.Configuration.RemainingSecondsOfIntro ?? 2) * TimeSpan.TicksPerSecond;
if (Plugin.Instance!.Intros.TryGetValue(request.ItemId, out var introValue) && introValue.Valid)
if (Plugin.Instance!.Intros.TryGetValue(request.ItemId, out var introValue))
{
segments.Add(new MediaSegmentDto
{
StartTicks = TimeSpan.FromSeconds(introValue.Start).Ticks,
EndTicks = TimeSpan.FromSeconds(introValue.End).Ticks - remainingTicks,
StartTicks = (long)(introValue.Start * TimeSpan.TicksPerSecond),
EndTicks = (long)(introValue.End * TimeSpan.TicksPerSecond) - remainingTicks,
ItemId = request.ItemId,
Type = MediaSegmentType.Intro
});
}
if (Plugin.Instance!.Credits.TryGetValue(request.ItemId, out var creditValue) && creditValue.Valid)
if (Plugin.Instance!.Credits.TryGetValue(request.ItemId, out var creditValue))
{
var creditEndTicks = TimeSpan.FromSeconds(creditValue.End).Ticks;
if (Plugin.Instance.GetItem(request.ItemId) is not null and var item &&
item.RunTimeTicks - TimeSpan.TicksPerSecond < creditEndTicks)
{
creditEndTicks = item.RunTimeTicks ?? creditEndTicks;
}
else
{
creditEndTicks -= remainingTicks;
}
var creditEndTicks = (long)(creditValue.End * TimeSpan.TicksPerSecond);
var runTimeTicks = Plugin.Instance.GetItem(request.ItemId)?.RunTimeTicks ?? long.MaxValue;
segments.Add(new MediaSegmentDto
{
StartTicks = TimeSpan.FromSeconds(creditValue.Start).Ticks,
EndTicks = creditEndTicks,
StartTicks = (long)(creditValue.Start * TimeSpan.TicksPerSecond),
EndTicks = runTimeTicks > creditEndTicks + TimeSpan.TicksPerSecond
? creditEndTicks - remainingTicks
: runTimeTicks,
ItemId = request.ItemId,
Type = MediaSegmentType.Outro
});