Update SegmentProvider.cs

This commit is contained in:
rlauu 2024-10-20 13:27:05 +02:00
parent 00b272e77e
commit 0735b41f3d

View File

@ -5,6 +5,8 @@ using System.Threading.Tasks;
using Jellyfin.Data.Enums; using Jellyfin.Data.Enums;
using MediaBrowser.Controller; using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Model; using MediaBrowser.Model;
using MediaBrowser.Model.MediaSegments; using MediaBrowser.Model.MediaSegments;
@ -15,54 +17,55 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper.Providers
/// </summary> /// </summary>
public class SegmentProvider : IMediaSegmentProvider public class SegmentProvider : IMediaSegmentProvider
{ {
private static long RemainingTicks => TimeSpan.FromSeconds(Plugin.Instance?.Configuration.RemainingSecondsOfIntro ?? 2).Ticks;
/// <inheritdoc/> /// <inheritdoc/>
public string Name => Plugin.Instance!.Name; public string Name => Plugin.Instance!.Name;
/// <inheritdoc/> /// <inheritdoc/>
public Task<IReadOnlyList<MediaSegmentDto>> GetMediaSegments(MediaSegmentGenerationRequest request, CancellationToken cancellationToken) public Task<IReadOnlyList<MediaSegmentDto>> GetMediaSegments(MediaSegmentGenerationRequest request, CancellationToken cancellationToken)
{ {
var segments = new List<MediaSegmentDto>(); ArgumentNullException.ThrowIfNull(request);
if (Plugin.Instance!.Intros.TryGetValue(request.ItemId, out var introValue)) var segments = new List<MediaSegmentDto>();
var remainingTicks = TimeSpan.FromSeconds(Plugin.Instance?.Configuration.RemainingSecondsOfIntro ?? 2).Ticks;
if (Plugin.Instance!.Intros.TryGetValue(request.ItemId, out var introValue) && introValue.Valid)
{ {
segments.Add(new MediaSegmentDto segments.Add(new MediaSegmentDto
{ {
StartTicks = TimeSpan.FromSeconds(introValue.Start).Ticks, StartTicks = TimeSpan.FromSeconds(introValue.Start).Ticks,
EndTicks = TimeSpan.FromSeconds(introValue.End).Ticks - RemainingTicks, EndTicks = TimeSpan.FromSeconds(introValue.End).Ticks - remainingTicks,
ItemId = request.ItemId, ItemId = request.ItemId,
Type = MediaSegmentType.Intro Type = MediaSegmentType.Intro
}); });
} }
if (Plugin.Instance!.Credits.TryGetValue(request.ItemId, out var creditValue)) if (Plugin.Instance!.Credits.TryGetValue(request.ItemId, out var creditValue) && creditValue.Valid)
{ {
var outroSegment = new MediaSegmentDto
{
StartTicks = TimeSpan.FromSeconds(creditValue.Start).Ticks,
ItemId = request.ItemId,
Type = MediaSegmentType.Outro
};
var creditEndTicks = TimeSpan.FromSeconds(creditValue.End).Ticks; var creditEndTicks = TimeSpan.FromSeconds(creditValue.End).Ticks;
if (Plugin.Instance.GetItem(request.ItemId) is IHasMediaSources item && creditEndTicks >= item.RunTimeTicks - TimeSpan.TicksPerSecond) if (Plugin.Instance.GetItem(request.ItemId) is not null and var item &&
item.RunTimeTicks - TimeSpan.TicksPerSecond < creditEndTicks)
{ {
outroSegment.EndTicks = item.RunTimeTicks ?? creditEndTicks; creditEndTicks = item.RunTimeTicks ?? creditEndTicks;
} }
else else
{ {
outroSegment.EndTicks = creditEndTicks - RemainingTicks; creditEndTicks -= remainingTicks;
} }
segments.Add(outroSegment); segments.Add(new MediaSegmentDto
{
StartTicks = TimeSpan.FromSeconds(creditValue.Start).Ticks,
EndTicks = creditEndTicks,
ItemId = request.ItemId,
Type = MediaSegmentType.Outro
});
} }
return Task.FromResult<IReadOnlyList<MediaSegmentDto>>(segments); return Task.FromResult<IReadOnlyList<MediaSegmentDto>>(segments);
} }
/// <inheritdoc/> /// <inheritdoc/>
public ValueTask<bool> Supports(BaseItem item) => ValueTask.FromResult(item is IHasMediaSources); public ValueTask<bool> Supports(BaseItem item) => ValueTask.FromResult(item is Episode or Movie);
} }
} }