diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/PluginServiceRegistrator.cs b/ConfusedPolarBear.Plugin.IntroSkipper/PluginServiceRegistrator.cs index d3e45d5..e5f6c42 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/PluginServiceRegistrator.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/PluginServiceRegistrator.cs @@ -1,3 +1,4 @@ +using ConfusedPolarBear.Plugin.IntroSkipper.Providers; using MediaBrowser.Controller; using MediaBrowser.Controller.Plugins; using Microsoft.Extensions.DependencyInjection; @@ -15,6 +16,7 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper serviceCollection.AddHostedService(); serviceCollection.AddHostedService(); serviceCollection.AddHostedService(); + serviceCollection.AddSingleton(); } } } diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Providers/SegmentProvider.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Providers/SegmentProvider.cs new file mode 100644 index 0000000..4c18a0b --- /dev/null +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Providers/SegmentProvider.cs @@ -0,0 +1,65 @@ +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.Controller.Entities.TV; +using MediaBrowser.Model; +using MediaBrowser.Model.MediaSegments; + +namespace ConfusedPolarBear.Plugin.IntroSkipper.Providers +{ + /// + /// Introskipper media segment provider. + /// + public class SegmentProvider : IMediaSegmentProvider + { + private readonly int _remainingSecondsOfIntro; + + /// + /// Initializes a new instance of the class. + /// + public SegmentProvider() + { + _remainingSecondsOfIntro = Plugin.Instance?.Configuration.RemainingSecondsOfIntro ?? 2; + } + + /// + 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 - _remainingSecondsOfIntro).Ticks, + ItemId = request.ItemId, + Type = MediaSegmentType.Intro + }); + } + + if (Plugin.Instance!.Credits.TryGetValue(request.ItemId, out var creditValue)) + { + segments.Add(new MediaSegmentDto + { + StartTicks = TimeSpan.FromSeconds(creditValue.Start).Ticks, + EndTicks = TimeSpan.FromSeconds(creditValue.End - _remainingSecondsOfIntro).Ticks, + ItemId = request.ItemId, + Type = MediaSegmentType.Outro + }); + } + + return Task.FromResult>(segments); + } + + /// + public ValueTask Supports(BaseItem item) => ValueTask.FromResult(item is Episode); + } +}