diff --git a/IntroSkipper/Manager/MediaSegmentUpdateManager.cs b/IntroSkipper/Manager/MediaSegmentUpdateManager.cs index 36dfbc8..fdf7480 100644 --- a/IntroSkipper/Manager/MediaSegmentUpdateManager.cs +++ b/IntroSkipper/Manager/MediaSegmentUpdateManager.cs @@ -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); } diff --git a/IntroSkipper/Providers/SegmentProvider.cs b/IntroSkipper/Providers/SegmentProvider.cs index 4a1fced..5104820 100644 --- a/IntroSkipper/Providers/SegmentProvider.cs +++ b/IntroSkipper/Providers/SegmentProvider.cs @@ -23,40 +23,30 @@ namespace IntroSkipper.Providers /// public Task> GetMediaSegments(MediaSegmentGenerationRequest request, CancellationToken cancellationToken) { - ArgumentNullException.ThrowIfNull(request); - var segments = new List(); - 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 });