using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Jellyfin.Data.Enums; using MediaBrowser.Controller; using MediaBrowser.Controller.Entities; using MediaBrowser.Model; using MediaBrowser.Model.MediaSegments; namespace ConfusedPolarBear.Plugin.IntroSkipper.Providers { /// /// Introskipper media segment provider. /// public class SegmentProvider : IMediaSegmentProvider { private readonly long _remainingTicks; /// /// Initializes a new instance of the class. /// public SegmentProvider() { _remainingTicks = TimeSpan.FromSeconds(Plugin.Instance?.Configuration.RemainingSecondsOfIntro ?? 2).Ticks; } /// public string Name => Plugin.Instance!.Name; /// public Task> GetMediaSegments(MediaSegmentGenerationRequest request, CancellationToken cancellationToken) { var segments = new List(); 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, ItemId = request.ItemId, Type = MediaSegmentType.Intro }); } if (Plugin.Instance!.Credits.TryGetValue(request.ItemId, out var creditValue)) { var outroSegment = new MediaSegmentDto { StartTicks = TimeSpan.FromSeconds(creditValue.Start).Ticks, ItemId = request.ItemId, Type = MediaSegmentType.Outro }; var creditEndTicks = TimeSpan.FromSeconds(creditValue.End).Ticks; if (Plugin.Instance.GetItem(request.ItemId) is IHasMediaSources item && creditEndTicks >= item.RunTimeTicks - TimeSpan.TicksPerSecond) { outroSegment.EndTicks = item.RunTimeTicks ?? creditEndTicks; } else { outroSegment.EndTicks = creditEndTicks - _remainingTicks; } segments.Add(outroSegment); } return Task.FromResult>(segments); } /// public ValueTask Supports(BaseItem item) => ValueTask.FromResult(item is IHasMediaSources); } }